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) |
| 80 | print ("playing test sound file (expected running time: %.2f sec)" |
| 81 | % expected_time) |
| 82 | t1 = time.time() |
| 83 | dsp.write(data) |
| 84 | dsp.close() |
| 85 | t2 = time.time() |
| 86 | elapsed_time = t2 - t1 |
| 87 | |
| 88 | percent_diff = (abs(elapsed_time - expected_time) / expected_time) * 100 |
| 89 | self.failUnless(percent_diff <= 10.0, |
| 90 | "elapsed time > 10% off of expected time") |
| 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): |
| 123 | |
| 124 | # Now try some configurations that are presumably bogus: eg. 300 |
| 125 | # channels currently exceeds even Hollywood's ambitions, and |
| 126 | # negative sampling rate is utter nonsense. setparameters() should |
| 127 | # accept these in non-strict mode, returning something other than |
| 128 | # was requested, but should barf in strict mode. |
| 129 | fmt = AFMT_S16_NE |
| 130 | rate = 44100 |
| 131 | channels = 2 |
| 132 | for config in [(fmt, 300, rate), # ridiculous nchannels |
| 133 | (fmt, -5, rate), # impossible nchannels |
| 134 | (fmt, channels, -50), # impossible rate |
| 135 | ]: |
| 136 | (fmt, channels, rate) = config |
| 137 | result = dsp.setparameters(fmt, channels, rate, False) |
| 138 | self.failIfEqual(result, config, |
| 139 | "unexpectedly got requested configuration") |
| 140 | |
| 141 | try: |
| 142 | result = dsp.setparameters(fmt, channels, rate, True) |
| 143 | except ossaudiodev.OSSAudioError as err: |
| 144 | pass |
| 145 | else: |
| 146 | self.fail("expected OSSAudioError") |
| 147 | |
| 148 | def test_playback(self): |
| 149 | sound_info = read_sound_file(findfile('audiotest.au')) |
| 150 | self.play_sound_file(*sound_info) |
| 151 | |
| 152 | def test_set_parameters(self): |
| 153 | dsp = ossaudiodev.open("w") |
| 154 | try: |
| 155 | self.set_parameters(dsp) |
| 156 | |
| 157 | # Disabled because it fails under Linux 2.6 with ALSA's OSS |
| 158 | # emulation layer. |
| 159 | #self.set_bad_parameters(dsp) |
| 160 | finally: |
| 161 | dsp.close() |
| 162 | self.failUnless(dsp.closed) |
| 163 | |
| 164 | |
| 165 | def test_main(): |
Greg Ward | 36dacfa | 2002-12-10 16:24:21 +0000 | [diff] [blame] | 166 | try: |
Greg Ward | 080c110 | 2003-05-29 00:23:17 +0000 | [diff] [blame] | 167 | dsp = ossaudiodev.open('w') |
Guido van Rossum | b940e11 | 2007-01-10 16:19:56 +0000 | [diff] [blame] | 168 | except IOError as msg: |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 169 | if msg.args[0] in (errno.EACCES, errno.ENOENT, |
| 170 | errno.ENODEV, errno.EBUSY): |
| 171 | raise TestSkipped(msg) |
| 172 | raise |
Guido van Rossum | 23cfc98 | 2007-08-21 22:49:52 +0000 | [diff] [blame] | 173 | dsp.close() |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 174 | test_support.run_unittest(__name__) |
Greg Ward | 36dacfa | 2002-12-10 16:24:21 +0000 | [diff] [blame] | 175 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 176 | if __name__ == "__main__": |
| 177 | test_main() |