I'm trying to run a bog-standard PyAudio script to get audio from my microphone and having issues. The code picks up the microphone device, but when I try to access the data via stream.read I get a byte string of 0s. I know the microphone works, I have captured my voice using Audacity so I have no
idea what is going wrong.
Here is the code. Requires pyaudio wave and sounddevice to run:
python -m pip install pyaudio wave sounddevice
import pyaudio
import wave
CHUNK = 1024
FORMAT = pyaudio.paInt16
CHANNELS = 2#1#2
RATE = 44100#16000#44100
RECORD_SECONDS = 5
WAVE_OUTPUT_FILENAME = "output.wav"
p = pyaudio.PyAudio()
# pick device
import sounddevice as sd
print(sd.query_devices())
device_num = int(input("choose device: "))
print(sd.query_devices(device_num))
# record
stream = p.open(format=FORMAT,
channels=CHANNELS,
rate=RATE,
input=True,
output=False,
frames_per_buffer=CHUNK,
input_device_index=device_num
)
print("* recording")
frames = []
for i in range(0, int(RATE / CHUNK * RECORD_SECONDS)):
data = stream.read(CHUNK)
print(data[:10])
frames.append(data)
print("* done recording")
stream.stop_stream()
stream.close()
p.terminate()
wf = wave.open(WAVE_OUTPUT_FILENAME, 'wb')
wf.setnchannels(CHANNELS)
wf.setsampwidth(p.get_sample_size(FORMAT))
wf.setframerate(RATE)
wf.writeframes(b''.join(frames))
wf.close()
gets me the following:
ALSA lib pcm.c:2495:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.rear
ALSA lib pcm.c:2495:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.center_lfe
ALSA lib pcm.c:2495:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.side
ALSA lib pcm_route.c:867:(find_matching_chmap) Found no matching channel map
ALSA lib pcm_route.c:867:(find_matching_chmap) Found no matching channel map
ALSA lib pcm_route.c:867:(find_matching_chmap) Found no matching channel map
ALSA lib pcm_route.c:867:(find_matching_chmap) Found no matching channel map
0 HDA Intel PCH: ALC289 Analog (hw:0,0), ALSA (2 in, 2 out)
1 HDA Intel PCH: HDMI 0 (hw:0,3), ALSA (0 in, 8 out)
2 HDA Intel PCH: HDMI 1 (hw:0,7), ALSA (0 in, 8 out)
3 HDA Intel PCH: HDMI 2 (hw:0,8), ALSA (0 in, 8 out)
4 HDA Intel PCH: HDMI 3 (hw:0,9), ALSA (0 in, 8 out)
5 HDA Intel PCH: HDMI 4 (hw:0,10), ALSA (0 in, 8 out)
6 HDA NVidia: HDMI 0 (hw:1,3), ALSA (0 in, 8 out)
7 HDA NVidia: HDMI 1 (hw:1,7), ALSA (0 in, 8 out)
8 HDA NVidia: HDMI 2 (hw:1,8), ALSA (0 in, 8 out)
9 HDA NVidia: HDMI 3 (hw:1,9), ALSA (0 in, 8 out)
10 HDA NVidia: HDMI 4 (hw:1,10), ALSA (0 in, 8 out)
11 sysdefault, ALSA (128 in, 128 out)
12 front, ALSA (0 in, 2 out)
13 surround40, ALSA (0 in, 2 out)
14 surround51, ALSA (0 in, 2 out)
15 surround71, ALSA (0 in, 2 out)
16 hdmi, ALSA (0 in, 8 out)
17 pulse, ALSA (32 in, 32 out)
18 dmix, ALSA (0 in, 2 out)
* 19 default, ALSA (32 in, 32 out) <-- this is the device I am using
choose device: 19
{'name': 'default', 'index': 19, 'hostapi': 0, 'max_input_channels': 32, 'max_output_channels': 32, 'default_low_input_latency': 0.008707482993197279, 'default_low_output_latency': 0.008707482993197279, 'default_high_input_latency': 0.034829931972789115, 'default_high_output_latency': 0.034829931972789115, 'default_samplerate': 44100.0}
* recording
b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00'
b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00'
<repeats this for a while>
b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00'