diff options
| author | Eddy Pedroni <epedroni@pm.me> | 2026-10-13 20:36:02 +0200 |
|---|---|---|
| committer | Eddy Pedroni <epedroni@pm.me> | 2026-10-13 20:36:02 +0200 |
| commit | 05a3406de3e26d21865db87960f80c5f40f9cd83 (patch) | |
| tree | 372597a6c1210cb1203238b7193ce6dcd565a5fa /solo-tool-project/src | |
| parent | edc8d820df84c147ddb6e723b9639bddd6d15344 (diff) | |
Add option to specify audio device (only applicable for metronome and recorder)
Diffstat (limited to 'solo-tool-project/src')
| -rw-r--r-- | solo-tool-project/src/solo_tool/metronome.py | 6 | ||||
| -rw-r--r-- | solo-tool-project/src/solo_tool/recorder.py | 6 | ||||
| -rw-r--r-- | solo-tool-project/src/solo_tool/utils.py | 7 |
3 files changed, 17 insertions, 2 deletions
diff --git a/solo-tool-project/src/solo_tool/metronome.py b/solo-tool-project/src/solo_tool/metronome.py index 9c65357..3ffc6bc 100644 --- a/solo-tool-project/src/solo_tool/metronome.py +++ b/solo-tool-project/src/solo_tool/metronome.py @@ -1,6 +1,8 @@ import pyaudio as pa from array import array +from solo_tool.utils import getAudioDeviceIndex + def _createClickSample(clickFrequency: int, samplingRate: int) -> array: clickDuration = 0.03 # seconds @@ -22,9 +24,10 @@ def _createOutputBuffer(bpm: int, clickFrequency: int, samplingRate: int) -> arr return clickSample + array('h', [0] * (samplesPerBeat - len(clickSample))) class Metronome: - def __init__(self, bufferSize: int, samplingRate: int): + def __init__(self, bufferSize: int, samplingRate: int, deviceName: str): self._bufferSize = bufferSize self._samplingRate = samplingRate + self._deviceName = deviceName self._sampleFormat = pa.paInt16 self._channels = 1 self._pa = pa.PyAudio() @@ -58,6 +61,7 @@ class Metronome: rate=self._samplingRate, frames_per_buffer=self._bufferSize, output=True, + output_device_index=getAudioDeviceIndex(self._deviceName, self._pa), stream_callback=self._callback) def stop(self) -> None: diff --git a/solo-tool-project/src/solo_tool/recorder.py b/solo-tool-project/src/solo_tool/recorder.py index 71c8ac8..c628832 100644 --- a/solo-tool-project/src/solo_tool/recorder.py +++ b/solo-tool-project/src/solo_tool/recorder.py @@ -3,6 +3,8 @@ from pathlib import Path import pyaudio as pa from pydub import AudioSegment, effects +from solo_tool.utils import getAudioDeviceIndex + class Recording: def __init__(self, frames: list, channels: int, samplingRate: int, sampleFormat: int): self._segment = AudioSegment( @@ -23,9 +25,10 @@ class Recording: self._segment.export(str(file), format="mp3", bitrate="320k") class Recorder: - def __init__(self, bufferSize: int, samplingRate: int): + def __init__(self, bufferSize: int, samplingRate: int, deviceName: str): self._bufferSize = bufferSize self._samplingRate = samplingRate + self._deviceName = deviceName self._sampleFormat = pa.paInt16 self._channels = 2 self._pa = pa.PyAudio() @@ -56,6 +59,7 @@ class Recorder: rate=self._samplingRate, frames_per_buffer=self._bufferSize, input=True, + input_device_index=getAudioDeviceIndex(self._deviceName, self._pa), stream_callback=self._callback) def stopRecording(self) -> Recording: diff --git a/solo-tool-project/src/solo_tool/utils.py b/solo-tool-project/src/solo_tool/utils.py new file mode 100644 index 0000000..df39fd9 --- /dev/null +++ b/solo-tool-project/src/solo_tool/utils.py @@ -0,0 +1,7 @@ +from pyaudio import PyAudio + +def getAudioDeviceIndex(name: str, pa: PyAudio) -> int: + for i in range(pa.get_device_count()): + if pa.get_device_info_by_index(i)["name"].startswith(name): + return i + raise Exception(f"Audio device not found: {name}") |
