Quantcast
Channel: Raspberry Pi Forums
Viewing all articles
Browse latest Browse all 3436

General discussion • Running PyAudio Script On Startup Issue

$
0
0
I have this python script that records audio and exports it. It works fine it I don't run it on startup, but when I run it on startup I get a huge error (or whatever it is)
My code is below:

Code:

import pyaudioimport waveimport timeimport RPi.GPIO as GPIOfrom pydub import AudioSegmentfrom io import BytesIOfrom datetime import datetimefrom gpiozero import LED# ConfigurationBUTTON_PIN = 27  # GPIO pin number for the buttonCHUNK = 1024FORMAT = pyaudio.paInt16CHANNELS = 1RATE = 44100RECORD_SECONDS = 0  # Not used directly in this case# Initialize PyAudioaudio = pyaudio.PyAudio()# GPIO setupGPIO.setmode(GPIO.BCM)GPIO.setup(BUTTON_PIN, GPIO.IN, pull_up_down=GPIO.PUD_UP)GPIO.setup(10,GPIO.OUT) #greenGPIO.setup(9,GPIO.OUT) #yellowGPIO.setup(11,GPIO.OUT) #reddef record_audio(filename):    stream = audio.open(format=FORMAT, channels=CHANNELS,                        rate=RATE, input=True, frames_per_buffer=CHUNK)    GPIO.output(11, GPIO.HIGH)    print("Recording started")        frames = []    while GPIO.input(BUTTON_PIN) == GPIO.LOW:        data = stream.read(CHUNK)        frames.append(data)        GPIO.output(11, GPIO.LOW)    print("Recording stopped")    stream.stop_stream()    stream.close()    # Save to WAV file    wf = wave.open(filename, 'wb')    wf.setnchannels(CHANNELS)    wf.setsampwidth(audio.get_sample_size(FORMAT))    wf.setframerate(RATE)    wf.writeframes(b''.join(frames))    wf.close()def main():    try:        GPIO.output(10, GPIO.HIGH) #11 = red | 10 = green | 9 = yellow        time.sleep(0.3)        GPIO.output(10, GPIO.LOW)        GPIO.output(9, GPIO.HIGH)        time.sleep(0.3)        GPIO.output(9, GPIO.LOW)        GPIO.output(11, GPIO.HIGH)        time.sleep(0.3)        GPIO.output(11, GPIO.LOW)        GPIO.output(9, GPIO.HIGH)        time.sleep(0.3)        GPIO.output(9, GPIO.LOW)        GPIO.output(10, GPIO.HIGH)        time.sleep(0.3)        GPIO.output(10, GPIO.LOW)        GPIO.output(10, GPIO.HIGH)        while True:            if GPIO.input(BUTTON_PIN) == GPIO.LOW:                # Button pressed: Record audio                timestamp = datetime.now().strftime("%m-%d-%Y_%H-%M-%S")                wav_filename = f"/home/henrikkoehler/Desktop/Audio_Files/{timestamp}.wav"                record_audio(wav_filename)                GPIO.output(9, GPIO.HIGH)                time.sleep(1)                GPIO.output(9, GPIO.LOW)                print(f"Audio saved to {wav_filename}")                # Wait a bit to avoid multiple recordings from a single press                time.sleep(1)            else:                time.sleep(0.1)  # Polling delay    except KeyboardInterrupt:        print("Exiting...")        finally:        audio.terminate()        GPIO.cleanup()if __name__ == "__main__":    main()

Statistics: Posted by Henrik Gill — Wed Aug 07, 2024 1:47 pm — Replies 6 — Views 67



Viewing all articles
Browse latest Browse all 3436

Trending Articles