| 1 | n/a | from test import support |
|---|
| 2 | n/a | support.requires('audio') |
|---|
| 3 | n/a | |
|---|
| 4 | n/a | from test.support import findfile |
|---|
| 5 | n/a | |
|---|
| 6 | n/a | ossaudiodev = support.import_module('ossaudiodev') |
|---|
| 7 | n/a | |
|---|
| 8 | n/a | import errno |
|---|
| 9 | n/a | import sys |
|---|
| 10 | n/a | import sunau |
|---|
| 11 | n/a | import time |
|---|
| 12 | n/a | import audioop |
|---|
| 13 | n/a | import unittest |
|---|
| 14 | n/a | |
|---|
| 15 | n/a | # Arggh, AFMT_S16_NE not defined on all platforms -- seems to be a |
|---|
| 16 | n/a | # fairly recent addition to OSS. |
|---|
| 17 | n/a | try: |
|---|
| 18 | n/a | from ossaudiodev import AFMT_S16_NE |
|---|
| 19 | n/a | except ImportError: |
|---|
| 20 | n/a | if sys.byteorder == "little": |
|---|
| 21 | n/a | AFMT_S16_NE = ossaudiodev.AFMT_S16_LE |
|---|
| 22 | n/a | else: |
|---|
| 23 | n/a | AFMT_S16_NE = ossaudiodev.AFMT_S16_BE |
|---|
| 24 | n/a | |
|---|
| 25 | n/a | |
|---|
| 26 | n/a | def read_sound_file(path): |
|---|
| 27 | n/a | with open(path, 'rb') as fp: |
|---|
| 28 | n/a | au = sunau.open(fp) |
|---|
| 29 | n/a | rate = au.getframerate() |
|---|
| 30 | n/a | nchannels = au.getnchannels() |
|---|
| 31 | n/a | encoding = au._encoding |
|---|
| 32 | n/a | fp.seek(0) |
|---|
| 33 | n/a | data = fp.read() |
|---|
| 34 | n/a | |
|---|
| 35 | n/a | if encoding != sunau.AUDIO_FILE_ENCODING_MULAW_8: |
|---|
| 36 | n/a | raise RuntimeError("Expect .au file with 8-bit mu-law samples") |
|---|
| 37 | n/a | |
|---|
| 38 | n/a | # Convert the data to 16-bit signed. |
|---|
| 39 | n/a | data = audioop.ulaw2lin(data, 2) |
|---|
| 40 | n/a | return (data, rate, 16, nchannels) |
|---|
| 41 | n/a | |
|---|
| 42 | n/a | class OSSAudioDevTests(unittest.TestCase): |
|---|
| 43 | n/a | |
|---|
| 44 | n/a | def play_sound_file(self, data, rate, ssize, nchannels): |
|---|
| 45 | n/a | try: |
|---|
| 46 | n/a | dsp = ossaudiodev.open('w') |
|---|
| 47 | n/a | except OSError as msg: |
|---|
| 48 | n/a | if msg.args[0] in (errno.EACCES, errno.ENOENT, |
|---|
| 49 | n/a | errno.ENODEV, errno.EBUSY): |
|---|
| 50 | n/a | raise unittest.SkipTest(msg) |
|---|
| 51 | n/a | raise |
|---|
| 52 | n/a | |
|---|
| 53 | n/a | # at least check that these methods can be invoked |
|---|
| 54 | n/a | dsp.bufsize() |
|---|
| 55 | n/a | dsp.obufcount() |
|---|
| 56 | n/a | dsp.obuffree() |
|---|
| 57 | n/a | dsp.getptr() |
|---|
| 58 | n/a | dsp.fileno() |
|---|
| 59 | n/a | |
|---|
| 60 | n/a | # Make sure the read-only attributes work. |
|---|
| 61 | n/a | self.assertFalse(dsp.closed) |
|---|
| 62 | n/a | self.assertEqual(dsp.name, "/dev/dsp") |
|---|
| 63 | n/a | self.assertEqual(dsp.mode, "w", "bad dsp.mode: %r" % dsp.mode) |
|---|
| 64 | n/a | |
|---|
| 65 | n/a | # And make sure they're really read-only. |
|---|
| 66 | n/a | for attr in ('closed', 'name', 'mode'): |
|---|
| 67 | n/a | try: |
|---|
| 68 | n/a | setattr(dsp, attr, 42) |
|---|
| 69 | n/a | except (TypeError, AttributeError): |
|---|
| 70 | n/a | pass |
|---|
| 71 | n/a | else: |
|---|
| 72 | n/a | self.fail("dsp.%s not read-only" % attr) |
|---|
| 73 | n/a | |
|---|
| 74 | n/a | # Compute expected running time of sound sample (in seconds). |
|---|
| 75 | n/a | expected_time = float(len(data)) / (ssize/8) / nchannels / rate |
|---|
| 76 | n/a | |
|---|
| 77 | n/a | # set parameters based on .au file headers |
|---|
| 78 | n/a | dsp.setparameters(AFMT_S16_NE, nchannels, rate) |
|---|
| 79 | n/a | self.assertTrue(abs(expected_time - 3.51) < 1e-2, expected_time) |
|---|
| 80 | n/a | t1 = time.time() |
|---|
| 81 | n/a | dsp.write(data) |
|---|
| 82 | n/a | dsp.close() |
|---|
| 83 | n/a | t2 = time.time() |
|---|
| 84 | n/a | elapsed_time = t2 - t1 |
|---|
| 85 | n/a | |
|---|
| 86 | n/a | percent_diff = (abs(elapsed_time - expected_time) / expected_time) * 100 |
|---|
| 87 | n/a | self.assertTrue(percent_diff <= 10.0, |
|---|
| 88 | n/a | "elapsed time (%s) > 10%% off of expected time (%s)" % |
|---|
| 89 | n/a | (elapsed_time, expected_time)) |
|---|
| 90 | n/a | |
|---|
| 91 | n/a | def set_parameters(self, dsp): |
|---|
| 92 | n/a | # Two configurations for testing: |
|---|
| 93 | n/a | # config1 (8-bit, mono, 8 kHz) should work on even the most |
|---|
| 94 | n/a | # ancient and crufty sound card, but maybe not on special- |
|---|
| 95 | n/a | # purpose high-end hardware |
|---|
| 96 | n/a | # config2 (16-bit, stereo, 44.1kHz) should work on all but the |
|---|
| 97 | n/a | # most ancient and crufty hardware |
|---|
| 98 | n/a | config1 = (ossaudiodev.AFMT_U8, 1, 8000) |
|---|
| 99 | n/a | config2 = (AFMT_S16_NE, 2, 44100) |
|---|
| 100 | n/a | |
|---|
| 101 | n/a | for config in [config1, config2]: |
|---|
| 102 | n/a | (fmt, channels, rate) = config |
|---|
| 103 | n/a | if (dsp.setfmt(fmt) == fmt and |
|---|
| 104 | n/a | dsp.channels(channels) == channels and |
|---|
| 105 | n/a | dsp.speed(rate) == rate): |
|---|
| 106 | n/a | break |
|---|
| 107 | n/a | else: |
|---|
| 108 | n/a | raise RuntimeError("unable to set audio sampling parameters: " |
|---|
| 109 | n/a | "you must have really weird audio hardware") |
|---|
| 110 | n/a | |
|---|
| 111 | n/a | # setparameters() should be able to set this configuration in |
|---|
| 112 | n/a | # either strict or non-strict mode. |
|---|
| 113 | n/a | result = dsp.setparameters(fmt, channels, rate, False) |
|---|
| 114 | n/a | self.assertEqual(result, (fmt, channels, rate), |
|---|
| 115 | n/a | "setparameters%r: returned %r" % (config, result)) |
|---|
| 116 | n/a | |
|---|
| 117 | n/a | result = dsp.setparameters(fmt, channels, rate, True) |
|---|
| 118 | n/a | self.assertEqual(result, (fmt, channels, rate), |
|---|
| 119 | n/a | "setparameters%r: returned %r" % (config, result)) |
|---|
| 120 | n/a | |
|---|
| 121 | n/a | def set_bad_parameters(self, dsp): |
|---|
| 122 | n/a | # Now try some configurations that are presumably bogus: eg. 300 |
|---|
| 123 | n/a | # channels currently exceeds even Hollywood's ambitions, and |
|---|
| 124 | n/a | # negative sampling rate is utter nonsense. setparameters() should |
|---|
| 125 | n/a | # accept these in non-strict mode, returning something other than |
|---|
| 126 | n/a | # was requested, but should barf in strict mode. |
|---|
| 127 | n/a | fmt = AFMT_S16_NE |
|---|
| 128 | n/a | rate = 44100 |
|---|
| 129 | n/a | channels = 2 |
|---|
| 130 | n/a | for config in [(fmt, 300, rate), # ridiculous nchannels |
|---|
| 131 | n/a | (fmt, -5, rate), # impossible nchannels |
|---|
| 132 | n/a | (fmt, channels, -50), # impossible rate |
|---|
| 133 | n/a | ]: |
|---|
| 134 | n/a | (fmt, channels, rate) = config |
|---|
| 135 | n/a | result = dsp.setparameters(fmt, channels, rate, False) |
|---|
| 136 | n/a | self.assertNotEqual(result, config, |
|---|
| 137 | n/a | "unexpectedly got requested configuration") |
|---|
| 138 | n/a | |
|---|
| 139 | n/a | try: |
|---|
| 140 | n/a | result = dsp.setparameters(fmt, channels, rate, True) |
|---|
| 141 | n/a | except ossaudiodev.OSSAudioError as err: |
|---|
| 142 | n/a | pass |
|---|
| 143 | n/a | else: |
|---|
| 144 | n/a | self.fail("expected OSSAudioError") |
|---|
| 145 | n/a | |
|---|
| 146 | n/a | def test_playback(self): |
|---|
| 147 | n/a | sound_info = read_sound_file(findfile('audiotest.au')) |
|---|
| 148 | n/a | self.play_sound_file(*sound_info) |
|---|
| 149 | n/a | |
|---|
| 150 | n/a | def test_set_parameters(self): |
|---|
| 151 | n/a | dsp = ossaudiodev.open("w") |
|---|
| 152 | n/a | try: |
|---|
| 153 | n/a | self.set_parameters(dsp) |
|---|
| 154 | n/a | |
|---|
| 155 | n/a | # Disabled because it fails under Linux 2.6 with ALSA's OSS |
|---|
| 156 | n/a | # emulation layer. |
|---|
| 157 | n/a | #self.set_bad_parameters(dsp) |
|---|
| 158 | n/a | finally: |
|---|
| 159 | n/a | dsp.close() |
|---|
| 160 | n/a | self.assertTrue(dsp.closed) |
|---|
| 161 | n/a | |
|---|
| 162 | n/a | def test_mixer_methods(self): |
|---|
| 163 | n/a | # Issue #8139: ossaudiodev didn't initialize its types properly, |
|---|
| 164 | n/a | # therefore some methods were unavailable. |
|---|
| 165 | n/a | with ossaudiodev.openmixer() as mixer: |
|---|
| 166 | n/a | self.assertGreaterEqual(mixer.fileno(), 0) |
|---|
| 167 | n/a | |
|---|
| 168 | n/a | def test_with(self): |
|---|
| 169 | n/a | with ossaudiodev.open('w') as dsp: |
|---|
| 170 | n/a | pass |
|---|
| 171 | n/a | self.assertTrue(dsp.closed) |
|---|
| 172 | n/a | |
|---|
| 173 | n/a | def test_on_closed(self): |
|---|
| 174 | n/a | dsp = ossaudiodev.open('w') |
|---|
| 175 | n/a | dsp.close() |
|---|
| 176 | n/a | self.assertRaises(ValueError, dsp.fileno) |
|---|
| 177 | n/a | self.assertRaises(ValueError, dsp.read, 1) |
|---|
| 178 | n/a | self.assertRaises(ValueError, dsp.write, b'x') |
|---|
| 179 | n/a | self.assertRaises(ValueError, dsp.writeall, b'x') |
|---|
| 180 | n/a | self.assertRaises(ValueError, dsp.bufsize) |
|---|
| 181 | n/a | self.assertRaises(ValueError, dsp.obufcount) |
|---|
| 182 | n/a | self.assertRaises(ValueError, dsp.obufcount) |
|---|
| 183 | n/a | self.assertRaises(ValueError, dsp.obuffree) |
|---|
| 184 | n/a | self.assertRaises(ValueError, dsp.getptr) |
|---|
| 185 | n/a | |
|---|
| 186 | n/a | mixer = ossaudiodev.openmixer() |
|---|
| 187 | n/a | mixer.close() |
|---|
| 188 | n/a | self.assertRaises(ValueError, mixer.fileno) |
|---|
| 189 | n/a | |
|---|
| 190 | n/a | def test_main(): |
|---|
| 191 | n/a | try: |
|---|
| 192 | n/a | dsp = ossaudiodev.open('w') |
|---|
| 193 | n/a | except (ossaudiodev.error, OSError) as msg: |
|---|
| 194 | n/a | if msg.args[0] in (errno.EACCES, errno.ENOENT, |
|---|
| 195 | n/a | errno.ENODEV, errno.EBUSY): |
|---|
| 196 | n/a | raise unittest.SkipTest(msg) |
|---|
| 197 | n/a | raise |
|---|
| 198 | n/a | dsp.close() |
|---|
| 199 | n/a | support.run_unittest(__name__) |
|---|
| 200 | n/a | |
|---|
| 201 | n/a | if __name__ == "__main__": |
|---|
| 202 | n/a | test_main() |
|---|