Stream audio from pyaudio with Flask to HTML5

后端 未结 4 1486
小鲜肉
小鲜肉 2021-02-01 10:51

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:



        
相关标签:
4条回答
  • 2021-02-01 11:43

    After lots research and tinkering I finally found the solution.

    Basically it came down to serving pyaudio.paFloat32 audio data through WebSockets using Flask's SocketIO implementation and receiving/playing the data in JavaScript using HTML5's AudioContext.

    As this is requires quite some code, I think it would not be a good idea to post it all here. Instead, feel free to check out the project I'm using it in: simpleCam

    The relevant code is in: - noise_detector.py (recording) - server.py (WebSocket transfer) - static/js/player.js (receiving/playing)

    Thanks everyone for the support!

    0 讨论(0)
  • 2021-02-01 11:44

    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 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')
    
    0 讨论(0)
  • 2021-02-01 11:44

    Instead of datasize = len(samples) * channels * bitsPerSample in the header function write datasize = 2000*10**6.

    def gen_audio():
    
        CHUNK = 512
        sampleRate = 44100
        bitsPerSample = 16
        channels = 2
        wav_header = genHeader(sampleRate, bitsPerSample, channels)
    
        audio = AudioRead()
        data = audio.get_audio_chunck()
        chunck = wav_header + data
        while True:
            yield (chunck)
            data = audio.get_audio_chunck()
            chunck = data
    
    0 讨论(0)
  • 2021-02-01 11:45

    This question was asked long time ago, but since I spent entire day to figure out how to implement the same, I want to give the answer. Maybe it will be helpful for somebody.

    "[Errno 32] Broken pipe" error comes from the fact that client can not play audio and closes this stream. Audio can not be played due to absence of the header in the data stream. You can easily create the header using genHeader(sampleRate, bitsPerSample, channels, samples) function from the code here . This header has to be attached at least to the first chunck of sent data ( chunck=header+data ). Pay attention, that audio can be played ONLY untill client reaches file size in download that you have to specify in the header. So, workaround would be to set in the header some big files size, e.g. 2Gb.

    0 讨论(0)
提交回复
热议问题