2003-02-14 15:29:22 -04:00
|
|
|
from test import test_support
|
|
|
|
test_support.requires('audio')
|
|
|
|
|
2009-03-26 18:10:30 -03:00
|
|
|
from test.test_support import findfile, run_unittest
|
2000-10-06 16:39:55 -03:00
|
|
|
|
2000-08-04 09:59:40 -03:00
|
|
|
import errno
|
2000-10-07 21:20:20 -03:00
|
|
|
import sys
|
|
|
|
import audioop
|
2007-08-24 15:46:27 -03:00
|
|
|
import unittest
|
2000-10-06 16:39:55 -03:00
|
|
|
|
2010-01-30 03:22:54 -04:00
|
|
|
linuxaudiodev = test_support.import_module('linuxaudiodev', deprecated=True)
|
|
|
|
sunaudio = test_support.import_module('sunaudio', deprecated=True)
|
|
|
|
|
2000-10-06 16:39:55 -03:00
|
|
|
SND_FORMAT_MULAW_8 = 1
|
2000-06-10 01:22:57 -03:00
|
|
|
|
2007-08-24 15:46:27 -03:00
|
|
|
class LinuxAudioDevTests(unittest.TestCase):
|
2000-10-06 16:39:55 -03:00
|
|
|
|
2007-08-24 15:46:27 -03:00
|
|
|
def setUp(self):
|
|
|
|
self.dev = linuxaudiodev.open('w')
|
2000-10-06 16:39:55 -03:00
|
|
|
|
2007-08-24 15:46:27 -03:00
|
|
|
def tearDown(self):
|
|
|
|
self.dev.close()
|
2000-10-06 16:39:55 -03:00
|
|
|
|
2007-08-24 15:46:27 -03:00
|
|
|
def test_methods(self):
|
|
|
|
# at least check that these methods can be invoked
|
|
|
|
self.dev.bufsize()
|
|
|
|
self.dev.obufcount()
|
|
|
|
self.dev.obuffree()
|
|
|
|
self.dev.getptr()
|
|
|
|
self.dev.fileno()
|
2000-10-07 21:20:20 -03:00
|
|
|
|
2007-08-24 15:46:27 -03:00
|
|
|
def test_play_sound_file(self):
|
|
|
|
path = findfile("audiotest.au")
|
|
|
|
fp = open(path, 'r')
|
|
|
|
size, enc, rate, nchannels, extra = sunaudio.gethdr(fp)
|
|
|
|
data = fp.read()
|
|
|
|
fp.close()
|
2000-10-07 21:20:20 -03:00
|
|
|
|
2007-08-24 15:46:27 -03:00
|
|
|
if enc != SND_FORMAT_MULAW_8:
|
|
|
|
self.fail("Expect .au file with 8-bit mu-law samples")
|
2000-10-06 16:39:55 -03:00
|
|
|
|
2007-08-24 15:46:27 -03:00
|
|
|
# convert the data to 16-bit signed
|
|
|
|
data = audioop.ulaw2lin(data, 2)
|
2000-10-06 16:39:55 -03:00
|
|
|
|
2007-08-24 15:46:27 -03:00
|
|
|
# set the data format
|
|
|
|
if sys.byteorder == 'little':
|
|
|
|
fmt = linuxaudiodev.AFMT_S16_LE
|
|
|
|
else:
|
|
|
|
fmt = linuxaudiodev.AFMT_S16_BE
|
|
|
|
|
|
|
|
# set parameters based on .au file headers
|
|
|
|
self.dev.setparameters(rate, 16, nchannels, fmt)
|
|
|
|
self.dev.write(data)
|
|
|
|
self.dev.flush()
|
2000-06-10 01:22:57 -03:00
|
|
|
|
2007-08-24 15:46:27 -03:00
|
|
|
def test_errors(self):
|
|
|
|
size = 8
|
|
|
|
fmt = linuxaudiodev.AFMT_U8
|
|
|
|
rate = 8000
|
|
|
|
nchannels = 1
|
|
|
|
try:
|
|
|
|
self.dev.setparameters(-1, size, nchannels, fmt)
|
|
|
|
except ValueError, err:
|
2010-11-21 09:34:58 -04:00
|
|
|
self.assertEqual(err.args[0], "expected rate >= 0, not -1")
|
2007-08-24 15:46:27 -03:00
|
|
|
try:
|
|
|
|
self.dev.setparameters(rate, -2, nchannels, fmt)
|
|
|
|
except ValueError, err:
|
2010-11-21 09:34:58 -04:00
|
|
|
self.assertEqual(err.args[0], "expected sample size >= 0, not -2")
|
2007-08-24 15:46:27 -03:00
|
|
|
try:
|
|
|
|
self.dev.setparameters(rate, size, 3, fmt)
|
|
|
|
except ValueError, err:
|
2010-11-21 09:34:58 -04:00
|
|
|
self.assertEqual(err.args[0], "nchannels must be 1 or 2, not 3")
|
2007-08-24 15:46:27 -03:00
|
|
|
try:
|
|
|
|
self.dev.setparameters(rate, size, nchannels, 177)
|
|
|
|
except ValueError, err:
|
2010-11-21 09:34:58 -04:00
|
|
|
self.assertEqual(err.args[0], "unknown audio encoding: 177")
|
2007-08-24 15:46:27 -03:00
|
|
|
try:
|
|
|
|
self.dev.setparameters(rate, size, nchannels, linuxaudiodev.AFMT_U16_LE)
|
|
|
|
except ValueError, err:
|
2010-11-21 09:34:58 -04:00
|
|
|
self.assertEqual(err.args[0], "for linear unsigned 16-bit little-endian "
|
|
|
|
"audio, expected sample size 16, not 8")
|
2007-08-24 15:46:27 -03:00
|
|
|
try:
|
|
|
|
self.dev.setparameters(rate, 16, nchannels, fmt)
|
|
|
|
except ValueError, err:
|
2010-11-21 09:34:58 -04:00
|
|
|
self.assertEqual(err.args[0], "for linear unsigned 8-bit audio, expected "
|
|
|
|
"sample size 8, not 16")
|
2007-08-24 15:46:27 -03:00
|
|
|
|
|
|
|
def test_main():
|
|
|
|
try:
|
|
|
|
dsp = linuxaudiodev.open('w')
|
2007-08-24 16:22:34 -03:00
|
|
|
except linuxaudiodev.error, msg:
|
2007-08-24 15:46:27 -03:00
|
|
|
if msg.args[0] in (errno.EACCES, errno.ENOENT, errno.ENODEV, errno.EBUSY):
|
2009-03-26 18:10:30 -03:00
|
|
|
raise unittest.SkipTest(msg)
|
2007-08-24 15:46:27 -03:00
|
|
|
raise
|
|
|
|
dsp.close()
|
|
|
|
run_unittest(LinuxAudioDevTests)
|
2000-06-10 01:22:57 -03:00
|
|
|
|
2007-08-24 15:46:27 -03:00
|
|
|
if __name__ == '__main__':
|
|
|
|
test_main()
|