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

123456789101112131415161718192021222324252627282930313233343536
  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. """
  10. Microphone operator that records the audio
  11. """
  12. def on_event(
  13. self,
  14. dora_event,
  15. send_output,
  16. ) -> DoraStatus:
  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. elif dora_event["type"] == "INPUT":
  29. print("Microphone is not recording", dora_event["value"][0].as_py())
  30. return DoraStatus.CONTINUE