如何使用 ALAudioDevice "subscribe" 方法调用 "process" 函数

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

我是 NAO 编程的新手,我在 ALAudioDevice API 方面遇到了一些麻烦。 我的问题是以下一个:我写了一个 python 模块,它应该记录来自前置麦克风的原始数据。 ALAudioDevice API 的文档说方法 "subscribe(...)" 自动调用函数 "process" 并定期将来自麦克风的原始数据作为输入。我写了一个代码来执行这个过程(见下文),它运行时没有引发 错误标志。但是,订阅会绕过 "process" 函数,并且模块根本不会获得任何音频。 有人遇到过同样的问题吗?

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

您似乎正在从 Choregraphe 盒子订阅音频。我不确定它是否有效。

但在此配置中,Python 代码是在与 ALAudioDevice 服务相同的进程中执行的。因此,您可能应该将回调命名为 "process" 而不是 "processRemote".

否则,您仍然可以从 a separate Python script 执行此操作。