python alsa problems on BeagleBoard xm (Angstrom)

116 views
Skip to first unread message

Mik

unread,
Mar 6, 2012, 9:40:14 AM3/6/12
to Beagle Board
Dear all,

I am a beginner with BeagleBoard (xM) and audio. I am using Angstrom.
I've been investigating this issue without success and I have a kind
of feeling that I am missing something basic.

I am trying to use alsa audio from a python script.
The sample script provided with alsa audio package works on PC, but
not on BeagleBoard.
In the BeagleBoard the audio comes out just as noise.

I thought it would be wrong settings, but then I played the audio from

root@beagleboard:~# aplay -t wav -c 2 -r 44100 -f S16_LE -v
pyNewsPrint/audio/song01.wav

and the output provides me the same parameters I am using. (At least
these I am configuring).

Any idea?

this is the output of 'aplay' (working):


Playing WAVE 'pyNewsPrint/audio/song01.wav' : Signed 16 bit Little
Endian, Rate 44100 Hz,o
Plug PCM: Hardware PCM card 0 'omap3beagle' device 0 subdevice 0
Its setup is:
stream : PLAYBACK
access : RW_INTERLEAVED
format : S16_LE
subformat : STD
channels : 2
rate : 44100
exact rate : 44100 (44100/1)
msbits : 16
buffer_size : 22016
period_size : 512
period_time : 11609
tstamp_mode : NONE
period_step : 1
avail_min : 512
period_event : 0
start_threshold : 22016
stop_threshold : 22016
silence_threshold: 0
silence_size : 0
boundary : 1442840576
appl_ptr : 0
hw_ptr : 0


#######################################################################
and this is the python script:
import sys
import wave
import getopt
import alsaaudio

def play(device, f):

sys.stdout.write('%d channels, %d sampling rate\n' %
(f.getnchannels(),

f.getframerate()))
# Set attributes
print "DBG: channels are "+str( f.getnchannels() )
device.setchannels(f.getnchannels())
print "DBG: rate is "+str( f.getframerate() )
device.setrate(f.getframerate())

# 8bit is unsigned in wav files
if f.getsampwidth() == 1:
device.setformat(alsaaudio.PCM_FORMAT_U8)
# Otherwise we assume signed data, little endian
elif f.getsampwidth() == 2:
device.setformat(alsaaudio.PCM_FORMAT_S16_LE)
elif f.getsampwidth() == 3:
device.setformat(alsaaudio.PCM_FORMAT_S24_LE)
elif f.getsampwidth() == 4:
device.setformat(alsaaudio.PCM_FORMAT_S32_LE)
else:
raise ValueError('Unsupported format')

device.setperiodsize(320)
#tried this as well: device.setperiodsize( f.getframerate() )
#tried this as well: device.setperiodsize(512)

data = f.readframes(320)
#tried this as well: data = f.readframes(512)

while data:
# Read data from stdin
device.write(data)
data = f.readframes(320)


def usage():
sys.stderr.write('usage: playwav.py [-c <card>] <file>\n')
sys.exit(2)

if __name__ == '__main__':

card = 'default'

opts, args = getopt.getopt(sys.argv[1:], 'c:')
for o, a in opts:
if o == '-c':
card = a

if not args:
usage()

f = wave.open(args[0], 'rb')
device = alsaaudio.PCM(card=card)
#tired this as well: device =
alsaaudio.PCM(alsaaudio.PCM_PLAYBACK, alsaaudio.PCM_NORMAL, card=card)

play(device, f)

f.close()


Any idea will be really appreciated!
Thanks!
Mik
Reply all
Reply to author
Forward
Message has been deleted
0 new messages