-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_recording.py
49 lines (36 loc) · 1.01 KB
/
test_recording.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
# brew install portaudio
# pip install pyaudio
# pip install pydub
# pip install wave
import pyaudio
import wave
from pydub import AudioSegment
chunk = 1024 # Buffer size
sample_format = pyaudio.paInt16 # 16-bit resolution
channels = 1 # Stereo
fs = 44100 # Sample rate
seconds = 5 # Duration of recording
p = pyaudio.PyAudio()
stream = p.open(format=sample_format,
channels=channels,
rate=fs,
frames_per_buffer=chunk,
input=True)
print("Recording...")
frames = []
for i in range(int(fs / chunk * seconds)):
data = stream.read(chunk)
frames.append(data)
print("Finished recording.")
stream.stop_stream()
stream.close()
p.terminate()
wave_filename = "output.wav"
wf = wave.open(wave_filename, 'wb')
wf.setnchannels(channels)
wf.setsampwidth(p.get_sample_size(sample_format))
wf.setframerate(fs)
wf.writeframes(b''.join(frames))
wf.close()
sound = AudioSegment.from_file("output.wav")
sound.export("output_file.mp3", format="mp3", bitrate="128k")