Commit a0589d49 authored by Greg Ward's avatar Greg Ward

Renamed test_errors() to test_setparameters() and completely rewrote it

to test the new setparameters() interface.

Modified play_sound_file() to print the elapsed time taken to play the
test sample (to the nearest 0.1 sec).
parent 119400af
...@@ -53,43 +53,67 @@ def play_sound_file(data, rate, ssize, nchannels): ...@@ -53,43 +53,67 @@ def play_sound_file(data, rate, ssize, nchannels):
# set parameters based on .au file headers # set parameters based on .au file headers
dsp.setparameters(fmt, nchannels, rate) dsp.setparameters(fmt, nchannels, rate)
t1 = time.time()
print "playing test sound file..."
dsp.write(data) dsp.write(data)
dsp.flush()
dsp.close() dsp.close()
t2 = time.time()
print "elapsed time: %.1f sec" % (t2-t1)
def test_errors(): def test_setparameters():
dsp = ossaudiodev.open("w") dsp = ossaudiodev.open("w")
fmt = ossaudiodev.AFMT_U8
rate = 8000 # Two configurations for testing:
nchannels = 1 # config1 (8-bit, mono, 8 kHz) should work on even the most
try: # ancient and crufty sound card, but maybe not on special-
dsp.setparameters(fmt, nchannels, -1) # purpose high-end hardware
except ossaudiodev.error, msg: # config2 (16-bit, stereo, 44.1kHz) should work on all but the
print msg # most ancient and crufty hardware
try: config1 = (ossaudiodev.AFMT_U8, 1, 8000)
dsp.setparameters(fmt, nchannels, rate) config2 = (ossaudiodev.AFMT_S16_NE, 2, 44100)
except ossaudiodev.error, msg:
print msg for config in [config1, config2]:
try: (fmt, channels, rate) = config
dsp.setparameters(fmt, 3, rate) if (dsp.setfmt(fmt) == fmt and
except ossaudiodev.error, msg: dsp.channels(channels) == channels and
print msg dsp.speed(rate) == rate):
try: break
dsp.setparameters(177, nchannels, rate) else:
except ossaudiodev.error, msg: raise RuntimeError("unable to set audio sampling parameters: "
print msg "you must have really weird audio hardware")
try:
dsp.setparameters(ossaudiodev.AFMT_U16_LE, nchannels, rate) # setparameters() should be able to set this configuration in
except ossaudiodev.error, msg: # either strict or non-strict mode.
print msg result = dsp.setparameters(fmt, channels, rate, False)
try: assert result == (fmt, channels, rate), \
dsp.setparameters(rate, nchannels, fmt) "setparameters%r: returned %r" % (config + result)
except ossaudiodev.error, msg: result = dsp.setparameters(fmt, channels, rate, True)
print msg assert result == (fmt, channels, rate), \
"setparameters%r: returned %r" % (config + result)
# 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.
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)
assert result != config, \
"setparameters: unexpectedly got requested configuration"
try:
result = dsp.setparameters(fmt, channels, rate, True)
raise AssertionError("setparameters: expected OSSAudioError")
except ossaudiodev.OSSAudioError, err:
print "setparameters: got OSSAudioError as expected"
def test(): def test():
(data, rate, ssize, nchannels) = read_sound_file(findfile('audiotest.au')) (data, rate, ssize, nchannels) = read_sound_file(findfile('audiotest.au'))
play_sound_file(data, rate, ssize, nchannels) play_sound_file(data, rate, ssize, nchannels)
test_errors() test_setparameters()
test() test()
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment