I'm trying to record some sound on a Raspberry Pi using sounddevice in python. I have been following the solution from this post. When I run the program below, it all works for a short time (time may vary) and then either starts detecting no sound (volume_norm is always 0) or it hangs, eventually crashing or needing to be interrupted.
import numpy as np
import sounddevice as sd
from datetime import datetime
duration = 1800 # in seconds
scaling_factor = 100
def audio_callback(indata, frames, time, status):
current = datetime.now()
volume_norm = np.linalg.norm(indata) * scaling_factor
print("{0}\t{1}".format(current.strftime("%d/%m/%Y %H:%M:%S"), volume_norm))
stream = sd.InputStream(callback=audio_callback)
with stream:
sd.sleep(duration * 1000)
Whether the program hangs, crashes or is interrupted the same message is displayed:
Expression 'alsa_snd_pcm_drop( stream->capture.pcm )' failed in 'src/hostapi/alsa/pa_linux_alsa.c', line: 3046
I've not managed to find anything when Googling that error message. In case it's important I can check that sounddevice can detect the USB microphone and can see:
{'hostapi': 0, 'default_high_output_latency': -1.0, 'default_low_input_latency': 0.008684807256235827, 'default_samplerate': 44100.0, 'max_input_channels': 1, 'default_low_output_latency': -1.0, 'max_output_channels': 0, 'default_high_input_latency': 0.034829931972789115, 'name': 'USB PnP Sound Device: Audio (hw:1,0)'}
Please let me know if anyone knows how to solve this?