blob: f2ce67bad796ed8c0d6aa80a0413385d045b78ba [file] [log] [blame]
Guido van Rossum4507ec72003-02-14 19:29:22 +00001from test import test_support
2test_support.requires('audio')
3
Christian Heimesc5f05e42008-02-23 17:40:11 +00004from test.test_support import findfile, TestSkipped
Greg Ward36dacfa2002-12-10 16:24:21 +00005
6import errno
Greg Ward36dacfa2002-12-10 16:24:21 +00007import ossaudiodev
Greg Ward36dacfa2002-12-10 16:24:21 +00008import sys
Greg Ward36dacfa2002-12-10 16:24:21 +00009import sunaudio
10import time
11import audioop
Collin Winterfb5b9892007-04-25 20:41:34 +000012import unittest
Greg Ward36dacfa2002-12-10 16:24:21 +000013
Greg Ward8a709b32003-06-03 00:32:44 +000014# Arggh, AFMT_S16_NE not defined on all platforms -- seems to be a
15# fairly recent addition to OSS.
16try:
17 from ossaudiodev import AFMT_S16_NE
18except ImportError:
19 if sys.byteorder == "little":
20 AFMT_S16_NE = ossaudiodev.AFMT_S16_LE
21 else:
22 AFMT_S16_NE = ossaudiodev.AFMT_S16_BE
23
24
Greg Ward36dacfa2002-12-10 16:24:21 +000025SND_FORMAT_MULAW_8 = 1
26
Greg Ward55a87902002-12-10 16:27:35 +000027def read_sound_file(path):
Fred Drake7511bd92003-01-08 07:09:43 +000028 fp = open(path, 'rb')
Greg Ward36dacfa2002-12-10 16:24:21 +000029 size, enc, rate, nchannels, extra = sunaudio.gethdr(fp)
30 data = fp.read()
31 fp.close()
32
33 if enc != SND_FORMAT_MULAW_8:
Collin Winterfb5b9892007-04-25 20:41:34 +000034 raise RuntimeError("Expect .au file with 8-bit mu-law samples")
Greg Ward36dacfa2002-12-10 16:24:21 +000035
Greg Ward55a87902002-12-10 16:27:35 +000036 # Convert the data to 16-bit signed.
37 data = audioop.ulaw2lin(data, 2)
38 return (data, rate, 16, nchannels)
39
Collin Winterfb5b9892007-04-25 20:41:34 +000040class OSSAudioDevTests(unittest.TestCase):
Greg Ward55a87902002-12-10 16:27:35 +000041
Collin Winterfb5b9892007-04-25 20:41:34 +000042 def play_sound_file(self, data, rate, ssize, nchannels):
Greg Ward50682d02005-03-07 01:41:11 +000043 try:
Collin Winterfb5b9892007-04-25 20:41:34 +000044 dsp = ossaudiodev.open('w')
45 except IOError, msg:
46 if msg[0] in (errno.EACCES, errno.ENOENT, errno.ENODEV, errno.EBUSY):
47 raise TestSkipped(msg)
48 raise
Greg Ward50682d02005-03-07 01:41:11 +000049
Collin Winterfb5b9892007-04-25 20:41:34 +000050 # at least check that these methods can be invoked
51 dsp.bufsize()
52 dsp.obufcount()
53 dsp.obuffree()
54 dsp.getptr()
55 dsp.fileno()
Greg Ward7802af42006-07-23 02:25:53 +000056
Collin Winterfb5b9892007-04-25 20:41:34 +000057 # Make sure the read-only attributes work.
58 self.failUnless(dsp.close)
59 self.assertEqual(dsp.name, "/dev/dsp")
60 self.assertEqual(dsp.mode, "w", "bad dsp.mode: %r" % dsp.mode)
Greg Ward7802af42006-07-23 02:25:53 +000061
Collin Winterfb5b9892007-04-25 20:41:34 +000062 # And make sure they're really read-only.
63 for attr in ('closed', 'name', 'mode'):
64 try:
65 setattr(dsp, attr, 42)
66 except TypeError:
67 pass
68 else:
69 self.fail("dsp.%s not read-only" % attr)
Greg Ward36dacfa2002-12-10 16:24:21 +000070
Collin Winterfb5b9892007-04-25 20:41:34 +000071 # Compute expected running time of sound sample (in seconds).
72 expected_time = float(len(data)) / (ssize/8) / nchannels / rate
Greg Ward4f12d462003-05-29 01:27:39 +000073
Collin Winterfb5b9892007-04-25 20:41:34 +000074 # set parameters based on .au file headers
75 dsp.setparameters(AFMT_S16_NE, nchannels, rate)
Georg Brandle7445de2007-08-24 18:35:27 +000076 self.assertEquals("%.2f" % expected_time, "2.93")
Collin Winterfb5b9892007-04-25 20:41:34 +000077 t1 = time.time()
78 dsp.write(data)
Greg Ward50682d02005-03-07 01:41:11 +000079 dsp.close()
Collin Winterfb5b9892007-04-25 20:41:34 +000080 t2 = time.time()
81 elapsed_time = t2 - t1
Greg Ward36dacfa2002-12-10 16:24:21 +000082
Collin Winterfb5b9892007-04-25 20:41:34 +000083 percent_diff = (abs(elapsed_time - expected_time) / expected_time) * 100
84 self.failUnless(percent_diff <= 10.0,
85 "elapsed time > 10% off of expected time")
86
87 def set_parameters(self, dsp):
88 # Two configurations for testing:
89 # config1 (8-bit, mono, 8 kHz) should work on even the most
90 # ancient and crufty sound card, but maybe not on special-
91 # purpose high-end hardware
92 # config2 (16-bit, stereo, 44.1kHz) should work on all but the
93 # most ancient and crufty hardware
94 config1 = (ossaudiodev.AFMT_U8, 1, 8000)
95 config2 = (AFMT_S16_NE, 2, 44100)
96
97 for config in [config1, config2]:
98 (fmt, channels, rate) = config
99 if (dsp.setfmt(fmt) == fmt and
100 dsp.channels(channels) == channels and
101 dsp.speed(rate) == rate):
102 break
103 else:
104 raise RuntimeError("unable to set audio sampling parameters: "
105 "you must have really weird audio hardware")
106
107 # setparameters() should be able to set this configuration in
108 # either strict or non-strict mode.
109 result = dsp.setparameters(fmt, channels, rate, False)
110 self.assertEqual(result, (fmt, channels, rate),
111 "setparameters%r: returned %r" % (config, result))
112
113 result = dsp.setparameters(fmt, channels, rate, True)
114 self.assertEqual(result, (fmt, channels, rate),
115 "setparameters%r: returned %r" % (config, result))
116
117 def set_bad_parameters(self, dsp):
Collin Winterfb5b9892007-04-25 20:41:34 +0000118 # Now try some configurations that are presumably bogus: eg. 300
119 # channels currently exceeds even Hollywood's ambitions, and
120 # negative sampling rate is utter nonsense. setparameters() should
121 # accept these in non-strict mode, returning something other than
122 # was requested, but should barf in strict mode.
123 fmt = AFMT_S16_NE
124 rate = 44100
125 channels = 2
126 for config in [(fmt, 300, rate), # ridiculous nchannels
127 (fmt, -5, rate), # impossible nchannels
128 (fmt, channels, -50), # impossible rate
129 ]:
130 (fmt, channels, rate) = config
131 result = dsp.setparameters(fmt, channels, rate, False)
132 self.failIfEqual(result, config,
133 "unexpectedly got requested configuration")
134
135 try:
136 result = dsp.setparameters(fmt, channels, rate, True)
137 except ossaudiodev.OSSAudioError, err:
138 pass
139 else:
140 self.fail("expected OSSAudioError")
141
142 def test_playback(self):
143 sound_info = read_sound_file(findfile('audiotest.au'))
144 self.play_sound_file(*sound_info)
145
146 def test_set_parameters(self):
147 dsp = ossaudiodev.open("w")
148 try:
149 self.set_parameters(dsp)
150
151 # Disabled because it fails under Linux 2.6 with ALSA's OSS
152 # emulation layer.
153 #self.set_bad_parameters(dsp)
154 finally:
155 dsp.close()
156 self.failUnless(dsp.closed)
157
158
159def test_main():
Collin Wintercf795b42007-04-25 21:50:25 +0000160 try:
161 dsp = ossaudiodev.open('w')
Neal Norwitz1b2f62d2007-08-26 22:16:23 +0000162 except (ossaudiodev.error, IOError), msg:
Collin Wintercf795b42007-04-25 21:50:25 +0000163 if msg[0] in (errno.EACCES, errno.ENOENT, errno.ENODEV, errno.EBUSY):
164 raise TestSkipped(msg)
165 raise
Georg Brandlfe3b4b92007-08-24 18:46:27 +0000166 dsp.close()
Collin Winterfb5b9892007-04-25 20:41:34 +0000167 test_support.run_unittest(__name__)
168
169if __name__ == "__main__":
170 test_main()