blob: 729f6eaa123a84ec90c1a7a0cfebe7a4f5e614ee [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
16SND_FORMAT_MULAW_8 = 1
17
Greg Ward55a87902002-12-10 16:27:35 +000018def read_sound_file(path):
Fred Drake7511bd92003-01-08 07:09:43 +000019 fp = open(path, 'rb')
Greg Ward36dacfa2002-12-10 16:24:21 +000020 size, enc, rate, nchannels, extra = sunaudio.gethdr(fp)
21 data = fp.read()
22 fp.close()
23
24 if enc != SND_FORMAT_MULAW_8:
25 print "Expect .au file with 8-bit mu-law samples"
26 return
27
Greg Ward55a87902002-12-10 16:27:35 +000028 # Convert the data to 16-bit signed.
29 data = audioop.ulaw2lin(data, 2)
30 return (data, rate, 16, nchannels)
31
32
33def play_sound_file(data, rate, ssize, nchannels):
Greg Ward36dacfa2002-12-10 16:24:21 +000034 try:
Greg Ward080c1102003-05-29 00:23:17 +000035 dsp = ossaudiodev.open('w')
Fred Drake7511bd92003-01-08 07:09:43 +000036 except IOError, msg:
Greg Ward36dacfa2002-12-10 16:24:21 +000037 if msg[0] in (errno.EACCES, errno.ENODEV, errno.EBUSY):
38 raise TestSkipped, msg
39 raise TestFailed, msg
40
Greg Ward36dacfa2002-12-10 16:24:21 +000041 # set the data format
42 if sys.byteorder == 'little':
43 fmt = ossaudiodev.AFMT_S16_LE
44 else:
45 fmt = ossaudiodev.AFMT_S16_BE
46
47 # at least check that these methods can be invoked
Greg Ward080c1102003-05-29 00:23:17 +000048 dsp.bufsize()
49 dsp.obufcount()
50 dsp.obuffree()
51 dsp.getptr()
52 dsp.fileno()
Greg Ward36dacfa2002-12-10 16:24:21 +000053
54 # set parameters based on .au file headers
Greg Ward080c1102003-05-29 00:23:17 +000055 dsp.setparameters(fmt, nchannels, rate)
Greg Ward4f12d462003-05-29 01:27:39 +000056 t1 = time.time()
57 print "playing test sound file..."
Greg Ward080c1102003-05-29 00:23:17 +000058 dsp.write(data)
Greg Ward080c1102003-05-29 00:23:17 +000059 dsp.close()
Greg Ward4f12d462003-05-29 01:27:39 +000060 t2 = time.time()
61 print "elapsed time: %.1f sec" % (t2-t1)
Greg Ward36dacfa2002-12-10 16:24:21 +000062
Greg Ward4f12d462003-05-29 01:27:39 +000063def test_setparameters():
Greg Ward080c1102003-05-29 00:23:17 +000064 dsp = ossaudiodev.open("w")
Greg Ward4f12d462003-05-29 01:27:39 +000065
66 # Two configurations for testing:
67 # config1 (8-bit, mono, 8 kHz) should work on even the most
68 # ancient and crufty sound card, but maybe not on special-
69 # purpose high-end hardware
70 # config2 (16-bit, stereo, 44.1kHz) should work on all but the
71 # most ancient and crufty hardware
72 config1 = (ossaudiodev.AFMT_U8, 1, 8000)
73 config2 = (ossaudiodev.AFMT_S16_NE, 2, 44100)
74
75 for config in [config1, config2]:
76 (fmt, channels, rate) = config
77 if (dsp.setfmt(fmt) == fmt and
78 dsp.channels(channels) == channels and
79 dsp.speed(rate) == rate):
80 break
81 else:
82 raise RuntimeError("unable to set audio sampling parameters: "
83 "you must have really weird audio hardware")
84
85 # setparameters() should be able to set this configuration in
86 # either strict or non-strict mode.
87 result = dsp.setparameters(fmt, channels, rate, False)
88 assert result == (fmt, channels, rate), \
89 "setparameters%r: returned %r" % (config + result)
90 result = dsp.setparameters(fmt, channels, rate, True)
91 assert result == (fmt, channels, rate), \
92 "setparameters%r: returned %r" % (config + result)
93
94 # Now try some configurations that are presumably bogus: eg. 300
95 # channels currently exceeds even Hollywood's ambitions, and
96 # negative sampling rate is utter nonsense. setparameters() should
97 # accept these in non-strict mode, returning something other than
98 # was requested, but should barf in strict mode.
99 for config in [(fmt, 300, rate), # ridiculous nchannels
100 (fmt, -5, rate), # impossible nchannels
101 (fmt, channels, -50), # impossible rate
102 ]:
103 (fmt, channels, rate) = config
104 result = dsp.setparameters(fmt, channels, rate, False)
105 assert result != config, \
106 "setparameters: unexpectedly got requested configuration"
107
108 try:
109 result = dsp.setparameters(fmt, channels, rate, True)
110 raise AssertionError("setparameters: expected OSSAudioError")
111 except ossaudiodev.OSSAudioError, err:
112 print "setparameters: got OSSAudioError as expected"
Greg Ward36dacfa2002-12-10 16:24:21 +0000113
114def test():
Greg Ward55a87902002-12-10 16:27:35 +0000115 (data, rate, ssize, nchannels) = read_sound_file(findfile('audiotest.au'))
116 play_sound_file(data, rate, ssize, nchannels)
Greg Ward4f12d462003-05-29 01:27:39 +0000117 test_setparameters()
Greg Ward36dacfa2002-12-10 16:24:21 +0000118
119test()