Moshe Zadka | 040c17f | 2000-08-04 13:26:03 +0000 | [diff] [blame] | 1 | from test_support import verbose, findfile, TestFailed, TestSkipped |
Jeremy Hylton | e2b7c4d | 2000-10-06 19:39:55 +0000 | [diff] [blame] | 2 | |
Moshe Zadka | 323a508 | 2000-08-04 12:59:40 +0000 | [diff] [blame] | 3 | import errno |
Jeremy Hylton | e2b7c4d | 2000-10-06 19:39:55 +0000 | [diff] [blame] | 4 | import fcntl |
| 5 | import linuxaudiodev |
Fred Drake | 1f2d464 | 2000-06-10 04:22:57 +0000 | [diff] [blame] | 6 | import os |
Guido van Rossum | 923ebe4 | 2000-10-08 00:20:20 +0000 | [diff] [blame^] | 7 | import sys |
Jeremy Hylton | e2b7c4d | 2000-10-06 19:39:55 +0000 | [diff] [blame] | 8 | import select |
| 9 | import sunaudio |
| 10 | import time |
Guido van Rossum | 923ebe4 | 2000-10-08 00:20:20 +0000 | [diff] [blame^] | 11 | import audioop |
Jeremy Hylton | e2b7c4d | 2000-10-06 19:39:55 +0000 | [diff] [blame] | 12 | |
| 13 | SND_FORMAT_MULAW_8 = 1 |
Fred Drake | 1f2d464 | 2000-06-10 04:22:57 +0000 | [diff] [blame] | 14 | |
| 15 | def play_sound_file(path): |
| 16 | fp = open(path, 'r') |
Jeremy Hylton | e2b7c4d | 2000-10-06 19:39:55 +0000 | [diff] [blame] | 17 | size, enc, rate, nchannels, extra = sunaudio.gethdr(fp) |
Fred Drake | 1f2d464 | 2000-06-10 04:22:57 +0000 | [diff] [blame] | 18 | data = fp.read() |
| 19 | fp.close() |
Jeremy Hylton | e2b7c4d | 2000-10-06 19:39:55 +0000 | [diff] [blame] | 20 | |
| 21 | if enc != SND_FORMAT_MULAW_8: |
| 22 | print "Expect .au file with 8-bit mu-law samples" |
| 23 | return |
| 24 | |
Fred Drake | 1f2d464 | 2000-06-10 04:22:57 +0000 | [diff] [blame] | 25 | try: |
| 26 | a = linuxaudiodev.open('w') |
| 27 | except linuxaudiodev.error, msg: |
Moshe Zadka | 92a6913 | 2000-08-04 15:25:58 +0000 | [diff] [blame] | 28 | if msg[0] in (errno.EACCES, errno.ENODEV): |
| 29 | raise TestSkipped, msg |
Fred Drake | 1f2d464 | 2000-06-10 04:22:57 +0000 | [diff] [blame] | 30 | raise TestFailed, msg |
Jeremy Hylton | e2b7c4d | 2000-10-06 19:39:55 +0000 | [diff] [blame] | 31 | |
Guido van Rossum | 923ebe4 | 2000-10-08 00:20:20 +0000 | [diff] [blame^] | 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 |
| 38 | else: |
| 39 | fmt = linuxaudiodev.AFMT_S16_BE |
| 40 | |
Jeremy Hylton | e2b7c4d | 2000-10-06 19:39:55 +0000 | [diff] [blame] | 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 |
Guido van Rossum | 923ebe4 | 2000-10-08 00:20:20 +0000 | [diff] [blame^] | 49 | a.setparameters(rate, 16, nchannels, fmt) |
Jeremy Hylton | e2b7c4d | 2000-10-06 19:39:55 +0000 | [diff] [blame] | 50 | a.write(data) |
| 51 | a.flush() |
| 52 | a.close() |
| 53 | |
| 54 | def 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 |
Fred Drake | 1f2d464 | 2000-06-10 04:22:57 +0000 | [diff] [blame] | 84 | |
| 85 | def test(): |
| 86 | play_sound_file(findfile('audiotest.au')) |
Jeremy Hylton | e2b7c4d | 2000-10-06 19:39:55 +0000 | [diff] [blame] | 87 | test_errors() |
Fred Drake | 1f2d464 | 2000-06-10 04:22:57 +0000 | [diff] [blame] | 88 | |
| 89 | test() |