FAQ
Hi,
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):
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')
audio.write(beep)
audio.close()


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
with:

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



2. Accessing audio via ALSA
============================import alsaaudio
#
def Beep_alsa(frequency, duration00, amplitude=1):
sample = 44100
pcm = alsaaudio.PCM(type=alsaaudio.PCM_PLAYBACK)
pcm.setchannels(1)
pcm.setrate(sample)
pcm.setformat(alsaaudio.PCM_FORMAT_S16_LE)
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)

pcm.setperiodsize(160)
for idx in xrange(0,len(beep),320):
frm = beep[idx:idx+320]
if len(frm) == 320:
pcm.write(frm)


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.


N

Search Discussions

  • Tim Harig at Sep 11, 2010 at 9:42 pm

    On 2010-09-11, News123 wrote:
    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?
    I would suggest libao:
    http://www.xiph.org/ao/
    and it's python bindings:
    http://nixbit.com/cat/multimedia/audio/libao/
  • News123 at Sep 12, 2010 at 12:53 pm
    Hi Tim,



    On 09/11/2010 11:42 PM, Tim Harig wrote:
    On 2010-09-11, News123 wrote:
    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?
    I would suggest libao:
    http://www.xiph.org/ao/
    and it's python bindings:
    http://nixbit.com/cat/multimedia/audio/libao/

    Thanks a lot.

    Yes, libao is a nice solution.

    After having fixed some audio setup issues on my PC I have now the ALSA
    and the libao solution working.



    import ao
    pcm = ao.AudioDevice("pulse", bits, rate=sample,
    channels=1, byte_format=1)
    pcmdata = create_pcmdata()
    pcm.play(pcmdata)

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
grouppython-list @
categoriespython
postedSep 11, '10 at 8:39p
activeSep 12, '10 at 12:53p
posts3
users2
websitepython.org

2 users in discussion

News123: 2 posts Tim Harig: 1 post

People

Translate

site design / logo © 2022 Grokbase