blob: 9f64406d1ec9dbed7166e94ff7b6ee5e7e1f607f [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
43
44def play_sound_file(data, rate, ssize, nchannels):
Greg Ward36dacfa2002-12-10 16:24:21 +000045 try:
Greg Ward080c1102003-05-29 00:23:17 +000046 dsp = ossaudiodev.open('w')
Fred Drake7511bd92003-01-08 07:09:43 +000047 except IOError, msg:
Neal Norwitzc6d1f912006-01-05 07:16:13 +000048 if msg[0] in (errno.EACCES, errno.ENOENT, errno.ENODEV, errno.EBUSY):
Greg Ward36dacfa2002-12-10 16:24:21 +000049 raise TestSkipped, msg
50 raise TestFailed, msg
51
Greg Ward36dacfa2002-12-10 16:24:21 +000052 # at least check that these methods can be invoked
Greg Ward080c1102003-05-29 00:23:17 +000053 dsp.bufsize()
54 dsp.obufcount()
55 dsp.obuffree()
56 dsp.getptr()
57 dsp.fileno()
Greg Ward36dacfa2002-12-10 16:24:21 +000058
Greg Ward50682d02005-03-07 01:41:11 +000059 # Make sure the read-only attributes work.
60 assert dsp.closed is False, "dsp.closed is not False"
61 assert dsp.name == "/dev/dsp"
62 assert dsp.mode == 'w', "bad dsp.mode: %r" % dsp.mode
63
64 # And make sure they're really read-only.
65 for attr in ('closed', 'name', 'mode'):
66 try:
67 setattr(dsp, attr, 42)
68 raise RuntimeError("dsp.%s not read-only" % attr)
69 except TypeError:
70 pass
71
Greg Ward7802af42006-07-23 02:25:53 +000072 # Compute expected running time of sound sample (in seconds).
73 expected_time = float(len(data)) / (ssize/8) / nchannels / rate
74
Greg Ward36dacfa2002-12-10 16:24:21 +000075 # set parameters based on .au file headers
Greg Ward8a709b32003-06-03 00:32:44 +000076 dsp.setparameters(AFMT_S16_NE, nchannels, rate)
Greg Ward7802af42006-07-23 02:25:53 +000077 print ("playing test sound file (expected running time: %.2f sec)"
78 % expected_time)
Greg Ward4f12d462003-05-29 01:27:39 +000079 t1 = time.time()
Greg Ward080c1102003-05-29 00:23:17 +000080 dsp.write(data)
Greg Ward080c1102003-05-29 00:23:17 +000081 dsp.close()
Greg Ward4f12d462003-05-29 01:27:39 +000082 t2 = time.time()
Greg Ward7802af42006-07-23 02:25:53 +000083 elapsed_time = t2 - t1
84
85 percent_diff = (abs(elapsed_time - expected_time) / expected_time) * 100
86 #print ("actual running time was %.2f sec (%.1f%% difference)"
87 # % (elapsed_time, percent_diff))
88 assert percent_diff <= 10.0, \
89 ("elapsed time (%.2f sec) > 10%% off of expected time (%.2f sec)"
90 % (elapsed_time, expected_time))
Greg Ward36dacfa2002-12-10 16:24:21 +000091
Greg Ward50682d02005-03-07 01:41:11 +000092def test_setparameters(dsp):
Greg Ward4f12d462003-05-29 01:27:39 +000093 # 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)
Andrew MacIntyre348c2612003-07-02 14:05:08 +0000100 config2 = (AFMT_S16_NE, 2, 44100)
Greg Ward4f12d462003-05-29 01:27:39 +0000101
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 assert result == (fmt, channels, rate), \
116 "setparameters%r: returned %r" % (config + result)
117 result = dsp.setparameters(fmt, channels, rate, True)
118 assert result == (fmt, channels, rate), \
119 "setparameters%r: returned %r" % (config + result)
120
Greg Ward50682d02005-03-07 01:41:11 +0000121def test_bad_setparameters(dsp):
122
Greg Ward4f12d462003-05-29 01:27:39 +0000123 # Now try some configurations that are presumably bogus: eg. 300
124 # channels currently exceeds even Hollywood's ambitions, and
125 # negative sampling rate is utter nonsense. setparameters() should
126 # accept these in non-strict mode, returning something other than
127 # was requested, but should barf in strict mode.
Greg Ward50682d02005-03-07 01:41:11 +0000128 fmt = AFMT_S16_NE
129 rate = 44100
130 channels = 2
Greg Ward4f12d462003-05-29 01:27:39 +0000131 for config in [(fmt, 300, rate), # ridiculous nchannels
132 (fmt, -5, rate), # impossible nchannels
133 (fmt, channels, -50), # impossible rate
134 ]:
135 (fmt, channels, rate) = config
136 result = dsp.setparameters(fmt, channels, rate, False)
137 assert result != config, \
138 "setparameters: unexpectedly got requested configuration"
139
140 try:
141 result = dsp.setparameters(fmt, channels, rate, True)
142 raise AssertionError("setparameters: expected OSSAudioError")
143 except ossaudiodev.OSSAudioError, err:
144 print "setparameters: got OSSAudioError as expected"
Greg Ward36dacfa2002-12-10 16:24:21 +0000145
146def test():
Greg Ward55a87902002-12-10 16:27:35 +0000147 (data, rate, ssize, nchannels) = read_sound_file(findfile('audiotest.au'))
148 play_sound_file(data, rate, ssize, nchannels)
Greg Ward50682d02005-03-07 01:41:11 +0000149
150 dsp = ossaudiodev.open("w")
151 try:
152 test_setparameters(dsp)
153
154 # Disabled because it fails under Linux 2.6 with ALSA's OSS
155 # emulation layer.
156 #test_bad_setparameters(dsp)
157 finally:
158 dsp.close()
159 assert dsp.closed is True, "dsp.closed is not True"
Greg Ward36dacfa2002-12-10 16:24:21 +0000160
161test()