blob: 7924a24e32070deac817ba842b9a18a6a6e0c69b [file] [log] [blame]
Guido van Rossum8d691c82000-09-01 19:25:51 +00001from test_support import verbose, findfile, TestFailed, TestSkipped
Guido van Rossume8d2f552000-10-09 22:14:43 +00002
Guido van Rossum8d691c82000-09-01 19:25:51 +00003import errno
Guido van Rossume8d2f552000-10-09 22:14:43 +00004import fcntl
5import linuxaudiodev
Guido van Rossum3e06ab12000-06-29 19:35:29 +00006import os
Guido van Rossume8d2f552000-10-09 22:14:43 +00007import sys
8import select
9import sunaudio
10import time
11import audioop
12
13SND_FORMAT_MULAW_8 = 1
Guido van Rossum3e06ab12000-06-29 19:35:29 +000014
15def play_sound_file(path):
16 fp = open(path, 'r')
Guido van Rossume8d2f552000-10-09 22:14:43 +000017 size, enc, rate, nchannels, extra = sunaudio.gethdr(fp)
Guido van Rossum3e06ab12000-06-29 19:35:29 +000018 data = fp.read()
19 fp.close()
Guido van Rossume8d2f552000-10-09 22:14:43 +000020
21 if enc != SND_FORMAT_MULAW_8:
22 print "Expect .au file with 8-bit mu-law samples"
23 return
24
Guido van Rossum3e06ab12000-06-29 19:35:29 +000025 try:
26 a = linuxaudiodev.open('w')
27 except linuxaudiodev.error, msg:
Guido van Rossum8d691c82000-09-01 19:25:51 +000028 if msg[0] in (errno.EACCES, errno.ENODEV):
29 raise TestSkipped, msg
Guido van Rossum3e06ab12000-06-29 19:35:29 +000030 raise TestFailed, msg
Guido van Rossume8d2f552000-10-09 22:14:43 +000031
32 # convert the data to 16-bit signed
33 data = audioop.ulaw2lin(data, 2)
34
35 # set the data format
36 if sys.byteorder == 'little':
37 fmt = linuxaudiodev.AFMT_S16_LE
Guido van Rossum3e06ab12000-06-29 19:35:29 +000038 else:
Guido van Rossume8d2f552000-10-09 22:14:43 +000039 fmt = linuxaudiodev.AFMT_S16_BE
40
41 # at least check that these methods can be invoked
42 a.bufsize()
43 a.obufcount()
44 a.obuffree()
45 a.getptr()
46 a.fileno()
47
48 # set parameters based on .au file headers
49 a.setparameters(rate, 16, nchannels, fmt)
50 a.write(data)
51 a.flush()
52 a.close()
53
54def test_errors():
55 a = linuxaudiodev.open("w")
56 size = 8
57 fmt = linuxaudiodev.AFMT_U8
58 rate = 8000
59 nchannels = 1
60 try:
61 a.setparameters(-1, size, nchannels, fmt)
62 except ValueError, msg:
63 print msg
64 try:
65 a.setparameters(rate, -2, nchannels, fmt)
66 except ValueError, msg:
67 print msg
68 try:
69 a.setparameters(rate, size, 3, fmt)
70 except ValueError, msg:
71 print msg
72 try:
73 a.setparameters(rate, size, nchannels, 177)
74 except ValueError, msg:
75 print msg
76 try:
77 a.setparameters(rate, size, nchannels, linuxaudiodev.AFMT_U16_LE)
78 except ValueError, msg:
79 print msg
80 try:
81 a.setparameters(rate, 16, nchannels, fmt)
82 except ValueError, msg:
83 print msg
Guido van Rossum3e06ab12000-06-29 19:35:29 +000084
85def test():
86 play_sound_file(findfile('audiotest.au'))
Guido van Rossume8d2f552000-10-09 22:14:43 +000087 test_errors()
Guido van Rossum3e06ab12000-06-29 19:35:29 +000088
89test()