Various functions in aifc module provide support for reading and writing AIFF (Audio Interchange File Format) and AIFF-C files. AIFF format is for storing digital audio samples in a file. Its newer version AIFF-C has the ability to compress the audio data
Audio file has number of parameters describing the audio data.
The sampling rate or frame rate: number of times per second the sound is sampled.
The number of channels: indicate if the audio is mono, stereo, or quadro.
frame : consists of one sample per channel.
The sample size: size in bytes of each sample.
Thus a frame consists of channels * samplesize bytes. Audio data of 1 sec is channels * samplesize * framerate bytes.
Following functions are defined in aifc module:
aifc.open()
This function opens an AIFF or AIFF-C file and return an object instance for reading/writing audio data depending on mode.It must be 'r' or 'rb' if the file must be opened for reading. It should be 'w' or 'wb' when the file must be opened for writing.
The object with write mode uses following functions
aiff() | Create an AIFF file. |
aifc() | Create an AIFF-C file. |
setnchannels() | Specify the number of channels in the audio file. |
setsampwidth() | Specify the size in bytes of audio samples. |
setframerate() | Specify the sampling frequency in frames per second. |
setnframes() | Specify the number of frames that are to be written to the audio file. |
setcomptype() | Specify the compression type. compression is not possible for AIFF file. compression types supported - b'NONE', b'ULAW', b'ALAW', b'G722'. |
setparams() | Set all the above parameters at once. The argument is a tuple consisting of the various parameters. |
writeframes() | Write data to the output file. T |
writeframesraw() | Like writeframes(), except that the header of the audio file is not updated. |
Following program creates an AIFF file
import aifc, struct sampleRate = 44100.0 # hertz duration = 1.0 # seconds frequency = 440.0 # hertz obj = aifc.open('sound.aiff','w') obj.setnchannels(1) # mono obj.setsampwidth(2) obj.setframerate(sampleRate) for i in range(99999): value = random.randint(-32767, 32767) data = struct.pack('<h', value) obj.writeframesraw( data ) obj.close()
The aiff or aiff-c read object uses following functions
getnchannels() | Return the number of audio channels (1 for mono, 2 for stereo). |
getsampwidth() | Return the size in bytes of individual samples. |
getframerate() | Return the sampling rate (number of audio frames per second). |
getnframes() | Return the number of audio frames in the file. |
getcomptype() | Return a bytes array of length 4 describing the type of compression used in the audio file. |
getparams() | Returns a namedtuple() (nchannels, sampwidth, framerate, nframes, comptype, compname |
readframes() | Read and return the next nframes frames from the audio file. |
setpos(pos) | Seek to the specified frame number. |
These functions are available for readable as well as writable aiff object:
rewind() | Rewind the read pointer. The next readframes() will start from the beginning. |
tell() | Return the current frame number. |
close() | Close the AIFF file. After calling this method, the object can no longer be used. |
Example
Following program reads the attributes of aiff file
import aifc obj = aifc.open('sound.aiff','r') print( "Number of channels",obj.getnchannels()) print ( "Sample width",obj.getsampwidth()) print ( "Frame rate.",obj.getframerate()) print ("Number of frames",obj.getnframes()) print ( "parameters:",obj.getparams()) obj.close()
Output
Number of channels 1 Sample width 2 Frame rate. 44100 Number of frames 99999 parameters: _aifc_params(nchannels=1, sampwidth=2, framerate=44100, nframes=99999, comptype=b'NONE', compname=b'not compressed')