pyalsaaudio
pyalsaaudio copied to clipboard
Recorded stereo audio is half the length of mono audio
I've just tried the following script on my Raspberry Pi:
if __name__ == "__main__":
fs = 192000
channels = 2
period = int(fs * 0.005)
devices_capture = alsaaudio.pcms(alsaaudio.PCM_CAPTURE)
for device_capture in devices_capture:
if 'hw' in device_capture and 'hifiberry' in device_capture:
break
inp = alsaaudio.PCM(alsaaudio.PCM_CAPTURE, alsaaudio.PCM_NONBLOCK, channels=channels, rate=fs, format=alsaaudio.PCM_FORMAT_FLOAT_LE, periodsize=period, device=device_capture)
keep = []
t0 = time.time()
while (time.time() - t0) < 10:
l, data = inp.read()
if l:
aa = numpy.frombuffer(data, numpy.float32)
keep.append(aa)
time.sleep(0.001)
inp.close()
alll = numpy.concatenate(keep)
(the hifiberry is a high quality audio capture card for the Pi)
This results in a numpy array of shape (1919836,). If we divide by the sample rate we get 1919836/192000=9.99914583 seconds...but that would be mono audio: a recording with two channels should have double that number of samples.
If I change the channels variable to just 1, I get a final shape of (1919892,), effectively the same value. I also changed the period to 1024 with no effect.
I can record 2-channel (32-bit floating point, 192000 Hz) audio just fine to normal lengths with arecord, and in either case I can clearly see signals only on the left or right channel (so it really is stereo, it's just not pulling everything).
What could be a possible reason for this?
first, inp.dumpinfo(), to see whether the settings are actually what you intended.
also, don't make the pcm non-blocking - that way you're getting quite the busy loop. if that also changes the output, we'd have a starting point.