NAO v4机器人远程音频处理故障求助(Python NAOqi SDK 2.1.4.13)
Hey there, sorry to hear you've been stuck on this for days—let's dig into why you're not getting audio data from your NAO v4's buffer. Looking at your code and setup, there are a few key issues to check, starting with a critical mistake in your callback binding.
1. Fix the Callback Binding (Most Likely Root Cause)
In your SoundReceiverModule.__init__ method, you're binding a method named "callback" to your module, but your actual audio processing method is called processRemote. NAOqi can't trigger the callback if the names don't match!
Change this line:
self.BIND_PYTHON( self.getName(),"callback" );
To:
self.BIND_PYTHON( self.getName(), "processRemote" );
This tells NAOqi to route audio buffer events to your processRemote method instead of looking for a non-existent callback method.
2. Verify Callback Triggering
Once you fix the binding, add a simple print statement at the start of processRemote to confirm the callback is actually being called:
def processRemote( self, nbOfChannels, nbrOfSamplesByChannel, aTimeStamp, buffer ): print("Audio buffer received! Channels: {}, Samples/Channel: {}".format(nbOfChannels, nbrOfSamplesByChannel)) # Rest of your code...
If this doesn't print, your module isn't properly registered with NAOqi. Double-check:
- The global
SoundReceivervariable is correctly initialized (NAOqi requires modules to be global to avoid garbage collection). - Your broker is running and connected to the robot (no network issues, correct IP/port).
3. Validate Audio Device Preferences
Your start method sets client preferences, but let's confirm they align with your NAO v4's capabilities:
- NAO v4 supports 48kHz sampling rate (your setting is correct), but ensure no other client is overriding this.
- Try explicitly setting
nDeinterleave=1(non-interleaved data) temporarily to simplify debugging—this will pass separate buffers for each channel instead of interleaved data, making it easier to verify if data is coming through.
Adjust the preferences line to:
audio.setClientPreferences( self.getName(), nSampleRate, nNbrChannelFlag, 1 );
And update your processRemote data handling to account for non-interleaved data if you test this.
4. Check for Audio Device Conflicts
Make sure no other processes on the robot are using the audio device (e.g., speech synthesis, other audio subscriptions). You can stop conflicting services via the robot's web interface or using:
audio = naoqi.ALProxy( "ALAudioDevice", self.strNaoIp, 9559 ); # List all subscribed clients print("Current audio subscribers:", audio.getSubscribers()) # Unsubscribe any unknown clients if needed for client in audio.getSubscribers(): if client != self.getName(): audio.unsubscribe(client)
5. Debug Buffer Data Handling
If the callback triggers but you're not seeing valid data:
- Print the length of the buffer to confirm it matches the expected size:
print("Buffer length:", len(buffer))(for 4 channels, 128 samples/channel, this should be 41282 = 1024 bytes, since each sample is 16-bit). - In Python 2,
np.fromstring(str(buffer), dtype=np.int16)should work, but try usingnp.frombuffer(buffer, dtype=np.int16)instead—it's more reliable for binary data.
Corrected Code Snippet (Key Fixes)
Here's the updated SoundReceiverModule with the critical callback binding fix:
class SoundReceiverModule(naoqi.ALModule): """ Use this object to get call back from the ALMemory of the naoqi world. Your callback needs to be a method with two parameter (variable name, value). """ def __init__( self, strModuleName, strNaoIp ): try: naoqi.ALModule.__init__(self, strModuleName ); # Fix: Bind to the actual processRemote method self.BIND_PYTHON( self.getName(), "processRemote" ); self.strNaoIp = strNaoIp; self.outfile = None; self.aOutfile = [None]*(4-1); # ASSUME max nbr channels = 4 except BaseException, err: print( "ERR: abcdk.naoqitools.SoundReceiverModule: loading error: %s" % str(err) ); # __init__ - end # Rest of your methods remain mostly the same...
Final Checks
- Ensure your robot's firmware version is compatible with NAOqi SDK 2.1.4.13 (NAO v4 should work, but mismatched versions can cause odd issues).
- Test with a minimal script first (just subscribe and print callback events) before adding file saving or processing logic.
Hope this gets you past the stuck point—let me know if you still run into issues after trying these fixes!
内容的提问来源于stack exchange,提问作者Ronald Moore

