Skip to content

Commit

Permalink
refactor: Simplify audio file handling and conversion logic
Browse files Browse the repository at this point in the history
  • Loading branch information
paul-gauthier committed Jan 4, 2025
1 parent 19114a6 commit de1d566
Showing 1 changed file with 3 additions and 10 deletions.
13 changes: 3 additions & 10 deletions aider/voice.py
Original file line number Diff line number Diff line change
Expand Up @@ -147,27 +147,20 @@ def raw_record_and_transcribe(self, history, language):
print("\nWarning: {temp_wav} is too large, switching to mp3 format.")
self.audio_format = "mp3"

filename = temp_wav
if self.audio_format != "wav":
try:
filename = tempfile.mktemp(suffix=f".{self.audio_format}")
new_filename = tempfile.mktemp(suffix=f".{self.audio_format}")
audio = AudioSegment.from_wav(temp_wav)
audio.export(filename, format=self.audio_format)
os.remove(temp_wav)
print(
f"Converted to {self.audio_format}, new size:"
f" {os.path.getsize(filename) / 1024 / 1024:.1f}MB"
)
filename = new_filename
except (CouldntDecodeError, CouldntEncodeError) as e:
print(f"Error converting audio: {e}")
filename = temp_wav # fall back to original file
except (OSError, FileNotFoundError) as e:
print(f"File system error during conversion: {e}")
filename = temp_wav # fall back to original file
except Exception as e:
print(f"Unexpected error during audio conversion: {e}")
filename = temp_wav # fall back to original file
else:
filename = temp_wav

with open(filename, "rb") as fh:
try:
Expand Down

0 comments on commit de1d566

Please sign in to comment.