| 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 ossaudiodev
|
|---|
| 9 | import os
|
|---|
| 10 | import sys
|
|---|
| 11 | import select
|
|---|
| 12 | import sunaudio
|
|---|
| 13 | import time
|
|---|
| 14 | import audioop
|
|---|
| 15 |
|
|---|
| 16 | # Arggh, AFMT_S16_NE not defined on all platforms -- seems to be a
|
|---|
| 17 | # fairly recent addition to OSS.
|
|---|
| 18 | try:
|
|---|
| 19 | from ossaudiodev import AFMT_S16_NE
|
|---|
| 20 | except 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 |
|
|---|
| 27 | SND_FORMAT_MULAW_8 = 1
|
|---|
| 28 |
|
|---|
| 29 | def 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
|
|---|
| 44 | def _assert(expr, message=None):
|
|---|
| 45 | if not expr:
|
|---|
| 46 | raise AssertionError(message or "assertion failed")
|
|---|
| 47 |
|
|---|
| 48 | def 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")
|
|---|
|
|---|