-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrecord_and_split.py
82 lines (63 loc) · 2.18 KB
/
record_and_split.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
import torch
import time
import pyaudio
import wave
import os
from utils_vad import get_speech_timestamps, read_audio, init_jit_model, save_audio
# Fix the number of threads (as it is shown in Silero demos)
torch.set_num_threads(1)
# Init the model
model = init_jit_model('./silero_vad.jit')
window_size_samples = 512
# Config for microphone
CHUNK = 1024
FORMAT = pyaudio.paInt16
CHANNELS = 1
RATE = 16000
RECORD_SECONDS = 20
REMOVE_ORIGINAL_RECORDING = False
def split_speech(filename):
wav = read_audio(filename, sampling_rate=16000)
speech_timestamps = get_speech_timestamps(wav,
model,
sampling_rate=16000,
window_size_samples=window_size_samples)
for idx, ts in enumerate(speech_timestamps):
segment = wav[ts['start'] : ts['end']]
new_filename = filename.replace('data/','').replace('.wav', f'_{idx}.wav')
save_dir = f'speech/{new_filename}'
save_audio(save_dir, segment)
print(f'File {save_dir} saved')
if REMOVE_ORIGINAL_RECORDING:
os.remove(filename)
def run():
while True:
output_filename = f"data/recording_{time.time()}.wav"
p = pyaudio.PyAudio()
stream = p.open(format=FORMAT,
channels=CHANNELS,
rate=RATE,
input=True,
frames_per_buffer=CHUNK)
# Recording audio
print("* recording")
frames = []
for _ in range(0, int(RATE / CHUNK * RECORD_SECONDS)):
data = stream.read(CHUNK)
frames.append(data)
print("* done recording")
stream.stop_stream()
stream.close()
p.terminate()
# Save a recording into a WAV file
wf = wave.open(output_filename, 'wb')
wf.setnchannels(CHANNELS)
wf.setsampwidth(p.get_sample_size(FORMAT))
wf.setframerate(RATE)
wf.writeframes(b''.join(frames))
wf.close()
# Split the recording by VAD
split_speech(output_filename)
print('Done, going to the next recording...')
if __name__ == '__main__':
run()