Skip to content

Commit

Permalink
refactor: Replace pydub with soundfile for future Python 3.13 support A…
Browse files Browse the repository at this point in the history
  • Loading branch information
themanyone committed Dec 10, 2024
1 parent 16332b2 commit e6367c5
Showing 1 changed file with 10 additions and 7 deletions.
17 changes: 10 additions & 7 deletions aider/voice.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@
warnings.filterwarnings("ignore", category=SyntaxWarning)


from pydub import AudioSegment # noqa

try:
import soundfile as sf
except (OSError, ModuleNotFoundError):
Expand All @@ -38,7 +36,7 @@ class Voice:

def __init__(self, audio_format="wav", device_name=None):
if sf is None:
raise SoundDeviceError
raise SoundDeviceError("Soundfile library not found. Please install it using 'pip install soundfile'.")
try:
print("Initializing sound device...")
import sounddevice as sd
Expand Down Expand Up @@ -68,7 +66,7 @@ def __init__(self, audio_format="wav", device_name=None):
self.device_id = None

except (OSError, ModuleNotFoundError):
raise SoundDeviceError
raise SoundDeviceError("Sounddevice library not found. Please install it using 'pip install sounddevice'.")
if audio_format not in ["wav", "mp3", "webm"]:
raise ValueError(f"Unsupported audio format: {audio_format}")
self.audio_format = audio_format
Expand Down Expand Up @@ -142,8 +140,12 @@ def raw_record_and_transcribe(self, history, language):

if self.audio_format != "wav":
filename = tempfile.mktemp(suffix=f".{self.audio_format}")
audio = AudioSegment.from_wav(temp_wav)
audio.export(filename, format=self.audio_format)
try:
data, samplerate = sf.read(temp_wav)
sf.write(filename, data, samplerate, format=self.audio_format)
except Exception as e:
print(f"Error converting audio format: {e}")
return None
os.remove(temp_wav)
else:
filename = temp_wav
Expand All @@ -155,7 +157,7 @@ def raw_record_and_transcribe(self, history, language):
)
except Exception as err:
print(f"Unable to transcribe {filename}: {err}")
return
return None

if self.audio_format != "wav":
os.remove(filename)
Expand All @@ -169,3 +171,4 @@ def raw_record_and_transcribe(self, history, language):
if not api_key:
raise ValueError("Please set the OPENAI_API_KEY environment variable.")
print(Voice().record_and_transcribe())

0 comments on commit e6367c5

Please sign in to comment.