-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrecords.py
46 lines (34 loc) · 954 Bytes
/
records.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
#! /usr/bin/env python3
import wave
import pyaudio
RATE: int = 8000
CHUNK: int = int(RATE / 2)
FORMAT = pyaudio.paInt16
CHANNELS: int = 1
Record_Seconds: int = 10
def main() -> None:
p = pyaudio.PyAudio()
stream = p.open(
format=FORMAT,
channels=CHANNELS,
rate=RATE,
input=True,
frames_per_buffer=CHUNK
)
print("now recording...")
all = []
for i in range(0, int(RATE / CHUNK * Record_Seconds)):
frames = stream.read(CHUNK)
print(f"-------------- {type(frames)}, {len(frames)}, {frames[0]}, {frames[1]}")
all.append(frames)
print("Finished Recording...")
stream.close()
p.terminate()
wavFile = wave.open("output.wav", "wb")
wavFile.setnchannels(CHANNELS)
wavFile.setsampwidth(p.get_sample_size(FORMAT))
wavFile.setframerate(RATE)
wavFile.writeframes(b"".join(all))
wavFile.close()
if __name__ == "__main__":
main()