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