I have some problem using pyaudio.
I am working on ubuntu 18.04 using python 3.6.7, pyaudio version 0.2.11 and have the following dependencies:
- libportaudiocpp0
- portaudio19-dev
- libportaudio2-dev
Basically I am running this:
python
import pyaudio
import wave
CHUNK = 2048*50
FORMAT = pyaudio.paInt16
CHANNELS = 2
RATE = 44100
RECORD_SECONDS = 3
WAVE_OUTPUT_FILENAME = "test.wav"
p = pyaudio.PyAudio()
info = p.get_host_api_info_by_index(0)
numdevices = info.get('deviceCount')
for i in range(0, numdevices):
if (p.get_device_info_by_host_api_device_index(0,i).get('maxInputChannels')) > 0:
print("Input Device id ", i, " - ", p.get_device_info_by_host_api_device_index(0, i).get('name'))
stream = p.open(format=FORMAT,
channels=CHANNELS,
rate=RATE,
input=True,
frames_per_buffer=CHUNK, input_device_index=0)
print("* recording")
frames = []
for i in range(0, int(RATE / CHUNK * RECORD_SECONDS)):
data = stream.read(CHUNK)
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()
A .wav file is produced but it doesn't contain any sound.
I know my microphone (the one included in the laptop is working), I can Skype for example.
Here is the output of the script:
ALSA lib pcm_dmix.c:1052:(snd_pcm_dmix_open) unable to open slave ALSA lib pcm_route.c:867:(find_matching_chmap) Found no matching channel >map ALSA lib pcm_dmix.c:1052:(snd_pcm_dmix_open) unable to open slave Input Device id 0 - HDA Intel PCH: ALC3228 Analog (hw:0,0) Input Device id 4 - sysdefault Input Device id 5 - pulse Input Device id 6 - default * recording * done recording