blob: f668eb41d7ed07a5c641095e8071c87240e43125 [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:
Greg Ward36dacfa2002-12-10 16:24:21 +000048 if msg[0] in (errno.EACCES, errno.ENODEV, errno.EBUSY):
49 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 Ward36dacfa2002-12-10 16:24:21 +000072 # set parameters based on .au file headers
Greg Ward8a709b32003-06-03 00:32:44 +000073 dsp.setparameters(AFMT_S16_NE, nchannels, rate)
Greg Ward4f12d462003-05-29 01:27:39 +000074 t1 = time.time()
75 print "playing test sound file..."
Greg Ward080c1102003-05-29 00:23:17 +000076 dsp.write(data)
Greg Ward080c1102003-05-29 00:23:17 +000077 dsp.close()
Greg Ward4f12d462003-05-29 01:27:39 +000078 t2 = time.time()
79 print "elapsed time: %.1f sec" % (t2-t1)
Greg Ward36dacfa2002-12-10 16:24:21 +000080
Greg Ward50682d02005-03-07 01:41:11 +000081def test_setparameters(dsp):
Greg Ward4f12d462003-05-29 01:27:39 +000082 # Two configurations for testing:
83 # config1 (8-bit, mono, 8 kHz) should work on even the most
84 # ancient and crufty sound card, but maybe not on special-
85 # purpose high-end hardware
86 # config2 (16-bit, stereo, 44.1kHz) should work on all but the
87 # most ancient and crufty hardware
88 config1 = (ossaudiodev.AFMT_U8, 1, 8000)
Andrew MacIntyre348c2612003-07-02 14:05:08 +000089 config2 = (AFMT_S16_NE, 2, 44100)
Greg Ward4f12d462003-05-29 01:27:39 +000090
91 for config in [config1, config2]:
92 (fmt, channels, rate) = config
93 if (dsp.setfmt(fmt) == fmt and
94 dsp.channels(channels) == channels and
95 dsp.speed(rate) == rate):
96 break
97 else:
98 raise RuntimeError("unable to set audio sampling parameters: "
99 "you must have really weird audio hardware")
100
101 # setparameters() should be able to set this configuration in
102 # either strict or non-strict mode.
103 result = dsp.setparameters(fmt, channels, rate, False)
104 assert result == (fmt, channels, rate), \
105 "setparameters%r: returned %r" % (config + result)
106 result = dsp.setparameters(fmt, channels, rate, True)
107 assert result == (fmt, channels, rate), \
108 "setparameters%r: returned %r" % (config + result)
109
Greg Ward50682d02005-03-07 01:41:11 +0000110def test_bad_setparameters(dsp):
111
Greg Ward4f12d462003-05-29 01:27:39 +0000112 # Now try some configurations that are presumably bogus: eg. 300
113 # channels currently exceeds even Hollywood's ambitions, and
114 # negative sampling rate is utter nonsense. setparameters() should
115 # accept these in non-strict mode, returning something other than
116 # was requested, but should barf in strict mode.
Greg Ward50682d02005-03-07 01:41:11 +0000117 fmt = AFMT_S16_NE
118 rate = 44100
119 channels = 2
Greg Ward4f12d462003-05-29 01:27:39 +0000120 for config in [(fmt, 300, rate), # ridiculous nchannels
121 (fmt, -5, rate), # impossible nchannels
122 (fmt, channels, -50), # impossible rate
123 ]:
124 (fmt, channels, rate) = config
125 result = dsp.setparameters(fmt, channels, rate, False)
126 assert result != config, \
127 "setparameters: unexpectedly got requested configuration"
128
129 try:
130 result = dsp.setparameters(fmt, channels, rate, True)
131 raise AssertionError("setparameters: expected OSSAudioError")
132 except ossaudiodev.OSSAudioError, err:
133 print "setparameters: got OSSAudioError as expected"
Greg Ward36dacfa2002-12-10 16:24:21 +0000134
135def test():
Greg Ward55a87902002-12-10 16:27:35 +0000136 (data, rate, ssize, nchannels) = read_sound_file(findfile('audiotest.au'))
137 play_sound_file(data, rate, ssize, nchannels)
Greg Ward50682d02005-03-07 01:41:11 +0000138
139 dsp = ossaudiodev.open("w")
140 try:
141 test_setparameters(dsp)
142
143 # Disabled because it fails under Linux 2.6 with ALSA's OSS
144 # emulation layer.
145 #test_bad_setparameters(dsp)
146 finally:
147 dsp.close()
148 assert dsp.closed is True, "dsp.closed is not True"
Greg Ward36dacfa2002-12-10 16:24:21 +0000149
150test()