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():
69 dsp
= ossaudiodev
.open("w")
71 # Two configurations for testing:
72 # config1 (8-bit, mono, 8 kHz) should work on even the most
73 # ancient and crufty sound card, but maybe not on special-
74 # purpose high-end hardware
75 # config2 (16-bit, stereo, 44.1kHz) should work on all but the
76 # most ancient and crufty hardware
77 config1
= (ossaudiodev
.AFMT_U8
, 1, 8000)
78 config2
= (AFMT_S16_NE
, 2, 44100)
80 for config
in [config1
, config2
]:
81 (fmt
, channels
, rate
) = config
82 if (dsp
.setfmt(fmt
) == fmt
and
83 dsp
.channels(channels
) == channels
and
84 dsp
.speed(rate
) == rate
):
87 raise RuntimeError("unable to set audio sampling parameters: "
88 "you must have really weird audio hardware")
90 # setparameters() should be able to set this configuration in
91 # either strict or non-strict mode.
92 result
= dsp
.setparameters(fmt
, channels
, rate
, False)
93 assert result
== (fmt
, channels
, rate
), \
94 "setparameters%r: returned %r" % (config
+ result
)
95 result
= dsp
.setparameters(fmt
, channels
, rate
, True)
96 assert result
== (fmt
, channels
, rate
), \
97 "setparameters%r: returned %r" % (config
+ result
)
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.
104 for config
in [(fmt
, 300, rate
), # ridiculous nchannels
105 (fmt
, -5, rate
), # impossible nchannels
106 (fmt
, channels
, -50), # impossible rate
108 (fmt
, channels
, rate
) = config
109 result
= dsp
.setparameters(fmt
, channels
, rate
, False)
110 assert result
!= config
, \
111 "setparameters: unexpectedly got requested configuration"
114 result
= dsp
.setparameters(fmt
, channels
, rate
, True)
115 raise AssertionError("setparameters: expected OSSAudioError")
116 except ossaudiodev
.OSSAudioError
, err
:
117 print "setparameters: got OSSAudioError as expected"
120 (data
, rate
, ssize
, nchannels
) = read_sound_file(findfile('audiotest.au'))
121 play_sound_file(data
, rate
, ssize
, nchannels
)