I need to capture 192 kHz audio for some bioacoustics experiments using Python 3. I have the hardware, a Sound Devices USBPre 2 sound card, a microphone with good frequency response curve up to 100 kHz, and I have enabled my os (ubuntu 13.04) to sample from this card at 192 kHz.
I have tried recording with PyAudio. It appears to work, and will give me a wav file with a sampling rate of 192 kHz. However, when I look at the spectra there is no power above 24 kHz, suggesting that PyAudio doesn't really capture at 192 kHz, but rather at 48 kHz. However, when I record using Audacity with input from JACK, I get a nice recording with power up to 96kHz. So, I have the impression that PyAudio doesn't actually sample the sound at 192 kHz even though it should be able to. How to fix this?
I start JACK without errors:
/usr/bin/jackd -R -dalsa -Chw:1,0 -n3 -o1 -p2048 -r192000
jackd 0.122.0
Copyright 2001-2009 Paul Davis, Stephane Letz, Jack O'Quinn, Torben Hohn and others.
jackd comes with ABSOLUTELY NO WARRANTY
This is free software, and you are welcome to redistribute it
under certain conditions; see the file COPYING for details
JACK compiled with System V SHM support.
loading driver ..
apparent rate = 192000
creating alsa driver ... -|hw:1,0|2048|3|192000|0|1|nomon|swmeter|-|32bit
control device hw:0
configuring for 192000Hz, period = 2048 frames (10.7 ms), buffer = 3 periods
ALSA: final selected sample format for capture: 24bit little-endian
ALSA: use 3 periods for capture
Initialize PyAudio (without any real errors (as far as I can tell)):
p = pyaudio.PyAudio()
ALSA lib pcm.c:2217:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.rear
ALSA lib pcm.c:2217:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.center_lfe
ALSA lib pcm.c:2217:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.side
bt_audio_service_open: connect() failed: Connection refused (111)
bt_audio_service_open: connect() failed: Connection refused (111)
bt_audio_service_open: connect() failed: Connection refused (111)
bt_audio_service_open: connect() failed: Connection refused (111)
ALSA lib pcm_dmix.c:957:(snd_pcm_dmix_open) The dmix plugin supports only playback stream
Open a PyAudio stream:
stream = p.open(format=pyaudio.paInt32,
channels=1,rate=192000,
input=True,
frames_per_buffer=2048)
I have images of the spectrograms, in case someone wants to verify my interpretation that PyAudio doesn't capture at 192 kHz (but Audacity does):
Spectrogram of sound captured usig PyAudio
Spectrogram of sound captured usig Audacity
How can I record sound at 192 000 samples/s using PyAudio? Suggestions of other ways to capture sound using Python 3 are also welcome.
p.open(..., input_device_index=dev_idx)
, and you make sure you use the right one, or loop over them to try them all. Same for gathering device info. – Kwangtungp.open(..., input_device_index=dev_idx)
I get the following errorOSError: [Errno Invalid number of channels] -9998
, similar to what I get when tryp.is_format_supported(...)
:ValueError: ('Invalid number of channels', -9998)
. – Koestler[Errno Input overflowed] -9981
. I managed to probe for the right configuration (got it working just now) by looping over various sample rates, channel numbers and device indexes. I'll try to pretty up the code a bit and come up with an "answer", maybe it'll be of some help to you. – Kwangtungpyaudio.paFloat32
instead ofpyaudio.paInt32
as your format? (Still cleaning up my code) – KwangtungOSError: [Errno Invalid number of channels] -9998
– Koestler