source: trunk/essentials/dev-lang/python/Lib/test/test_ossaudiodev.py@ 3317

Last change on this file since 3317 was 3225, checked in by bird, 19 years ago

Python 2.5

File size: 5.3 KB
Line 
1from test import test_support
2test_support.requires('audio')
3
4from test.test_support import verbose, findfile, TestFailed, TestSkipped
5
6import errno
7import fcntl
8import ossaudiodev
9import os
10import sys
11import select
12import sunaudio
13import time
14import audioop
15
16# Arggh, AFMT_S16_NE not defined on all platforms -- seems to be a
17# fairly recent addition to OSS.
18try:
19 from ossaudiodev import AFMT_S16_NE
20except ImportError:
21 if sys.byteorder == "little":
22 AFMT_S16_NE = ossaudiodev.AFMT_S16_LE
23 else:
24 AFMT_S16_NE = ossaudiodev.AFMT_S16_BE
25
26
27SND_FORMAT_MULAW_8 = 1
28
29def read_sound_file(path):
30 fp = open(path, 'rb')
31 size, enc, rate, nchannels, extra = sunaudio.gethdr(fp)
32 data = fp.read()
33 fp.close()
34
35 if enc != SND_FORMAT_MULAW_8:
36 print "Expect .au file with 8-bit mu-law samples"
37 return
38
39 # Convert the data to 16-bit signed.
40 data = audioop.ulaw2lin(data, 2)
41 return (data, rate, 16, nchannels)
42
43# version of assert that still works with -O
44def _assert(expr, message=None):
45 if not expr:
46 raise AssertionError(message or "assertion failed")
47
48def play_sound_file(data, rate, ssize, nchannels):
49 try:
50 dsp = ossaudiodev.open('w')
51 except IOError, msg:
52 if msg[0] in (errno.EACCES, errno.ENOENT, errno.ENODEV, errno.EBUSY):
53 raise TestSkipped, msg
54 raise TestFailed, msg
55
56 # at least check that these methods can be invoked
57 dsp.bufsize()
58 dsp.obufcount()
59 dsp.obuffree()
60 dsp.getptr()
61 dsp.fileno()
62
63 # Make sure the read-only attributes work.
64 _assert(dsp.closed is False, "dsp.closed is not False")