PageRenderTime 16ms CodeModel.GetById 2ms app.highlight 11ms RepoModel.GetById 1ms app.codeStats 0ms

/Lib/test/test_linuxaudiodev.py

http://unladen-swallow.googlecode.com/
Python | 98 lines | 79 code | 15 blank | 4 comment | 5 complexity | 79cabb98c468e12c82ab514539efa937 MD5 | raw file
 1from test import test_support
 2test_support.requires('audio')
 3
 4from test.test_support import findfile, TestSkipped, run_unittest
 5
 6import errno
 7linuxaudiodev = test_support.import_module('linuxaudiodev', deprecated=True)
 8import sys
 9import sunaudio
10import audioop
11import unittest
12
13SND_FORMAT_MULAW_8 = 1
14
15class LinuxAudioDevTests(unittest.TestCase):
16
17    def setUp(self):
18        self.dev = linuxaudiodev.open('w')
19
20    def tearDown(self):
21        self.dev.close()
22
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()
30
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()
37
38        if enc != SND_FORMAT_MULAW_8:
39            self.fail("Expect .au file with 8-bit mu-law samples")
40
41        # convert the data to 16-bit signed
42        data = audioop.ulaw2lin(data, 2)
43
44        # set the data format
45        if sys.byteorder == 'little':
46            fmt = linuxaudiodev.AFMT_S16_LE
47        else:
48            fmt = linuxaudiodev.AFMT_S16_BE
49
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()
54
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")
86
87def 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 TestSkipped(msg)
93        raise
94    dsp.close()
95    run_unittest(LinuxAudioDevTests)
96
97if __name__ == '__main__':
98    test_main()