-
Notifications
You must be signed in to change notification settings - Fork 2
/
record.py
54 lines (49 loc) · 1.7 KB
/
record.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
import numpy as np
from pyaudio import PyAudio, paInt16
import wave
import sys, os
import configs
import msvcrt
def save_wave_file(fileName, data):
wf = wave.open(fileName, 'wb')
wf.setnchannels(configs.channels)
wf.setsampwidth(configs.sampwidth)
wf.setframerate(configs.framerate)
wf.writeframes("".join(data))
wf.close()
def record_wave(name, time):
pa = PyAudio()
stream = pa.open(format = paInt16, channels = 1, rate = configs.framerate, input = True, frames_per_buffer = configs.NUM_SAMPLES)
save_buffer = []
count = 0
print "Press Enter to start recording . . ."
msvcrt.getch()
print "File " + name + ".wav recording . . ."
for i in range(0, int(configs.framerate / configs.NUM_SAMPLES * time)):
string_audio_data = stream.read(configs.NUM_SAMPLES)
save_buffer.append(string_audio_data)
count += 1
fileName = name + ".wav"
save_wave_file(fileName, save_buffer)
save_buffer = []
print fileName, "saved"
def main():
if len(sys.argv) < 4:
print "Error arguments!"
exit()
elif len(sys.argv) == 4:
if sys.argv[1] == "-train":
author = sys.argv[2]
count = int(sys.argv[3])
folder_path = r'./wav/' + author + r'/'
if not os.path.isdir(folder_path):
os.mkdir(folder_path)
for i in range(0, count):
record_wave(os.path.join(folder_path, '%d' % i), configs.TIME)
elif sys.argv[1] == "-dialogue":
folder_path = r'./wav/'
fileName = sys.argv[2]
time = int(sys.argv[3])
record_wave(os.path.join(folder_path, fileName), time)
if __name__ == "__main__":
main()