2003-02-14 15:29:22 -04:00
|
|
|
from test import test_support
|
|
|
|
test_support.requires('audio')
|
|
|
|
|
2002-12-10 12:24:21 -04:00
|
|
|
from test.test_support import verbose, findfile, TestFailed, TestSkipped
|
|
|
|
|
|
|
|
import errno
|
|
|
|
import fcntl
|
|
|
|
import ossaudiodev
|
|
|
|
import os
|
|
|
|
import sys
|
|
|
|
import select
|
|
|
|
import sunaudio
|
|
|
|
import time
|
|
|
|
import audioop
|
|
|
|
|
|
|
|
SND_FORMAT_MULAW_8 = 1
|
|
|
|
|
2002-12-10 12:27:35 -04:00
|
|
|
def read_sound_file(path):
|
2003-01-08 03:09:43 -04:00
|
|
|
fp = open(path, 'rb')
|
2002-12-10 12:24:21 -04:00
|
|
|
size, enc, rate, nchannels, extra = sunaudio.gethdr(fp)
|
|
|
|
data = fp.read()
|
|
|
|
fp.close()
|
|
|
|
|
|
|
|
if enc != SND_FORMAT_MULAW_8:
|
|
|
|
print "Expect .au file with 8-bit mu-law samples"
|
|
|
|
return
|
|
|
|
|
2002-12-10 12:27:35 -04:00
|
|
|
# Convert the data to 16-bit signed.
|
|
|
|
data = audioop.ulaw2lin(data, 2)
|
|
|
|
return (data, rate, 16, nchannels)
|
|
|
|
|
|
|
|
|
|
|
|
def play_sound_file(data, rate, ssize, nchannels):
|
2002-12-10 12:24:21 -04:00
|
|
|
try:
|
2003-05-28 21:23:17 -03:00
|
|
|
dsp = ossaudiodev.open('w')
|
2003-01-08 03:09:43 -04:00
|
|
|
except IOError, msg:
|
2002-12-10 12:24:21 -04:00
|
|
|
if msg[0] in (errno.EACCES, errno.ENODEV, errno.EBUSY):
|
|
|
|
raise TestSkipped, msg
|
|
|
|
raise TestFailed, msg
|
|
|
|
|
|
|
|
# set the data format
|
|
|
|
if sys.byteorder == 'little':
|
|
|
|
fmt = ossaudiodev.AFMT_S16_LE
|
|
|
|
else:
|
|
|
|
fmt = ossaudiodev.AFMT_S16_BE
|
|
|
|
|
|
|
|
# at least check that these methods can be invoked
|
2003-05-28 21:23:17 -03:00
|
|
|
dsp.bufsize()
|
|
|
|
dsp.obufcount()
|
|
|
|
dsp.obuffree()
|
|
|
|
dsp.getptr()
|
|
|
|
dsp.fileno()
|
2002-12-10 12:24:21 -04:00
|
|
|
|
|
|
|
# set parameters based on .au file headers
|
2003-05-28 21:23:17 -03:00
|
|
|
dsp.setparameters(fmt, nchannels, rate)
|
|
|
|
dsp.write(data)
|
|
|
|
dsp.flush()
|
|
|
|
dsp.close()
|
2002-12-10 12:24:21 -04:00
|
|
|
|
|
|
|
def test_errors():
|
2003-05-28 21:23:17 -03:00
|
|
|
dsp = ossaudiodev.open("w")
|
2002-12-10 12:24:21 -04:00
|
|
|
fmt = ossaudiodev.AFMT_U8
|
|
|
|
rate = 8000
|
|
|
|
nchannels = 1
|
|
|
|
try:
|
2003-05-28 21:23:17 -03:00
|
|
|
dsp.setparameters(fmt, nchannels, -1)
|
|
|
|
except ossaudiodev.error, msg:
|
2002-12-10 12:24:21 -04:00
|
|
|
print msg
|
|
|
|
try:
|
2003-05-28 21:23:17 -03:00
|
|
|
dsp.setparameters(fmt, nchannels, rate)
|
|
|
|
except ossaudiodev.error, msg:
|
2002-12-10 12:24:21 -04:00
|
|
|
print msg
|
|
|
|
try:
|
2003-05-28 21:23:17 -03:00
|
|
|
dsp.setparameters(fmt, 3, rate)
|
|
|
|
except ossaudiodev.error, msg:
|
2002-12-10 12:24:21 -04:00
|
|
|
print msg
|
|
|
|
try:
|
2003-05-28 21:23:17 -03:00
|
|
|
dsp.setparameters(177, nchannels, rate)
|
|
|
|
except ossaudiodev.error, msg:
|
2002-12-10 12:24:21 -04:00
|
|
|
print msg
|
|
|
|
try:
|
2003-05-28 21:23:17 -03:00
|
|
|
dsp.setparameters(ossaudiodev.AFMT_U16_LE, nchannels, rate)
|
|
|
|
except ossaudiodev.error, msg:
|
2002-12-10 12:24:21 -04:00
|
|
|
print msg
|
|
|
|
try:
|
2003-05-28 21:23:17 -03:00
|
|
|
dsp.setparameters(rate, nchannels, fmt)
|
|
|
|
except ossaudiodev.error, msg:
|
2002-12-10 12:24:21 -04:00
|
|
|
print msg
|
|
|
|
|
|
|
|
def test():
|
2002-12-10 12:27:35 -04:00
|
|
|
(data, rate, ssize, nchannels) = read_sound_file(findfile('audiotest.au'))
|
|
|
|
play_sound_file(data, rate, ssize, nchannels)
|
2002-12-10 12:24:21 -04:00
|
|
|
test_errors()
|
|
|
|
|
|
|
|
test()
|