如何从树莓派从Flask服务器流音频?

时间:2019-05-23 04:44:13

标签: python audio flask stream raspberry-pi

我正在尝试从树莓派上的aux插孔或连接的麦克风获取音频流,并将其流传输到网页上,以便可以通过本地网络上的另一台设备进行连接。

我已经尝试过使用flask进行多种操作,包括尝试将音频与pycamera附加在一起,然后只是不显示摄像机。我还尝试了几种不同的受支持格式,例如mp3和ogg。流pycamera可以完美工作,但这是来自api的视频。唯一可行的方法是使用yield函数并以WAV格式进行流式传输,但是仅能输出6个小时的固体丑陋录音。

from flask import Flask, Response, render_template, stream_with_context
import pyaudio
import sys
import sounddevice as sd

app = Flask(__name__)

FORMAT=pyaudio.paInt16
CHANNELS=2
RATE=44100
CHUNK=1024
RECORD_SECONDS=15
FILE_NAME="RECORDING.wav"

audio=pyaudio.PyAudio() #instantiate the pyaudio

#recording prerequisites
stream=audio.open(format=FORMAT,channels=CHANNELS,
                  rate=RATE,
                  input=True,
                  frames_per_buffer=CHUNK)

def gen():
    while True:
        data = stream.read(CHUNK)
        print("data")
        yield data


@app.route('/')
def hello_world():
    return render_template("index.html")


@app.route('/audioFeed')
def audioFeed():
    return Response(stream_with_context(gen()))


if __name__ == '__main__':
    app.run()
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>The Earth is convex</title>
</head>
<body>

<audio controls>
<source src="{{ url_for('audioFeed') }}" type="audio/wav">
</audio>

</body>
</html>

这是输出,我想知道为什么它在gen()函数的while循环中没有连续产生。

FLASK_APP = app.py
FLASK_ENV = development
FLASK_DEBUG = 0
In folder C:/Users/Ben/Documents/AudioBox
C:\Users\Ben\Random\Scripts\python.exe -m flask run
 * Serving Flask-SocketIO app "app"
 * Forcing debug mode off
 * Serving Flask app "app"
 * Forcing debug mode off
 * Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)
127.0.0.1 - - [22/May/2019 23:39:03] "GET / HTTP/1.1" 200 -
data
data
127.0.0.1 - - [22/May/2019 23:39:03] "GET /audioFeed HTTP/1.1" 200 -
data
data
data
data
data
data
127.0.0.1 - - [22/May/2019 23:39:03] "GET /favicon.ico HTTP/1.1" 404 -

0 个答案:

没有答案