Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 1 | from test import support |
| 2 | support.requires('audio') |
Guido van Rossum | 4507ec7 | 2003-02-14 19:29:22 +0000 | [diff] [blame] | 3 | |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 4 | from test.support import findfile, TestSkipped |
Greg Ward | 36dacfa | 2002-12-10 16:24:21 +0000 | [diff] [blame] | 5 | |
| 6 | import errno |
Greg Ward | 36dacfa | 2002-12-10 16:24:21 +0000 | [diff] [blame] | 7 | import ossaudiodev |
Greg Ward | 36dacfa | 2002-12-10 16:24:21 +0000 | [diff] [blame] | 8 | import sys |
Greg Ward | 36dacfa | 2002-12-10 16:24:21 +0000 | [diff] [blame] | 9 | import sunaudio |
| 10 | import time |
| 11 | import audioop |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 12 | import unittest |
Greg Ward | 36dacfa | 2002-12-10 16:24:21 +0000 | [diff] [blame] | 13 | |
Greg Ward | 8a709b3 | 2003-06-03 00:32:44 +0000 | [diff] [blame] | 14 | # Arggh, AFMT_S16_NE not defined on all platforms -- seems to be a |
| 15 | # fairly recent addition to OSS. |
| 16 | try: |
| 17 | from ossaudiodev import AFMT_S16_NE |
| 18 | except ImportError: |
| 19 | if sys.byteorder == "little": |
| 20 | AFMT_S16_NE = ossaudiodev.AFMT_S16_LE |
| 21 | else: |
| 22 | AFMT_S16_NE = ossaudiodev.AFMT_S16_BE |
| 23 | |
| 24 | |
Greg Ward | 36dacfa | 2002-12-10 16:24:21 +0000 | [diff] [blame] | 25 | SND_FORMAT_MULAW_8 = 1 |
| 26 | |
Greg Ward | 55a8790 | 2002-12-10 16:27:35 +0000 | [diff] [blame] | 27 | def read_sound_file(path): |
Fred Drake | 7511bd9 | 2003-01-08 07:09:43 +0000 | [diff] [blame] | 28 | fp = open(path, 'rb') |
Greg Ward | 36dacfa | 2002-12-10 16:24:21 +0000 | [diff] [blame] | 29 | size, enc, rate, nchannels, extra = sunaudio.gethdr(fp) |
| 30 | data = fp.read() |
| 31 | fp.close() |
| 32 | |
| 33 | if enc != SND_FORMAT_MULAW_8: |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 34 | raise RuntimeError("Expect .au file with 8-bit mu-law samples") |
Greg Ward | 36dacfa | 2002-12-10 16:24:21 +0000 | [diff] [blame] | 35 | |
Greg Ward | 55a8790 | 2002-12-10 16:27:35 +0000 | [diff] [blame] | 36 | # Convert the data to 16-bit signed. |
| 37 | data = audioop.ulaw2lin(data, 2) |
| 38 | return (data, rate, 16, nchannels) |
| 39 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 40 | class OSSAudioDevTests(unittest.TestCase): |
Greg Ward | 55a8790 | 2002-12-10 16:27:35 +0000 | [diff] [blame] | 41 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 42 | def play_sound_file(self, data, rate, ssize, nchannels): |
| 43 | try: |
| 44 | dsp = ossaudiodev.open('w') |
| 45 | except IOError as msg: |
| 46 | if msg.args[0] in (errno.EACCES, errno.ENOENT, |
| 47 | errno.ENODEV, errno.EBUSY): |
| 48 | raise TestSkipped(msg) |
| 49 | raise |
| 50 | |
| 51 | # at least check that these methods can be invoked |
| 52 | dsp.bufsize() |
| 53 | dsp.obufcount() |
| 54 | dsp.obuffree() |
| 55 | dsp.getptr() |
| 56 | dsp.fileno() |
| 57 | |
| 58 | # Make sure the read-only attributes work. |
| 59 | self.failUnless(dsp.close) |
| 60 | self.assertEqual(dsp.name, "/dev/dsp") |
| 61 | self.assertEqual(dsp.mode, "w", "bad dsp.mode: %r" % dsp.mode) |
| 62 | |
| 63 | # And make sure they're really read-only. |
| 64 | for attr in ('closed', 'name', 'mode'): |
| 65 | try: |
| 66 | setattr(dsp, attr, 42) |
| 67 | except TypeError: |
| 68 | pass |
| 69 | else: |
| 70 | self.fail("dsp.%s not read-only" % attr) |
| 71 | |
| 72 | # Compute expected running time of sound sample (in seconds). |
| 73 | expected_time = float(len(data)) / (ssize/8) / nchannels / rate |
| 74 | |
| 75 | # set parameters based on .au file headers |
| 76 | dsp.setparameters(AFMT_S16_NE, nchannels, rate) |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 77 | self.assertEquals("%.2f" % expected_time, "2.93") |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 78 | t1 = time.time() |
| 79 | dsp.write(data) |
| 80 | dsp.close() |
| 81 | t2 = time.time() |
| 82 | elapsed_time = t2 - t1 |
| 83 | |
| 84 | percent_diff = (abs(elapsed_time - expected_time) / expected_time) * 100 |
| 85 | self.failUnless(percent_diff <= 10.0, |
Guido van Rossum | e760631 | 2007-08-26 20:03:04 +0000 | [diff] [blame] | 86 | "elapsed time (%s) > 10%% off of expected time (%s)" % |
Neal Norwitz | 2bf4d5b | 2007-08-26 22:16:55 +0000 | [diff] [blame] | 87 | (elapsed_time, expected_time)) |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 88 | |
| 89 | def set_parameters(self, dsp): |
| 90 | # Two configurations for testing: |
| 91 | # config1 (8-bit, mono, 8 kHz) should work on even the most |
| 92 | # ancient and crufty sound card, but maybe not on special- |
| 93 | # purpose high-end hardware |
| 94 | # config2 (16-bit, stereo, 44.1kHz) should work on all but the |
| 95 | # most ancient and crufty hardware |
| 96 | config1 = (ossaudiodev.AFMT_U8, 1, 8000) |
| 97 | config2 = (AFMT_S16_NE, 2, 44100) |
| 98 | |
| 99 | for config in [config1, config2]: |
| 100 | (fmt, channels, rate) = config |
| 101 | if (dsp.setfmt(fmt) == fmt and |
| 102 | dsp.channels(channels) == channels and |
| 103 | dsp.speed(rate) == rate): |
| 104 | break |
| 105 | else: |
| 106 | raise RuntimeError("unable to set audio sampling parameters: " |
| 107 | "you must have really weird audio hardware") |
| 108 | |
| 109 | # setparameters() should be able to set this configuration in |
| 110 | # either strict or non-strict mode. |
| 111 | result = dsp.setparameters(fmt, channels, rate, False) |
| 112 | self.assertEqual(result, (fmt, channels, rate), |
| 113 | "setparameters%r: returned %r" % (config, result)) |
| 114 | |
| 115 | result = dsp.setparameters(fmt, channels, rate, True) |
| 116 | self.assertEqual(result, (fmt, channels, rate), |
| 117 | "setparameters%r: returned %r" % (config, result)) |
| 118 | |
| 119 | def set_bad_parameters(self, dsp): |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 120 | # Now try some configurations that are presumably bogus: eg. 300 |
| 121 | # channels currently exceeds even Hollywood's ambitions, and |
| 122 | # negative sampling rate is utter nonsense. setparameters() should |
| 123 | # accept these in non-strict mode, returning something other than |
| 124 | # was requested, but should barf in strict mode. |
| 125 | fmt = AFMT_S16_NE |
| 126 | rate = 44100 |
| 127 | channels = 2 |
| 128 | for config in [(fmt, 300, rate), # ridiculous nchannels |
| 129 | (fmt, -5, rate), # impossible nchannels |
| 130 | (fmt, channels, -50), # impossible rate |
| 131 | ]: |
| 132 | (fmt, channels, rate) = config |
| 133 | result = dsp.setparameters(fmt, channels, rate, False) |
| 134 | self.failIfEqual(result, config, |
| 135 | "unexpectedly got requested configuration") |
| 136 | |
| 137 | try: |
| 138 | result = dsp.setparameters(fmt, channels, rate, True) |
| 139 | except ossaudiodev.OSSAudioError as err: |
| 140 | pass |
| 141 | else: |
| 142 | self.fail("expected OSSAudioError") |
| 143 | |
| 144 | def test_playback(self): |
| 145 | sound_info = read_sound_file(findfile('audiotest.au')) |
| 146 | self.play_sound_file(*sound_info) |
| 147 | |
| 148 | def test_set_parameters(self): |
| 149 | dsp = ossaudiodev.open("w") |
| 150 | try: |
| 151 | self.set_parameters(dsp) |
| 152 | |
| 153 | # Disabled because it fails under Linux 2.6 with ALSA's OSS |
| 154 | # emulation layer. |
| 155 | #self.set_bad_parameters(dsp) |
| 156 | finally: |
| 157 | dsp.close() |
| 158 | self.failUnless(dsp.closed) |
| 159 | |
| 160 | |
| 161 | def test_main(): |
Greg Ward | 36dacfa | 2002-12-10 16:24:21 +0000 | [diff] [blame] | 162 | try: |
Greg Ward | 080c110 | 2003-05-29 00:23:17 +0000 | [diff] [blame] | 163 | dsp = ossaudiodev.open('w') |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 164 | except (ossaudiodev.error, IOError) as msg: |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 165 | if msg.args[0] in (errno.EACCES, errno.ENOENT, |
| 166 | errno.ENODEV, errno.EBUSY): |
| 167 | raise TestSkipped(msg) |
| 168 | raise |
Guido van Rossum | 23cfc98 | 2007-08-21 22:49:52 +0000 | [diff] [blame] | 169 | dsp.close() |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 170 | support.run_unittest(__name__) |
Greg Ward | 36dacfa | 2002-12-10 16:24:21 +0000 | [diff] [blame] | 171 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 172 | if __name__ == "__main__": |
| 173 | test_main() |