element14 Community
element14 Community
    Register Log In
  • Site
  • Search
  • Log In Register
  • About Us
  • Community Hub
    Community Hub
    • What's New on element14
    • Feedback and Support
    • Benefits of Membership
    • Personal Blogs
    • Members Area
    • Achievement Levels
  • Learn
    Learn
    • Ask an Expert
    • eBooks
    • element14 presents
    • Learning Center
    • Tech Spotlight
    • STEM Academy
    • Webinars, Training and Events
    • Learning Groups
  • Technologies
    Technologies
    • 3D Printing
    • FPGA
    • Industrial Automation
    • Internet of Things
    • Power & Energy
    • Sensors
    • Technology Groups
  • Challenges & Projects
    Challenges & Projects
    • Design Challenges
    • element14 presents Projects
    • Project14
    • Arduino Projects
    • Raspberry Pi Projects
    • Project Groups
  • Products
    Products
    • Arduino
    • Avnet Boards Community
    • Dev Tools
    • Manufacturers
    • Multicomp Pro
    • Product Groups
    • Raspberry Pi
    • RoadTests & Reviews
  • Store
    Store
    • Visit Your Store
    • Choose another store...
      • Europe
      •  Austria (German)
      •  Belgium (Dutch, French)
      •  Bulgaria (Bulgarian)
      •  Czech Republic (Czech)
      •  Denmark (Danish)
      •  Estonia (Estonian)
      •  Finland (Finnish)
      •  France (French)
      •  Germany (German)
      •  Hungary (Hungarian)
      •  Ireland
      •  Israel
      •  Italy (Italian)
      •  Latvia (Latvian)
      •  
      •  Lithuania (Lithuanian)
      •  Netherlands (Dutch)
      •  Norway (Norwegian)
      •  Poland (Polish)
      •  Portugal (Portuguese)
      •  Romania (Romanian)
      •  Russia (Russian)
      •  Slovakia (Slovak)
      •  Slovenia (Slovenian)
      •  Spain (Spanish)
      •  Sweden (Swedish)
      •  Switzerland(German, French)
      •  Turkey (Turkish)
      •  United Kingdom
      • Asia Pacific
      •  Australia
      •  China
      •  Hong Kong
      •  India
      •  Korea (Korean)
      •  Malaysia
      •  New Zealand
      •  Philippines
      •  Singapore
      •  Taiwan
      •  Thailand (Thai)
      • Americas
      •  Brazil (Portuguese)
      •  Canada
      •  Mexico (Spanish)
      •  United States
      Can't find the country/region you're looking for? Visit our export site or find a local distributor.
  • Translate
  • Profile
  • Settings
element14 presents
  • Challenges & Projects
  • More
element14 presents
element14 presents Forum Making a Soundboard with a Raspberry Pi
  • Blog
  • Forum
  • Documents
  • Polls
  • Files
  • Members
  • Mentions
  • Sub-Groups
  • Tags
  • More
  • Cancel
  • New
Join element14 presents to participate - click to join for free!
Actions
  • Share
  • More
  • Cancel
Forum Thread Details
  • State Not Answered
  • +1 person also asked this people also asked this
  • Replies 9 replies
  • Subscribers 107 subscribers
  • Views 3464 views
  • Users 0 members are here
  • python
  • help
  • pi
  • projects
  • raspberry pi
  • Python script
  • help!
Related

Making a Soundboard with a Raspberry Pi

raf1983
raf1983 over 1 year ago

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

  • Sign in to reply
  • Cancel

Top Replies

  • shabaz
    shabaz over 1 year ago +1
    Hi, I dont currently have time to check your script others might help you out, in the meantime, an alternative approach possibly worth reading to rule in or out, could be to use a Pi Pico. An example…
  • fmilburn
    fmilburn over 1 year ago +1
    I haven't programmed in Python in a while but I wrote a program using pygame on a laptop a few years back that can be found here . My program used wav files rather than MP3. Notice how simple it is - I…
  • beacon_dave
    beacon_dave over 1 year ago +1
    raf1983 said: Detected device: HID 0410:0001 That print output should give you a clue where to start. The next step after that is to test if "0410:0001" is in device.name, so you could add a print…
  • robogary
    robogary over 1 year ago +1
    https://magpi.raspberrypi.com/books/handbook-2023 another resource
  • shabaz
    0 shabaz over 1 year ago

    Hi,

    I dont currently have time to check your script others might help you out, in the meantime, an alternative approach possibly worth reading to rule in or out, could be to use a Pi Pico. An example complete project is here that might give you ideas:

     Pocket Scarface – Pi Pico Python Based MP3 Clip Player 

    • Cancel
    • Vote Up +1 Vote Down
    • Sign in to reply
    • Verify Answer
    • Cancel
  • fmilburn
    0 fmilburn over 1 year ago

    I haven't programmed in Python in a while but I wrote a program using pygame on a laptop a few years back that can be found here.  My program used wav files rather than MP3.  Notice how simple it is - I've placed the sound files in the same folder as the script and there are no fancy paths.  Not that fancy paths are necessarily bad.

    There is a print statement in your script - is the pi connected to a monitor or using it headless?  I suggest looking at breaking your program into small parts and testing the pieces.  For example, start with a simple print statement and nothing more.  When the print statement works add sound.  If the sound doesn't work, try using the same sound statements I used in my script and maybe use one of the free online programs to change the mp3 file to a wav file.  When the sound file is working, move to input from the numeric keyboard.  Then add the loop, and so forth...

    The more you can isolate the problem the easier it will be to debug it and get help as needed.

    • Cancel
    • Vote Up +1 Vote Down
    • Sign in to reply
    • Verify Answer
    • Cancel
  • raf1983
    0 raf1983 over 1 year ago in reply to fmilburn

    I am using it headless. I'm a compleat novice at this but till work through your suggestions and see what I can find.

    Thank you

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Verify Answer
    • Cancel
  • raf1983
    0 raf1983 over 1 year ago in reply to shabaz

    Thanks you Thumbsup tone1

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Verify Answer
    • Cancel
  • beacon_dave
    0 beacon_dave over 1 year ago
    raf1983 said:
    Detected device: HID 0410:0001

    That print output should give you a clue where to start.

    The next step after that is to test if "0410:0001" is in device.name, so you could add a print statement after that to confirm whether or not it actually found it or not.

    If it finds it, it then returns it back to the calling function

       number_pad = find_number_pad()

    so you can add a print statement there to confirm whether that was successful or not.

    You aren't seeing anything being output from the next print statement however

       print(f"Event: {event}")

    so looks like the problem may be between these two areas.

    • Cancel
    • Vote Up +1 Vote Down
    • Sign in to reply
    • Verify Answer
    • Cancel
  • robogary
    0 robogary over 1 year ago

    https://magpi.raspberrypi.com/books/handbook-2023   another resource 

    • Cancel
    • Vote Up +1 Vote Down
    • Sign in to reply
    • Verify Answer
    • Cancel
  • raf1983
    0 raf1983 over 1 year ago in reply to beacon_dave

    Ok I'll have a look. Thank you for your time. This is smashing my head in lol.

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Verify Answer
    • Cancel
  • beacon_dave
    0 beacon_dave over 1 year ago in reply to raf1983

    Perhaps a bit less head smashing and a few more debugging print statements to help you visually understand the program flow.

    I recall there was a Pygame based soundboard project in this MagPi Essentials book (available as a free PDF download)

    https://magpi.raspberrypi.com/books/essentials-games-vol1

    "In chapter five, we learn about loading and playing sounds in your Pygame projects by making a fun farmyard soundboard."

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Verify Answer
    • Cancel
  • raf1983
    0 raf1983 over 1 year ago in reply to beacon_dave

    Thank you all have a look

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Verify Answer
    • Cancel
element14 Community

element14 is the first online community specifically for engineers. Connect with your peers and get expert answers to your questions.

  • Members
  • Learn
  • Technologies
  • Challenges & Projects
  • Products
  • Store
  • About Us
  • Feedback & Support
  • FAQs
  • Terms of Use
  • Privacy Policy
  • Legal and Copyright Notices
  • Sitemap
  • Cookies

An Avnet Company © 2025 Premier Farnell Limited. All Rights Reserved.

Premier Farnell Ltd, registered in England and Wales (no 00876412), registered office: Farnell House, Forge Lane, Leeds LS12 2NE.

ICP 备案号 10220084.

Follow element14

  • X
  • Facebook
  • linkedin
  • YouTube