You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

microphone_op.py 893 B

10 months ago
10 months ago
10 months ago
12345678910111213141516171819202122232425262728293031323334
  1. """TODO: Add docstring."""
  2. import numpy as np
  3. import pyarrow as pa
  4. import sounddevice as sd
  5. from dora import DoraStatus
  6. # Set the parameters for recording
  7. SAMPLE_RATE = 16000
  8. MAX_DURATION = 5
  9. class Operator:
  10. """Microphone operator that records the audio."""
  11. def on_event(
  12. self,
  13. dora_event,
  14. send_output,
  15. ) -> DoraStatus:
  16. """TODO: Add docstring."""
  17. if dora_event["type"] == "INPUT":
  18. audio_data = sd.rec(
  19. int(SAMPLE_RATE * MAX_DURATION),
  20. samplerate=SAMPLE_RATE,
  21. channels=1,
  22. dtype=np.int16,
  23. blocking=True,
  24. )
  25. audio_data = audio_data.ravel().astype(np.float32) / 32768.0
  26. if len(audio_data) > 0:
  27. send_output("audio", pa.array(audio_data), dora_event["metadata"])
  28. return DoraStatus.CONTINUE