N
News123
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
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