-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathusb_audio
More file actions
52 lines (40 loc) · 1.17 KB
/
usb_audio
File metadata and controls
52 lines (40 loc) · 1.17 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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Fri Feb 28 11:34:35 2020
@author: solasholola
"""
import pyaudio
import wave
FORMAT = pyaudio.paInt16 # Records in 16bits
CHANNELS = 1 # Mono channel
RATE = 44100 # Sample Rate
CHUNK = 1024 #byte size
RECORD_SECONDS = 5
WAVE_OUTPUT_FILENAME = "file.wav"
audio = pyaudio.PyAudio()
# start Recording
stream = audio.open(format = FORMAT,
channels = CHANNELS,
rate = RATE,
input = True,
frames_per_buffer = CHUNK,
# USB
input_device_index = 0)
print ("recording...")
frames = []
for i in range(0, int((RATE / CHUNK) * RECORD_SECONDS)):
data = stream.read(CHUNK, exception_on_overflow = False)
frames.append(data)
print ("finished recording")
# stop Recording
stream.stop_stream()
stream.close()
audio.terminate()
# creates wave file with audio read in
waveFile = wave.open(WAVE_OUTPUT_FILENAME, 'wb')
waveFile.setnchannels(CHANNELS)
waveFile.setsampwidth(audio.get_sample_size(FORMAT))
waveFile.setframerate(RATE)
waveFile.writeframes(b''.join(frames))
waveFile.close()