0001 from test import test_support 0002 test_support.requires('audio') 0003 0004 from test.test_support import verbose, findfile, TestFailed, TestSkipped 0005 0006 import errno 0007 import fcntl 0008 import linuxaudiodev 0009 import os 0010 import sys 0011 import select 0012 import sunaudio 0013 import time 0014 import audioop 0015 0016 SND_FORMAT_MULAW_8 = 1 0017 0018 def play_sound_file(path): 0019 fp = open(path, 'r') 0020 size, enc, rate, nchannels, extra = sunaudio.gethdr(fp) 0021 data = fp.read() 0022 fp.close() 0023 0024 if enc != SND_FORMAT_MULAW_8: 0025 print "Expect .au file with 8-bit mu-law samples" 0026 return 0027 0028 try: 0029 a = linuxaudiodev.open('w') 0030 except linuxaudiodev.error, msg: 0031 if msg[0] in (errno.EACCES, errno.ENODEV, errno.EBUSY): 0032 raise TestSkipped, msg 0033 raise TestFailed, msg 0034 0035 # convert the data to 16-bit signed 0036 data = audioop.ulaw2lin(data, 2) 0037 0038 # set the data format 0039 if sys.byteorder == 'little': 0040 fmt = linuxaudiodev.AFMT_S16_LE 0041 else: 0042 fmt = linuxaudiodev.AFMT_S16_BE 0043 0044 # at least check that these methods can be invoked 0045 a.bufsize() 0046 a.obufcount() 0047 a.obuffree() 0048 a.getptr() 0049 a.fileno() 0050 0051 # set parameters based on .au file headers 0052 a.setparameters(rate, 16, nchannels, fmt) 0053 a.write(data) 0054 a.flush() 0055 a.close() 0056 0057 def test_errors(): 0058 a = linuxaudiodev.open("w") 0059 size = 8 0060 fmt = linuxaudiodev.AFMT_U8 0061 rate = 8000 0062 nchannels = 1 0063 try: 0064 a.setparameters(-1, size, nchannels, fmt) 0065 except ValueError, msg: 0066 print msg 0067 try: 0068 a.setparameters(rate, -2, nchannels, fmt) 0069 except ValueError, msg: 0070 print msg 0071 try: 0072 a.setparameters(rate, size, 3, fmt) 0073 except ValueError, msg: 0074 print msg 0075 try: 0076 a.setparameters(rate, size, nchannels, 177) 0077 except ValueError, msg: 0078 print msg 0079 try: 0080 a.setparameters(rate, size, nchannels, linuxaudiodev.AFMT_U16_LE) 0081 except ValueError, msg: 0082 print msg 0083 try: 0084 a.setparameters(rate, 16, nchannels, fmt) 0085 except ValueError, msg: 0086 print msg 0087 0088 def test(): 0089 play_sound_file(findfile('audiotest.au')) 0090 test_errors() 0091 0092 test() 0093
Generated by PyXR 0.9.4