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 834 B

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