Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 4f12d46

Browse files
committed
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).
1 parent 080c110 commit 4f12d46

1 file changed

Lines changed: 54 additions & 30 deletions

File tree

Lib/test/test_ossaudiodev.py

Lines changed: 54 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -53,43 +53,67 @@ def play_sound_file(data, rate, ssize, nchannels):
5353

5454
# set parameters based on .au file headers
5555
dsp.setparameters(fmt, nchannels, rate)
56+
t1 = time.time()
57+
print "playing test sound file..."
5658
dsp.write(data)
57-
dsp.flush()
5859
dsp.close()
60+
t2 = time.time()
61+
print "elapsed time: %.1f sec" % (t2-t1)
5962

60-
def test_errors():
63+
def test_setparameters():
6164
dsp = ossaudiodev.open("w")
62-
fmt = ossaudiodev.AFMT_U8
63-
rate = 8000
64-
nchannels = 1
65-
try:
66-
dsp.setparameters(fmt, nchannels, -1)
67-
except ossaudiodev.error, msg:
68-
print msg
69-
try:
70-
dsp.setparameters(fmt, nchannels, rate)
71-
except ossaudiodev.error, msg:
72-
print msg
73-
try:
74-
dsp.setparameters(fmt, 3, rate)
75-
except ossaudiodev.error, msg:
76-
print msg
77-
try:
78-
dsp.setparameters(177, nchannels, rate)
79-
except ossaudiodev.error, msg:
80-
print msg
81-
try:
82-
dsp.setparameters(ossaudiodev.AFMT_U16_LE, nchannels, rate)
83-
except ossaudiodev.error, msg:
84-
print msg
85-
try:
86-
dsp.setparameters(rate, nchannels, fmt)
87-
except ossaudiodev.error, msg:
88-
print msg
65+
66+
# Two configurations for testing:
67+
# config1 (8-bit, mono, 8 kHz) should work on even the most
68+
# ancient and crufty sound card, but maybe not on special-
69+
# purpose high-end hardware
70+
# config2 (16-bit, stereo, 44.1kHz) should work on all but the
71+
# most ancient and crufty hardware
72+
config1 = (ossaudiodev.AFMT_U8, 1, 8000)
73+
config2 = (ossaudiodev.AFMT_S16_NE, 2, 44100)
74+
75+
for config in [config1, config2]:
76+
(fmt, channels, rate) = config
77+
if (dsp.setfmt(fmt) == fmt and
78+
dsp.channels(channels) == channels and
79+
dsp.speed(rate) == rate):
80+
break
81+
else:
82+
raise RuntimeError("unable to set audio sampling parameters: "
83+
"you must have really weird audio hardware")
84+
85+
# setparameters() should be able to set this configuration in
86+
# either strict or non-strict mode.
87+
result = dsp.setparameters(fmt, channels, rate, False)
88+
assert result == (fmt, channels, rate), \
89+
"setparameters%r: returned %r" % (config + result)
90+
result = dsp.setparameters(fmt, channels, rate, True)
91+
assert result == (fmt, channels, rate), \
92+
"setparameters%r: returned %r" % (config + result)
93+
94+
# Now try some configurations that are presumably bogus: eg. 300
95+
# channels currently exceeds even Hollywood's ambitions, and
96+
# negative sampling rate is utter nonsense. setparameters() should
97+
# accept these in non-strict mode, returning something other than
98+
# was requested, but should barf in strict mode.
99+
for config in [(fmt, 300, rate), # ridiculous nchannels
100+
(fmt, -5, rate), # impossible nchannels
101+
(fmt, channels, -50), # impossible rate
102+
]:
103+
(fmt, channels, rate) = config
104+
result = dsp.setparameters(fmt, channels, rate, False)
105+
assert result != config, \
106+
"setparameters: unexpectedly got requested configuration"
107+
108+
try:
109+
result = dsp.setparameters(fmt, channels, rate, True)
110+
raise AssertionError("setparameters: expected OSSAudioError")
111+
except ossaudiodev.OSSAudioError, err:
112+
print "setparameters: got OSSAudioError as expected"
89113

90114
def test():
91115
(data, rate, ssize, nchannels) = read_sound_file(findfile('audiotest.au'))
92116
play_sound_file(data, rate, ssize, nchannels)
93-
test_errors()
117+
test_setparameters()
94118

95119
test()

0 commit comments

Comments
 (0)