How to play sound in Python WITHOUT interrupting music/other sounds from playing

Posted by Morlock on Stack Overflow See other posts from Stack Overflow or by Morlock
Published on 2010-01-24T01:40:38Z Indexed on 2010/03/09 0:51 UTC
Read the original article Hit count: 386

Filed under:
|
|
|

I'm working on a timer in python which sounds a chime when the waiting time is over. I use the following code:

from wave import open as wave_open
from ossaudiodev import open as oss_open

def _play_chime():
    """
    Play a sound file once.

    """
    sound_file = wave_open('chime.wav','rb')
    (nc,sw,fr,nf,comptype, compname) = sound_file.getparams( )
    dsp = oss_open('/dev/dsp','w')
    try:
      from ossaudiodev import AFMT_S16_NE
    except ImportError:
      if byteorder == "little":
        AFMT_S16_NE = ossaudiodev.AFMT_S16_LE
      else:
        AFMT_S16_NE = ossaudiodev.AFMT_S16_BE
    dsp.setparameters(AFMT_S16_NE, nc, fr)
    data = sound_file.readframes(nf)
    sound_file.close()
    dsp.write(data)
    dsp.close()

It works pretty good, unless any other device is already outputing sound.

How could I do basically the same (under linux) without having the prerequisite that no sound is being played?

If you think the process would require an API to ensure software mixing, please suggest a method :)

Thx for the support

© Stack Overflow or respective owner

Related posts about python

Related posts about sound