Desmond-Dong commited on
Commit
45876b9
·
1 Parent(s): 9a32f45

"fix-numpy-ufunc-type-mismatch-error"

Browse files
reachy_mini_ha_voice/voice_assistant.py CHANGED
@@ -398,35 +398,31 @@ class VoiceAssistantService:
398
  time.sleep(0.01)
399
  continue
400
 
401
- # Validate dtype - SDK returns float32
402
- # Check for string/bytes dtype which cannot be converted
403
- if audio_data.dtype.kind in ('S', 'U', 'O'): # bytes, unicode, object
404
- _LOGGER.debug("Audio data has non-numeric dtype: %s, skipping", audio_data.dtype)
405
- time.sleep(0.01)
406
- continue
407
-
408
  if audio_data.dtype != np.float32:
409
- _LOGGER.debug("Unexpected audio dtype: %s, converting", audio_data.dtype)
 
 
 
 
410
  try:
411
- # Ensure we have numeric data before conversion
412
- if not np.issubdtype(audio_data.dtype, np.number):
413
- _LOGGER.debug("Non-numeric audio dtype: %s, skipping", audio_data.dtype)
414
- time.sleep(0.01)
415
- continue
416
  audio_data = audio_data.astype(np.float32)
417
  except (TypeError, ValueError) as e:
418
- _LOGGER.debug("Failed to convert audio dtype: %s", e)
419
  time.sleep(0.01)
420
  continue
421
 
422
  # Convert stereo to mono (take mean of channels)
423
  # SDK returns shape (samples, 2) for stereo
 
424
  if audio_data.ndim == 2 and audio_data.shape[1] == 2:
425
- audio_chunk_array = np.mean(audio_data, axis=1, dtype=np.float32)
426
  elif audio_data.ndim == 2:
427
- audio_chunk_array = audio_data[:, 0].astype(np.float32)
428
  elif audio_data.ndim == 1:
429
- audio_chunk_array = audio_data.astype(np.float32)
430
  else:
431
  _LOGGER.debug("Unexpected audio shape: %s", audio_data.shape)
432
  time.sleep(0.01)
 
398
  time.sleep(0.01)
399
  continue
400
 
401
+ # Convert to float32 BEFORE any math operations
402
+ # This prevents "ufunc 'add' did not contain a loop" errors
403
+ # when SDK returns unexpected types (e.g., dtype='S1')
 
 
 
 
404
  if audio_data.dtype != np.float32:
405
+ # Check for non-numeric types that cannot be converted
406
+ if audio_data.dtype.kind in ('S', 'U', 'O', 'V'): # bytes, unicode, object, void
407
+ _LOGGER.debug("Audio data has non-numeric dtype: %s, skipping", audio_data.dtype)
408
+ time.sleep(0.01)
409
+ continue
410
  try:
 
 
 
 
 
411
  audio_data = audio_data.astype(np.float32)
412
  except (TypeError, ValueError) as e:
413
+ _LOGGER.debug("Failed to convert audio to float32: %s (dtype=%s)", e, audio_data.dtype)
414
  time.sleep(0.01)
415
  continue
416
 
417
  # Convert stereo to mono (take mean of channels)
418
  # SDK returns shape (samples, 2) for stereo
419
+ # audio_data is already float32 at this point
420
  if audio_data.ndim == 2 and audio_data.shape[1] == 2:
421
+ audio_chunk_array = np.mean(audio_data, axis=1)
422
  elif audio_data.ndim == 2:
423
+ audio_chunk_array = audio_data[:, 0]
424
  elif audio_data.ndim == 1:
425
+ audio_chunk_array = audio_data
426
  else:
427
  _LOGGER.debug("Unexpected audio shape: %s", audio_data.shape)
428
  time.sleep(0.01)