Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Read and write AIFF and AIFC files using Python (aifc)
The aifc module in Python provides functionality for reading and writing AIFF (Audio Interchange File Format) and AIFF-C files. AIFF is a standard format for storing digital audio samples, while AIFF-C is an extended version that supports audio compression.
Audio File Parameters
Audio files contain several key parameters that describe the audio data ?
- Sampling rate (frame rate): Number of times per second the sound is sampled
- Number of channels: Indicates mono (1), stereo (2), or quadro (4) audio
- Frame: Consists of one sample per channel
- Sample size: Size in bytes of each sample
A frame consists of channels × sample size bytes. Audio data for 1 second requires channels × sample size × frame rate bytes.
Opening AIFF Files with aifc.open()
The aifc.open() function opens AIFF or AIFF-C files and returns an object for reading or writing audio data ?
import aifc
# Open for reading
audio_file = aifc.open('sound.aiff', 'r')
# Open for writing
audio_file = aifc.open('sound.aiff', 'w')
Writing AIFF Files
When opening a file in write mode, you can use these methods to configure and write audio data ?
| Method | Description |
|---|---|
aiff() |
Create an AIFF file |
aifc() |
Create an AIFF-C file |
setnchannels() |
Specify number of channels |
setsampwidth() |
Specify sample size in bytes |
setframerate() |
Specify sampling frequency |
setnframes() |
Specify number of frames to write |
writeframes() |
Write audio data to file |
Creating an AIFF File
import aifc
import struct
import random
# Configure audio parameters
sample_rate = 44100.0 # Hz
duration = 1.0 # seconds
frequency = 440.0 # Hz
# Create AIFF file
audio_file = aifc.open('sound.aiff', 'w')
audio_file.setnchannels(1) # mono
audio_file.setsampwidth(2) # 2 bytes per sample
audio_file.setframerate(sample_rate)
# Write audio data
for i in range(44100): # 1 second of audio
value = random.randint(-32767, 32767)
data = struct.pack('<h', value)
audio_file.writeframesraw(data)
audio_file.close()
Reading AIFF Files
When opening a file in read mode, you can use these methods to extract audio information ?
| Method | Description |
|---|---|
getnchannels() |
Return number of audio channels |
getsampwidth() |
Return sample size in bytes |
getframerate() |
Return sampling rate |
getnframes() |
Return number of frames |
getparams() |
Return all parameters as named tuple |
readframes() |
Read specified number of frames |
Reading AIFF File Properties
import aifc
# Open and read AIFF file properties
audio_file = aifc.open('sound.aiff', 'r')
print("Number of channels:", audio_file.getnchannels())
print("Sample width:", audio_file.getsampwidth())
print("Frame rate:", audio_file.getframerate())
print("Number of frames:", audio_file.getnframes())
print("Parameters:", audio_file.getparams())
audio_file.close()
The output would be ?
Number of channels: 1 Sample width: 2 Frame rate: 44100 Number of frames: 44100 Parameters: _aifc_params(nchannels=1, sampwidth=2, framerate=44100, nframes=44100, comptype=b'NONE', compname=b'not compressed')
Common Methods
These methods work with both readable and writable AIFF objects ?
| Method | Description |
|---|---|
rewind() |
Reset read pointer to beginning |
tell() |
Return current frame number |
close() |
Close the AIFF file |
Conclusion
The aifc module provides comprehensive support for AIFF and AIFF-C audio files in Python. Use write mode methods to create audio files with specific parameters, and read mode methods to extract audio properties and data from existing files.
