gpt4 book ai didi

python - 使用 Flask 将音频从 pyaudio 流式传输到 HTML5

转载 作者:太空狗 更新时间:2023-10-29 15:53:19 39 4
gpt4 key购买 nike

我想通过 Flask 将我麦克风的音频(通过 pyaudio 录制)流式传输到任何连接的客户端。

这是音频的来源:

    def getSound(self):
# Current chunk of audio data
data = self.stream.read(self.CHUNK)
self.frames.append(data)
wave = self.save(list(self.frames))

return data

这是我的 flask 代码:

@app.route('/audiofeed')
def audiofeed():
def gen(microphone):
while True:
sound = microphone.getSound()
#with open('tmp.wav', 'rb') as myfile:
# yield myfile.read()

yield sound

return Response(stream_with_context(gen(Microphone())))

这是客户端:

    <audio controls>
<source src="{{ url_for('audiofeed') }}" type="audio/x-wav;codec=pcm">
Your browser does not support the audio element.
</audio>

它有时确实有效,但大多数时候我收到“[Errno 32] Broken pipe

当使用 open("tmp.wav")-part 取消注释时(self.save() 可选地获取所有先前的帧并将它们保存在 tmp.wav 中),我得到了一个流,但所有的都出来了扬声器发出“咔哒”声。

我愿意接受任何建议。如何将我的麦克风输入实时流式传输(无预录!)到网络浏览器?

谢谢!

最佳答案

试试这个对我有用。 shell cmd“cat”运行完美,请参阅代码我正在使用 FLASK

import subprocess
import os
import inspect
from flask import Flask
from flask import Response

@app.route('/playaudio')
def playaudio():
sendFileName=""
def generate():

# get_list_all_files_name this function gives all internal files inside the folder

filesAudios=get_list_all_files_name(currentDir+"/streamingAudios/1")

# audioPath is audio file path in system
for audioPath in filesAudios:
data=subprocess.check_output(['cat',audioPath])
yield data
return Response(generate(), mimetype='audio/mp3')

关于python - 使用 Flask 将音频从 pyaudio 流式传输到 HTML5,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47106364/

39 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com