play sound on Ubuntu 10.4. (pulse audio?)

News123 news1234 at
Sat Sep 11 22:39:39 CEST 2010


>From a python script I'd like to play sound on Ubuntu 10.4

I made two attempts:

the first one is drectly writing to '/dev/audio', which works, but only
if no other application plays audio (like for example amarok)

The second attempt was playing audio via ALSA.
The application doesn't fail, but I can hear nothing

Now I don't know how to continue

More details below

1.) write directly to /dev/audio

def Beep_dev_au(frequency, duration, amplitude=10):
    sample = 8000
    half_period = int(sample/frequency/2)
    beep = chr(amplitude)*half_period+chr(0)*half_period
    beep *= int(duration*frequency/1000)
    audio = file('/dev/audio', 'wb')

the function works well and beeps if no other application is playing audio.

When running it while for example amarok is playing audio, it will fail

IOError: [Errno 16] Device or resource busy: '/dev/audio'

2. Accessing audio via ALSA
import alsaaudio
def Beep_alsa(frequency, duration=1000, amplitude=1):
    sample = 44100
    pcm = alsaaudio.PCM(type=alsaaudio.PCM_PLAYBACK)
    half_period = int(sample/frequency/2)
    beep = (chr(0)+chr(amplitude))*half_period+(chr(0)*2)*half_period
    print "L",len(beep)
    beep *= int(duration*sample/half_period/2000)

    for idx in xrange(0,len(beep),320):
        frm = beep[idx:idx+320]
        if len(frm) == 320:

this function doesn't raise an exception, but  hear nothing.

I'm a little lost.

As far as I understand Ubuntu uses the pulse audio system by default.
(That's probably why ALSA is not working)

What would be the correct way to play audio on such a system or even better.

What would be the way to detect how to play audio on a linux system
without knowing ufront whether the system uses pulse, ALSA or whatever?

Thanks for your suggestions.


More information about the Python-list mailing list