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