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?