Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/python3
- import sys
- import signal
- import RPi.GPIO as GPIO
- import pyaudio
- import wave
- GPIO.setmode(GPIO.BCM)
- # GPIO 23 (pin 16) & 24 (pin 18) set to HIGH, with both ports should be
- # wired to connect to GND on button press. The pins on EITHER SIDE of
- # these are GND.
- GPIO.setup(23, GPIO.IN, pull_up_down=GPIO.PUD_UP)
- GPIO.setup(24, GPIO.IN, pull_up_down=GPIO.PUD_UP)
- # define callback
- def callback(in_data, frame_count, time_info, status):
- print ("Callback")
- data = wf.readframes(frame_count)
- return (data, pyaudio.paContinue)
- def gpio23(channel):
- print ("Pin 16 to ground detected")
- def gpio24(channel):
- print ("Pin 18 to ground detected")
- def signal_handler(sig, frame):
- print ("Cleanup")
- GPIO.cleanup()
- sys.exit(0)
- p = pyaudio.PyAudio()
- wf = wave.open('sounda.wav', 'rb')
- stream = p.open(format=p.get_format_from_width(wf.getsampwidth()),
- channels=wf.getnchannels(),
- rate=wf.getframerate(),
- output=True,
- stream_callback=callback)
- # start the sound stream
- stream.start_stream()
- GPIO.add_event_detect(23, GPIO.FALLING, callback=gpio23, bouncetime=300)
- GPIO.add_event_detect(24, GPIO.FALLING, callback=gpio24, bouncetime=300)
- print("Pausing...")
- signal.signal(signal.SIGINT, signal_handler)
- signal.pause()
- print("Here I am")
- GPIO.cleanup() # clean up GPIO on normal exit
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement