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 "<frozen runpy>", line 198, in _run_module_as_main
  File "<frozen runpy>", line 88, in _run_code
  File "/Users/gauthier/Projects/aider/aider/voice.py", line 42, in <module>
    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.
This commit is contained in:
Paul Gauthier 2023-08-10 15:49:26 -03:00
parent de995a5d62
commit 1f42b0839f

View file

@ -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()