A module is a Python file that contains Python executable statements and methods; while, a Python function is a group of related statements that are called to perform a specific task. One of the Python modules is the aifc module, which has the getparams()
function. The getparams()
function of the aifc module will form the basis of this shot.
The AIFC module supports the reading and writing of the Audio Interchange File Format, a format for storing digital audio samples in a file. This module helps a programmer to write into and read from an audio file with ease. AIFC files contain digitally sampled sound, like the ones stored in CD-ROMs. An AIFC file, on the other hand, is a compressed AIFF audio file used by media players and gaming consoles. This implies that an AIFC file has a smaller file size compared to same file of AIFC format.
getparams()
functionThe getparams()
function is a function inside the AIFC module. From our definition of a earlier, we understood that a function is a group of statements that perform a specific task. The getparams()
function of the aifc module is a function that returns a namedtuple()
. See the syntax below:
aifc.getparams()
namedtuple()
namedtuple()
contents: (nchannels, sampwidth, framerate, nframes, comptype, compname)
Let’s understand what happens when we call the getparams()
method:
import aifc
aifc_object = aifc.open('beats.aiff','r')
print ( "params:", aifc_object.getparams())
aifc_object.close()
Output => params: _aifc_params(nchannels=2, sampwidth=3, framerate=51200, nframes=88977, comptype=b'NONE', compname=b'not compressed')
Note: We used an imaginary audio file called beats.aiff.
The first line of the code snippet is used to import the aifc module, which houses the getparams()
function. After we have imported the aifc module, we will have access to all functions inside it, including getparams()
(the interest in this shot).
The second code snippet allows us to open and read the content of our audio file (beats.aiff). This creates an instance of the aifc class that makes the getparams()
function accessible to us.
The third line that contains the print statement is used to print the output of the getparams()
function call. We will look into the interesting output of this line of code soon.
We are required to close the file after we are done with the operation we want to perform. This is the function of line 4.
The output of the getparams()
object method is namedtuple()
. To better understand this output, let us consider what a namedtuple()
is.
namedtuple()
namedtuple()
is a Python function inside the collections module. This is used to create tuples with named fields – the items of the tuple can be accessed using the field names and the dot notation.
namedtuple()
makes Python codes more readable by accessing values through field names instead of integer indices, which is difficult to read and understand. In addition to code readability, namedtuple()
helps to write cleaner codes, which are easy to maintain.
Let us delve into the content of the namedtuple()
, which is returned by the getparams.
An audio file has number of parameters describing the audio data as demonstrated in the output of the code snippets above. They are as follows:
Sampling rate or frame rate: This counts how many times a sound is sampled and it is measured per seconds.
Number of channels: There are different types of audio files and this ranges from stereo, mono, quadro, etc. This function of this parameter is to detect and indicate if an audio file is of type stereo, mono, or quadro.
Frame: This consists of one sample per channel.
Sample size: This is the size in bytes of each sample.
A frame is made up of nchannels * samplesize bytes
, and a second’s worth of audio consists of nchannels * samplesize * framerate bytes
.
From the example above, we have a CD quality audio has a sample size of two bytes (16 bits), uses two channels (stereo) and has a frame rate of 51200 frames/second. This gives a frame size of 4 bytes (2*2), and a second’s worth occupies 2*2*51200 bytes (204,800 bytes)
.
Python, as a high-level language, has so many modules that makes the work of developers simple.
File handling is an important operation in every programming language, including python; and, because it is open source, more modules will be added. The aifc module is just one of many wonderful modules in Python. Also, the getparams()
function is one of many functions in the aifc module.
You may want to visit the official
to read more on the Python aifc module. Python docs https://docs.python.org/3/