Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: save microphone output to audio files #121

Merged
merged 3 commits into from
Dec 1, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -133,4 +133,8 @@ common/Perception/face_detection/output/
common/Perception/lasr_object_detection_yolo/models
.vscode/
common/navigation/choosing_wait_position/src/choosing_wait_position/final_lift_key_point/models
legacy/choosing_wait_position/src/choosing_wait_position/final_lift_key_point/models/keypointsrcnn_weights.pth
legacy/choosing_wait_position/src/choosing_wait_position/final_lift_key_point/models/keypointsrcnn_weights.pth

# Python extension setup files
.pylintrc
mypy.ini
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,7 @@ catkin_install_python(PROGRAMS
nodes/simple_transcribe_microphone
nodes/transcribe_microphone
scripts/list_microphones.py
scripts/test_microphones.py
DESTINATION ${CATKIN_PACKAGE_BIN_DESTINATION}
)

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
#!/usr/bin/env python3

import os
import argparse
import speech_recognition as sr


def parse_args() -> dict:
"""Parse command line arguments into a dictionary.

Returns:
dict: name: value pairs of command line arguments
"""

parser = argparse.ArgumentParser(description="Test microphones")
parser.add_argument("-m", "--microphone", type=int, help="Microphone index")
parser.add_argument(
"-o", "--output_dir", type=str, help="Directory to save audio files"
)

return vars(parser.parse_args())


def main(args: dict) -> None:
"""Generate audio files from microphone input.

Args:
args (dict): dictionary of command line arguments.
"""

# Adapted from https://github.com/Uberi/speech_recognition/blob/master/examples/write_audio.py

mic_index = args["microphone"]
output_dir = args["output_dir"]

r = sr.Recognizer()
with sr.Microphone(device_index=mic_index) as source:
print("Say something!")
audio = r.listen(source)

with open(os.path.join(output_dir, "microphone.raw"), "wb") as f:
f.write(audio.get_raw_data())

with open(os.path.join(output_dir, "microphone.wav"), "wb") as f:
f.write(audio.get_wav_data())

with open(os.path.join(output_dir, "microphone.flac"), "wb") as f:
f.write(audio.get_flac_data())

with open(os.path.join(output_dir, "microphone.aiff"), "wb") as f:
f.write(audio.get_aiff_data())


if __name__ == "__main__":
main(parse_args())