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