blob: 21aa0ff5fc4cd57fcea980511baca8e3898e62fc [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
59 # set parameters based on .au file headers
Greg Ward8a709b32003-06-03 00:32:44 +000060 dsp.setparameters(AFMT_S16_NE, nchannels, rate)
Greg Ward4f12d462003-05-29 01:27:39 +000061 t1 = time.time()
62 print "playing test sound file..."
Greg Ward080c1102003-05-29 00:23:17 +000063 dsp.write(data)
Greg Ward080c1102003-05-29 00:23:17 +000064 dsp.close()
Greg Ward4f12d462003-05-29 01:27:39 +000065 t2 = time.time()
66 print "elapsed time: %.1f sec" % (t2-t1)
Greg Ward36dacfa2002-12-10 16:24:21 +000067
Greg Ward4f12d462003-05-29 01:27:39 +000068def test_setparameters():
Greg Ward080c1102003-05-29 00:23:17 +000069 dsp = ossaudiodev.open("w")
Greg Ward4f12d462003-05-29 01:27:39 +000070
71 # Two configurations for testing:
72 # config1 (8-bit, mono, 8 kHz) should work on even the most
73 # ancient and crufty sound card, but maybe not on special-
74 # purpose high-end hardware
75 # config2 (16-bit, stereo, 44.1kHz) should work on all but the
76 # most ancient and crufty hardware
77 config1 = (ossaudiodev.AFMT_U8, 1, 8000)
78 config2 = (ossaudiodev.AFMT_S16_NE, 2, 44100)
79
80 for config in [config1, config2]:
81 (fmt, channels, rate) = config
82 if (dsp.setfmt(fmt) == fmt and
83 dsp.channels(channels) == channels and
84 dsp.speed(rate) == rate):
85 break
86 else:
87 raise RuntimeError("unable to set audio sampling parameters: "
88 "you must have really weird audio hardware")
89
90 # setparameters() should be able to set this configuration in
91 # either strict or non-strict mode.
92 result = dsp.setparameters(fmt, channels, rate, False)
93 assert result == (fmt, channels, rate), \
94 "setparameters%r: returned %r" % (config + result)
95 result = dsp.setparameters(fmt, channels, rate, True)
96 assert result == (fmt, channels, rate), \
97 "setparameters%r: returned %r" % (config + result)
98
99 # Now try some configurations that are presumably bogus: eg. 300
100 # channels currently exceeds even Hollywood's ambitions, and
101 # negative sampling rate is utter nonsense. setparameters() should
102 # accept these in non-strict mode, returning something other than
103 # was requested, but should barf in strict mode.
104 for config in [(fmt, 300, rate), # ridiculous nchannels
105 (fmt, -5, rate), # impossible nchannels
106 (fmt, channels, -50), # impossible rate
107 ]:
108 (fmt, channels, rate) = config
109 result = dsp.setparameters(fmt, channels, rate, False)
110 assert result != config, \
111 "setparameters: unexpectedly got requested configuration"
112
113 try:
114 result = dsp.setparameters(fmt, channels, rate, True)
115 raise AssertionError("setparameters: expected OSSAudioError")
116 except ossaudiodev.OSSAudioError, err:
117 print "setparameters: got OSSAudioError as expected"
Greg Ward36dacfa2002-12-10 16:24:21 +0000118
119def test():
Greg Ward55a87902002-12-10 16:27:35 +0000120 (data, rate, ssize, nchannels) = read_sound_file(findfile('audiotest.au'))
121 play_sound_file(data, rate, ssize, nchannels)
Greg Ward4f12d462003-05-29 01:27:39 +0000122 test_setparameters()
Greg Ward36dacfa2002-12-10 16:24:21 +0000123
124test()