aifc
— Read and write AIFF and AIFC filesSource code: Lib/aifc.py
This module provides support for reading and writing AIFF and AIFF-C files. AIFF is Audio Interchange File Format, a format for storing digital audio samples in a file. AIFF-C is a newer version of the format that includes the ability to compress the audio data.
Audio files have a number of parameters that describe the audio data. The
sampling rate or frame rate is the number of times per second the sound is
sampled. The number of channels indicate if the audio is mono, stereo, or
quadro. Each frame consists of one sample per channel. The sample size is the
size in bytes of each sample. Thus a frame consists of
nchannels * samplesize
bytes, and a second’s worth of audio consists of
nchannels * samplesize * framerate
bytes.
For example, CD quality audio has a sample size of two bytes (16 bits), uses two channels (stereo) and has a frame rate of 44,100 frames/second. This gives a frame size of 4 bytes (2*2), and a second’s worth occupies 2*2*44100 bytes (176,400 bytes).
Module aifc
defines the following function:
aifc.
open
(file, mode=None)Open an AIFF or AIFF-C file and return an object instance with methods that are
described below. The argument file is either a string naming a file or a
file object. mode must be 'r'
or 'rb'
when the file must be
opened for reading, or 'w'
or 'wb'
when the file must be opened for writing.
If omitted, file.mode
is used if it exists, otherwise 'rb'
is used. When
used for writing, the file object should be seekable, unless you know ahead of
time how many samples you are going to write in total and use
writeframesraw()
and setnframes()
.
The open()
function may be used in a with
statement. When
the with
block completes, the close()
method is called.
Changed in version 3.4: Support for the with
statement was added.
Objects returned by open()
when a file is opened for reading have the
following methods:
aifc.
getnchannels
()aifc.
getsampwidth
()aifc.
getframerate
()aifc.
getnframes
()aifc.
getcomptype
()b'NONE'
.aifc.
getcompname
()b'not compressed'
.aifc.
getparams
()namedtuple()
(nchannels, sampwidth, framerate, nframes, comptype, compname)
, equivalent to output of the get*()
methods.aifc.
getmarkers
()aifc.
getmark
(id)getmarkers()
for the mark with the given id.aifc.
readframes
(nframes)aifc.
rewind
()readframes()
will start from the beginning.aifc.
setpos
(pos)aifc.
tell
()aifc.
close
()Objects returned by open()
when a file is opened for writing have all the
above methods, except for readframes()
and setpos()
. In addition
the following methods exist. The get*()
methods can only be called after
the corresponding set*()
methods have been called. Before the first
writeframes()
or writeframesraw()
, all parameters except for the
number of frames must be filled in.
aifc.
aiff
()'.aiff'
in which case the default is an AIFF file.aifc.
aifc
()'.aiff'
in which case the default is an AIFF file.aifc.
setnchannels
(nchannels)aifc.
setsampwidth
(width)aifc.
setframerate
(rate)aifc.
setnframes
(nframes)aifc.
setcomptype
(type, name)b'NONE'
, b'ULAW'
, b'ALAW'
, b'G722'
.aifc.
setparams
(nchannels, sampwidth, framerate, comptype, compname)getparams()
call as argument to setparams()
.aifc.
setmark
(id, pos, name)close()
.aifc.
tell
()setmark()
.aifc.
writeframes
(data)Write data to the output file. This method can only be called after the audio file parameters have been set.
Changed in version 3.4: Any bytes-like object is now accepted.
aifc.
writeframesraw
(data)Like writeframes()
, except that the header of the audio file is not
updated.
Changed in version 3.4: Any bytes-like object is now accepted.
aifc.
close
()