"""Stuff to parse AIFF-C and AIFF files.
Unless explicitly stated otherwise, the description below is true
both for AIFF-C files and AIFF files.
An AIFF-C file has the following structure.
An AIFF file has the string "AIFF" instead of "AIFC".
A chunk consists of an identifier (4 bytes) followed by a size (4 bytes,
big endian order), followed by the data. The size field does not include
the size of the 8 byte header.
The following chunk types are recognized.
<version number of AIFF-C defining document> (AIFF-C only).
<marker ID> (2 bytes, must be > 0)
<marker name> ("pstring")
<# of channels> (2 bytes)
<# of sound frames> (4 bytes)
<size of the samples> (2 bytes)
<sampling frequency> (10 bytes, IEEE 80-bit extended
<compression type> (4 bytes)
<human-readable version of compression type> ("pstring")
<offset> (4 bytes, not used by this program)
<blocksize> (4 bytes, not used by this program)
A pstring consists of 1 byte length, a string of characters, and 0 or 1
byte pad to make the total length even.
where file is either the name of a file or an open file pointer.
The open file pointer must have methods read(), seek(), and close().
In some types of audio files, if the setpos() method is not used,
the seek() method is not necessary.
This returns an instance of a class with the following public methods:
getnchannels() -- returns number of audio channels (1 for
getsampwidth() -- returns sample width in bytes
getframerate() -- returns sampling frequency
getnframes() -- returns number of audio frames
getcomptype() -- returns compression type ('NONE' for AIFF files)
getcompname() -- returns human-readable version of
compression type ('not compressed' for AIFF files)
getparams() -- returns a tuple consisting of all of the
getmarkers() -- get the list of marks in the audio file or None
getmark(id) -- get mark with the specified id (raises an error
if the mark does not exist)
readframes(n) -- returns at most n frames of audio
rewind() -- rewind to the beginning of the audio stream
setpos(pos) -- seek to the specified position
tell() -- return the current position
close() -- close the instance (make it unusable)
The position returned by tell(), the position given to setpos() and
the position of marks are all compatible and have nothing to do with
the actual position in the file.
The close() method is called automatically when the class instance
where file is either the name of a file or an open file pointer.
The open file pointer must have methods write(), tell(), seek(), and
This returns an instance of a class with the following public methods:
aiff() -- create an AIFF file (AIFF-C default)
aifc() -- create an AIFF-C file
setnchannels(n) -- set the number of channels
setsampwidth(n) -- set the sample width
setframerate(n) -- set the frame rate
setnframes(n) -- set the number of frames
-- set the compression type and the
human-readable compression type
-- set all parameters at once
-- add specified mark to the list of marks
tell() -- return current position in output file (useful
in combination with setmark())
-- write audio frames without pathing up the
-- write audio frames and patch up the file header
close() -- patch up the file header and close the
You should set the parameters before the first writeframesraw or
writeframes. The total number of frames does not need to be set,
but when it is set to the correct value, the header does not have to
It is best to first set all parameters, perhaps possibly the
compression type, and then write audio frames using writeframesraw.
When all frames have been written, either call writeframes('') or
close() to patch up the sizes in the header.
Marks can be added anytime. If there are any marks, ypu must call
close() after all frames have been written.
The close() method is called automatically when the class instance
When a file is opened with the extension '.aiff', an AIFF file is
written, otherwise an AIFF-C file is written. This default can be
changed by calling aiff() or aifc() before the first writeframes or
__all__
= ["Error","open","openfp"]
_AIFC_version
= 0xA2805140L
# Version 1 of AIFF-C
_skiplist
= 'COMT', 'INST', 'MIDI', 'AESD', \
'APPL', 'NAME', 'AUTH', '(c) ', 'ANNO'
return struct
.unpack('>l', file.read(4))[0]
return struct
.unpack('>L', file.read(4))[0]
return struct
.unpack('>h', file.read(2))[0]
length
= ord(file.read(1))
_HUGE_VAL
= 1.79769313486231e+308 # See <limits.h>
def _read_float(f
): # 10 bytes
expon
= _read_short(f
) # 2 bytes
himant
= _read_ulong(f
) # 4 bytes
lomant
= _read_ulong(f
) # 4 bytes
if expon
== himant
== lomant
== 0:
f
= (himant
* 0x100000000L
+ lomant
) * pow(2.0, expon
- 63)
f
.write(struct
.pack('>h', x
))
f
.write(struct
.pack('>L', x
))
fmant
, expon
= math
.frexp(x
)
if expon
> 16384 or fmant
>= 1: # Infinity or NaN
if expon
< 0: # denormalized
fmant
= math
.ldexp(fmant
, expon
)
fmant
= math
.ldexp(fmant
, 32)
fsmant
= math
.floor(fmant
)
fmant
= math
.ldexp(fmant
- fsmant
, 32)
fsmant
= math
.floor(fmant
)
# Variables used in this class:
# These variables are available to the user though appropriate
# _file -- the open file with methods read(), close(), and seek()
# set through the __init__() method
# _nchannels -- the number of audio channels
# available through the getnchannels() method
# _nframes -- the number of audio frames
# available through the getnframes() method
# _sampwidth -- the number of bytes per audio sample
# available through the getsampwidth() method
# _framerate -- the sampling frequency
# available through the getframerate() method
# _comptype -- the AIFF-C compression type ('NONE' if AIFF)
# available through the getcomptype() method
# _compname -- the human-readable AIFF-C compression type
# available through the getcomptype() method
# _markers -- the marks in the audio file
# available through the getmarkers() and getmark()
# _soundpos -- the position in the audio stream
# available through the tell() method, set through the
# These variables are used internally only:
# _version -- the AIFF-C version number
# _decomp -- the decompressor from builtin module cl
# _comm_chunk_read -- 1 iff the COMM chunk has been read
# _aifc -- 1 iff reading an AIFF-C file
# _ssnd_seek_needed -- 1 iff positioned correctly in audio
# _ssnd_chunk -- instantiation of a chunk class for the SSND chunk
# _framesize -- size of one frame in the file
if self
._file
.getname() != 'FORM':
raise Error
, 'file does not start with FORM id'
formdata
= self
._file
.read(4)
raise Error
, 'not an AIFF or AIFF-C file'
self
._comm
_chunk
_read
= 0
self
._ssnd
_seek
_needed
= 1
chunk
= Chunk(self
._file
)
chunkname
= chunk
.getname()
self
._read
_comm
_chunk
(chunk
)
self
._comm
_chunk
_read
= 1
elif chunkname
== 'SSND':
self
._ssnd
_seek
_needed
= 0
elif chunkname
== 'FVER':
self
._version
= _read_ulong(chunk
)
elif chunkname
== 'MARK':
elif chunkname
in _skiplist
:
raise Error
, 'unrecognized chunk type '+chunk
.chunkname
if not self
._comm
_chunk
_read
or not self
._ssnd
_chunk
:
raise Error
, 'COMM chunk and/or SSND chunk missing'
if self
._aifc
and self
._decomp
:
params
= [cl
.ORIGINAL_FORMAT
, 0,
cl
.BITS_PER_COMPONENT
, self
._sampwidth
* 8,
cl
.FRAME_RATE
, self
._framerate
]
elif self
._nchannels
== 2:
params
[1] = cl
.STEREO_INTERLEAVED
raise Error
, 'cannot compress more than 2 channels'
self
._decomp
.SetParams(params
)
f
= __builtin__
.open(f
, 'rb')
# else, assume it is an open file object already
self
._ssnd
_seek
_needed
= 1
self
._decomp
.CloseDecompressor()
return self
.getnchannels(), self
.getsampwidth(), \
self
.getframerate(), self
.getnframes(), \
self
.getcomptype(), self
.getcompname()
if len(self
._markers
) == 0:
for marker
in self
._markers
:
raise Error
, 'marker %r does not exist' % (id,)
if pos
< 0 or pos
> self
._nframes
:
raise Error
, 'position not in range'
self
._ssnd
_seek
_needed
= 1
def readframes(self
, nframes
):
if self
._ssnd
_seek
_needed
:
dummy
= self
._ssnd
_chunk
.read(8)
pos
= self
._soundpos
* self
._framesize
self
._ssnd
_chunk
.seek(pos
+ 8)
self
._ssnd
_seek
_needed
= 0
data
= self
._ssnd
_chunk
.read(nframes
* self
._framesize
)
if self
._convert
and data
:
data
= self
._convert
(data
)
self
._soundpos
= self
._soundpos
+ len(data
) / (self
._nchannels
* self
._sampwidth
)
def _decomp_data(self
, data
):
dummy
= self
._decomp
.SetParam(cl
.FRAME_BUFFER_SIZE
,
return self
._decomp
.Decompress(len(data
) / self
._nchannels
,
def _ulaw2lin(self
, data
):
return audioop
.ulaw2lin(data
, 2)
def _adpcm2lin(self
, data
):
if not hasattr(self
, '_adpcmstate'):
data
, self
._adpcmstate
= audioop
.adpcm2lin(data
, 2,
def _read_comm_chunk(self
, chunk
):
self
._nchannels
= _read_short(chunk
)
self
._nframes
= _read_long(chunk
)
self
._sampwidth
= (_read_short(chunk
) + 7) / 8
self
._framerate
= int(_read_float(chunk
))
self
._framesize
= self
._nchannels
* self
._sampwidth
#DEBUG: SGI's soundeditor produces a bad size :-(
if chunk
.chunksize
== 18:
print 'Warning: bad COMM chunk size'
self
._comptype
= chunk
.read(4)
length
= ord(chunk
.file.read(1))
chunk
.chunksize
= chunk
.chunksize
+ length
self
._compname
= _read_string(chunk
)
if self
._comptype
!= 'NONE':
if self
._comptype
== 'G722':
self
._convert
= self
._adpcm
2lin
self
._framesize
= self
._framesize
/ 4
# for ULAW and ALAW try Compression Library
if self
._comptype
== 'ULAW':
self
._convert
= self
._ulaw
2lin
self
._framesize
= self
._framesize
/ 2
raise Error
, 'cannot read compressed AIFF-C files'
if self
._comptype
== 'ULAW':
self
._framesize
= self
._framesize
/ 2
elif self
._comptype
== 'ALAW':
self
._framesize
= self
._framesize
/ 2
raise Error
, 'unsupported compression type'
self
._decomp
= cl
.OpenDecompressor(scheme
)
self
._convert
= self
._decomp
_data
self
._compname
= 'not compressed'
def _readmark(self
, chunk
):
nmarkers
= _read_short(chunk
)
# Some files appear to contain invalid counts.
# Cope with this by testing for EOF.
for i
in range(nmarkers
):
name
= _read_string(chunk
)
# some files appear to have
# dummy markers consisting of
# a position 0 and name ''
self
._markers
.append((id, pos
, name
))
print 'Warning: MARK chunk contains only',
print len(self
._markers
),
if len(self
._markers
) == 1: print 'marker',
print 'instead of', nmarkers
# Variables used in this class:
# These variables are user settable through appropriate methods
# _file -- the open file with methods write(), close(), tell(), seek()
# set through the __init__() method
# _comptype -- the AIFF-C compression type ('NONE' in AIFF)
# set through the setcomptype() or setparams() method
# _compname -- the human-readable AIFF-C compression type
# set through the setcomptype() or setparams() method
# _nchannels -- the number of audio channels
# set through the setnchannels() or setparams() method
# _sampwidth -- the number of bytes per audio sample
# set through the setsampwidth() or setparams() method
# _framerate -- the sampling frequency
# set through the setframerate() or setparams() method
# _nframes -- the number of audio frames written to the header
# set through the setnframes() or setparams() method
# _aifc -- whether we're writing an AIFF-C file or an AIFF file
# set through the aifc() method, reset through the
# These variables are used internally only:
# _version -- the AIFF-C version number
# _comp -- the compressor from builtin module cl
# _nframeswritten -- the number of audio frames actually written
# _datalength -- the size of the audio samples written to the header
# _datawritten -- the size of the audio samples actually written
f
= __builtin__
.open(f
, 'wb')
# else, assume it is an open file object already
if filename
[-5:] == '.aiff':
self
._version
= _AIFC_version
self
._compname
= 'not compressed'
self
._aifc
= 1 # AIFF-C is default
raise Error
, 'cannot change parameters after starting to write'
raise Error
, 'cannot change parameters after starting to write'
def setnchannels(self
, nchannels
):
raise Error
, 'cannot change parameters after starting to write'
raise Error
, 'bad # of channels'
self
._nchannels
= nchannels
raise Error
, 'number of channels not set'
def setsampwidth(self
, sampwidth
):
raise Error
, 'cannot change parameters after starting to write'
if sampwidth
< 1 or sampwidth
> 4:
raise Error
, 'bad sample width'
self
._sampwidth
= sampwidth
raise Error
, 'sample width not set'
def setframerate(self
, framerate
):
raise Error
, 'cannot change parameters after starting to write'
raise Error
, 'bad frame rate'
self
._framerate
= framerate
raise Error
, 'frame rate not set'
def setnframes(self
, nframes
):
raise Error
, 'cannot change parameters after starting to write'
return self
._nframeswritten
def setcomptype(self
, comptype
, compname
):
raise Error
, 'cannot change parameters after starting to write'
if comptype
not in ('NONE', 'ULAW', 'ALAW', 'G722'):
raise Error
, 'unsupported compression type'
self
._comptype
= comptype
self
._compname
= compname
## def setversion(self, version):
## if self._nframeswritten:
## raise Error, 'cannot change parameters after starting to write'
## self._version = version
def setparams(self
, (nchannels
, sampwidth
, framerate
, nframes
, comptype
, compname
)):
raise Error
, 'cannot change parameters after starting to write'
if comptype
not in ('NONE', 'ULAW', 'ALAW', 'G722'):
raise Error
, 'unsupported compression type'
self
.setnchannels(nchannels
)
self
.setsampwidth(sampwidth
)
self
.setframerate(framerate
)
self
.setcomptype(comptype
, compname
)
if not self
._nchannels
or not self
._sampwidth
or not self
._framerate
:
raise Error
, 'not all parameters set'
return self
._nchannels
, self
._sampwidth
, self
._framerate
, \
self
._nframes
, self
._comptype
, self
._compname
def setmark(self
, id, pos
, name
):
raise Error
, 'marker ID must be > 0'
raise Error
, 'marker position must be >= 0'
if type(name
) != type(''):
raise Error
, 'marker name must be a string'
for i
in range(len(self
._markers
)):
if id == self
._markers
[i
][0]:
self
._markers
[i
] = id, pos
, name
self
._markers
.append((id, pos
, name
))
for marker
in self
._markers
:
raise Error
, 'marker %r does not exist' % (id,)
if len(self
._markers
) == 0:
return self
._nframeswritten
def writeframesraw(self
, data
):
self
._ensure
_header
_written
(len(data
))
nframes
= len(data
) / (self
._sampwidth
* self
._nchannels
)
data
= self
._convert
(data
)
self
._nframeswritten
= self
._nframeswritten
+ nframes
self
._datawritten
= self
._datawritten
+ len(data
)
def writeframes(self
, data
):
self
.writeframesraw(data
)
if self
._nframeswritten
!= self
._nframes
or \
self
._datalength
!= self
._datawritten
:
self
._ensure
_header
_written
(0)
if self
._datawritten
& 1:
self
._datawritten
= self
._datawritten
+ 1
if self
._nframeswritten
!= self
._nframes
or \
self
._datalength
!= self
._datawritten
or \
self
._comp
.CloseCompressor()
def _comp_data(self
, data
):
dummy
= self
._comp
.SetParam(cl
.FRAME_BUFFER_SIZE
, len(data
))
dummy
= self
._comp
.SetParam(cl
.COMPRESSED_BUFFER_SIZE
, len(data
))
return self
._comp
.Compress(self
._nframes
, data
)
def _lin2ulaw(self
, data
):
return audioop
.lin2ulaw(data
, 2)
def _lin2adpcm(self
, data
):
if not hasattr(self
, '_adpcmstate'):
data
, self
._adpcmstate
= audioop
.lin2adpcm(data
, 2,
def _ensure_header_written(self
, datasize
):
if not self
._nframeswritten
:
if self
._comptype
in ('ULAW', 'ALAW'):
raise Error
, 'sample width must be 2 when compressing with ULAW or ALAW'
if self
._comptype
== 'G722':
raise Error
, 'sample width must be 2 when compressing with G7.22 (ADPCM)'
raise Error
, '# channels not specified'
raise Error
, 'sample width not specified'
raise Error
, 'sampling rate not specified'
self
._write
_header
(datasize
)
def _init_compression(self
):
if self
._comptype
== 'G722':
self
._convert
= self
._lin
2adpcm
if self
._comptype
== 'ULAW':
self
._convert
= self
._lin
2ulaw
raise Error
, 'cannot write compressed AIFF-C files'
if self
._comptype
== 'ULAW':
elif self
._comptype
== 'ALAW':
raise Error
, 'unsupported compression type'
self
._comp
= cl
.OpenCompressor(scheme
)
params
= [cl
.ORIGINAL_FORMAT
, 0,
cl
.BITS_PER_COMPONENT
, self
._sampwidth
* 8,
cl
.FRAME_RATE
, self
._framerate
,
cl
.FRAME_BUFFER_SIZE
, 100,
cl
.COMPRESSED_BUFFER_SIZE
, 100]
elif self
._nchannels
== 2:
params
[1] = cl
.STEREO_INTERLEAVED
raise Error
, 'cannot compress more than 2 channels'
self
._comp
.SetParams(params
)
# the compressor produces a header which we ignore
dummy
= self
._comp
.Compress(0, '')
self
._convert
= self
._comp
_data
def _write_header(self
, initlength
):
if self
._aifc
and self
._comptype
!= 'NONE':
self
._nframes
= initlength
/ (self
._nchannels
* self
._sampwidth
)
self
._datalength
= self
._nframes
* self
._nchannels
* self
._sampwidth
self
._datalength
= self
._datalength
+ 1
if self
._comptype
in ('ULAW', 'ALAW'):
self
._datalength
= self
._datalength
/ 2
self
._datalength
= self
._datalength
+ 1
elif self
._comptype
== 'G722':
self
._datalength
= (self
._datalength
+ 3) / 4
self
._datalength
= self
._datalength
+ 1
self
._form
_length
_pos
= self
._file
.tell()
commlength
= self
._write
_form
_length
(self
._datalength
)
_write_long(self
._file
, 4)
_write_long(self
._file
, self
._version
)
_write_long(self
._file
, commlength
)
_write_short(self
._file
, self
._nchannels
)
self
._nframes
_pos
= self
._file
.tell()
_write_long(self
._file
, self
._nframes
)
_write_short(self
._file
, self
._sampwidth
* 8)
_write_float(self
._file
, self
._framerate
)
self
._file
.write(self
._comptype
)
_write_string(self
._file
, self
._compname
)
self
._ssnd
_length
_pos
= self
._file
.tell()
_write_long(self
._file
, self
._datalength
+ 8)
_write_long(self
._file
, 0)
_write_long(self
._file
, 0)
def _write_form_length(self
, datalength
):
commlength
= 18 + 5 + len(self
._compname
)
commlength
= commlength
+ 1
_write_long(self
._file
, 4 + verslength
+ self
._marklength
+ \
8 + commlength
+ 16 + datalength
)
curpos
= self
._file
.tell()
if self
._datawritten
& 1:
datalength
= self
._datawritten
+ 1
datalength
= self
._datawritten
if datalength
== self
._datalength
and \
self
._nframes
== self
._nframeswritten
and \
self
._file
.seek(curpos
, 0)
self
._file
.seek(self
._form
_length
_pos
, 0)
dummy
= self
._write
_form
_length
(datalength
)
self
._file
.seek(self
._nframes
_pos
, 0)
_write_long(self
._file
, self
._nframeswritten
)
self
._file
.seek(self
._ssnd
_length
_pos
, 0)
_write_long(self
._file
, datalength
+ 8)
self
._file
.seek(curpos
, 0)
self
._nframes
= self
._nframeswritten
self
._datalength
= datalength
if len(self
._markers
) == 0:
for marker
in self
._markers
:
length
= length
+ len(name
) + 1 + 6
_write_long(self
._file
, length
)
self
._marklength
= length
+ 8
_write_short(self
._file
, len(self
._markers
))
for marker
in self
._markers
:
_write_short(self
._file
, id)
_write_long(self
._file
, pos
)
_write_string(self
._file
, name
)
elif mode
in ('w', 'wb'):
raise Error
, "mode must be 'r', 'rb', 'w', or 'wb'"
openfp
= open # B/W compatibility
if __name__
== '__main__':
sys
.argv
.append('/usr/demos/data/audio/bach.aiff')
print "nchannels =", f
.getnchannels()
print "nframes =", f
.getnframes()
print "sampwidth =", f
.getsampwidth()
print "framerate =", f
.getframerate()
print "comptype =", f
.getcomptype()
print "compname =", f
.getcompname()
g
.setparams(f
.getparams())
data
= f
.readframes(1024)