Guido van Rossum | 4507ec7 | 2003-02-14 19:29:22 +0000 | [diff] [blame] | 1 | from test import test_support |
| 2 | test_support.requires('audio') |
| 3 | |
Greg Ward | 36dacfa | 2002-12-10 16:24:21 +0000 | [diff] [blame] | 4 | from test.test_support import verbose, findfile, TestFailed, TestSkipped |
| 5 | |
| 6 | import errno |
| 7 | import fcntl |
| 8 | import ossaudiodev |
| 9 | import os |
| 10 | import sys |
| 11 | import select |
| 12 | import sunaudio |
| 13 | import time |
| 14 | import audioop |
| 15 | |
Greg Ward | 8a709b3 | 2003-06-03 00:32:44 +0000 | [diff] [blame] | 16 | # Arggh, AFMT_S16_NE not defined on all platforms -- seems to be a |
| 17 | # fairly recent addition to OSS. |
| 18 | try: |
| 19 | from ossaudiodev import AFMT_S16_NE |
| 20 | except 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 Ward | 36dacfa | 2002-12-10 16:24:21 +0000 | [diff] [blame] | 27 | SND_FORMAT_MULAW_8 = 1 |
| 28 | |
Greg Ward | 55a8790 | 2002-12-10 16:27:35 +0000 | [diff] [blame] | 29 | def read_sound_file(path): |
Fred Drake | 7511bd9 | 2003-01-08 07:09:43 +0000 | [diff] [blame] | 30 | fp = open(path, 'rb') |
Greg Ward | 36dacfa | 2002-12-10 16:24:21 +0000 | [diff] [blame] | 31 | 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 Ward | 55a8790 | 2002-12-10 16:27:35 +0000 | [diff] [blame] | 39 | # Convert the data to 16-bit signed. |
| 40 | data = audioop.ulaw2lin(data, 2) |
| 41 | return (data, rate, 16, nchannels) |
| 42 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 43 | # version of assert that still works with -O |
| 44 | def _assert(expr, message=None): |
| 45 | if not expr: |
| 46 | raise AssertionError(message or "assertion failed") |
Greg Ward | 55a8790 | 2002-12-10 16:27:35 +0000 | [diff] [blame] | 47 | |
| 48 | def play_sound_file(data, rate, ssize, nchannels): |
Greg Ward | 36dacfa | 2002-12-10 16:24:21 +0000 | [diff] [blame] | 49 | try: |
Greg Ward | 080c110 | 2003-05-29 00:23:17 +0000 | [diff] [blame] | 50 | dsp = ossaudiodev.open('w') |
Guido van Rossum | b940e11 | 2007-01-10 16:19:56 +0000 | [diff] [blame] | 51 | except IOError as msg: |
Neal Norwitz | c6d1f91 | 2006-01-05 07:16:13 +0000 | [diff] [blame] | 52 | if msg[0] in (errno.EACCES, errno.ENOENT, errno.ENODEV, errno.EBUSY): |
Greg Ward | 36dacfa | 2002-12-10 16:24:21 +0000 | [diff] [blame] | 53 | raise TestSkipped, msg |
| 54 | raise TestFailed, msg |
| 55 | |
Greg Ward | 36dacfa | 2002-12-10 16:24:21 +0000 | [diff] [blame] | 56 | # at least check that these methods can be invoked |
Greg Ward | 080c110 | 2003-05-29 00:23:17 +0000 | [diff] [blame] | 57 | dsp.bufsize() |
| 58 | dsp.obufcount() |
| 59 | dsp.obuffree() |
| 60 | dsp.getptr() |
| 61 | dsp.fileno() |
Greg Ward | 36dacfa | 2002-12-10 16:24:21 +0000 | [diff] [blame] | 62 | |
Greg Ward | 50682d0 | 2005-03-07 01:41:11 +0000 | [diff] [blame] | 63 | # Make sure the read-only attributes work. |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 64 | _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 Ward | 50682d0 | 2005-03-07 01:41:11 +0000 | [diff] [blame] | 67 | |
| 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 Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 76 | # Compute expected running time of sound sample (in seconds). |
| 77 | expected_time = float(len(data)) / (ssize/8) / nchannels / rate |
| 78 | |
Greg Ward | 36dacfa | 2002-12-10 16:24:21 +0000 | [diff] [blame] | 79 | # set parameters based on .au file headers |
Greg Ward | 8a709b3 | 2003-06-03 00:32:44 +0000 | [diff] [blame] | 80 | dsp.setparameters(AFMT_S16_NE, nchannels, rate) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 81 | print ("playing test sound file (expected running time: %.2f sec)" |
| 82 | % expected_time) |
Greg Ward | 4f12d46 | 2003-05-29 01:27:39 +0000 | [diff] [blame] | 83 | t1 = time.time() |
Greg Ward | 080c110 | 2003-05-29 00:23:17 +0000 | [diff] [blame] | 84 | dsp.write(data) |
Greg Ward | 080c110 | 2003-05-29 00:23:17 +0000 | [diff] [blame] | 85 | dsp.close() |
Greg Ward | 4f12d46 | 2003-05-29 01:27:39 +0000 | [diff] [blame] | 86 | t2 = time.time() |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 87 | 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 Ward | 36dacfa | 2002-12-10 16:24:21 +0000 | [diff] [blame] | 93 | |
Greg Ward | 50682d0 | 2005-03-07 01:41:11 +0000 | [diff] [blame] | 94 | def test_setparameters(dsp): |
Greg Ward | 4f12d46 | 2003-05-29 01:27:39 +0000 | [diff] [blame] | 95 | # 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 MacIntyre | 348c261 | 2003-07-02 14:05:08 +0000 | [diff] [blame] | 102 | config2 = (AFMT_S16_NE, 2, 44100) |
Greg Ward | 4f12d46 | 2003-05-29 01:27:39 +0000 | [diff] [blame] | 103 | |
| 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 Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 117 | _assert(result == (fmt, channels, rate), |
| 118 | "setparameters%r: returned %r" % (config, result)) |
Greg Ward | 4f12d46 | 2003-05-29 01:27:39 +0000 | [diff] [blame] | 119 | result = dsp.setparameters(fmt, channels, rate, True) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 120 | _assert(result == (fmt, channels, rate), |
| 121 | "setparameters%r: returned %r" % (config, result)) |
Greg Ward | 4f12d46 | 2003-05-29 01:27:39 +0000 | [diff] [blame] | 122 | |
Greg Ward | 50682d0 | 2005-03-07 01:41:11 +0000 | [diff] [blame] | 123 | def test_bad_setparameters(dsp): |
| 124 | |
Greg Ward | 4f12d46 | 2003-05-29 01:27:39 +0000 | [diff] [blame] | 125 | # 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 Ward | 50682d0 | 2005-03-07 01:41:11 +0000 | [diff] [blame] | 130 | fmt = AFMT_S16_NE |
| 131 | rate = 44100 |
| 132 | channels = 2 |
Greg Ward | 4f12d46 | 2003-05-29 01:27:39 +0000 | [diff] [blame] | 133 | 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 Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 139 | _assert(result != config, |
| 140 | "setparameters: unexpectedly got requested configuration") |
Greg Ward | 4f12d46 | 2003-05-29 01:27:39 +0000 | [diff] [blame] | 141 | |
| 142 | try: |
| 143 | result = dsp.setparameters(fmt, channels, rate, True) |
| 144 | raise AssertionError("setparameters: expected OSSAudioError") |
Guido van Rossum | b940e11 | 2007-01-10 16:19:56 +0000 | [diff] [blame] | 145 | except ossaudiodev.OSSAudioError as err: |
Greg Ward | 4f12d46 | 2003-05-29 01:27:39 +0000 | [diff] [blame] | 146 | print "setparameters: got OSSAudioError as expected" |
Greg Ward | 36dacfa | 2002-12-10 16:24:21 +0000 | [diff] [blame] | 147 | |
| 148 | def test(): |
Greg Ward | 55a8790 | 2002-12-10 16:27:35 +0000 | [diff] [blame] | 149 | (data, rate, ssize, nchannels) = read_sound_file(findfile('audiotest.au')) |
| 150 | play_sound_file(data, rate, ssize, nchannels) |
Greg Ward | 50682d0 | 2005-03-07 01:41:11 +0000 | [diff] [blame] | 151 | |
| 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 Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 161 | _assert(dsp.closed is True, "dsp.closed is not True") |
Greg Ward | 36dacfa | 2002-12-10 16:24:21 +0000 | [diff] [blame] | 162 | |
| 163 | test() |