-
Notifications
You must be signed in to change notification settings - Fork 0
/
audiotest.py
executable file
·46 lines (38 loc) · 1.34 KB
/
audiotest.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
import pyaudio
import wave
FORMAT = pyaudio.paInt16
CHANNELS = 1 # Number of channels
BITRATE = 44100 # Audio Bitrate
CHUNK_SIZE = 512 # Chunk size to
RECORDING_LENGTH = 10 # Recording Length in seconds
WAVE_OUTPUT_FILENAME = "audiotest.wav"
audio = pyaudio.PyAudio()
info = audio.get_host_api_info_by_index(0)
numdevices = info.get('deviceCount')
for i in range(0, numdevices):
if (audio.get_device_info_by_host_api_device_index(0, i).get('maxInputChannels')) > 0:
print("Input Device id ", i, " - ", audio.get_device_info_by_host_api_device_index(0, i).get('name'))
print("Which Input Device ID would you like to use?")
device_id = int(input()) # Choose a device
print("Recording using Input Device ID "+str(device_id))
stream = audio.open(
format=FORMAT,
channels=CHANNELS,
rate=BITRATE,
input=True,
input_device_index = device_id,
frames_per_buffer=CHUNK_SIZE
)
recording_frames = []
for i in range(int(BITRATE / CHUNK_SIZE * RECORDING_LENGTH)):
data = stream.read(CHUNK_SIZE)
recording_frames.append(data)
stream.stop_stream()
stream.close()
audio.terminate()
waveFile = wave.open(WAVE_OUTPUT_FILENAME, 'wb')
waveFile.setnchannels(CHANNELS)
waveFile.setsampwidth(audio.get_sample_size(FORMAT))
waveFile.setframerate(BITRATE)
waveFile.writeframes(b''.join(recording_frames))
waveFile.close()