From 1f42b0839f98b6a8a65202816e57c9bed865182f Mon Sep 17 00:00:00 2001 From: Paul Gauthier Date: Thu, 10 Aug 2023 15:49:26 -0300 Subject: [PATCH] aider: Fixed issue with passing the callback argument to the sd.rec function in record_and_transcribe function. # Aider chat conversation: USER: I ran this command: python -m aider.voice And got this output: Traceback (most recent call last): File "", line 198, in _run_module_as_main File "", line 88, in _run_code File "/Users/gauthier/Projects/aider/aider/voice.py", line 42, in print(record_and_transcribe(api_key)) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/Users/gauthier/Projects/aider/aider/voice.py", line 22, in record_and_transcribe recording = sd.rec(int(sample_rate * duration), samplerate=sample_rate, channels=1, callback=on_key_press) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/Users/gauthier/Projects/aider/.venv/lib/python3.11/site-packages/sounddevice.py", line 276, in rec ctx.start_stream(InputStream, samplerate, ctx.input_channels, TypeError: _CallbackContext.start_stream() got multiple values for argument 'callback' Recording started, press any key to stop... ASSISTANT: It seems like there's an issue with the way the `callback` argument is being passed to the `sd.rec` function in the `record_and_transcribe` function in the `aider/voice.py` file. The `sd.rec` function is a convenience function that internally creates an instance of `sd.InputStream` and starts recording. The `callback` argument should be passed to the `sd.InputStream` constructor, not the `sd.rec` function. Here's how we can fix this: 1. Modify the `aider/voice.py` file. ```python aider/voice.py <<<<<<< HEAD recording = sd.rec(int(sample_rate * duration), samplerate=sample_rate, channels=1, callback=on_key_press) ======= # Create an instance of InputStream with the callback stream = sd.InputStream(samplerate=sample_rate, channels=1, callback=on_key_press) stream.start() recording = sd.rec(int(sample_rate * duration), samplerate=sample_rate, channels=1) >>>>>>> updated ``` This change creates an instance of `sd.InputStream` with the `on_key_press` callback and starts the stream before calling `sd.rec`. This should ensure that the callback is correctly passed to the `InputStream` and not the `rec` function. --- aider/voice.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/aider/voice.py b/aider/voice.py index d9f12fb08..7b697bef1 100644 --- a/aider/voice.py +++ b/aider/voice.py @@ -19,7 +19,10 @@ def record_and_transcribe(api_key): # Start the recording print("Recording started, press any key to stop...") - recording = sd.rec(int(sample_rate * duration), samplerate=sample_rate, channels=1, callback=on_key_press) + # Create an instance of InputStream with the callback + stream = sd.InputStream(samplerate=sample_rate, channels=1, callback=on_key_press) + stream.start() + recording = sd.rec(int(sample_rate * duration), samplerate=sample_rate, channels=1) # Wait for a key press keyboard.wait()