-
Notifications
You must be signed in to change notification settings - Fork 0
/
recorder.py
105 lines (87 loc) · 3.07 KB
/
recorder.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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
import pyaudio
import wave
import os
import time
import threading
import queue
from groq import Groq
# Audio recording parameters
CHUNK = 1024
FORMAT = pyaudio.paInt16
CHANNELS = 1
RATE = 44100
WAVE_OUTPUT_FILENAME = "temp_audio.wav"
class AudioRecorder:
def __init__(self):
self.audio_queue = queue.Queue()
self.transcription_queue = queue.Queue()
self.stop_recording = threading.Event()
self.client = Groq()
def record_audio(self):
p = pyaudio.PyAudio()
stream = p.open(format=FORMAT,
channels=CHANNELS,
rate=RATE,
input=True,
frames_per_buffer=CHUNK)
while not self.stop_recording.is_set():
data = stream.read(CHUNK)
self.audio_queue.put(data)
stream.stop_stream()
stream.close()
p.terminate()
def save_audio(self, frames):
wf = wave.open(WAVE_OUTPUT_FILENAME, 'wb')
wf.setnchannels(CHANNELS)
wf.setsampwidth(pyaudio.PyAudio().get_sample_size(FORMAT))
wf.setframerate(RATE)
wf.writeframes(b''.join(frames))
wf.close()
def transcribe(self):
with open(WAVE_OUTPUT_FILENAME, "rb") as file:
transcription = self.client.audio.transcriptions.create(
file=(WAVE_OUTPUT_FILENAME, file.read()),
model="whisper-large-v3-turbo",
response_format="verbose_json",
)
return transcription.text
def transcribe_audio(self):
while not self.stop_recording.is_set():
frames = []
start_time = time.time()
while time.time() - start_time < 5 and not self.stop_recording.is_set(): # Collect 5 seconds of audio
try:
frames.append(self.audio_queue.get(timeout=0.1))
except queue.Empty:
continue
if frames:
self.save_audio(frames)
text = self.transcribe()
if text:
self.transcription_queue.put(text)
os.remove(WAVE_OUTPUT_FILENAME)
def continuous_transcribe(self):
self.stop_recording.clear()
self.audio_queue = queue.Queue()
self.transcription_queue = queue.Queue()
record_thread = threading.Thread(target=self.record_audio)
transcribe_thread = threading.Thread(target=self.transcribe_audio)
record_thread.start()
transcribe_thread.start()
try:
while not self.stop_recording.is_set():
try:
yield self.transcription_queue.get(timeout=0.1)
except queue.Empty:
continue
finally:
self.stop_recording.set()
record_thread.join()
transcribe_thread.join()
def stop(self):
self.stop_recording.set()
recorder = AudioRecorder()
def continuous_transcribe():
return recorder.continuous_transcribe()
def stop_recording():
recorder.stop()