The silencing of DeprecationWarning was not taking -3 into consideration. Since
[python.git] / Lib / test / test_linuxaudiodev.py
blob71d0633337937adaa7969e0d034dbc0011505c09
1 from test import test_support
2 test_support.requires('audio')
4 from test.test_support import findfile, run_unittest
6 import errno
7 linuxaudiodev = test_support.import_module('linuxaudiodev', deprecated=True)
8 import sys
9 import sunaudio
10 import audioop
11 import unittest
13 SND_FORMAT_MULAW_8 = 1
15 class LinuxAudioDevTests(unittest.TestCase):
17 def setUp(self):
18 self.dev = linuxaudiodev.open('w')
20 def tearDown(self):
21 self.dev.close()
23 def test_methods(self):
24 # at least check that these methods can be invoked
25 self.dev.bufsize()
26 self.dev.obufcount()
27 self.dev.obuffree()
28 self.dev.getptr()
29 self.dev.fileno()
31 def test_play_sound_file(self):
32 path = findfile("audiotest.au")
33 fp = open(path, 'r')
34 size, enc, rate, nchannels, extra = sunaudio.gethdr(fp)
35 data = fp.read()
36 fp.close()
38 if enc != SND_FORMAT_MULAW_8:
39 self.fail("Expect .au file with 8-bit mu-law samples")
41 # convert the data to 16-bit signed
42 data = audioop.ulaw2lin(data, 2)
44 # set the data format
45 if sys.byteorder == 'little':
46 fmt = linuxaudiodev.AFMT_S16_LE
47 else:
48 fmt = linuxaudiodev.AFMT_S16_BE
50 # set parameters based on .au file headers
51 self.dev.setparameters(rate, 16, nchannels, fmt)
52 self.dev.write(data)
53 self.dev.flush()
55 def test_errors(self):
56 size = 8
57 fmt = linuxaudiodev.AFMT_U8
58 rate = 8000
59 nchannels = 1
60 try:
61 self.dev.setparameters(-1, size, nchannels, fmt)
62 except ValueError, err:
63 self.assertEquals(err.args[0], "expected rate >= 0, not -1")
64 try:
65 self.dev.setparameters(rate, -2, nchannels, fmt)
66 except ValueError, err:
67 self.assertEquals(err.args[0], "expected sample size >= 0, not -2")
68 try:
69 self.dev.setparameters(rate, size, 3, fmt)
70 except ValueError, err:
71 self.assertEquals(err.args[0], "nchannels must be 1 or 2, not 3")
72 try:
73 self.dev.setparameters(rate, size, nchannels, 177)
74 except ValueError, err:
75 self.assertEquals(err.args[0], "unknown audio encoding: 177")
76 try:
77 self.dev.setparameters(rate, size, nchannels, linuxaudiodev.AFMT_U16_LE)
78 except ValueError, err:
79 self.assertEquals(err.args[0], "for linear unsigned 16-bit little-endian "
80 "audio, expected sample size 16, not 8")
81 try:
82 self.dev.setparameters(rate, 16, nchannels, fmt)
83 except ValueError, err:
84 self.assertEquals(err.args[0], "for linear unsigned 8-bit audio, expected "
85 "sample size 8, not 16")
87 def test_main():
88 try:
89 dsp = linuxaudiodev.open('w')
90 except linuxaudiodev.error, msg:
91 if msg.args[0] in (errno.EACCES, errno.ENOENT, errno.ENODEV, errno.EBUSY):
92 raise unittest.SkipTest(msg)
93 raise
94 dsp.close()
95 run_unittest(LinuxAudioDevTests)
97 if __name__ == '__main__':
98 test_main()