Download examples/python-operator-dataflow/microphone_op.py from TomPei/csg-robomaster: direct link, hf CLI and curl.
- Browser
- Download file 840 Bytes
-
https://huggingface.co/datasets/TomPei/csg-robomaster/resolve/main/examples/python-operator-dataflow/microphone_op.py
- Command line
-
hf download hf://datasets/TomPei/csg-robomaster/examples/python-operator-dataflow/microphone_op.py
-
curl -L -o microphone_op.py https://huggingface.co/datasets/TomPei/csg-robomaster/resolve/main/examples/python-operator-dataflow/microphone_op.py
840 Bytes
| import numpy as np | |
| import pyarrow as pa | |
| import sounddevice as sd | |
| from dora import DoraStatus | |
| # Set the parameters for recording | |
| SAMPLE_RATE = 16000 | |
| MAX_DURATION = 5 | |
| class Operator: | |
| """ | |
| Microphone operator that records the audio | |
| """ | |
| def on_event( | |
| self, | |
| dora_event, | |
| send_output, | |
| ) -> DoraStatus: | |
| if dora_event["type"] == "INPUT": | |
| audio_data = sd.rec( | |
| int(SAMPLE_RATE * MAX_DURATION), | |
| samplerate=SAMPLE_RATE, | |
| channels=1, | |
| dtype=np.int16, | |
| blocking=True, | |
| ) | |
| audio_data = audio_data.ravel().astype(np.float32) / 32768.0 | |
| if len(audio_data) > 0: | |
| send_output("audio", pa.array(audio_data), dora_event["metadata"]) | |
| return DoraStatus.CONTINUE | |