Skip to content

Commit

Permalink
refactor: Use temporary variable for audio format to avoid state muta…
Browse files Browse the repository at this point in the history
…tion
  • Loading branch information
paul-gauthier committed Jan 4, 2025
1 parent de1d566 commit 01ef235
Showing 1 changed file with 7 additions and 5 deletions.
12 changes: 7 additions & 5 deletions aider/voice.py
Original file line number Diff line number Diff line change
Expand Up @@ -141,18 +141,20 @@ def raw_record_and_transcribe(self, history, language):
while not self.q.empty():
file.write(self.q.get())

use_audio_format = self.audio_format

# Check file size and offer to convert to mp3 if too large
file_size = os.path.getsize(temp_wav)
if file_size > 24.9 * 1024 * 1024 and self.audio_format == "wav":
print("\nWarning: {temp_wav} is too large, switching to mp3 format.")
self.audio_format = "mp3"
use_audio_format = "mp3"

filename = temp_wav
if self.audio_format != "wav":
if use_audio_format != "wav":
try:
new_filename = tempfile.mktemp(suffix=f".{self.audio_format}")
new_filename = tempfile.mktemp(suffix=f".{use_audio_format}")
audio = AudioSegment.from_wav(temp_wav)
audio.export(filename, format=self.audio_format)
audio.export(filename, format=use_audio_format)
os.remove(temp_wav)
filename = new_filename
except (CouldntDecodeError, CouldntEncodeError) as e:
Expand All @@ -171,7 +173,7 @@ def raw_record_and_transcribe(self, history, language):
print(f"Unable to transcribe {filename}: {err}")
return

if self.audio_format != "wav":
if filename != temp_wav:
os.remove(filename)

text = transcript.text
Expand Down

0 comments on commit 01ef235

Please sign in to comment.