I'm very new to coding, so please excuse any obvious beginner's mistakes. I'm trying to make a Raspberry Pi 4 (running Bullseye) continuously listen for any noise over a certain volume using a USB mic, and when it hears a sound, play a random .wav file from a folder using a speaker in the 3.5mm jack. The program works perfectly in the compiler (Geany) and when I launch it from the terminal. This is the code from the program:
The problem I'm having is that I can't get it to work at reboot. I want it to start listening automatically once power is supplied to the Raspberry Pi. I've tried using crontab by following a few tutorials, but have had no luck. Currently the line added to crontab and the launcher.sh file are as follows:
When I run the program, although it works as expected, it spits out a bunch of error messages first. This is what it says when I run it in the compiler:
The logs from trying to run at reboot using crontab are similar but the last few lines are different:
My working theory is that those different lines are pointing to the solution. Unfortunately as a novice I don't really understand how the last line ("OSError: [Errno -9996] Invalid input device (no default output device)") can be correct if the USB mic and speaker work fine when launched manually. Is there some behavior I'm overlooking here? Are the devices not active/recognizable when the program is run at reboot? Could delaying the program help? I think I'm grasping at straws here.
It could be these error messages have nothing to do with the program not running on startup, but I can't figure out what else it might be. It seems odd that it works okay despite all the errors when I start it manually so I'm a bit stumped. If anyone has any ideas why this would work when I start the program manually but isn't working on reboot I would really appreciate it.
Sorry if I've explained anything poorly or left something important out, I'm self-taught so my knowledge is patchy. Hopefully the solution is something simple and obvious!
Code:
import simpleaudio as saimport os, randomimport numpyimport pyaudioimport analyse# Initialize PyAudiopyaud = pyaudio.PyAudio()stream = pyaud.open(format = pyaudio.paInt16,channels = 1,rate=44100,input=True)while True: # Read raw microphone data rawsamps = stream.read(1024) # Convert raw data to NumPy array samps = numpy.fromstring(rawsamps, dtype=numpy.int16) # Show the volume and pitch volume = (analyse.loudness(samps)) print (volume) if (volume > -7): newobj=random.choice(os.listdir("/home/galwaybae/wavs")) wave_obj = sa.WaveObject.from_wave_file("/home/galwaybae/wavs/"+newobj) play_obj = wave_obj.play() play_obj.wait_done()
The problem I'm having is that I can't get it to work at reboot. I want it to start listening automatically once power is supplied to the Raspberry Pi. I've tried using crontab by following a few tutorials, but have had no luck. Currently the line added to crontab and the launcher.sh file are as follows:
Code:
@reboot sh /home/galwaybae/launcher.sh >/home/pi/logs/cronlog 2>&1
Code:
cd /cd home/galwaybaepython prog.pycd /
When I run the program, although it works as expected, it spits out a bunch of error messages first. This is what it says when I run it in the compiler:
Code:
Expression 'GetExactSampleRate( hwParams, &defaultSr )' failed in 'src/hostapi/alsa/pa_linux_alsa.c', line: 895Expression 'GetExactSampleRate( hwParams, &defaultSr )' failed in 'src/hostapi/alsa/pa_linux_alsa.c', line: 895Expression 'GetExactSampleRate( hwParams, &defaultSr )' failed in 'src/hostapi/alsa/pa_linux_alsa.c', line: 895ALSA lib pcm.c:2660:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.rearALSA lib pcm.c:2660:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.center_lfeALSA lib pcm.c:2660:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.sideALSA lib confmisc.c:1281:(snd_func_refer) Unable to find definition 'cards.bcm2835_headpho.pcm.surround51.0:CARD=0'ALSA lib conf.c:4745:(_snd_config_evaluate) function snd_func_refer returned error: No such file or directoryALSA lib conf.c:5233:(snd_config_expand) Evaluate error: No such file or directoryALSA lib pcm.c:2660:(snd_pcm_open_noupdate) Unknown PCM surround21ALSA lib confmisc.c:1281:(snd_func_refer) Unable to find definition 'cards.bcm2835_headpho.pcm.surround51.0:CARD=0'ALSA lib conf.c:4745:(_snd_config_evaluate) function snd_func_refer returned error: No such file or directoryALSA lib conf.c:5233:(snd_config_expand) Evaluate error: No such file or directoryALSA lib pcm.c:2660:(snd_pcm_open_noupdate) Unknown PCM surround21ALSA lib confmisc.c:1281:(snd_func_refer) Unable to find definition 'cards.bcm2835_headpho.pcm.surround40.0:CARD=0'ALSA lib conf.c:4745:(_snd_config_evaluate) function snd_func_refer returned error: No such file or directoryALSA lib conf.c:5233:(snd_config_expand) Evaluate error: No such file or directoryALSA lib pcm.c:2660:(snd_pcm_open_noupdate) Unknown PCM surround40ALSA lib confmisc.c:1281:(snd_func_refer) Unable to find definition 'cards.bcm2835_headpho.pcm.surround51.0:CARD=0'ALSA lib conf.c:4745:(_snd_config_evaluate) function snd_func_refer returned error: No such file or directoryALSA lib conf.c:5233:(snd_config_expand) Evaluate error: No such file or directoryALSA lib pcm.c:2660:(snd_pcm_open_noupdate) Unknown PCM surround41ALSA lib confmisc.c:1281:(snd_func_refer) Unable to find definition 'cards.bcm2835_headpho.pcm.surround51.0:CARD=0'ALSA lib conf.c:4745:(_snd_config_evaluate) function snd_func_refer returned error: No such file or directoryALSA lib conf.c:5233:(snd_config_expand) Evaluate error: No such file or directoryALSA lib pcm.c:2660:(snd_pcm_open_noupdate) Unknown PCM surround50ALSA lib confmisc.c:1281:(snd_func_refer) Unable to find definition 'cards.bcm2835_headpho.pcm.surround51.0:CARD=0'ALSA lib conf.c:4745:(_snd_config_evaluate) function snd_func_refer returned error: No such file or directoryALSA lib conf.c:5233:(snd_config_expand) Evaluate error: No such file or directoryALSA lib pcm.c:2660:(snd_pcm_open_noupdate) Unknown PCM surround51ALSA lib confmisc.c:1281:(snd_func_refer) Unable to find definition 'cards.bcm2835_headpho.pcm.surround71.0:CARD=0'ALSA lib conf.c:4745:(_snd_config_evaluate) function snd_func_refer returned error: No such file or directoryALSA lib conf.c:5233:(snd_config_expand) Evaluate error: No such file or directoryALSA lib pcm.c:2660:(snd_pcm_open_noupdate) Unknown PCM surround71ALSA lib confmisc.c:1281:(snd_func_refer) Unable to find definition 'cards.bcm2835_headpho.pcm.iec958.0:CARD=0,AES0=4,AES1=130,AES2=0,AES3=2'ALSA lib conf.c:4745:(_snd_config_evaluate) function snd_func_refer returned error: No such file or directoryALSA lib conf.c:5233:(snd_config_expand) Evaluate error: No such file or directoryALSA lib pcm.c:2660:(snd_pcm_open_noupdate) Unknown PCM iec958ALSA lib confmisc.c:1281:(snd_func_refer) Unable to find definition 'cards.bcm2835_headpho.pcm.iec958.0:CARD=0,AES0=4,AES1=130,AES2=0,AES3=2'ALSA lib conf.c:4745:(_snd_config_evaluate) function snd_func_refer returned error: No such file or directoryALSA lib conf.c:5233:(snd_config_expand) Evaluate error: No such file or directoryALSA lib pcm.c:2660:(snd_pcm_open_noupdate) Unknown PCM spdifALSA lib confmisc.c:1281:(snd_func_refer) Unable to find definition 'cards.bcm2835_headpho.pcm.iec958.0:CARD=0,AES0=4,AES1=130,AES2=0,AES3=2'ALSA lib conf.c:4745:(_snd_config_evaluate) function snd_func_refer returned error: No such file or directoryALSA lib conf.c:5233:(snd_config_expand) Evaluate error: No such file or directoryALSA lib pcm.c:2660:(snd_pcm_open_noupdate) Unknown PCM spdifALSA lib confmisc.c:1281:(snd_func_refer) Unable to find definition 'cards.bcm2835_headpho.pcm.hdmi.0:CARD=0,AES0=4,AES1=130,AES2=0,AES3=2'ALSA lib conf.c:4745:(_snd_config_evaluate) function snd_func_refer returned error: No such file or directoryALSA lib conf.c:5233:(snd_config_expand) Evaluate error: No such file or directoryALSA lib pcm.c:2660:(snd_pcm_open_noupdate) Unknown PCM hdmiALSA lib confmisc.c:1281:(snd_func_refer) Unable to find definition 'cards.bcm2835_headpho.pcm.hdmi.0:CARD=0,AES0=4,AES1=130,AES2=0,AES3=2'ALSA lib conf.c:4745:(_snd_config_evaluate) function snd_func_refer returned error: No such file or directoryALSA lib conf.c:5233:(snd_config_expand) Evaluate error: No such file or directoryALSA lib pcm.c:2660:(snd_pcm_open_noupdate) Unknown PCM hdmiALSA lib pcm.c:2660:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.modemALSA lib pcm.c:2660:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.modemALSA lib pcm.c:2660:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.phonelineALSA lib pcm.c:2660:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.phonelineCannot connect to server socket err = No such file or directoryCannot connect to server request channeljack server is not running or cannot be startedJackShmReadWritePtr::~JackShmReadWritePtr - Init not done for -1, skipping unlockJackShmReadWritePtr::~JackShmReadWritePtr - Init not done for -1, skipping unlockCannot connect to server socket err = No such file or directoryCannot connect to server request channeljack server is not running or cannot be startedJackShmReadWritePtr::~JackShmReadWritePtr - Init not done for -1, skipping unlockJackShmReadWritePtr::~JackShmReadWritePtr - Init not done for -1, skipping unlockALSA lib pcm_oss.c:377:(_snd_pcm_oss_open) Unknown field portALSA lib pcm_oss.c:377:(_snd_pcm_oss_open) Unknown field portALSA lib pcm_a52.c:823:(_snd_pcm_a52_open) a52 is only for playbackALSA lib confmisc.c:1281:(snd_func_refer) Unable to find definition 'cards.bcm2835_headpho.pcm.iec958.0:CARD=0,AES0=6,AES1=130,AES2=0,AES3=2'ALSA lib conf.c:4745:(_snd_config_evaluate) function snd_func_refer returned error: No such file or directoryALSA lib conf.c:5233:(snd_config_expand) Evaluate error: No such file or directoryALSA lib pcm.c:2660:(snd_pcm_open_noupdate) Unknown PCM iec958:{AES0 0x6 AES1 0x82 AES2 0x0 AES3 0x2 CARD 0}ALSA lib pcm_usb_stream.c:486:(_snd_pcm_usb_stream_open) Invalid type for cardALSA lib pcm_usb_stream.c:486:(_snd_pcm_usb_stream_open) Invalid type for cardCannot connect to server socket err = No such file or directoryCannot connect to server request channeljack server is not running or cannot be startedJackShmReadWritePtr::~JackShmReadWritePtr - Init not done for -1, skipping unlockJackShmReadWritePtr::~JackShmReadWritePtr - Init not done for -1, skipping unlock/home/galwaybae/prog.py:14: DeprecationWarning: The binary mode of fromstring is deprecated, as it behaves surprisingly on unicode inputs. Use frombuffer instead samps = numpy.fromstring(rawsamps, dtype=numpy.int16) # Show the volume and pitch
The logs from trying to run at reboot using crontab are similar but the last few lines are different:
Code:
Expression 'GetExactSampleRate( hwParams, &defaultSr )' failed in 'src/hostapi/alsa/pa_linux_alsa.c', line: 895Expression 'GetExactSampleRate( hwParams, &defaultSr )' failed in 'src/hostapi/alsa/pa_linux_alsa.c', line: 895Expression 'GetExactSampleRate( hwParams, &defaultSr )' failed in 'src/hostapi/alsa/pa_linux_alsa.c', line: 895ALSA lib confmisc.c:1281:(snd_func_refer) Unable to find definition 'cards.bcm2835_headpho.pcm.front.0:CARD=0'ALSA lib conf.c:4745:(_snd_config_evaluate) function snd_func_refer returned error: No such file or directoryALSA lib conf.c:5233:(snd_config_expand) Evaluate error: No such file or directoryALSA lib pcm.c:2660:(snd_pcm_open_noupdate) Unknown PCM frontALSA lib pcm.c:2660:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.rearALSA lib pcm.c:2660:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.center_lfeALSA lib pcm.c:2660:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.sideALSA lib confmisc.c:1281:(snd_func_refer) Unable to find definition 'cards.bcm2835_headpho.pcm.surround51.0:CARD=0'ALSA lib conf.c:4745:(_snd_config_evaluate) function snd_func_refer returned error: No such file or directoryALSA lib conf.c:5233:(snd_config_expand) Evaluate error: No such file or directoryALSA lib pcm.c:2660:(snd_pcm_open_noupdate) Unknown PCM surround21ALSA lib confmisc.c:1281:(snd_func_refer) Unable to find definition 'cards.bcm2835_headpho.pcm.surround51.0:CARD=0'ALSA lib conf.c:4745:(_snd_config_evaluate) function snd_func_refer returned error: No such file or directoryALSA lib conf.c:5233:(snd_config_expand) Evaluate error: No such file or directoryALSA lib pcm.c:2660:(snd_pcm_open_noupdate) Unknown PCM surround21ALSA lib confmisc.c:1281:(snd_func_refer) Unable to find definition 'cards.bcm2835_headpho.pcm.surround40.0:CARD=0'ALSA lib conf.c:4745:(_snd_config_evaluate) function snd_func_refer returned error: No such file or directoryALSA lib conf.c:5233:(snd_config_expand) Evaluate error: No such file or directoryALSA lib pcm.c:2660:(snd_pcm_open_noupdate) Unknown PCM surround40ALSA lib confmisc.c:1281:(snd_func_refer) Unable to find definition 'cards.bcm2835_headpho.pcm.surround51.0:CARD=0'ALSA lib conf.c:4745:(_snd_config_evaluate) function snd_func_refer returned error: No such file or directoryALSA lib conf.c:5233:(snd_config_expand) Evaluate error: No such file or directoryALSA lib pcm.c:2660:(snd_pcm_open_noupdate) Unknown PCM surround41ALSA lib confmisc.c:1281:(snd_func_refer) Unable to find definition 'cards.bcm2835_headpho.pcm.surround51.0:CARD=0'ALSA lib conf.c:4745:(_snd_config_evaluate) function snd_func_refer returned error: No such file or directoryALSA lib conf.c:5233:(snd_config_expand) Evaluate error: No such file or directoryALSA lib pcm.c:2660:(snd_pcm_open_noupdate) Unknown PCM surround50ALSA lib confmisc.c:1281:(snd_func_refer) Unable to find definition 'cards.bcm2835_headpho.pcm.surround51.0:CARD=0'ALSA lib conf.c:4745:(_snd_config_evaluate) function snd_func_refer returned error: No such file or directoryALSA lib conf.c:5233:(snd_config_expand) Evaluate error: No such file or directoryALSA lib pcm.c:2660:(snd_pcm_open_noupdate) Unknown PCM surround51ALSA lib confmisc.c:1281:(snd_func_refer) Unable to find definition 'cards.bcm2835_headpho.pcm.surround71.0:CARD=0'ALSA lib conf.c:4745:(_snd_config_evaluate) function snd_func_refer returned error: No such file or directoryALSA lib conf.c:5233:(snd_config_expand) Evaluate error: No such file or directoryALSA lib pcm.c:2660:(snd_pcm_open_noupdate) Unknown PCM surround71ALSA lib confmisc.c:1281:(snd_func_refer) Unable to find definition 'cards.bcm2835_headpho.pcm.iec958.0:CARD=0,AES0=4,AES1=130,AES2=0,AES3=2'ALSA lib conf.c:4745:(_snd_config_evaluate) function snd_func_refer returned error: No such file or directoryALSA lib conf.c:5233:(snd_config_expand) Evaluate error: No such file or directoryALSA lib pcm.c:2660:(snd_pcm_open_noupdate) Unknown PCM iec958ALSA lib confmisc.c:1281:(snd_func_refer) Unable to find definition 'cards.bcm2835_headpho.pcm.iec958.0:CARD=0,AES0=4,AES1=130,AES2=0,AES3=2'ALSA lib conf.c:4745:(_snd_config_evaluate) function snd_func_refer returned error: No such file or directoryALSA lib conf.c:5233:(snd_config_expand) Evaluate error: No such file or directoryALSA lib pcm.c:2660:(snd_pcm_open_noupdate) Unknown PCM spdifALSA lib confmisc.c:1281:(snd_func_refer) Unable to find definition 'cards.bcm2835_headpho.pcm.iec958.0:CARD=0,AES0=4,AES1=130,AES2=0,AES3=2'ALSA lib conf.c:4745:(_snd_config_evaluate) function snd_func_refer returned error: No such file or directoryALSA lib conf.c:5233:(snd_config_expand) Evaluate error: No such file or directoryALSA lib pcm.c:2660:(snd_pcm_open_noupdate) Unknown PCM spdifALSA lib confmisc.c:1281:(snd_func_refer) Unable to find definition 'cards.bcm2835_headpho.pcm.hdmi.0:CARD=0,AES0=4,AES1=130,AES2=0,AES3=2'ALSA lib conf.c:4745:(_snd_config_evaluate) function snd_func_refer returned error: No such file or directoryALSA lib conf.c:5233:(snd_config_expand) Evaluate error: No such file or directoryALSA lib pcm.c:2660:(snd_pcm_open_noupdate) Unknown PCM hdmiALSA lib confmisc.c:1281:(snd_func_refer) Unable to find definition 'cards.bcm2835_headpho.pcm.hdmi.0:CARD=0,AES0=4,AES1=130,AES2=0,AES3=2'ALSA lib conf.c:4745:(_snd_config_evaluate) function snd_func_refer returned error: No such file or directoryALSA lib conf.c:5233:(snd_config_expand) Evaluate error: No such file or directoryALSA lib pcm.c:2660:(snd_pcm_open_noupdate) Unknown PCM hdmiALSA lib pcm.c:2660:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.modemALSA lib pcm.c:2660:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.modemALSA lib pcm.c:2660:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.phonelineALSA lib pcm.c:2660:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.phonelineCannot connect to server socket err = No such file or directoryCannot connect to server request channeljack server is not running or cannot be startedJackShmReadWritePtr::~JackShmReadWritePtr - Init not done for -1, skipping unlockJackShmReadWritePtr::~JackShmReadWritePtr - Init not done for -1, skipping unlockCannot connect to server socket err = No such file or directoryCannot connect to server request channeljack server is not running or cannot be startedJackShmReadWritePtr::~JackShmReadWritePtr - Init not done for -1, skipping unlockJackShmReadWritePtr::~JackShmReadWritePtr - Init not done for -1, skipping unlockALSA lib pcm_oss.c:377:(_snd_pcm_oss_open) Unknown field portALSA lib pcm_oss.c:377:(_snd_pcm_oss_open) Unknown field portALSA lib pulse.c:242:(pulse_connect) PulseAudio: Unable to connect: Connection refusedALSA lib pulse.c:242:(pulse_connect) PulseAudio: Unable to connect: Connection refusedALSA lib pcm_a52.c:823:(_snd_pcm_a52_open) a52 is only for playbackALSA lib confmisc.c:1281:(snd_func_refer) Unable to find definition 'cards.bcm2835_headpho.pcm.iec958.0:CARD=0,AES0=6,AES1=130,AES2=0,AES3=2'ALSA lib conf.c:4745:(_snd_config_evaluate) function snd_func_refer returned error: No such file or directoryALSA lib conf.c:5233:(snd_config_expand) Evaluate error: No such file or directoryALSA lib pcm.c:2660:(snd_pcm_open_noupdate) Unknown PCM iec958:{AES0 0x6 AES1 0x82 AES2 0x0 AES3 0x2 CARD 0}ALSA lib pcm_usb_stream.c:486:(_snd_pcm_usb_stream_open) Invalid type for cardALSA lib pcm_usb_stream.c:486:(_snd_pcm_usb_stream_open) Invalid type for cardCannot connect to server socket err = No such file or directoryCannot connect to server request channeljack server is not running or cannot be startedJackShmReadWritePtr::~JackShmReadWritePtr - Init not done for -1, skipping unlockJackShmReadWritePtr::~JackShmReadWritePtr - Init not done for -1, skipping unlockTraceback (most recent call last): File "/home/galwaybae/prog.py", line 11, in <module> stream = pyaud.open(format = pyaudio.paInt16,channels = 1,rate=44100,input=True) File "/usr/lib/python3/dist-packages/pyaudio.py", line 750, in open stream = Stream(self, *args, **kwargs) File "/usr/lib/python3/dist-packages/pyaudio.py", line 441, in __init__ self._stream = pa.open(**arguments)OSError: [Errno -9996] Invalid input device (no default output device)
My working theory is that those different lines are pointing to the solution. Unfortunately as a novice I don't really understand how the last line ("OSError: [Errno -9996] Invalid input device (no default output device)") can be correct if the USB mic and speaker work fine when launched manually. Is there some behavior I'm overlooking here? Are the devices not active/recognizable when the program is run at reboot? Could delaying the program help? I think I'm grasping at straws here.
It could be these error messages have nothing to do with the program not running on startup, but I can't figure out what else it might be. It seems odd that it works okay despite all the errors when I start it manually so I'm a bit stumped. If anyone has any ideas why this would work when I start the program manually but isn't working on reboot I would really appreciate it.
Sorry if I've explained anything poorly or left something important out, I'm self-taught so my knowledge is patchy. Hopefully the solution is something simple and obvious!
Statistics: Posted by galwaybae — Mon Dec 18, 2023 2:07 pm