1 from test
import test_support
2 test_support
.requires('audio')
4 from test
.test_support
import findfile
6 ossaudiodev
= test_support
.import_module('ossaudiodev')
15 # Arggh, AFMT_S16_NE not defined on all platforms -- seems to be a
16 # fairly recent addition to OSS.
18 from ossaudiodev
import AFMT_S16_NE
20 if sys
.byteorder
== "little":
21 AFMT_S16_NE
= ossaudiodev
.AFMT_S16_LE
23 AFMT_S16_NE
= ossaudiodev
.AFMT_S16_BE
26 def read_sound_file(path
):
27 with
open(path
, 'rb') as fp
:
29 rate
= au
.getframerate()
30 nchannels
= au
.getnchannels()
31 encoding
= au
._encoding
35 if encoding
!= sunau
.AUDIO_FILE_ENCODING_MULAW_8
:
36 raise RuntimeError("Expect .au file with 8-bit mu-law samples")
38 # Convert the data to 16-bit signed.
39 data
= audioop
.ulaw2lin(data
, 2)
40 return (data
, rate
, 16, nchannels
)
42 class OSSAudioDevTests(unittest
.TestCase
):
44 def play_sound_file(self
, data
, rate
, ssize
, nchannels
):
46 dsp
= ossaudiodev
.open('w')
48 if msg
.args
[0] in (errno
.EACCES
, errno
.ENOENT
,
49 errno
.ENODEV
, errno
.EBUSY
):
50 raise unittest
.SkipTest(msg
)
53 # at least check that these methods can be invoked
60 # Make sure the read-only attributes work.
61 self
.assertFalse(dsp
.closed
)
62 self
.assertEqual(dsp
.name
, "/dev/dsp")
63 self
.assertEqual(dsp
.mode
, "w", "bad dsp.mode: %r" % dsp
.mode
)
65 # And make sure they're really read-only.
66 for attr
in ('closed', 'name', 'mode'):
68 setattr(dsp
, attr
, 42)
72 self
.fail("dsp.%s not read-only" % attr
)
74 # Compute expected running time of sound sample (in seconds).
75 expected_time
= float(len(data
)) / (ssize
//8) / nchannels
/ rate
77 # set parameters based on .au file headers
78 dsp
.setparameters(AFMT_S16_NE
, nchannels
, rate
)
79 self
.assertTrue(abs(expected_time
- 3.51) < 1e-2, expected_time
)
84 elapsed_time
= t2
- t1
86 percent_diff
= (abs(elapsed_time
- expected_time
) / expected_time
) * 100
87 self
.assertTrue(percent_diff
<= 10.0,
88 "elapsed time > 10% off of expected time")
90 def set_parameters(self
, dsp
):
91 # Two configurations for testing:
92 # config1 (8-bit, mono, 8 kHz) should work on even the most
93 # ancient and crufty sound card, but maybe not on special-
94 # purpose high-end hardware
95 # config2 (16-bit, stereo, 44.1kHz) should work on all but the
96 # most ancient and crufty hardware
97 config1
= (ossaudiodev
.AFMT_U8
, 1, 8000)
98 config2
= (AFMT_S16_NE
, 2, 44100)
100 for config
in [config1
, config2
]:
101 (fmt
, channels
, rate
) = config
102 if (dsp
.setfmt(fmt
) == fmt
and
103 dsp
.channels(channels
) == channels
and
104 dsp
.speed(rate
) == rate
):
107 raise RuntimeError("unable to set audio sampling parameters: "
108 "you must have really weird audio hardware")
110 # setparameters() should be able to set this configuration in
111 # either strict or non-strict mode.
112 result
= dsp
.setparameters(fmt
, channels
, rate
, False)
113 self
.assertEqual(result
, (fmt
, channels
, rate
),
114 "setparameters%r: returned %r" % (config
, result
))
116 result
= dsp
.setparameters(fmt
, channels
, rate
, True)
117 self
.assertEqual(result
, (fmt
, channels
, rate
),
118 "setparameters%r: returned %r" % (config
, result
))
120 def set_bad_parameters(self
, dsp
):
121 # Now try some configurations that are presumably bogus: eg. 300
122 # channels currently exceeds even Hollywood's ambitions, and
123 # negative sampling rate is utter nonsense. setparameters() should
124 # accept these in non-strict mode, returning something other than
125 # was requested, but should barf in strict mode.
129 for config
in [(fmt
, 300, rate
), # ridiculous nchannels
130 (fmt
, -5, rate
), # impossible nchannels
131 (fmt
, channels
, -50), # impossible rate
133 (fmt
, channels
, rate
) = config
134 result
= dsp
.setparameters(fmt
, channels
, rate
, False)
135 self
.assertNotEqual(result
, config
,
136 "unexpectedly got requested configuration")
139 result
= dsp
.setparameters(fmt
, channels
, rate
, True)
140 except ossaudiodev
.OSSAudioError
, err
:
143 self
.fail("expected OSSAudioError")
145 def test_playback(self
):
146 sound_info
= read_sound_file(findfile('audiotest.au'))
147 self
.play_sound_file(*sound_info
)
149 def test_set_parameters(self
):
150 dsp
= ossaudiodev
.open("w")
152 self
.set_parameters(dsp
)
154 # Disabled because it fails under Linux 2.6 with ALSA's OSS
156 #self.set_bad_parameters(dsp)
159 self
.assertTrue(dsp
.closed
)
164 dsp
= ossaudiodev
.open('w')
165 except (ossaudiodev
.error
, IOError), msg
:
166 if msg
.args
[0] in (errno
.EACCES
, errno
.ENOENT
,
167 errno
.ENODEV
, errno
.EBUSY
):
168 raise unittest
.SkipTest(msg
)
171 test_support
.run_unittest(__name__
)
173 if __name__
== "__main__":