Guido van Rossum | 2db9135 | 1992-10-18 17:09:59 +0000 | [diff] [blame] | 1 | # Convert "arbitrary" sound files to AIFF files (Apple and SGI's audio format). |
| 2 | # Input may be compressed. |
| 3 | # Uncompressed file type may be AIFF, WAV, VOC, 8SVX, NeXT/Sun, and others. |
| 4 | # An exception is raised if the file is not of a recognized type. |
| 5 | # Returned filename is either the input filename or a temporary filename; |
| 6 | # in the latter case the caller must ensure that it is removed. |
| 7 | # Other temporary files used are removed by the function. |
| 8 | |
| 9 | import os |
| 10 | import tempfile |
| 11 | import pipes |
| 12 | import sndhdr |
| 13 | |
| 14 | table = {} |
| 15 | |
| 16 | t = pipes.Template().init() |
| 17 | t.append('sox -t au - -t aiff -r 8000 -', '--') |
| 18 | table['au'] = t |
| 19 | |
| 20 | # XXX The following is actually sub-optimal. |
| 21 | # XXX The HCOM sampling rate can be 22k, 22k/2, 22k/3 or 22k/4. |
| 22 | # XXX We must force the output sampling rate else the SGI won't play |
| 23 | # XXX files sampled at 5.5k or 7.333k; however this means that files |
| 24 | # XXX sampled at 11k are unnecessarily expanded. |
| 25 | # XXX Similar comments apply to some other file types. |
| 26 | t = pipes.Template().init() |
| 27 | t.append('sox -t hcom - -t aiff -r 22050 -', '--') |
| 28 | table['hcom'] = t |
| 29 | |
| 30 | t = pipes.Template().init() |
| 31 | t.append('sox -t voc - -t aiff -r 11025 -', '--') |
| 32 | table['voc'] = t |
| 33 | |
| 34 | t = pipes.Template().init() |
| 35 | t.append('sox -t wav - -t aiff -', '--') |
| 36 | table['wav'] = t |
| 37 | |
| 38 | t = pipes.Template().init() |
| 39 | t.append('sox -t 8svx - -t aiff -r 16000 -', '--') |
| 40 | table['8svx'] = t |
| 41 | |
| 42 | t = pipes.Template().init() |
| 43 | t.append('sox -t sndt - -t aiff -r 16000 -', '--') |
| 44 | table['sndt'] = t |
| 45 | |
| 46 | t = pipes.Template().init() |
| 47 | t.append('sox -t sndr - -t aiff -r 16000 -', '--') |
| 48 | table['sndr'] = t |
| 49 | |
| 50 | uncompress = pipes.Template().init() |
| 51 | uncompress.append('uncompress', '--') |
| 52 | |
| 53 | |
| 54 | error = 'toaiff.error' # Exception |
| 55 | |
| 56 | def toaiff(filename): |
| 57 | temps = [] |
| 58 | ret = None |
| 59 | try: |
| 60 | ret = _toaiff(filename, temps) |
| 61 | finally: |
| 62 | for temp in temps[:]: |
| 63 | if temp <> ret: |
| 64 | try: |
| 65 | os.unlink(temp) |
| 66 | except os.error: |
| 67 | pass |
| 68 | temps.remove(temp) |
| 69 | return ret |
| 70 | |
| 71 | def _toaiff(filename, temps): |
| 72 | if filename[-2:] == '.Z': |
| 73 | fname = tempfile.mktemp() |
| 74 | temps.append(fname) |
| 75 | sts = uncompress.copy(filename, fname) |
| 76 | if sts: |
| 77 | raise error, filename + ': uncomress failed' |
| 78 | else: |
| 79 | fname = filename |
| 80 | try: |
| 81 | ftype = sndhdr.whathdr(fname) |
| 82 | if ftype: |
| 83 | ftype = ftype[0] # All we're interested in |
| 84 | except IOError: |
| 85 | if type(msg) == type(()) and len(msg) == 2 and \ |
| 86 | type(msg[0]) == type(0) and type(msg[1]) == type(''): |
| 87 | msg = msg[1] |
| 88 | if type(msg) <> type(''): |
| 89 | msg = `msg` |
| 90 | raise error, filename + ': ' + msg |
| 91 | if ftype == 'aiff': |
| 92 | return fname |
| 93 | if ftype == None or not table.has_key(ftype): |
| 94 | raise error, \ |
| 95 | filename + ': unsupported audio file type ' + `ftype` |
| 96 | temp = tempfile.mktemp() |
| 97 | temps.append(temp) |
| 98 | sts = table[ftype].copy(fname, temp) |
| 99 | if sts: |
| 100 | raise error, filename + ': conversion to aiff failed' |
| 101 | return temp |