ยปCore Development>Code coverage>Lib/test/test_ossaudiodev.py

Python code coverage for Lib/test/test_ossaudiodev.py

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