-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpyroy.py
More file actions
60 lines (43 loc) · 1.18 KB
/
pyroy.py
File metadata and controls
60 lines (43 loc) · 1.18 KB
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
import wave
import pyaudio
CHUNK = 1024
FORMAT = pyaudio.paInt16
CHANNELS = 2
RATE = 4410
RECORD_SECONDS = 5
WAVE_OUTPUT_FILENAME = "output.wav"
if __name__ == "__main__":
p = pyaudio.PyAudio()
stream = p.open(
format=FORMAT,
channels=CHANNELS,
rate=RATE,
input=True,
frames_per_buffer=CHUNK,
)
print("* first recording")
first_frames = []
for i in range(0, int(RATE / CHUNK * RECORD_SECONDS)):
data = stream.read(CHUNK)
first_frames.append(data)
print("* done first recording")
print("* second recording")
second_frames = []
for i in range(0, int(RATE / CHUNK * RECORD_SECONDS)):
data = stream.read(CHUNK)
second_frames.append(data)
print("* done second recording")
stream.stop_stream()
stream.close()
p.terminate()
print(len(first_frames))
print(len(second_frames))
frames = first_frames[:]
for x in second_frames:
frames.append(x)
wf = wave.open(WAVE_OUTPUT_FILENAME, "wb")
wf.setnchannels(CHANNELS)
wf.setsampwidth(p.get_sample_size(FORMAT))
wf.setframerate(RATE)
wf.writeframes(b"".join(frames))
wf.close()