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 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 4 | from test.test_support import verbose, findfile, TestSkipped |
Greg Ward | 36dacfa | 2002-12-10 16:24:21 +0000 | [diff] [blame] | 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 |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 15 | import unittest |
Greg Ward | 36dacfa | 2002-12-10 16:24:21 +0000 | [diff] [blame] | 16 | |
Greg Ward | 8a709b3 | 2003-06-03 00:32:44 +0000 | [diff] [blame] | 17 | # Arggh, AFMT_S16_NE not defined on all platforms -- seems to be a |
| 18 | # fairly recent addition to OSS. |
| 19 | try: |
| 20 | from ossaudiodev import AFMT_S16_NE |
| 21 | except ImportError: |
| 22 | if sys.byteorder == "little": |
| 23 | AFMT_S16_NE = ossaudiodev.AFMT_S16_LE |
| 24 | else: |
| 25 | AFMT_S16_NE = ossaudiodev.AFMT_S16_BE |
| 26 | |
| 27 | |
Greg Ward | 36dacfa | 2002-12-10 16:24:21 +0000 | [diff] [blame] | 28 | SND_FORMAT_MULAW_8 = 1 |
| 29 | |
Greg Ward | 55a8790 | 2002-12-10 16:27:35 +0000 | [diff] [blame] | 30 | def read_sound_file(path): |
Fred Drake | 7511bd9 | 2003-01-08 07:09:43 +0000 | [diff] [blame] | 31 | fp = open(path, 'rb') |
Greg Ward | 36dacfa | 2002-12-10 16:24:21 +0000 | [diff] [blame] | 32 | size, enc, rate, nchannels, extra = sunaudio.gethdr(fp) |
| 33 | data = fp.read() |
| 34 | fp.close() |
| 35 | |
| 36 | if enc != SND_FORMAT_MULAW_8: |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 37 | raise RuntimeError("Expect .au file with 8-bit mu-law samples") |
Greg Ward | 36dacfa | 2002-12-10 16:24:21 +0000 | [diff] [blame] | 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 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 43 | class OSSAudioDevTests(unittest.TestCase): |
Greg Ward | 55a8790 | 2002-12-10 16:27:35 +0000 | [diff] [blame] | 44 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 45 | def play_sound_file(self, data, rate, ssize, nchannels): |
| 46 | try: |
| 47 | dsp = ossaudiodev.open('w') |
| 48 | except IOError as msg: |
| 49 | if msg.args[0] in (errno.EACCES, errno.ENOENT, |
| 50 | errno.ENODEV, errno.EBUSY): |
| 51 | raise TestSkipped(msg) |
| 52 | raise |
| 53 | |
| 54 | # at least check that these methods can be invoked |
| 55 | dsp.bufsize() |
| 56 | dsp.obufcount() |
| 57 | dsp.obuffree() |
| 58 | dsp.getptr() |
| 59 | dsp.fileno() |
| 60 | |
| 61 | # Make sure the read-only attributes work. |
| 62 | self.failUnless(dsp.close) |
| 63 | self.assertEqual(dsp.name, "/dev/dsp") |
| 64 | self.assertEqual(dsp.mode, "w", "bad dsp.mode: %r" % dsp.mode) |
| 65 | |
| 66 | # And make sure they're really read-only. |
| 67 | for attr in ('closed', 'name', 'mode'): |
| 68 | try: |
| 69 | setattr(dsp, attr, 42) |
| 70 | except TypeError: |
| 71 | pass |
| 72 | else: |
| 73 | self.fail("dsp.%s not read-only" % attr) |
| 74 | |
| 75 | # Compute expected running time of sound sample (in seconds). |
| 76 | expected_time = float(len(data)) / (ssize/8) / nchannels / rate |
| 77 | |
| 78 | # set parameters based on .au file headers |
| 79 | dsp.setparameters(AFMT_S16_NE, nchannels, rate) |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame^] | 80 | self.assertEquals("%.2f" % expected_time, "2.93") |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 81 | t1 = time.time() |
| 82 | dsp.write(data) |
| 83 | dsp.close() |
| 84 | t2 = time.time() |
| 85 | elapsed_time = t2 - t1 |
| 86 | |
| 87 | percent_diff = (abs(elapsed_time - expected_time) / expected_time) * 100 |
| 88 | self.failUnless(percent_diff <= 10.0, |
Guido van Rossum | e760631 | 2007-08-26 20:03:04 +0000 | [diff] [blame] | 89 | "elapsed time (%s) > 10%% off of expected time (%s)" % |
Neal Norwitz | 2bf4d5b | 2007-08-26 22:16:55 +0000 | [diff] [blame] | 90 | (elapsed_time, expected_time)) |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 91 | |
| 92 | def set_parameters(self, dsp): |
| 93 | # 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) |
| 100 | config2 = (AFMT_S16_NE, 2, 44100) |
| 101 | |
| 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 | self.assertEqual(result, (fmt, channels, rate), |
| 116 | "setparameters%r: returned %r" % (config, result)) |
| 117 | |
| 118 | result = dsp.setparameters(fmt, channels, rate, True) |
| 119 | self.assertEqual(result, (fmt, channels, rate), |
| 120 | "setparameters%r: returned %r" % (config, result)) |
| 121 | |
| 122 | def set_bad_parameters(self, dsp): |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 123 | # 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. |
| 128 | fmt = AFMT_S16_NE |
| 129 | rate = 44100 |
| 130 | channels = 2 |
| 131 | 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 | self.failIfEqual(result, config, |
| 138 | "unexpectedly got requested configuration") |
| 139 | |
| 140 | try: |
| 141 | result = dsp.setparameters(fmt, channels, rate, True) |
| 142 | except ossaudiodev.OSSAudioError as err: |
| 143 | pass |
| 144 | else: |
| 145 | self.fail("expected OSSAudioError") |
| 146 | |
| 147 | def test_playback(self): |
| 148 | sound_info = read_sound_file(findfile('audiotest.au')) |
| 149 | self.play_sound_file(*sound_info) |
| 150 | |
| 151 | def test_set_parameters(self): |
| 152 | dsp = ossaudiodev.open("w") |
| 153 | try: |
| 154 | self.set_parameters(dsp) |
| 155 | |
| 156 | # Disabled because it fails under Linux 2.6 with ALSA's OSS |
| 157 | # emulation layer. |
| 158 | #self.set_bad_parameters(dsp) |
| 159 | finally: |
| 160 | dsp.close() |
| 161 | self.failUnless(dsp.closed) |
| 162 | |
| 163 | |
| 164 | def test_main(): |
Greg Ward | 36dacfa | 2002-12-10 16:24:21 +0000 | [diff] [blame] | 165 | try: |
Greg Ward | 080c110 | 2003-05-29 00:23:17 +0000 | [diff] [blame] | 166 | dsp = ossaudiodev.open('w') |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame^] | 167 | except (ossaudiodev.error, IOError) as msg: |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 168 | if msg.args[0] in (errno.EACCES, errno.ENOENT, |
| 169 | errno.ENODEV, errno.EBUSY): |
| 170 | raise TestSkipped(msg) |
| 171 | raise |
Guido van Rossum | 23cfc98 | 2007-08-21 22:49:52 +0000 | [diff] [blame] | 172 | dsp.close() |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 173 | test_support.run_unittest(__name__) |
Greg Ward | 36dacfa | 2002-12-10 16:24:21 +0000 | [diff] [blame] | 174 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 175 | if __name__ == "__main__": |
| 176 | test_main() |