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 | """ |
Guido van Rossum | 2db9135 | 1992-10-18 17:09:59 +0000 | [diff] [blame] | 10 | |
| 11 | import os |
| 12 | import tempfile |
| 13 | import pipes |
Fred Drake | 0652a4e | 1999-02-24 18:49:15 +0000 | [diff] [blame] | 14 | import sndhdr |
Guido van Rossum | 2db9135 | 1992-10-18 17:09:59 +0000 | [diff] [blame] | 15 | |
| 16 | table = {} |
| 17 | |
Guido van Rossum | 7bc817d | 1993-12-17 15:25:27 +0000 | [diff] [blame] | 18 | t = pipes.Template() |
Guido van Rossum | 2db9135 | 1992-10-18 17:09:59 +0000 | [diff] [blame] | 19 | t.append('sox -t au - -t aiff -r 8000 -', '--') |
| 20 | table['au'] = t |
| 21 | |
| 22 | # XXX The following is actually sub-optimal. |
| 23 | # XXX The HCOM sampling rate can be 22k, 22k/2, 22k/3 or 22k/4. |
| 24 | # XXX We must force the output sampling rate else the SGI won't play |
| 25 | # XXX files sampled at 5.5k or 7.333k; however this means that files |
| 26 | # XXX sampled at 11k are unnecessarily expanded. |
| 27 | # XXX Similar comments apply to some other file types. |
Guido van Rossum | 7bc817d | 1993-12-17 15:25:27 +0000 | [diff] [blame] | 28 | t = pipes.Template() |
Guido van Rossum | 2db9135 | 1992-10-18 17:09:59 +0000 | [diff] [blame] | 29 | t.append('sox -t hcom - -t aiff -r 22050 -', '--') |
| 30 | table['hcom'] = t |
| 31 | |
Guido van Rossum | 7bc817d | 1993-12-17 15:25:27 +0000 | [diff] [blame] | 32 | t = pipes.Template() |
Guido van Rossum | 2db9135 | 1992-10-18 17:09:59 +0000 | [diff] [blame] | 33 | t.append('sox -t voc - -t aiff -r 11025 -', '--') |
| 34 | table['voc'] = t |
| 35 | |
Guido van Rossum | 7bc817d | 1993-12-17 15:25:27 +0000 | [diff] [blame] | 36 | t = pipes.Template() |
Guido van Rossum | 2db9135 | 1992-10-18 17:09:59 +0000 | [diff] [blame] | 37 | t.append('sox -t wav - -t aiff -', '--') |
| 38 | table['wav'] = t |
| 39 | |
Guido van Rossum | 7bc817d | 1993-12-17 15:25:27 +0000 | [diff] [blame] | 40 | t = pipes.Template() |
Guido van Rossum | 2db9135 | 1992-10-18 17:09:59 +0000 | [diff] [blame] | 41 | t.append('sox -t 8svx - -t aiff -r 16000 -', '--') |
| 42 | table['8svx'] = t |
| 43 | |
Guido van Rossum | 7bc817d | 1993-12-17 15:25:27 +0000 | [diff] [blame] | 44 | t = pipes.Template() |
Guido van Rossum | 2db9135 | 1992-10-18 17:09:59 +0000 | [diff] [blame] | 45 | t.append('sox -t sndt - -t aiff -r 16000 -', '--') |
| 46 | table['sndt'] = t |
| 47 | |
Guido van Rossum | 7bc817d | 1993-12-17 15:25:27 +0000 | [diff] [blame] | 48 | t = pipes.Template() |
Guido van Rossum | 2db9135 | 1992-10-18 17:09:59 +0000 | [diff] [blame] | 49 | t.append('sox -t sndr - -t aiff -r 16000 -', '--') |
| 50 | table['sndr'] = t |
| 51 | |
Guido van Rossum | 7bc817d | 1993-12-17 15:25:27 +0000 | [diff] [blame] | 52 | uncompress = pipes.Template() |
Guido van Rossum | 2db9135 | 1992-10-18 17:09:59 +0000 | [diff] [blame] | 53 | uncompress.append('uncompress', '--') |
| 54 | |
| 55 | |
Fred Drake | 9b8d801 | 2000-08-17 04:45:13 +0000 | [diff] [blame] | 56 | class error(Exception): |
| 57 | pass |
Guido van Rossum | 2db9135 | 1992-10-18 17:09:59 +0000 | [diff] [blame] | 58 | |
| 59 | def toaiff(filename): |
| 60 | temps = [] |
| 61 | ret = None |
| 62 | try: |
| 63 | ret = _toaiff(filename, temps) |
| 64 | finally: |
| 65 | for temp in temps[:]: |
| 66 | if temp <> ret: |
| 67 | try: |
| 68 | os.unlink(temp) |
| 69 | except os.error: |
| 70 | pass |
| 71 | temps.remove(temp) |
| 72 | return ret |
| 73 | |
| 74 | def _toaiff(filename, temps): |
| 75 | if filename[-2:] == '.Z': |
| 76 | fname = tempfile.mktemp() |
| 77 | temps.append(fname) |
| 78 | sts = uncompress.copy(filename, fname) |
| 79 | if sts: |
Thomas Wouters | 7e47402 | 2000-07-16 12:04:32 +0000 | [diff] [blame] | 80 | raise error, filename + ': uncompress failed' |
Guido van Rossum | 2db9135 | 1992-10-18 17:09:59 +0000 | [diff] [blame] | 81 | else: |
| 82 | fname = filename |
| 83 | try: |
Fred Drake | 0652a4e | 1999-02-24 18:49:15 +0000 | [diff] [blame] | 84 | ftype = sndhdr.whathdr(fname) |
Guido van Rossum | 2db9135 | 1992-10-18 17:09:59 +0000 | [diff] [blame] | 85 | if ftype: |
| 86 | ftype = ftype[0] # All we're interested in |
| 87 | except IOError: |
| 88 | if type(msg) == type(()) and len(msg) == 2 and \ |
| 89 | type(msg[0]) == type(0) and type(msg[1]) == type(''): |
| 90 | msg = msg[1] |
| 91 | if type(msg) <> type(''): |
| 92 | msg = `msg` |
| 93 | raise error, filename + ': ' + msg |
| 94 | if ftype == 'aiff': |
| 95 | return fname |
| 96 | if ftype == None or not table.has_key(ftype): |
| 97 | raise error, \ |
| 98 | filename + ': unsupported audio file type ' + `ftype` |
| 99 | temp = tempfile.mktemp() |
| 100 | temps.append(temp) |
| 101 | sts = table[ftype].copy(fname, temp) |
| 102 | if sts: |
| 103 | raise error, filename + ': conversion to aiff failed' |
| 104 | return temp |