Hi All,
Im new to programing and to python (so please excuse and mistakes).
So im trying to make a soundboard for an old Raspberry to play Train staion sounds for my nephew.
This is the list of equiptment im using.
Ras Pi 2 B+
16GB sd card
USB NumberPad (Logik NO:LKBNP10)
I was hoping to use the onboard headphone jack but i have a UBS sound card if i need.
Have install (i think) all the softwear i need as i can play sound with ether “cvlc” or a sound test python script.
i have used “ChatGPT” to make a script but nomatter what i try i cant get the sctript for the sound board to work.
So this is what i want to do as a compleat project.
1: I would like the pi to play a sound once the pi is booted and the script is ready to use.
2: the script to run once the pi is booted.
3: A SoundBoard script
4: Use the keypad numbers 0-9 to play diffrent mp3 files (i have made 10 files to use)
5: As there are 18 buttons on the keypad (not including the NUM LOCK key) i would like to use the + and - to controle volume and the * key to shutdown the pi.
Hopfully these are possible.
Here is the script i have so far,
import pygame
import os
from evdev import InputDevice, ecodes
def play_sound(sound_path):
pygame.mixer.init()
sound = pygame.mixer.Sound(sound_path)
sound.play()
pygame.time.delay(int(sound.get_length() * 1000)) # Wait for sound to finish
def find_number_pad():
devices = [fn for fn in os.listdir(‘/dev/input/’) if fn.startswith(‘event’)]
for device_fn in devices:
device_path = f’/dev/input/{device_fn}’
try:
device = InputDevice(device_path)
print(f"Detected device: {device.name}")
if “0410:0001” in device.name:
return device
except FileNotFoundError:
pass
return None
def main():
sound_folder = “/home/rich/sounds” # Replace with the actual path to your so>
key_to_sound = {
ecodes.KEY_1: "11.mp3",
ecodes.KEY_2: "12.mp3",
ecodes.KEY_3: "13.mp3",
ecodes.KEY_4: "14.mp3",
ecodes.KEY_5: "15.mp3",
ecodes.KEY_6: "16.mp3",
ecodes.KEY_7: "17.mp3",
ecodes.KEY_8: "18.mp3",
ecodes.KEY_9: "19.mp3",
ecodes.KEY_0: "20.mp3"
}
number_pad = find_number_pad()
if not number_pad:
print(“USB number pad not found. Exiting.”)
return
pygame.init()
try:
while True:
for event in number_pad.read_loop():
print(f"Event: {event}")
if event.type == ecodes.EV_KEY and event.value == 1:
key = event.code
if key in key_to_sound:
sound_path = os.path.join(sound_folder, key_to_sound[key])
print(f"Playing sound: {sound_path}")
play_sound(sound_path)
except KeyboardInterrupt:
pygame.quit()
if name == “main”:
main()
when i run the script and press a number on the key pad this is what i get
pygame 2.5.2 (SDL 2.0.14, Python 3.9.2)
Hello from the pygame community. Contribute - pygame wiki
Detected device: vc4-hdmi
Detected device: HID 0410:0001
and the script just hangs.
0410:0001 is the device name of the keypad and i have set the headphone jack as the default in raspi config.
If anyone could help it would make my nephew so happy as this is a supprise for him.
Im more them happy to do a clean install of raspian lite if this will help.
Thanks in advance