-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrecording.py
73 lines (53 loc) · 1.79 KB
/
recording.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
import sounddevice as sd
from scipy.io.wavfile import write
import whisper
import subprocess
from pydub import AudioSegment
import queue
import tempfile
import sys
import soundfile as sf
import os
q = queue.Queue()
filename = None
def callback(indata, frames, time, status):
"""This is called (from a separate thread) for each audio block."""
if status:
print(status, file=sys.stderr)
q.put(indata.copy())
try:
device_info = sd.query_devices(kind='input')
# soundfile expects an int, sounddevice provides a float:
samplerate = int(16000)
filename = tempfile.mktemp(prefix='recording_',
suffix='.wav', dir='')
# Make sure the file is opened before recording anything:
print("Starting....")
with sf.SoundFile(filename, mode='x', samplerate=samplerate,
channels=1) as file:
with sd.InputStream(samplerate=samplerate,
channels=1, callback=callback):
print('#' * 80)
print('press Ctrl+C to stop the recording')
print('#' * 80)
while True:
file.write(q.get())
except KeyboardInterrupt:
print('\nRecording finished: ' + repr(filename))
sd.wait()
#write('output.wav', fs, myrecording)
print("-----Finished Recording-----")
#sounds = AudioSegment.from_wav(filename)
#sounds.export("output.mp3", format="mp3")
print("-----Converting Audio-----")
isCPP = True
if isCPP:
subprocess.run("cd whisper.cpp && ./main -nt -otxt -f " + filename, shell=True)
else:
model = whisper.load_model('base')
result = model.transcribe(filename)
print(result)
#Write the result to a text file
with open('result.txt', 'w') as f:
f.write(result["text"])
#Execute the whisper cpp program