Open the audio input with the device's channel count (fixes mono microphones)

#4
by alphatangobravo - opened

Summary

AudioAnalyzer.start() opens the input stream with a hard-coded channels=2. PortAudio rejects that for any single-channel capture device with Error opening InputStream: Invalid number of channels [PaErrorCode -9998], the exception is caught and logged, and the app keeps running with no audio at all โ€” the UI shows "Dancing!" but the robot never reacts, which is easy to mistake for a sensitivity or volume problem.

Mono inputs are common outside the ALSA-loopback setup the README describes: a laptop's built-in microphone when developing against the MuJoCo simulator, most USB microphones, and virtual devices that expose one channel.

Change

Ask the selected device for its max_input_channels and open the stream with min(2, that) instead of always 2. _audio_callback already averages across channels when more than one arrives and flattens otherwise, so no downstream change is needed. Stereo devices (including loopback_in) behave exactly as before.

channels = 2
try:
    info = sd.query_devices(self.device_index)
    channels = max(1, min(2, int(info["max_input_channels"])))
except Exception:
    pass
self.stream = sd.InputStream(device=self.device_index, channels=channels, ...)

Testing

  • macOS 26, Python 3.12, sounddevice 0.5, reachy_mini 1.14.0 daemon in --sim (MuJoCo) mode.
  • Before: selecting "MacBook Pro Microphone" and pressing Start Dancing logs Failed to start audio stream: โ€ฆ Invalid number of channels [PaErrorCode -9998]; no movement.
  • After: the stream opens, BPM detection locks onto a 120 BPM test track (reported ~109โ€“120), and the simulated robot sways and bobs in time.
  • No behaviour change for a 2-channel device: channels still resolves to 2.
CreaturesDigital changed pull request status to merged

Sign up or log in to comment