1

I tried to run a non-blocking audio stream socket server in Pepper Robot (NaoQi 2.5.5.5) using PyAudio 0.2.11. Here is the Python script that I wrote inside a box.

class MyClass(GeneratedClass):
def __init__(self):
    GeneratedClass.__init__(self)

def callback(self, in_data, frame_count, time_info, status):
    self.client.send(in_data)
    return None, pyaudio.paContinue

def onLoad(self):

    import pyaudio

    self.FORMAT = pyaudio.paInt16
    self.CHANNELS = 1
    self.RATE = 16000
    self.CHUNK = 1600
    self.audio = pyaudio.PyAudio()
    self.stream = self.audio.open(format=self.FORMAT, channels=self.CHANNELS, rate=self.RATE, input=True, frames_per_buffer=self.CHUNK, stream_callback=self.callback)

def onInput_startStream(self):
    self.client, self.address = self.s.accept()
    self.stream.start_stream()

def onInput_stopStream(self):
    self.client.close()
    self.stream.stop_stream()

def onUnload(self):
    #put clean-up code here
    pass

def onInput_onStart(self):
    import socket
    self.memory = ALProxy("ALMemory")
    ip_robot = self.memory.getData("ipRobot")
    port_robot = self.memory.getData("portRobot")

    self.s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    self.s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
    self.s.bind((ip_robot, port_robot))
    self.s.listen(1)

def onInput_onStop(self):
    self.onUnload() #it is recommended to reuse the clean-up as the box is stopped
    self.onStopped() #activate the output of the box

So when the box loaded, it will configure the pyaudio stream (onLoad function). Then if the onInput_onStart of Python Box triggered, socket server will be build. After that, if the onInput_onStartStream triggered it will start stream some buffer audio bytes from Pepper's Microphone to client. But when my robot starts the stream, it executes the callback function only one time. Is my code wrong?

rmlockerd
  • 3,776
  • 2
  • 15
  • 25
17Peps
  • 45
  • 4
  • I do not know much about PyAudio, however with callback-based APIs like that, there is a tendency to stop calling callbacks that produces errors. Did you check whether `callback` raises any exception? – Victor Paléologue Aug 13 '21 at 08:51
  • @VictorPaléologue yeah I had been checked, but the callback didn't raise any exception. When I tried to use the blocking audio stream, it's worked well. – 17Peps Aug 15 '21 at 09:19
  • In this example https://people.csail.mit.edu/hubert/pyaudio/docs/#example-callback-mode-audio-i-o they return the data buffer after processing. Maybe not doing so prevents next data to be provided. – Victor Paléologue Aug 16 '21 at 17:14
  • @VictorPaléologue Thanks, I will tried it soon on my Pepper – 17Peps Aug 25 '21 at 09:03

0 Answers0