Guido van Rossum | e7b146f | 2000-02-04 15:28:42 +0000 | [diff] [blame] | 1 | """Convert "arbitrary" sound files to AIFF (Apple and SGI's audio format). |
| 2 | |
| 3 | Input may be compressed. |
| 4 | Uncompressed file type may be AIFF, WAV, VOC, 8SVX, NeXT/Sun, and others. |
| 5 | An exception is raised if the file is not of a recognized type. |
| 6 | Returned filename is either the input filename or a temporary filename; |
| 7 | in the latter case the caller must ensure that it is removed. |
| 8 | Other temporary files used are removed by the function. |
| 9 | """ |
Brett Cannon | fa24d9a | 2008-05-09 05:32:42 +0000 | [diff] [blame] | 10 | from warnings import warnpy3k |
| 11 | warnpy3k("the toaiff module has been removed in Python 3.0", stacklevel=2) |
| 12 | del warnpy3k |
Guido van Rossum | 2db9135 | 1992-10-18 17:09:59 +0000 | [diff] [blame] | 13 | |
| 14 | import os |
| 15 | import tempfile |
| 16 | import pipes |
Fred Drake | 0652a4e | 1999-02-24 18:49:15 +0000 | [diff] [blame] | 17 | import sndhdr |
Guido van Rossum | 2db9135 | 1992-10-18 17:09:59 +0000 | [diff] [blame] | 18 | |
Skip Montanaro | 40fc160 | 2001-03-01 04:27:19 +0000 | [diff] [blame] | 19 | __all__ = ["error", "toaiff"] |
| 20 | |
Guido van Rossum | 2db9135 | 1992-10-18 17:09:59 +0000 | [diff] [blame] | 21 | table = {} |
| 22 | |
Guido van Rossum | 7bc817d | 1993-12-17 15:25:27 +0000 | [diff] [blame] | 23 | t = pipes.Template() |
Guido van Rossum | 2db9135 | 1992-10-18 17:09:59 +0000 | [diff] [blame] | 24 | t.append('sox -t au - -t aiff -r 8000 -', '--') |
| 25 | table['au'] = t |
| 26 | |
| 27 | # XXX The following is actually sub-optimal. |
| 28 | # XXX The HCOM sampling rate can be 22k, 22k/2, 22k/3 or 22k/4. |
| 29 | # XXX We must force the output sampling rate else the SGI won't play |
| 30 | # XXX files sampled at 5.5k or 7.333k; however this means that files |
| 31 | # XXX sampled at 11k are unnecessarily expanded. |
| 32 | # XXX Similar comments apply to some other file types. |
Guido van Rossum | 7bc817d | 1993-12-17 15:25:27 +0000 | [diff] [blame] | 33 | t = pipes.Template() |
Guido van Rossum | 2db9135 | 1992-10-18 17:09:59 +0000 | [diff] [blame] | 34 | t.append('sox -t hcom - -t aiff -r 22050 -', '--') |
| 35 | table['hcom'] = t |
| 36 | |
Guido van Rossum | 7bc817d | 1993-12-17 15:25:27 +0000 | [diff] [blame] | 37 | t = pipes.Template() |
Guido van Rossum | 2db9135 | 1992-10-18 17:09:59 +0000 | [diff] [blame] | 38 | t.append('sox -t voc - -t aiff -r 11025 -', '--') |
| 39 | table['voc'] = t |
| 40 | |
Guido van Rossum | 7bc817d | 1993-12-17 15:25:27 +0000 | [diff] [blame] | 41 | t = pipes.Template() |
Guido van Rossum | 2db9135 | 1992-10-18 17:09:59 +0000 | [diff] [blame] | 42 | t.append('sox -t wav - -t aiff -', '--') |
| 43 | table['wav'] = t |
| 44 | |
Guido van Rossum | 7bc817d | 1993-12-17 15:25:27 +0000 | [diff] [blame] | 45 | t = pipes.Template() |
Guido van Rossum | 2db9135 | 1992-10-18 17:09:59 +0000 | [diff] [blame] | 46 | t.append('sox -t 8svx - -t aiff -r 16000 -', '--') |
| 47 | table['8svx'] = t |
| 48 | |
Guido van Rossum | 7bc817d | 1993-12-17 15:25:27 +0000 | [diff] [blame] | 49 | t = pipes.Template() |
Guido van Rossum | 2db9135 | 1992-10-18 17:09:59 +0000 | [diff] [blame] | 50 | t.append('sox -t sndt - -t aiff -r 16000 -', '--') |
| 51 | table['sndt'] = t |
| 52 | |
Guido van Rossum | 7bc817d | 1993-12-17 15:25:27 +0000 | [diff] [blame] | 53 | t = pipes.Template() |
Guido van Rossum | 2db9135 | 1992-10-18 17:09:59 +0000 | [diff] [blame] | 54 | t.append('sox -t sndr - -t aiff -r 16000 -', '--') |
| 55 | table['sndr'] = t |
| 56 | |
Guido van Rossum | 7bc817d | 1993-12-17 15:25:27 +0000 | [diff] [blame] | 57 | uncompress = pipes.Template() |
Guido van Rossum | 2db9135 | 1992-10-18 17:09:59 +0000 | [diff] [blame] | 58 | uncompress.append('uncompress', '--') |
| 59 | |
| 60 | |
Fred Drake | 9b8d801 | 2000-08-17 04:45:13 +0000 | [diff] [blame] | 61 | class error(Exception): |
Tim Peters | b90f89a | 2001-01-15 03:26:36 +0000 | [diff] [blame] | 62 | pass |
Guido van Rossum | 2db9135 | 1992-10-18 17:09:59 +0000 | [diff] [blame] | 63 | |
| 64 | def toaiff(filename): |
Tim Peters | b90f89a | 2001-01-15 03:26:36 +0000 | [diff] [blame] | 65 | temps = [] |
| 66 | ret = None |
| 67 | try: |
| 68 | ret = _toaiff(filename, temps) |
| 69 | finally: |
| 70 | for temp in temps[:]: |
| 71 | if temp != ret: |
| 72 | try: |
| 73 | os.unlink(temp) |
| 74 | except os.error: |
| 75 | pass |
| 76 | temps.remove(temp) |
| 77 | return ret |
Guido van Rossum | 2db9135 | 1992-10-18 17:09:59 +0000 | [diff] [blame] | 78 | |
| 79 | def _toaiff(filename, temps): |
Tim Peters | b90f89a | 2001-01-15 03:26:36 +0000 | [diff] [blame] | 80 | if filename[-2:] == '.Z': |
Guido van Rossum | 3b0a329 | 2002-08-09 16:38:32 +0000 | [diff] [blame] | 81 | (fd, fname) = tempfile.mkstemp() |
| 82 | os.close(fd) |
Tim Peters | b90f89a | 2001-01-15 03:26:36 +0000 | [diff] [blame] | 83 | temps.append(fname) |
| 84 | sts = uncompress.copy(filename, fname) |
| 85 | if sts: |
| 86 | raise error, filename + ': uncompress failed' |
| 87 | else: |
| 88 | fname = filename |
| 89 | try: |
| 90 | ftype = sndhdr.whathdr(fname) |
| 91 | if ftype: |
| 92 | ftype = ftype[0] # All we're interested in |
Andrew M. Kuchling | 994b51e | 2001-08-13 14:40:47 +0000 | [diff] [blame] | 93 | except IOError, msg: |
Tim Peters | b90f89a | 2001-01-15 03:26:36 +0000 | [diff] [blame] | 94 | if type(msg) == type(()) and len(msg) == 2 and \ |
| 95 | type(msg[0]) == type(0) and type(msg[1]) == type(''): |
| 96 | msg = msg[1] |
| 97 | if type(msg) != type(''): |
Walter Dörwald | 70a6b49 | 2004-02-12 17:35:32 +0000 | [diff] [blame] | 98 | msg = repr(msg) |
Tim Peters | b90f89a | 2001-01-15 03:26:36 +0000 | [diff] [blame] | 99 | raise error, filename + ': ' + msg |
| 100 | if ftype == 'aiff': |
| 101 | return fname |
Raymond Hettinger | 54f0222 | 2002-06-01 14:18:47 +0000 | [diff] [blame] | 102 | if ftype is None or not ftype in table: |
Walter Dörwald | 70a6b49 | 2004-02-12 17:35:32 +0000 | [diff] [blame] | 103 | raise error, '%s: unsupported audio file type %r' % (filename, ftype) |
Guido van Rossum | dd8ddac | 2002-08-10 00:17:59 +0000 | [diff] [blame] | 104 | (fd, temp) = tempfile.mkstemp() |
Guido van Rossum | 3b0a329 | 2002-08-09 16:38:32 +0000 | [diff] [blame] | 105 | os.close(fd) |
Tim Peters | b90f89a | 2001-01-15 03:26:36 +0000 | [diff] [blame] | 106 | temps.append(temp) |
| 107 | sts = table[ftype].copy(fname, temp) |
| 108 | if sts: |
| 109 | raise error, filename + ': conversion to aiff failed' |
| 110 | return temp |