Search code examples
pythonnao-robot

How to call "process" function using ALAudioDevice "subscribe" method


I'm new at NAO programming and I'm having some trouble regarding the ALAudioDevice API. My problem is the following one: I wrote a python module that should record raw data from the front microphone. The documentation of the ALAudioDevice API says that the method "subscribe(...)" calls the function "process" automatically and regularly with raw data from microphones as inputs. I wrote a code to execute this process (see bellow), and it runs without raising the error flag. However, the subscribe is bypassing the function "process" and the module doesn't get any audio at all. Has someone had the same problem?

import qi

class AudioModule(object):
    def __init__(self):
        super(AudioModule, self).__init__()
        self.moduleName = "AudioModule"

        try :
            self.ALAudioDevice = ALProxy("ALAudioDevice")
        except Exception, e:
            self.logger.error("Error when creating proxy on ALAudioDevice:")
            self.logger.error(e)

    def begin_stream(self):
        self.ALAudioDevice.setClientPreferences(self.moduleName, 16000, 3, 0)
        self.ALAudioDevice.subscribe(self.moduleName)

    def end_stream(self):
        self.ALAudioDevice.unsubscribe(self.moduleName)

    def processRemote( self, nbOfChannels, samplesByChannel, altimestamp, buffer ):
        nbOfChannels = nbOfChannels
        mylogger = qi.Logger("data")
        mylogger.info("It works !" + str(nbOfChannels))

class MyClass(GeneratedClass):
    def __init__(self):
        GeneratedClass.__init__(self, False)
        self.audio = AudioModule()

    def onLoad(self):
        self.serviceId = self.session().registerService("AudioModule", self.audio)
        pass

    def onUnload(self):
        if self.serviceId != -1:
            self.session().unregisterService(self.serviceId)
            self.serviceId = -1
        pass

    def onInput_onStart(self):
        self.audio.begin_stream()
        self.onInput_onStop()
        pass

    def onInput_onStop(self):
        self.audio.end_stream()
        self.onUnload
        self.onStopped()
        pass

Solution

  • It appears you are subscribing to the audio from a Choregraphe box. I'm not sure it is supposed to work.

    But in this configuration the Python code is executed from within the same process as the ALAudioDevice service. So probably you should name your callback "process" instead of "processRemote".

    Otherwise, you can still do this from a separate Python script.