pythonnumpysocketspyaudiointercom

Intercom (not input- output Stream)


I have a problem: if I repeatedly press the "receive-audio" and "Start" buttons, I get the following error:

An error occurred: [Errno Not input stream] -9975

An error occurred: [Errno Not output stream] -9974

I want to both send and receive audio to the server side, but I can't.

enter image description here

here is my code:






    def send_audio(self):
        p = pyaudio.PyAudio()
        
        while self.is_running:
            try:
                self.stream = p.open(format=pyaudio.paInt16,
                                    channels=self.CHANNELS,
                                    rate=self.RATE,
                                    input=True,
                                    frames_per_buffer=self.CHUNK)
        
                while self.is_running:
                    data = self.stream.read(self.CHUNK)
                    audio_data = np.frombuffer(data, np.int16)
                    self.server_socket.sendall(audio_data)
        
                    if not self.is_running:
                        break
        
                self.stream.stop_stream()
                self.stream.close()
                p.terminate()
            
            except Exception as e:
                # Handle the exception (e.g., log the error, retry, etc.)
                print("An error occurred:", str(e))

    def receive_audio(self):
        p = pyaudio.PyAudio()

        while self.is_running_recv:
            try:
                self.stream = p.open(format=self.FORMAT,
                                    channels=self.CHANNELS,
                                    rate=self.RATE,
                                    output=True)
        
                while self.is_running_recv:
                    data = self.server_socket.recv(self.CHUNK)
                    
                    if not data:
                        break
                    if self.event.is_set():
                        self.stream.write(data)
        
                self.stream.stop_stream()
                self.stream.close()
                self.server_socket.close()
                p.terminate()
            
            except Exception as e:
                # Handle the exception (e.g., log the error, retry, etc.)
                print("An error occurred:", str(e))




Solution

  • i solved this problem. We should write stream instead of self.stream

    stream.stop_stream()
    stream.close()
    p.terminate()