Desmond-Dong commited on
Commit
f2e56b3
·
1 Parent(s): c775f95

"fix-microphone-volume-control-use-respeaker-helper"

Browse files
reachy_mini_ha_voice/reachy_controller.py CHANGED
@@ -121,25 +121,21 @@ class ReachyController:
121
 
122
  def get_microphone_volume(self) -> float:
123
  """Get microphone volume (0-100) using local SDK audio interface."""
124
- if not self.is_available:
125
- return 50.0 # Default if not available
 
126
 
127
  try:
128
- # Use Reachy Mini's local audio interface (like get_DoA)
129
- audio = self.reachy.media.audio
130
- if audio is not None and audio._respeaker is not None:
131
- # AUDIO_MGR_MIC_GAIN returns gain in dB (typically 0-15 dB)
132
- gain_db = audio._respeaker.read("AUDIO_MGR_MIC_GAIN")
133
- if gain_db is not None:
134
- # Convert dB gain to 0-100 percentage
135
- # Assuming 0 dB = 0%, 15 dB = 100%
136
- volume = min(100.0, max(0.0, (gain_db[0] / 15.0) * 100.0))
137
- logger.debug(f"Microphone gain: {gain_db[0]:.2f} dB -> {volume:.0f}%")
138
- return volume
139
  except Exception as e:
140
- logger.debug(f"Could not get microphone volume from SDK: {e}")
141
 
142
- return 50.0 # Default fallback
143
 
144
  def set_microphone_volume(self, volume: float) -> None:
145
  """
@@ -149,21 +145,20 @@ class ReachyController:
149
  volume: Volume level 0-100
150
  """
151
  volume = max(0.0, min(100.0, volume))
 
 
 
 
 
 
152
 
153
- if self.is_available:
154
- try:
155
- # Use Reachy Mini's local audio interface (like get_DoA)
156
- audio = self.reachy.media.audio
157
- if audio is not None and audio._respeaker is not None:
158
- # Convert 0-100% to dB gain (0-15 dB range)
159
- gain_db = (volume / 100.0) * 15.0
160
- audio._respeaker.write("AUDIO_MGR_MIC_GAIN", [gain_db])
161
- logger.info(f"Microphone volume set to {volume}% (gain: {gain_db:.2f} dB)")
162
- return
163
- except Exception as e:
164
- logger.error(f"Failed to set microphone volume: {e}")
165
- else:
166
- logger.warning("Cannot set microphone volume: robot not available")
167
 
168
  # ========== Phase 2: Motor Control ==========
169
 
 
121
 
122
  def get_microphone_volume(self) -> float:
123
  """Get microphone volume (0-100) using local SDK audio interface."""
124
+ respeaker = self._get_respeaker()
125
+ if respeaker is None:
126
+ return getattr(self, '_microphone_volume', 50.0)
127
 
128
  try:
129
+ # Try APMGR_MICGAIN first (0-31 range)
130
+ result = respeaker.read("APMGR_MICGAIN")
131
+ if result is not None:
132
+ # Convert 0-31 to 0-100%
133
+ self._microphone_volume = (result[0] / 31.0) * 100.0
134
+ return self._microphone_volume
 
 
 
 
 
135
  except Exception as e:
136
+ logger.debug(f"Could not get microphone volume: {e}")
137
 
138
+ return getattr(self, '_microphone_volume', 50.0)
139
 
140
  def set_microphone_volume(self, volume: float) -> None:
141
  """
 
145
  volume: Volume level 0-100
146
  """
147
  volume = max(0.0, min(100.0, volume))
148
+ self._microphone_volume = volume
149
+
150
+ respeaker = self._get_respeaker()
151
+ if respeaker is None:
152
+ logger.warning("Cannot set microphone volume: ReSpeaker not available")
153
+ return
154
 
155
+ try:
156
+ # Convert 0-100% to 0-31 range for APMGR_MICGAIN
157
+ gain = int((volume / 100.0) * 31.0)
158
+ respeaker.write("APMGR_MICGAIN", [gain])
159
+ logger.info(f"Microphone volume set to {volume}% (gain: {gain})")
160
+ except Exception as e:
161
+ logger.error(f"Failed to set microphone volume: {e}")
 
 
 
 
 
 
 
162
 
163
  # ========== Phase 2: Motor Control ==========
164