65
index
modules |
next |
previous |
21.3.
aifc
— Read and write AIFF and AIFC
files
Source 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.
Note: Some operations may only work under IRIX; these will raise
ImportError
when
attempting to import the
cl
module, which is only available on IRIX.
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
]
)
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()
.
Objects returned by
open()
when a file is opened for reading have the following methods:
Python » Python v2.7.6 documentation » The Python Standard Library » 21. Multimedia
Services »
Online Remove password from protected PDF file Find your password-protected PDF and upload it. If there is no strong encryption on your file, it will be unlocked and ready to download within seconds.
decrypt a pdf file online; change security settings pdf reader
68
aifc.
getnchannels
()
Return the number of audio channels (1 for mono, 2 for stereo).
aifc.
getsampwidth
()
Return the size in bytes of individual samples.
aifc.
getframerate
()
Return the sampling rate (number of audio frames per second).
aifc.
getnframes
()
Return the number of audio frames in the file.
aifc.
getcomptype
()
Return a four-character string describing the type of compression used in the audio
file. For AIFF files, the returned value is
'NONE'
.
aifc.
getcompname
()
Return a human-readable description of the type of compression used in the audio
file. For AIFF files, the returned value is
'not compressed'
.
aifc.
getparams
()
Return a tuple consisting of all of the above values in the above order.
aifc.
getmarkers
()
Return a list of markers in the audio file. A marker consists of a tuple of three
elements. The first is the mark ID (an integer), the second is the mark position in
frames from the beginning of the data (an integer), the third is the name of the mark
(a string).
aifc.
getmark
(
id
)
Return the tuple as described in
getmarkers()
for the mark with the given id.
aifc.
readframes
(
nframes
)
Read and return the next nframes frames from the audio file. The returned data is a
string containing for each frame the uncompressed samples of all channels.
aifc.
rewind
()
Rewind the read pointer. The next
readframes()
will start from the beginning.
aifc.
setpos
(
pos
)
Seek to the specified frame number.
82
aifc.
tell
()
Return the current frame number.
aifc.
close
()
Close the AIFF file. After calling this method, the object can no longer be used.
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
()
Create an AIFF file. The default is that an AIFF-C file is created, unless the name of
the file ends in
'.aiff'
in which case the default is an AIFF file.
aifc.
aifc
()
Create an AIFF-C file. The default is that an AIFF-C file is created, unless the name of
the file ends in
'.aiff'
in which case the default is an AIFF file.
aifc.
setnchannels
(
nchannels
)
Specify the number of channels in the audio file.
aifc.
setsampwidth
(
width
)
Specify the size in bytes of audio samples.
aifc.
setframerate
(
rate
)
Specify the sampling frequency in frames per second.
aifc.
setnframes
(
nframes
)
Specify the number of frames that are to be written to the audio file. If this parameter
is not set, or not set correctly, the file needs to support seeking.
aifc.
setcomptype
(
type, name
)
Specify the compression type. If not specified, the audio data will not be compressed.
In AIFF files, compression is not possible. The name parameter should be a human-
readable description of the compression type, the type parameter should be a four-
character string. Currently the following compression types are supported: NONE,
ULAW, ALAW, G722.
aifc.
setparams
(
nchannels, sampwidth, framerate, comptype, compname
)
Set all the above parameters at once. The argument is a tuple consisting of the
53
various parameters. This means that it is possible to use the result of a
getparams()
call as argument to
setparams()
.
aifc.
setmark
(
id, pos, name
)
Add a mark with the given id (larger than 0), and the given name at the given position.
This method can be called at any time before
close()
.
aifc.
tell
()
Return the current write position in the output file. Useful in combination with
setmark()
.
aifc.
writeframes
(
data
)
Write data to the output file. This method can only be called after the audio file
parameters have been set.
aifc.
writeframesraw
(
data
)
Like
writeframes()
, except that the header of the audio file is not updated.
aifc.
close
()
Close the AIFF file. The header of the file is updated to reflect the actual size of the
audio data. After calling this method, the object can no longer be used.
index
modules |
next |
previous |
Python » Python v2.7.6 documentation » The Python Standard Library » 21. Multimedia
Services »
© Copyright
1990-2013, Python Software Foundation.
The Python Software Foundation is a non-profit corporation. Please donate.
Last updated on Nov 10, 2013. Found a bug
?
Created using Sphinx
1.0.7.
66
index
modules |
next |
previous |
21.4.
sunau
— Read and write Sun AU files
Source code: Lib/sunau.py
The
sunau
module provides a convenient interface to the Sun AU sound format. Note that
this module is interface-compatible with the modules
aifc
and
wave
.
An audio file consists of a header followed by the data. The fields of the header are:
Field
Contents
magic word
The four bytes
.snd
.
header size
Size of the header, including info, in bytes.
data size
Physical size of the data, in bytes.
encoding
Indicates how the audio samples are encoded.
sample rate
The sampling rate.
# of channels
The number of channels in the samples.
info
ASCII string giving a description of the audio file (padded with null
bytes).
Apart from the info field, all header fields are 4 bytes in size. They are all 32-bit unsigned
integers encoded in big-endian byte order.
The
sunau
module defines the following functions:
sunau.
open
(
file, mode
)
If file is a string, open the file by that name, otherwise treat it as a seekable file-like
object. mode can be any of
'r'
Read only mode.
'w'
Write only mode.
Note that it does not allow read/write files.
A mode of
'r'
returns a
AU_read
object, while a mode of
'w'
or
'wb'
returns a
AU_write
object.
Python » Python v2.7.6 documentation » The Python Standard Library » 21. Multimedia
Services »
68
sunau.
openfp
(
file, mode
)
A synonym for
open()
, maintained for backwards compatibility.
The
sunau
module defines the following exception:
exception
sunau.
Error
An error raised when something is impossible because of Sun AU specs or
implementation deficiency.
The
sunau
module defines the following data items:
sunau.
AUDIO_FILE_MAGIC
An integer every valid Sun AU file begins with, stored in big-endian form. This is the
string
.snd
interpreted as an integer.
sunau.
AUDIO_FILE_ENCODING_MULAW_8
sunau.
AUDIO_FILE_ENCODING_LINEAR_8
sunau.
AUDIO_FILE_ENCODING_LINEAR_16
sunau.
AUDIO_FILE_ENCODING_LINEAR_24
sunau.
AUDIO_FILE_ENCODING_LINEAR_32
sunau.
AUDIO_FILE_ENCODING_ALAW_8
Values of the encoding field from the AU header which are supported by this module.
sunau.
AUDIO_FILE_ENCODING_FLOAT
sunau.
AUDIO_FILE_ENCODING_DOUBLE
sunau.
AUDIO_FILE_ENCODING_ADPCM_G721
sunau.
AUDIO_FILE_ENCODING_ADPCM_G722
sunau.
AUDIO_FILE_ENCODING_ADPCM_G723_3
sunau.
AUDIO_FILE_ENCODING_ADPCM_G723_5
Additional known values of the encoding field from the AU header, but which are not
supported by this module.
21.4.1. AU_read Objects
AU_read objects, as returned by
open()
above, have the following methods:
AU_read.
close
()
Close the stream, and make the instance unusable. (This is called automatically on
deletion.)
AU_read.
getnchannels
()
Returns number of audio channels (1 for mone, 2 for stereo).
AU_read.
getsampwidth
()
75
Returns sample width in bytes.
AU_read.
getframerate
()
Returns sampling frequency.
AU_read.
getnframes
()
Returns number of audio frames.
AU_read.
getcomptype
()
Returns compression type. Supported compression types are
'ULAW'
,
'ALAW'
and
'NONE'
.
AU_read.
getcompname
()
Human-readable version of
getcomptype()
. The supported types have the respective
names
'CCITT G.711 u-law'
,
'CCITT G.711 A-law'
and
'not compressed'
.
AU_read.
getparams
()
Returns a tuple
(nchannels, sampwidth, framerate, nframes, comptype,
compname)
, equivalent to output of the
get*()
methods.
AU_read.
readframes
(
n
)
Reads and returns at most n frames of audio, as a string of bytes. The data will be
returned in linear format. If the original data is in u-LAW format, it will be converted.
AU_read.
rewind
()
Rewind the file pointer to the beginning of the audio stream.
The following two methods define a term “position” which is compatible between them,
and is otherwise implementation dependent.
AU_read.
setpos
(
pos
)
Set the file pointer to the specified position. Only values returned from
tell()
should
be used for pos.
AU_read.
tell
()
Return current file pointer position. Note that the returned value has nothing to do with
the actual position in the file.
The following two functions are defined for compatibility with the
aifc
, and don’t do
anything interesting.
AU_read.
getmarkers
()
83
Returns
None
.
AU_read.
getmark
(
id
)
Raise an error.
21.4.2. AU_write Objects
AU_write objects, as returned by
open()
above, have the following methods:
AU_write.
setnchannels
(
n
)
Set the number of channels.
AU_write.
setsampwidth
(
n
)
Set the sample width (in bytes.)
AU_write.
setframerate
(
n
)
Set the frame rate.
AU_write.
setnframes
(
n
)
Set the number of frames. This can be later changed, when and if more frames are
written.
AU_write.
setcomptype
(
type, name
)
Set the compression type and description. Only
'NONE'
and
'ULAW'
are supported on
output.
AU_write.
setparams
(
tuple
)
The tuple should be
(nchannels, sampwidth, framerate, nframes, comptype,
compname)
, with values valid for the
set*()
methods. Set all parameters.
AU_write.
tell
()
Return current position in the file, with the same disclaimer for the
AU_read.tell()
and
AU_read.setpos()
methods.
AU_write.
writeframesraw
(
data
)
Write audio frames, without correcting nframes.
AU_write.
writeframes
(
data
)
Write audio frames and make sure nframes is correct.
AU_write.
close
()
20
Make sure nframes is correct, and close the file.
This method is called upon deletion.
Note that it is invalid to set any parameters after calling
writeframes()
or
writeframesraw()
.
index
modules |
next |
previous |
Python » Python v2.7.6 documentation » The Python Standard Library » 21. Multimedia
Services »
© Copyright
1990-2013, Python Software Foundation.
The Python Software Foundation is a non-profit corporation. Please donate.
Last updated on Nov 10, 2013. Found a bug
?
Created using Sphinx
1.0.7.
Documents you may be interested
Documents you may be interested