Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > play sound on Ubuntu 10.4. (pulse audio?)

Reply
Thread Tools

play sound on Ubuntu 10.4. (pulse audio?)

 
 
News123
Guest
Posts: n/a
 
      09-11-2010
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)*hal f_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



 
Reply With Quote
 
 
 
 
Tim Harig
Guest
Posts: n/a
 
      09-11-2010
On 2010-09-11, News123 <(E-Mail Removed)> 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/
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: Ubuntu is About to Drop Another Turd! Ubuntu 10.04 Clogwog Computer Support 0 04-29-2010 08:00 PM
Learn python packaging for Ubuntu and Debian in the Ubuntu Open Week SPE - Stani's Python Editor Python 1 04-29-2008 11:51 AM
Sound Drivers for Ubuntu Linux 5.10 Aardvark Computer Support 11 03-07-2006 10:25 PM



Advertisments