-
Notifications
You must be signed in to change notification settings - Fork 1
/
record.py
47 lines (40 loc) · 1.16 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
import pyaudio
import wave
import audioop
FORMAT = pyaudio.paInt16
CHANNELS = 2
RATE = 44100
CHUNK = 1024
RECORD_SECONDS = 2
WAVE_OUTPUT_FILENAME = "file"
audio = pyaudio.PyAudio()
# start Recording
iter = 1
while True:
ip = raw_input()
if ip == "e":
break
if ip == "r":
stream = audio.open(format=FORMAT, channels=CHANNELS,
rate=RATE, input=True,
frames_per_buffer=CHUNK)
print ("recording...")
frames = []
for i in range(0, int(RATE / CHUNK * RECORD_SECONDS)):
data = stream.read(CHUNK)
mx = audioop.max(data, 2)
print (mx)
frames.append(data)
print ("finished recording")
waveFile = wave.open(WAVE_OUTPUT_FILENAME + str(iter) + ".wav", 'wb')
waveFile.setnchannels(CHANNELS)
waveFile.setsampwidth(audio.get_sample_size(FORMAT))
waveFile.setframerate(RATE)
waveFile.writeframes(b''.join(frames))
waveFile.close()
iter += 1
# stop Recording
stream.stop_stream()
stream.close()
audio.terminate()
print("Total files generated : " + str(iter-1))