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