Address

Author Topic: Playing simultaneous sounds on Raspberry Pi  (Read 9829 times)

digitalman2112

  • Lab Dad (ex-treasurer)
  • FamiLAB Member
  • Hero Member
  • *
  • Posts: 1096
  • Work Hard, Play Hard!
    • View Profile
    • Raising Geeks
Playing simultaneous sounds on Raspberry Pi
« on: February 04, 2013, 06:21:15 PM »
I'm learning all about alsa and pulseaudio in my quest to be able to play multiple sounds from python at the same time - but I haven't hit the right combination yet...


I found an example that uses os.system to call mp123 to play mp3s - but can't get simultaneous playback. Could be that mp123 isn't configured to use pulse, or maybe I should use a different player.




Anyone have any suggestions / examples for how to do this?


Thanks!

tetsuharu

  • FamiLAB Member
  • Sr. Member
  • *
  • Posts: 487
  • My sincerest contrafibularities.
    • View Profile
Re: Playing simultaneous sounds on Raspberry Pi
« Reply #1 on: February 04, 2013, 08:29:52 PM »
It's kind of sad that I can never find decent standalone packages, but I would probably leverage parts of the pygame library for this.

Specifically the sound mixer: http://www.pygame.org/docs/ref/mixer.html

It'll help you load audio files and mix them together.

tetsuharu

  • FamiLAB Member
  • Sr. Member
  • *
  • Posts: 487
  • My sincerest contrafibularities.
    • View Profile
Re: Playing simultaneous sounds on Raspberry Pi
« Reply #2 on: February 04, 2013, 08:36:05 PM »
I can't seem to get it to play mp3's, only ogg and wav, but it's not too difficult to convert.
(lame --decode original.mp3 && sox original.wav new.ogg)

so I have Python 2.7.3 installed with pip, so I ran:
  sudo pip install pygame

and found this example code that demonstrates reading a file and playing the file at different times in the left and right channel:

# example1.py
# play a sound to the left, to the right and to the center

# import the time standard module
import time

# import the pygame module
import pygame


# start pygame
pygame.init()

# load a sound file into memory
sound = pygame.mixer.Sound("gescom - c1.wav")

# start playing the sound
# and remember on which channel it is being played
channel = sound.play()
# set the volume of the channel
# so the sound is only heard to the left
channel.set_volume(1, 0)
# wait for 1 second
time.sleep(10)

# do the same to the right
channel = sound.play()
channel.set_volume(0, 1)
time.sleep(10)

# do the same to the center
channel = sound.play()
channel.set_volume(1, 1)
time.sleep(10)

digitalman2112

  • Lab Dad (ex-treasurer)
  • FamiLAB Member
  • Hero Member
  • *
  • Posts: 1096
  • Work Hard, Play Hard!
    • View Profile
    • Raising Geeks
Re: Playing simultaneous sounds on Raspberry Pi
« Reply #3 on: February 04, 2013, 08:50:41 PM »
Aha - I hadn't dug into the pygame library - I will try that!


tetsuharu

  • FamiLAB Member
  • Sr. Member
  • *
  • Posts: 487
  • My sincerest contrafibularities.
    • View Profile
Re: Playing simultaneous sounds on Raspberry Pi
« Reply #4 on: February 04, 2013, 08:54:42 PM »
You can use a joystick-like device (like a ripped up xbox controller or old usb joystick) to hook up electrical switches, then make them trigger sounds, ex:


import time
import pygame

pygame.init()

# load sounds
sound1 = pygame.mixer.Sound("Paul Ruskay - Swarmer Battle Music (Homeworld OST).wav")

# get something to read events
joystick = pygame.joystick.Joystick(0)
joystick.init()

clock = pygame.time.Clock()
while True:
    pygame.event.pump()

    # if any of the buttons are pressed, play the sound in the left channel
    for i in range(0, joystick.get_numbuttons()):
        if joystick.get_button(i) != 0:
            channel = sound1.play()
            channel.set_volume(1,0)
    clock.tick(50)


apparently, pygame actually has it's own C code to do mixing and threading and such https://github.com/xamox/pygame/blob/master/src/mixer.c neat

digitalman2112

  • Lab Dad (ex-treasurer)
  • FamiLAB Member
  • Hero Member
  • *
  • Posts: 1096
  • Work Hard, Play Hard!
    • View Profile
    • Raising Geeks
Re: Playing simultaneous sounds on Raspberry Pi
« Reply #5 on: February 04, 2013, 09:11:40 PM »
Awesome. The 6.3vac pinball LEDs arrived today. My bag o' big PNP transistors should arrive tomorrow, and I hope to have the switch and LED matrix wired to the Pi by the weekend (optimistic, but possible).


I also need to switch 5 solenoids - each of which spikes about 5 amps and then settles to <2 amps at 22vdc. I ordered a 15Amp 24vdc power supply which I'm hoping will do the trick :)



Still need to determine if the Pi can switch / poll the matrix (6x4 matrix for 22 lamps, 6x4 for 21 switches), or if I'll be offloading that to an atmega...


I figure scoring rules, score display, and sound playback should be trivial with python (and even easier now that you showed my pygame...)











[size=78%] [/size]

macrutan

  • FamiLAB Member
  • Newbie
  • *
  • Posts: 35
    • View Profile
Re: Playing simultaneous sounds on Raspberry Pi
« Reply #6 on: February 28, 2013, 02:08:35 PM »
How is the sound issue going?
Also how is the pinball machine?


If the piGame solution doesn't work. Consider a midi solution that could trigger samples in a polyphonic mode. I'll do a search... hopefully that can be done as software only...


Mac

digitalman2112

  • Lab Dad (ex-treasurer)
  • FamiLAB Member
  • Hero Member
  • *
  • Posts: 1096
  • Work Hard, Play Hard!
    • View Profile
    • Raising Geeks
Re: Playing simultaneous sounds on Raspberry Pi
« Reply #7 on: February 28, 2013, 03:04:07 PM »
How is the sound issue going?
Also how is the pinball machine?

Finally tried out pygame on the plane this week, and sound / score updates works well on my laptop. Still need to try it on the Pi. There was some lag between the sound button push and the sound, but I'm optimistic that I can work it out.

Pygame supports both sounds AND background music which is extra awesome.

The pinball machine is going well - you can read more about it here - http://forums.familab.org/index.php/topic,767.0.html

I hope to have it portable enough to bring it to the next pi class...if not then definitely the one in April.

digitalman2112

  • Lab Dad (ex-treasurer)
  • FamiLAB Member
  • Hero Member
  • *
  • Posts: 1096
  • Work Hard, Play Hard!
    • View Profile
    • Raising Geeks
Re: Playing simultaneous sounds on Raspberry Pi
« Reply #8 on: March 12, 2013, 06:57:30 AM »
Thanks for the leads on this. Pygame works great, but getting started was a bit more difficult than I expected. I still don't have proper playback of mp3 sounds, but .wav works great.

This tutorial has a link to known good .wav files - just comment out the arduino serial stuff to test on the pi.

http://blog.makezine.com/2012/10/22/a-halloween-sound-trigger-with-raspberry-pi-and-arduino/

tomlong74

  • FamiLAB Member
  • Full Member
  • *
  • Posts: 127
    • View Profile
Re: Playing simultaneous sounds on Raspberry Pi
« Reply #9 on: March 12, 2013, 09:31:24 AM »
Pygame works great, but ... I still don't have proper playback of mp3 sounds, but .wav works great.

Most game engines don't support .mp3 and will support .ogg & wav.
Audacity will convert pretty much all of your audio formats for you and is free.

Cheers,
-TomLong

digitalman2112

  • Lab Dad (ex-treasurer)
  • FamiLAB Member
  • Hero Member
  • *
  • Posts: 1096
  • Work Hard, Play Hard!
    • View Profile
    • Raising Geeks
Re: Playing simultaneous sounds on Raspberry Pi
« Reply #10 on: April 01, 2013, 07:29:14 AM »
Circling back on this one with an update...

1) Pygame is AWESOME. Multiple stereo audio channels with individual volume levels and playback controls / events PLUS a background music channel - wow.

2) Pygame formats - It says it supports MP3, but as Tom mentions, OGG is seamless, so I'm using Audacity (thanks for the tip Tom!) to convert everything to OGG.

This video shows the background music and sound effects in action.

Fireball Home - now with (Raspberry Pi) background music!

We've learned a TON about the pi, pygame, and audio / video on this project - let me know if I can share any of it for your project...


Lensman

  • Administrator
  • Full Member
  • *****
  • Posts: 163
    • View Profile
Re: Playing simultaneous sounds on Raspberry Pi
« Reply #11 on: April 01, 2013, 11:42:31 AM »
One note on the relay power supply -- this is the kind of load big filter caps were built for.  A big enough capacitor can supply the inrush current, and take a lot of the instantaneous load off the power supply.

digitalman2112

  • Lab Dad (ex-treasurer)
  • FamiLAB Member
  • Hero Member
  • *
  • Posts: 1096
  • Work Hard, Play Hard!
    • View Profile
    • Raising Geeks
Re: Playing simultaneous sounds on Raspberry Pi
« Reply #12 on: April 01, 2013, 11:53:29 AM »
One note on the relay power supply -- this is the kind of load big filter caps were built for.  A big enough capacitor can supply the inrush current, and take a lot of the instantaneous load off the power supply.

I've been hoping someone would come along and help me figure that out. Can I get 30 mins sometime soon on the phone or hangout to tell you what I'm doing and have you help me pick a cap? :)



Lensman

  • Administrator
  • Full Member
  • *****
  • Posts: 163
    • View Profile
Re: Playing simultaneous sounds on Raspberry Pi
« Reply #13 on: April 01, 2013, 12:56:56 PM »
yes indeed - flip me a link to what solenoids you're using, and how many you want to fire at once -- i can do a quick sizing for when we talk.

digitalman2112

  • Lab Dad (ex-treasurer)
  • FamiLAB Member
  • Hero Member
  • *
  • Posts: 1096
  • Work Hard, Play Hard!
    • View Profile
    • Raising Geeks
Re: Playing simultaneous sounds on Raspberry Pi
« Reply #14 on: April 01, 2013, 02:00:26 PM »
Haha, these are 30 year old pinball solenoids - don't really have spec sheets - I'll see what I can dig up. What measurements are critical? :)