Flask and pyaudio entegras

26 Views Asked by At

I have a problem with flask_socketio. This problem was not happening to me in local with socket.

The following code is a function on the side that receives the sound. When the get_soundfunction is active, the programme closes after 2-3 seconds.but it does not enter the except block in any way

def get_sound(self): 
        
        try:

            @sio.on("data1")
            def get_audio(data):
                try:
                    if self.stream is None:
                        p = pyaudio.PyAudio() 
                        self.stream = p.open(
                            format=pyaudio.paInt16,
                            channels=1,
                            rate=44100,
                            output=True
                        )

                    
                        audio_data = data.get("audio_data", b"")
                        if audio_data:
                            print(audio_data)
                            self.stream.write(audio_data)
                except Exception as e:
                    print("Hata:", str(e))
                    if self.stream is not None:
                        print("cloded")
                        self.stream.close()
                        self.stream.stop_stream()

        except Exception as e:
            print("Hata1:", str(e))

the code here is the function to send the sound.

def send_audio(self):

        p = pyaudio.PyAudio()
        stream = p.open(
            format=pyaudio.paInt16,
            channels=1,
            rate=44100,
            input=True,
            frames_per_buffer=1024,
        )

        try:
            while True:
                
                    data = stream.read(self.CHUNK)
                    audio_data = np.frombuffer(data, dtype=np.int16)

                    audio_data = audio_data.tobytes()

                    sio.emit(
                        "audio_data", {"audio_data": audio_data })  # Bytlara dönüştürülen ses verilerini 'audio_data' sözcüğü ile emitle 
        except Exception as e:
            print("error")
        finally:
            stream.stop_stream()
            stream.close()
            p.terminate()
0

There are 0 best solutions below