|
1 from test import test_support |
|
2 test_support.requires('audio') |
|
3 |
|
4 from test.test_support import verbose, findfile, TestFailed, TestSkipped |
|
5 |
|
6 import errno |
|
7 import fcntl |
|
8 import linuxaudiodev |
|
9 import os |
|
10 import sys |
|
11 import select |
|
12 import sunaudio |
|
13 import time |
|
14 import audioop |
|
15 |
|
16 SND_FORMAT_MULAW_8 = 1 |
|
17 |
|
18 def play_sound_file(path): |
|
19 fp = open(path, 'r') |
|
20 size, enc, rate, nchannels, extra = sunaudio.gethdr(fp) |
|
21 data = fp.read() |
|
22 fp.close() |
|
23 |
|
24 if enc != SND_FORMAT_MULAW_8: |
|
25 print "Expect .au file with 8-bit mu-law samples" |
|
26 return |
|
27 |
|
28 try: |
|
29 a = linuxaudiodev.open('w') |
|
30 except linuxaudiodev.error, msg: |
|
31 if msg[0] in (errno.EACCES, errno.ENOENT, errno.ENODEV, errno.EBUSY): |
|
32 raise TestSkipped, msg |
|
33 raise TestFailed, msg |
|
34 |
|
35 # convert the data to 16-bit signed |
|
36 data = audioop.ulaw2lin(data, 2) |
|
37 |
|
38 # set the data format |
|
39 if sys.byteorder == 'little': |
|
40 fmt = linuxaudiodev.AFMT_S16_LE |
|
41 else: |
|
42 fmt = linuxaudiodev.AFMT_S16_BE |
|
43 |
|
44 # at least check that these methods can be invoked |
|
45 a.bufsize() |
|
46 a.obufcount() |
|
47 a.obuffree() |
|
48 a.getptr() |
|
49 a.fileno() |
|
50 |
|
51 # set parameters based on .au file headers |
|
52 a.setparameters(rate, 16, nchannels, fmt) |
|
53 a.write(data) |
|
54 a.flush() |
|
55 a.close() |
|
56 |
|
57 def test_errors(): |
|
58 a = linuxaudiodev.open("w") |
|
59 size = 8 |
|
60 fmt = linuxaudiodev.AFMT_U8 |
|
61 rate = 8000 |
|
62 nchannels = 1 |
|
63 try: |
|
64 a.setparameters(-1, size, nchannels, fmt) |
|
65 except ValueError, msg: |
|
66 print msg |
|
67 try: |
|
68 a.setparameters(rate, -2, nchannels, fmt) |
|
69 except ValueError, msg: |
|
70 print msg |
|
71 try: |
|
72 a.setparameters(rate, size, 3, fmt) |
|
73 except ValueError, msg: |
|
74 print msg |
|
75 try: |
|
76 a.setparameters(rate, size, nchannels, 177) |
|
77 except ValueError, msg: |
|
78 print msg |
|
79 try: |
|
80 a.setparameters(rate, size, nchannels, linuxaudiodev.AFMT_U16_LE) |
|
81 except ValueError, msg: |
|
82 print msg |
|
83 try: |
|
84 a.setparameters(rate, 16, nchannels, fmt) |
|
85 except ValueError, msg: |
|
86 print msg |
|
87 |
|
88 def test(): |
|
89 play_sound_file(findfile('audiotest.au')) |
|
90 test_errors() |
|
91 |
|
92 test() |