play sound on Ubuntu 10.4. (pulse audio?)

Discussion in 'Python' started by News123, Sep 11, 2010.

  1. News123

    News123 Guest

    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=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')
    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, duration=1000, 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
    News123, Sep 11, 2010
    #1
    1. Advertising

  2. News123

    Tim Harig Guest

    Tim Harig, Sep 11, 2010
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. YeeCN

    How to beep or play any sound?

    YeeCN, May 10, 2004, in forum: ASP .Net
    Replies:
    1
    Views:
    536
    Guest
    May 13, 2004
  2. Lord2702

    How to play sound

    Lord2702, Oct 4, 2004, in forum: ASP .Net
    Replies:
    1
    Views:
    581
    =?Utf-8?B?c3Jpbmk=?=
    Oct 4, 2004
  3. Rick Lederman

    Play a sound

    Rick Lederman, Dec 17, 2004, in forum: ASP .Net
    Replies:
    3
    Views:
    699
    Steven Cheng[MSFT]
    Dec 20, 2004
  4. SPE - Stani's Python Editor
    Replies:
    1
    Views:
    452
    SPE - Stani's Python Editor
    Apr 29, 2008
  5. sick dick
    Replies:
    0
    Views:
    387
    sick dick
    Apr 3, 2006
Loading...

Share This Page