blob: 0cdc93cc73020b0103f3154d072b02a66df8a86b [file] [log] [blame]
Guido van Rossum4507ec72003-02-14 19:29:22 +00001from test import test_support
2test_support.requires('audio')
3
Guido van Rossumd8faa362007-04-27 19:54:29 +00004from test.test_support import verbose, findfile, TestSkipped
Greg Ward36dacfa2002-12-10 16:24:21 +00005
6import errno
7import fcntl
8import ossaudiodev
9import os
10import sys
11import select
12import sunaudio
13import time
14import audioop
Guido van Rossumd8faa362007-04-27 19:54:29 +000015import unittest
Greg Ward36dacfa2002-12-10 16:24:21 +000016
Greg Ward8a709b32003-06-03 00:32:44 +000017# Arggh, AFMT_S16_NE not defined on all platforms -- seems to be a
18# fairly recent addition to OSS.
19try:
20 from ossaudiodev import AFMT_S16_NE
21except ImportError:
22 if sys.byteorder == "little":
23 AFMT_S16_NE = ossaudiodev.AFMT_S16_LE
24 else:
25 AFMT_S16_NE = ossaudiodev.AFMT_S16_BE
26
27
Greg Ward36dacfa2002-12-10 16:24:21 +000028SND_FORMAT_MULAW_8 = 1
29
Greg Ward55a87902002-12-10 16:27:35 +000030def read_sound_file(path):
Fred Drake7511bd92003-01-08 07:09:43 +000031 fp = open(path, 'rb')
Greg Ward36dacfa2002-12-10 16:24:21 +000032 size, enc, rate, nchannels, extra = sunaudio.gethdr(fp)
33 data = fp.read()
34 fp.close()
35
36 if enc != SND_FORMAT_MULAW_8:
Guido van Rossumd8faa362007-04-27 19:54:29 +000037 raise RuntimeError("Expect .au file with 8-bit mu-law samples")
Greg Ward36dacfa2002-12-10 16:24:21 +000038
Greg Ward55a87902002-12-10 16:27:35 +000039 # Convert the data to 16-bit signed.
40 data = audioop.ulaw2lin(data, 2)
41 return (data, rate, 16, nchannels)
42
Guido van Rossumd8faa362007-04-27 19:54:29 +000043class OSSAudioDevTests(unittest.TestCase):
Greg Ward55a87902002-12-10 16:27:35 +000044
Guido van Rossumd8faa362007-04-27 19:54:29 +000045 def play_sound_file(self, data, rate, ssize, nchannels):
46 try:
47 dsp = ossaudiodev.open('w')
48 except IOError as msg:
49 if msg.args[0] in (errno.EACCES, errno.ENOENT,
50 errno.ENODEV, errno.EBUSY):
51 raise TestSkipped(msg)
52 raise
53
54 # at least check that these methods can be invoked
55 dsp.bufsize()
56 dsp.obufcount()
57 dsp.obuffree()
58 dsp.getptr()
59 dsp.fileno()
60
61 # Make sure the read-only attributes work.
62 self.failUnless(dsp.close)
63 self.assertEqual(dsp.name, "/dev/dsp")
64 self.assertEqual(dsp.mode, "w", "bad dsp.mode: %r" % dsp.mode)
65
66 # And make sure they're really read-only.
67 for attr in ('closed', 'name', 'mode'):
68 try:
69 setattr(dsp, attr, 42)
70 except TypeError:
71 pass
72 else:
73 self.fail("dsp.%s not read-only" % attr)
74
75 # Compute expected running time of sound sample (in seconds).
76 expected_time = float(len(data)) / (ssize/8) / nchannels / rate
77
78 # set parameters based on .au file headers
79 dsp.setparameters(AFMT_S16_NE, nchannels, rate)
80 print ("playing test sound file (expected running time: %.2f sec)"
81 % expected_time)
82 t1 = time.time()
83 dsp.write(data)
84 dsp.close()
85 t2 = time.time()
86 elapsed_time = t2 - t1
87
88 percent_diff = (abs(elapsed_time - expected_time) / expected_time) * 100
89 self.failUnless(percent_diff <= 10.0,
90 "elapsed time > 10% off of expected time")
91
92 def set_parameters(self, dsp):
93 # Two configurations for testing:
94 # config1 (8-bit, mono, 8 kHz) should work on even the most
95 # ancient and crufty sound card, but maybe not on special-
96 # purpose high-end hardware
97 # config2 (16-bit, stereo, 44.1kHz) should work on all but the
98 # most ancient and crufty hardware
99 config1 = (ossaudiodev.AFMT_U8, 1, 8000)
100 config2 = (AFMT_S16_NE, 2, 44100)
101
102 for config in [config1, config2]:
103 (fmt, channels, rate) = config
104 if (dsp.setfmt(fmt) == fmt and
105 dsp.channels(channels) == channels and
106 dsp.speed(rate) == rate):
107 break
108 else:
109 raise RuntimeError("unable to set audio sampling parameters: "
110 "you must have really weird audio hardware")
111
112 # setparameters() should be able to set this configuration in
113 # either strict or non-strict mode.
114 result = dsp.setparameters(fmt, channels, rate, False)
115 self.assertEqual(result, (fmt, channels, rate),
116 "setparameters%r: returned %r" % (config, result))
117
118 result = dsp.setparameters(fmt, channels, rate, True)
119 self.assertEqual(result, (fmt, channels, rate),
120 "setparameters%r: returned %r" % (config, result))
121
122 def set_bad_parameters(self, dsp):
123
124 # Now try some configurations that are presumably bogus: eg. 300
125 # channels currently exceeds even Hollywood's ambitions, and
126 # negative sampling rate is utter nonsense. setparameters() should
127 # accept these in non-strict mode, returning something other than
128 # was requested, but should barf in strict mode.
129 fmt = AFMT_S16_NE
130 rate = 44100
131 channels = 2
132 for config in [(fmt, 300, rate), # ridiculous nchannels
133 (fmt, -5, rate), # impossible nchannels
134 (fmt, channels, -50), # impossible rate
135 ]:
136 (fmt, channels, rate) = config
137 result = dsp.setparameters(fmt, channels, rate, False)
138 self.failIfEqual(result, config,
139 "unexpectedly got requested configuration")
140
141 try:
142 result = dsp.setparameters(fmt, channels, rate, True)
143 except ossaudiodev.OSSAudioError as err:
144 pass
145 else:
146 self.fail("expected OSSAudioError")
147
148 def test_playback(self):
149 sound_info = read_sound_file(findfile('audiotest.au'))
150 self.play_sound_file(*sound_info)
151
152 def test_set_parameters(self):
153 dsp = ossaudiodev.open("w")
154 try:
155 self.set_parameters(dsp)
156
157 # Disabled because it fails under Linux 2.6 with ALSA's OSS
158 # emulation layer.
159 #self.set_bad_parameters(dsp)
160 finally:
161 dsp.close()
162 self.failUnless(dsp.closed)
163
164
165def test_main():
Greg Ward36dacfa2002-12-10 16:24:21 +0000166 try:
Greg Ward080c1102003-05-29 00:23:17 +0000167 dsp = ossaudiodev.open('w')
Guido van Rossumb940e112007-01-10 16:19:56 +0000168 except IOError as msg:
Guido van Rossumd8faa362007-04-27 19:54:29 +0000169 if msg.args[0] in (errno.EACCES, errno.ENOENT,
170 errno.ENODEV, errno.EBUSY):
171 raise TestSkipped(msg)
172 raise
Guido van Rossum23cfc982007-08-21 22:49:52 +0000173 dsp.close()
Guido van Rossumd8faa362007-04-27 19:54:29 +0000174 test_support.run_unittest(__name__)
Greg Ward36dacfa2002-12-10 16:24:21 +0000175
Guido van Rossumd8faa362007-04-27 19:54:29 +0000176if __name__ == "__main__":
177 test_main()