blob: 0377e9c4ea21ab69bb650cedb2877adb18195040 [file] [log] [blame]
Guido van Rossum4507ec72003-02-14 19:29:22 +00001from test import test_support
2test_support.requires('audio')
3
Greg Ward36dacfa2002-12-10 16:24:21 +00004from test.test_support import verbose, findfile, TestFailed, TestSkipped
5
6import errno
7import fcntl
8import ossaudiodev
9import os
10import sys
11import select
12import sunaudio
13import time
14import audioop
15
Greg Ward8a709b32003-06-03 00:32:44 +000016# Arggh, AFMT_S16_NE not defined on all platforms -- seems to be a
17# fairly recent addition to OSS.
18try:
19 from ossaudiodev import AFMT_S16_NE
20except ImportError:
21 if sys.byteorder == "little":
22 AFMT_S16_NE = ossaudiodev.AFMT_S16_LE
23 else:
24 AFMT_S16_NE = ossaudiodev.AFMT_S16_BE
25
26
Greg Ward36dacfa2002-12-10 16:24:21 +000027SND_FORMAT_MULAW_8 = 1
28
Greg Ward55a87902002-12-10 16:27:35 +000029def read_sound_file(path):
Fred Drake7511bd92003-01-08 07:09:43 +000030 fp = open(path, 'rb')
Greg Ward36dacfa2002-12-10 16:24:21 +000031 size, enc, rate, nchannels, extra = sunaudio.gethdr(fp)
32 data = fp.read()
33 fp.close()
34
35 if enc != SND_FORMAT_MULAW_8:
36 print "Expect .au file with 8-bit mu-law samples"
37 return
38
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
Thomas Wouters0e3f5912006-08-11 14:57:12 +000043# version of assert that still works with -O
44def _assert(expr, message=None):
45 if not expr:
46 raise AssertionError(message or "assertion failed")
Greg Ward55a87902002-12-10 16:27:35 +000047
48def play_sound_file(data, rate, ssize, nchannels):
Greg Ward36dacfa2002-12-10 16:24:21 +000049 try:
Greg Ward080c1102003-05-29 00:23:17 +000050 dsp = ossaudiodev.open('w')
Guido van Rossumb940e112007-01-10 16:19:56 +000051 except IOError as msg:
Neal Norwitzc6d1f912006-01-05 07:16:13 +000052 if msg[0] in (errno.EACCES, errno.ENOENT, errno.ENODEV, errno.EBUSY):
Greg Ward36dacfa2002-12-10 16:24:21 +000053 raise TestSkipped, msg
54 raise TestFailed, msg
55
Greg Ward36dacfa2002-12-10 16:24:21 +000056 # at least check that these methods can be invoked
Greg Ward080c1102003-05-29 00:23:17 +000057 dsp.bufsize()
58 dsp.obufcount()
59 dsp.obuffree()
60 dsp.getptr()
61 dsp.fileno()
Greg Ward36dacfa2002-12-10 16:24:21 +000062
Greg Ward50682d02005-03-07 01:41:11 +000063 # Make sure the read-only attributes work.
Thomas Wouters0e3f5912006-08-11 14:57:12 +000064 _assert(dsp.closed is False, "dsp.closed is not False")
65 _assert(dsp.name == "/dev/dsp")
66 _assert(dsp.mode == 'w', "bad dsp.mode: %r" % dsp.mode)
Greg Ward50682d02005-03-07 01:41:11 +000067
68 # And make sure they're really read-only.
69 for attr in ('closed', 'name', 'mode'):
70 try:
71 setattr(dsp, attr, 42)
72 raise RuntimeError("dsp.%s not read-only" % attr)
73 except TypeError:
74 pass
75
Thomas Wouters0e3f5912006-08-11 14:57:12 +000076 # Compute expected running time of sound sample (in seconds).
77 expected_time = float(len(data)) / (ssize/8) / nchannels / rate
78
Greg Ward36dacfa2002-12-10 16:24:21 +000079 # set parameters based on .au file headers
Greg Ward8a709b32003-06-03 00:32:44 +000080 dsp.setparameters(AFMT_S16_NE, nchannels, rate)
Thomas Wouters0e3f5912006-08-11 14:57:12 +000081 print ("playing test sound file (expected running time: %.2f sec)"
82 % expected_time)
Greg Ward4f12d462003-05-29 01:27:39 +000083 t1 = time.time()
Greg Ward080c1102003-05-29 00:23:17 +000084 dsp.write(data)
Greg Ward080c1102003-05-29 00:23:17 +000085 dsp.close()
Greg Ward4f12d462003-05-29 01:27:39 +000086 t2 = time.time()
Thomas Wouters0e3f5912006-08-11 14:57:12 +000087 elapsed_time = t2 - t1
88
89 percent_diff = (abs(elapsed_time - expected_time) / expected_time) * 100
90 _assert(percent_diff <= 10.0, \
91 ("elapsed time (%.2f sec) > 10%% off of expected time (%.2f sec)"
92 % (elapsed_time, expected_time)))
Greg Ward36dacfa2002-12-10 16:24:21 +000093
Greg Ward50682d02005-03-07 01:41:11 +000094def test_setparameters(dsp):
Greg Ward4f12d462003-05-29 01:27:39 +000095 # Two configurations for testing:
96 # config1 (8-bit, mono, 8 kHz) should work on even the most
97 # ancient and crufty sound card, but maybe not on special-
98 # purpose high-end hardware
99 # config2 (16-bit, stereo, 44.1kHz) should work on all but the
100 # most ancient and crufty hardware
101 config1 = (ossaudiodev.AFMT_U8, 1, 8000)
Andrew MacIntyre348c2612003-07-02 14:05:08 +0000102 config2 = (AFMT_S16_NE, 2, 44100)
Greg Ward4f12d462003-05-29 01:27:39 +0000103
104 for config in [config1, config2]:
105 (fmt, channels, rate) = config
106 if (dsp.setfmt(fmt) == fmt and
107 dsp.channels(channels) == channels and
108 dsp.speed(rate) == rate):
109 break
110 else:
111 raise RuntimeError("unable to set audio sampling parameters: "
112 "you must have really weird audio hardware")
113
114 # setparameters() should be able to set this configuration in
115 # either strict or non-strict mode.
116 result = dsp.setparameters(fmt, channels, rate, False)
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000117 _assert(result == (fmt, channels, rate),
118 "setparameters%r: returned %r" % (config, result))
Greg Ward4f12d462003-05-29 01:27:39 +0000119 result = dsp.setparameters(fmt, channels, rate, True)
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000120 _assert(result == (fmt, channels, rate),
121 "setparameters%r: returned %r" % (config, result))
Greg Ward4f12d462003-05-29 01:27:39 +0000122
Greg Ward50682d02005-03-07 01:41:11 +0000123def test_bad_setparameters(dsp):
124
Greg Ward4f12d462003-05-29 01:27:39 +0000125 # Now try some configurations that are presumably bogus: eg. 300
126 # channels currently exceeds even Hollywood's ambitions, and
127 # negative sampling rate is utter nonsense. setparameters() should
128 # accept these in non-strict mode, returning something other than
129 # was requested, but should barf in strict mode.
Greg Ward50682d02005-03-07 01:41:11 +0000130 fmt = AFMT_S16_NE
131 rate = 44100
132 channels = 2
Greg Ward4f12d462003-05-29 01:27:39 +0000133 for config in [(fmt, 300, rate), # ridiculous nchannels
134 (fmt, -5, rate), # impossible nchannels
135 (fmt, channels, -50), # impossible rate
136 ]:
137 (fmt, channels, rate) = config
138 result = dsp.setparameters(fmt, channels, rate, False)
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000139 _assert(result != config,
140 "setparameters: unexpectedly got requested configuration")
Greg Ward4f12d462003-05-29 01:27:39 +0000141
142 try:
143 result = dsp.setparameters(fmt, channels, rate, True)
144 raise AssertionError("setparameters: expected OSSAudioError")
Guido van Rossumb940e112007-01-10 16:19:56 +0000145 except ossaudiodev.OSSAudioError as err:
Greg Ward4f12d462003-05-29 01:27:39 +0000146 print "setparameters: got OSSAudioError as expected"
Greg Ward36dacfa2002-12-10 16:24:21 +0000147
148def test():
Greg Ward55a87902002-12-10 16:27:35 +0000149 (data, rate, ssize, nchannels) = read_sound_file(findfile('audiotest.au'))
150 play_sound_file(data, rate, ssize, nchannels)
Greg Ward50682d02005-03-07 01:41:11 +0000151
152 dsp = ossaudiodev.open("w")
153 try:
154 test_setparameters(dsp)
155
156 # Disabled because it fails under Linux 2.6 with ALSA's OSS
157 # emulation layer.
158 #test_bad_setparameters(dsp)
159 finally:
160 dsp.close()
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000161 _assert(dsp.closed is True, "dsp.closed is not True")
Greg Ward36dacfa2002-12-10 16:24:21 +0000162
163test()