1 from test
import test_support
2 test_support
.requires('audio')
4 from test
.test_support
import verbose
, findfile
, TestFailed
, TestSkipped
16 # Arggh, AFMT_S16_NE not defined on all platforms -- seems to be a
17 # fairly recent addition to OSS.
19 from ossaudiodev
import AFMT_S16_NE
21 if sys
.byteorder
== "little":
22 AFMT_S16_NE
= ossaudiodev
.AFMT_S16_LE
24 AFMT_S16_NE
= ossaudiodev
.AFMT_S16_BE
27 SND_FORMAT_MULAW_8
= 1
29 def read_sound_file(path
):
31 size
, enc
, rate
, nchannels
, extra
= sunaudio
.gethdr(fp
)
35 if enc
!= SND_FORMAT_MULAW_8
:
36 print "Expect .au file with 8-bit mu-law samples"
39 # Convert the data to 16-bit signed.
40 data
= audioop
.ulaw2lin(data
, 2)
41 return (data
, rate
, 16, nchannels
)
44 def play_sound_file(data
, rate
, ssize
, nchannels
):
46 dsp
= ossaudiodev
.open('w')
48 if msg
[0] in (errno
.EACCES
, errno
.ENODEV
, errno
.EBUSY
):
49 raise TestSkipped
, msg
52 # at least check that these methods can be invoked
59 # set parameters based on .au file headers
60 dsp
.setparameters(AFMT_S16_NE
, nchannels
, rate
)
62 print "playing test sound file..."
66 print "elapsed time: %.1f sec" % (t2
-t1
)
68 def test_setparameters(dsp
):
69 # Two configurations for testing:
70 # config1 (8-bit, mono, 8 kHz) should work on even the most
71 # ancient and crufty sound card, but maybe not on special-
72 # purpose high-end hardware
73 # config2 (16-bit, stereo, 44.1kHz) should work on all but the
74 # most ancient and crufty hardware
75 config1
= (ossaudiodev
.AFMT_U8
, 1, 8000)
76 config2
= (AFMT_S16_NE
, 2, 44100)
78 for config
in [config1
, config2
]:
79 (fmt
, channels
, rate
) = config
80 if (dsp
.setfmt(fmt
) == fmt
and
81 dsp
.channels(channels
) == channels
and
82 dsp
.speed(rate
) == rate
):
85 raise RuntimeError("unable to set audio sampling parameters: "
86 "you must have really weird audio hardware")
88 # setparameters() should be able to set this configuration in
89 # either strict or non-strict mode.
90 result
= dsp
.setparameters(fmt
, channels
, rate
, False)
91 assert result
== (fmt
, channels
, rate
), \
92 "setparameters%r: returned %r" % (config
+ result
)
93 result
= dsp
.setparameters(fmt
, channels
, rate
, True)
94 assert result
== (fmt
, channels
, rate
), \
95 "setparameters%r: returned %r" % (config
+ result
)
97 def test_bad_setparameters(dsp
):
99 # Now try some configurations that are presumably bogus: eg. 300
100 # channels currently exceeds even Hollywood's ambitions, and
101 # negative sampling rate is utter nonsense. setparameters() should
102 # accept these in non-strict mode, returning something other than
103 # was requested, but should barf in strict mode.
107 for config
in [(fmt
, 300, rate
), # ridiculous nchannels
108 (fmt
, -5, rate
), # impossible nchannels
109 (fmt
, channels
, -50), # impossible rate
111 (fmt
, channels
, rate
) = config
112 result
= dsp
.setparameters(fmt
, channels
, rate
, False)
113 assert result
!= config
, \
114 "setparameters: unexpectedly got requested configuration"
117 result
= dsp
.setparameters(fmt
, channels
, rate
, True)
118 raise AssertionError("setparameters: expected OSSAudioError")
119 except ossaudiodev
.OSSAudioError
, err
:
120 print "setparameters: got OSSAudioError as expected"
123 (data
, rate
, ssize
, nchannels
) = read_sound_file(findfile('audiotest.au'))
124 play_sound_file(data
, rate
, ssize
, nchannels
)
126 dsp
= ossaudiodev
.open("w")
128 test_setparameters(dsp
)
130 # Disabled because it fails under Linux 2.6 with ALSA's OSS
132 #test_bad_setparameters(dsp)