Stream audio from pyaudio with Flask to HTML5

2024/10/6 6:47:02

I want to stream the audio of my microphone (that is being recorded via pyaudio) via Flask to any client that connects.

This is where the audio comes from:

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

Here's my flask-code:

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

And this is the client:

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

It does work sometimes, but most of the times I'm getting "[Errno 32] Broken pipe"

When uncommenting that with open("tmp.wav")-part (the self.save() optionally takes all previous frames and saves them in tmp.wav), I kind of get a stream, but all that comes out of the speakers is a "clicking"-noise.

I'm open for any suggestions. How do I get the input of my microphone live-streamed (no pre-recording!) to a webbrowser?

Thanks!

Answer

Try This its worked for me. shell cmd "cat" is working perfect see the code iam using 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 folderfilesAudios=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 datareturn Response(generate(), mimetype='audio/mp3')
https://en.xdnf.cn/q/70402.html

Related Q&A

Adding into Path var while silent installation of Python - possible bug?

I need to passively install Python in my applications package installation so i use the following:python-3.5.4-amd64.exe /passive PrependPath=1according this: 3.1.4. Installing Without UI I use the Pre…

Pandas add new columns based on splitting another column

I have a pandas dataframe like the following:A B US,65,AMAZON 2016 US,65,EBAY 2016My goal is to get to look like this:A B country code com US.65.AMAZON 2016…

Proper way to insert HTML into Flask

I know how to send some plain text from Python with render_template when a URL is visited:@app.route(/success.html") ... return render_template("index.html", text="This text goes in…

How to add a new class to an existing classifier in deep learning?

I trained a deep learning model to classify the given images into three classes. Now I want to add one more class to my model. I tried to check out "Online learning", but it seems to train on…

Count unique elements along an axis of a NumPy array

I have a three-dimensional array likeA=np.array([[[1,1], [1,0]],[[1,2], [1,0]],[[1,0], [0,0]]])Now I would like to obtain an array that has a nonzero value in a given position if only a unique nonzero …

influxdb python: 404 page not found

I am trying to use the influxdb-python lib which I found here. But I cant even get the tutorial programm to work. When I run the following example code:$ python>>> from influxdb import InfluxD…

Django Table already exist

Here is my Django Migration file. When I run python manage.py makemigrations/migrate I get this error.Error:-django.db.utils.OperationalError: (1050, "Table tickets_duration already exists")I…

Python round() too slow, faster way to reduce precision?

I am doing the following:TOLERANCE = 13 some_float = ... round(some_float, TOLERANCE)This is run many times, so performance is important. I have to round some_float due to floating point representation…

Reading .doc file in Python using antiword in Windows (also .docx)

I tried reading a .doc file like - with open(file.doc, errors=ignore) as f:text = f.read()It did read that file but with huge junk, I cant remove that junk as I dont know from where it starts and where…

Error installing package with pip

Im trying to install a charting tool (matplotlib-v1.4.2) for python 3.4 in Windows 7, so far all my trails doesnt seem to do the job.Attempts:Ive downloaded pip from GitHub python -m pip install matplo…