-
Notifications
You must be signed in to change notification settings - Fork 5
/
listen_lobe.py
68 lines (60 loc) · 2.31 KB
/
listen_lobe.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
import os
import sounddevice as sd
import soundfile as sf
from groq import Groq
import threading
class AuroraRecorder:
def __init__(self):
"""Initialize the AuroraRecorder with default settings."""
self.client = Groq() if os.getenv("GROQ_API_KEY") else None
self.recording = False
self.audio_path = "output.wav"
self.transcription = None
self._recording_thread = None
self._lock = threading.Lock()
def start_recording(self):
"""Start recording audio."""
with self._lock:
if self.recording:
print("Already recording")
return
self.recording = True
self.transcription = None
self._recording_thread = threading.Thread(target=self._record_audio)
self._recording_thread.start()
def stop_recording(self):
"""Stop recording audio."""
with self._lock:
if not self.recording:
print("Not recording")
return
self.recording = False
if self._recording_thread:
self._recording_thread.join() # Ensure the recording thread has finished
def _record_audio(self):
"""Record audio in a separate thread."""
print("Recording audio...")
samplerate = 16000 # 16kHz
channels = 1 # mono
recording = sd.rec(int(60 * samplerate), samplerate=samplerate, channels=channels)
while self.recording:
sd.sleep(100)
sd.stop()
sf.write(self.audio_path, recording, samplerate)
print(f"Audio recorded and saved to {self.audio_path}.")
self.transcribe_audio(self.audio_path)
def transcribe_audio(self, file_path):
"""Transcribe the recorded audio using Groq."""
if not os.path.isfile(file_path):
print("The provided file path is not valid.")
return None
with open(file_path, "rb") as file:
response = self.client.audio.transcriptions.create(
file=(file_path, file.read()),
model="whisper-large-v3",
response_format="json",
language="en",
temperature=0.0
)
self.transcription = response.text
print(f"Transcription: {self.transcription}")