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 ossaudiodev 0009 import os 0010 import sys 0011 import select 0012 import sunaudio 0013 import time 0014 import audioop 0015 0016 # Arggh, AFMT_S16_NE not defined on all platforms -- seems to be a 0017 # fairly recent addition to OSS. 0018 try: 0019 from ossaudiodev import AFMT_S16_NE 0020 except ImportError: 0021 if sys.byteorder == "little": 0022 AFMT_S16_NE = ossaudiodev.AFMT_S16_LE 0023 else: 0024 AFMT_S16_NE = ossaudiodev.AFMT_S16_BE 0025 0026 0027 SND_FORMAT_MULAW_8 = 1 0028 0029 def read_sound_file(path): 0030 fp = open(path, 'rb') 0031 size, enc, rate, nchannels, extra = sunaudio.gethdr(fp) 0032 data = fp.read() 0033 fp.close() 0034 0035 if enc != SND_FORMAT_MULAW_8: 0036 print "Expect .au file with 8-bit mu-law samples" 0037 return 0038 0039 # Convert the data to 16-bit signed. 0040 data = audioop.ulaw2lin(data, 2) 0041 return (data, rate, 16, nchannels) 0042 0043 0044 def play_sound_file(data, rate, ssize, nchannels): 0045 try: 0046 dsp = ossaudiodev.open('w') 0047 except IOError, msg: 0048 if msg[0] in (errno.EACCES, errno.ENODEV, errno.EBUSY): 0049 raise TestSkipped, msg 0050 raise TestFailed, msg 0051 0052 # at least check that these methods can be invoked 0053 dsp.bufsize() 0054 dsp.obufcount() 0055 dsp.obuffree() 0056 dsp.getptr() 0057 dsp.fileno() 0058 0059 # set parameters based on .au file headers 0060 dsp.setparameters(AFMT_S16_NE, nchannels, rate) 0061 t1 = time.time() 0062 print "playing test sound file..." 0063 dsp.write(data) 0064 dsp.close() 0065 t2 = time.time() 0066 print "elapsed time: %.1f sec" % (t2-t1) 0067 0068 def test_setparameters(): 0069 dsp = ossaudiodev.open("w") 0070 0071 # Two configurations for testing: 0072 # config1 (8-bit, mono, 8 kHz) should work on even the most 0073 # ancient and crufty sound card, but maybe not on special- 0074 # purpose high-end hardware 0075 # config2 (16-bit, stereo, 44.1kHz) should work on all but the 0076 # most ancient and crufty hardware 0077 config1 = (ossaudiodev.AFMT_U8, 1, 8000) 0078 config2 = (AFMT_S16_NE, 2, 44100) 0079 0080 for config in [config1, config2]: 0081 (fmt, channels, rate) = config 0082 if (dsp.setfmt(fmt) == fmt and 0083 dsp.channels(channels) == channels and 0084 dsp.speed(rate) == rate): 0085 break 0086 else: 0087 raise RuntimeError("unable to set audio sampling parameters: " 0088 "you must have really weird audio hardware") 0089 0090 # setparameters() should be able to set this configuration in 0091 # either strict or non-strict mode. 0092 result = dsp.setparameters(fmt, channels, rate, False) 0093 assert result == (fmt, channels, rate), \ 0094 "setparameters%r: returned %r" % (config + result) 0095 result = dsp.setparameters(fmt, channels, rate, True) 0096 assert result == (fmt, channels, rate), \ 0097 "setparameters%r: returned %r" % (config + result) 0098 0099 # Now try some configurations that are presumably bogus: eg. 300 0100 # channels currently exceeds even Hollywood's ambitions, and 0101 # negative sampling rate is utter nonsense. setparameters() should 0102 # accept these in non-strict mode, returning something other than 0103 # was requested, but should barf in strict mode. 0104 for config in [(fmt, 300, rate), # ridiculous nchannels 0105 (fmt, -5, rate), # impossible nchannels 0106 (fmt, channels, -50), # impossible rate 0107 ]: 0108 (fmt, channels, rate) = config 0109 result = dsp.setparameters(fmt, channels, rate, False) 0110 assert result != config, \ 0111 "setparameters: unexpectedly got requested configuration" 0112 0113 try: 0114 result = dsp.setparameters(fmt, channels, rate, True) 0115 raise AssertionError("setparameters: expected OSSAudioError") 0116 except ossaudiodev.OSSAudioError, err: 0117 print "setparameters: got OSSAudioError as expected" 0118 0119 def test(): 0120 (data, rate, ssize, nchannels) = read_sound_file(findfile('audiotest.au')) 0121 play_sound_file(data, rate, ssize, nchannels) 0122 test_setparameters() 0123 0124 test() 0125
Generated by PyXR 0.9.4