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
|
2002-12-10 12:24:21 -04:00
|
|
|
|
2009-03-30 20:05:48 -03:00
|
|
|
ossaudiodev = test_support.import_module('ossaudiodev')
|
|
|
|
|
2002-12-10 12:24:21 -04:00
|
|
|
import errno
|
|
|
|
import sys
|
2008-07-18 16:30:22 -03:00
|
|
|
import sunau
|
2002-12-10 12:24:21 -04:00
|
|
|
import time
|
|
|
|
import audioop
|
2007-04-25 17:41:34 -03:00
|
|
|
import unittest
|
2002-12-10 12:24:21 -04:00
|
|
|
|
2003-06-02 21:32:44 -03:00
|
|
|
# Arggh, AFMT_S16_NE not defined on all platforms -- seems to be a
|
|
|
|
# fairly recent addition to OSS.
|
|
|
|
try:
|
|
|
|
from ossaudiodev import AFMT_S16_NE
|
|
|
|
except ImportError:
|
|
|
|
if sys.byteorder == "little":
|
|
|
|
AFMT_S16_NE = ossaudiodev.AFMT_S16_LE
|
|
|
|
else:
|
|
|
|
AFMT_S16_NE = ossaudiodev.AFMT_S16_BE
|
|
|
|
|
|
|
|
|
2002-12-10 12:27:35 -04:00
|
|
|
def read_sound_file(path):
|
2008-07-18 16:30:22 -03:00
|
|
|
with open(path, 'rb') as fp:
|
|
|
|
au = sunau.open(fp)
|
|
|
|
rate = au.getframerate()
|
|
|
|
nchannels = au.getnchannels()
|
|
|
|
encoding = au._encoding
|
|
|
|
fp.seek(0)
|
|
|
|
data = fp.read()
|
2002-12-10 12:24:21 -04:00
|
|
|
|
2008-07-18 16:30:22 -03:00
|
|
|
if encoding != sunau.AUDIO_FILE_ENCODING_MULAW_8:
|
2007-04-25 17:41:34 -03:00
|
|
|
raise RuntimeError("Expect .au file with 8-bit mu-law samples")
|
2002-12-10 12:24:21 -04:00
|
|
|
|
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)
|
|
|
|
|
2007-04-25 17:41:34 -03:00
|
|
|
class OSSAudioDevTests(unittest.TestCase):
|
2003-05-28 22:27:39 -03:00
|
|
|
|
2007-04-25 17:41:34 -03:00
|
|
|
def play_sound_file(self, data, rate, ssize, nchannels):
|
2003-05-28 22:27:39 -03:00
|
|
|
try:
|
2007-04-25 17:41:34 -03:00
|
|
|
dsp = ossaudiodev.open('w')
|
|
|
|
except IOError, msg:
|
2010-02-03 01:37:26 -04: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-04-25 17:41:34 -03:00
|
|
|
raise
|
|
|
|
|
|
|
|
# at least check that these methods can be invoked
|
|
|
|
dsp.bufsize()
|
|
|
|
dsp.obufcount()
|
|
|
|
dsp.obuffree()
|
|
|
|
dsp.getptr()
|
|
|
|
dsp.fileno()
|
|
|
|
|
|
|
|
# Make sure the read-only attributes work.
|
2009-06-30 19:57:08 -03:00
|
|
|
self.assertFalse(dsp.closed)
|
2007-04-25 17:41:34 -03:00
|
|
|
self.assertEqual(dsp.name, "/dev/dsp")
|
|
|
|
self.assertEqual(dsp.mode, "w", "bad dsp.mode: %r" % dsp.mode)
|
|
|
|
|
|
|
|
# And make sure they're really read-only.
|
|
|
|
for attr in ('closed', 'name', 'mode'):
|
|
|
|
try:
|
|
|
|
setattr(dsp, attr, 42)
|
|
|
|
except TypeError:
|
|
|
|
pass
|
|
|
|
else:
|
|
|
|
self.fail("dsp.%s not read-only" % attr)
|
|
|
|
|
|
|
|
# Compute expected running time of sound sample (in seconds).
|
2010-02-03 01:37:26 -04:00
|
|
|
expected_time = float(len(data)) / (ssize//8) / nchannels / rate
|
2007-04-25 17:41:34 -03:00
|
|
|
|
|
|
|
# set parameters based on .au file headers
|
|
|
|
dsp.setparameters(AFMT_S16_NE, nchannels, rate)
|
2010-05-05 13:15:09 -03:00
|
|
|
self.assertTrue(abs(expected_time - 3.51) < 1e-2, expected_time)
|
2007-04-25 17:41:34 -03:00
|
|
|
t1 = time.time()
|
|
|
|
dsp.write(data)
|
2005-03-06 21:41:11 -04:00
|
|
|
dsp.close()
|
2007-04-25 17:41:34 -03:00
|
|
|
t2 = time.time()
|
|
|
|
elapsed_time = t2 - t1
|
|
|
|
|
|
|
|
percent_diff = (abs(elapsed_time - expected_time) / expected_time) * 100
|
2009-06-30 19:57:08 -03:00
|
|
|
self.assertTrue(percent_diff <= 10.0,
|
2007-04-25 17:41:34 -03:00
|
|
|
"elapsed time > 10% off of expected time")
|
|
|
|
|
|
|
|
def set_parameters(self, dsp):
|
|
|
|
# Two configurations for testing:
|
|
|
|
# config1 (8-bit, mono, 8 kHz) should work on even the most
|
|
|
|
# ancient and crufty sound card, but maybe not on special-
|
|
|
|
# purpose high-end hardware
|
|
|
|
# config2 (16-bit, stereo, 44.1kHz) should work on all but the
|
|
|
|
# most ancient and crufty hardware
|
|
|
|
config1 = (ossaudiodev.AFMT_U8, 1, 8000)
|
|
|
|
config2 = (AFMT_S16_NE, 2, 44100)
|
|
|
|
|
|
|
|
for config in [config1, config2]:
|
|
|
|
(fmt, channels, rate) = config
|
|
|
|
if (dsp.setfmt(fmt) == fmt and
|
|
|
|
dsp.channels(channels) == channels and
|
|
|
|
dsp.speed(rate) == rate):
|
|
|
|
break
|
|
|
|
else:
|
|
|
|
raise RuntimeError("unable to set audio sampling parameters: "
|
|
|
|
"you must have really weird audio hardware")
|
|
|
|
|
|
|
|
# setparameters() should be able to set this configuration in
|
|
|
|
# either strict or non-strict mode.
|
|
|
|
result = dsp.setparameters(fmt, channels, rate, False)
|
|
|
|
self.assertEqual(result, (fmt, channels, rate),
|
|
|
|
"setparameters%r: returned %r" % (config, result))
|
|
|
|
|
|
|
|
result = dsp.setparameters(fmt, channels, rate, True)
|
|
|
|
self.assertEqual(result, (fmt, channels, rate),
|
|
|
|
"setparameters%r: returned %r" % (config, result))
|
|
|
|
|
|
|
|
def set_bad_parameters(self, dsp):
|
|
|
|
# Now try some configurations that are presumably bogus: eg. 300
|
|
|
|
# channels currently exceeds even Hollywood's ambitions, and
|
|
|
|
# negative sampling rate is utter nonsense. setparameters() should
|
|
|
|
# accept these in non-strict mode, returning something other than
|
|
|
|
# was requested, but should barf in strict mode.
|
|
|
|
fmt = AFMT_S16_NE
|
|
|
|
rate = 44100
|
|
|
|
channels = 2
|
|
|
|
for config in [(fmt, 300, rate), # ridiculous nchannels
|
|
|
|
(fmt, -5, rate), # impossible nchannels
|
|
|
|
(fmt, channels, -50), # impossible rate
|
|
|
|
]:
|
|
|
|
(fmt, channels, rate) = config
|
|
|
|
result = dsp.setparameters(fmt, channels, rate, False)
|
2009-06-30 19:57:08 -03:00
|
|
|
self.assertNotEqual(result, config,
|
2007-04-25 17:41:34 -03:00
|
|
|
"unexpectedly got requested configuration")
|
|
|
|
|
|
|
|
try:
|
|
|
|
result = dsp.setparameters(fmt, channels, rate, True)
|
|
|
|
except ossaudiodev.OSSAudioError, err:
|
|
|
|
pass
|
|
|
|
else:
|
|
|
|
self.fail("expected OSSAudioError")
|
|
|
|
|
|
|
|
def test_playback(self):
|
|
|
|
sound_info = read_sound_file(findfile('audiotest.au'))
|
|
|
|
self.play_sound_file(*sound_info)
|
|
|
|
|
|
|
|
def test_set_parameters(self):
|
|
|
|
dsp = ossaudiodev.open("w")
|
|
|
|
try:
|
|
|
|
self.set_parameters(dsp)
|
|
|
|
|
|
|
|
# Disabled because it fails under Linux 2.6 with ALSA's OSS
|
|
|
|
# emulation layer.
|
|
|
|
#self.set_bad_parameters(dsp)
|
|
|
|
finally:
|
|
|
|
dsp.close()
|
2009-06-30 19:57:08 -03:00
|
|
|
self.assertTrue(dsp.closed)
|
2007-04-25 17:41:34 -03:00
|
|
|
|
|
|
|
|
|
|
|
def test_main():
|
2007-04-25 18:50:25 -03:00
|
|
|
try:
|
|
|
|
dsp = ossaudiodev.open('w')
|
2007-08-26 19:16:23 -03:00
|
|
|
except (ossaudiodev.error, IOError), msg:
|
2010-02-03 01:37:26 -04: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-04-25 18:50:25 -03:00
|
|
|
raise
|
2007-08-24 15:46:27 -03:00
|
|
|
dsp.close()
|
2007-04-25 17:41:34 -03:00
|
|
|
test_support.run_unittest(__name__)
|
2002-12-10 12:24:21 -04:00
|
|
|
|
2007-04-25 17:41:34 -03:00
|
|
|
if __name__ == "__main__":
|
|
|
|
test_main()
|