-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaudio-pyaudio-2.py
39 lines (32 loc) · 1.1 KB
/
audio-pyaudio-2.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
"""
Example taken from https://github.com/CristiFati/pyaudio/blob/master/examples/record.py
"""
# import python modules
import wave
# import pyaudio modules
import pyaudio
# declare constants
CHUNK = 1024 # size of audio chunk per sample
FORMAT = pyaudio.paInt16 # 16 Bit integer = CD quality
CHANNELS = 1 # mono
RATE = 44100 # 44.1kHz = CD quality
RECORD_SECONDS = 5 # duration
# open a blank file and get ready to fill it
with wave.open('output.wav', 'wb') as wf:
# start a pyaudio object and set params
p = pyaudio.PyAudio()
wf.setnchannels(CHANNELS)
wf.setsampwidth(p.get_sample_size(FORMAT))
wf.setframerate(RATE)
# open a stream and start filling the pyaudio object
stream = p.open(format=FORMAT, channels=CHANNELS, rate=RATE, input=True)
# write the chunks every cycle to the blank wave file
print('Recording...')
for _ in range(0, RATE // CHUNK * RECORD_SECONDS):
# writeframes!!
wf.writeframes(stream.read(CHUNK))
print('Done')
# close the stream
stream.close()
# and terminate the audio object like a grown up
p.terminate()