0% found this document useful (0 votes)
20 views12 pages

Sec 4 - Audio Signal Acquisition - Read&Write Wave - Plot

The document provides an overview of audio processing basics, covering audio formats such as MP3 and WAV, along with key signal parameters like number of channels, sample width, frame rate, and number of frames. It explains the relationship between frames, samples, and channels, and includes Python code examples for reading and processing WAV files using the wave module. Additionally, it poses tasks related to calculating audio file properties and duration.

Uploaded by

amrt6958
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
20 views12 pages

Sec 4 - Audio Signal Acquisition - Read&Write Wave - Plot

The document provides an overview of audio processing basics, covering audio formats such as MP3 and WAV, along with key signal parameters like number of channels, sample width, frame rate, and number of frames. It explains the relationship between frames, samples, and channels, and includes Python code examples for reading and processing WAV files using the wave module. Additionally, it poses tasks related to calculating audio file properties and duration.

Uploaded by

amrt6958
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 12

Speech recognition

Audio signal acquisition


Sec 4
Audio processing basics

• Audio formats
• Signal parameters
• Wave module
• Plot waveform
• Microphone recording
• Load mp3
Audio processing basics - Audio formats
MP3 (not hi-res):
• Popular,
• lossy compressed format
• ensures small file size but is far from the best sound quality.
• Convenient for storing music on phones and iPods.

WAV (hi-res):
• The standard format in which all CDs are encoded.
• Great sound quality but it's uncompressed, meaning huge file sizes
(especially for hi-res files).
• It has poor metadata support (that is, album artwork, artist and song
title information).
Audio processing basics - Signal parameters

Audio signal parameters


- number of channels Number of
Type Description
- Sample width Channels
- framrate/ sample_rate, e.g.44100 Hz 1 Mono Single channel, same sound in both ears

- Number of frames 2 Stereo Left & Right channels, spatial sound

- Values of a frame import wave


obj =wave.open("example_WAV.wav",'rb’)
print("Number of channels ",obj.getnchannels())
Audio processing basics - Signal parameters

Audio signal parameters . Sample Width?


•Measured in bytes (not bits).
- number of channels •Common values: 1 byte (8-bit), 2 bytes (16-bit), 3
- Sample width bytes (24-bit), 4 bytes (32-bit).
- framrate/ sample_rate, e.g.44100 Hz Example:
- Number of frames If sample width = 2, then each sample is 2 bytes (16-bit
PCM audio).
- Values of a frame import wave
obj =wave.open("example_WAV.wav",'rb’)
print(" sample width ",obj.getsampwidth())
Audio processing basics - Signal parameters

Audio signal parameters Frame Rate (Sample Rate)?


•Measured in Hz (samples per second).
- number of channels
- Sample width Example:
If frame rate = 44,100 Hz, then 44,100 samples per
- framrate/ sample_rate, e.g.44100 Hz second are recorded for each channel.
- Number of frames
- Values of a frame import wave
obj =wave.open("example_WAV.wav",'rb’)
print(" Frame rate ",obj.getframerate())
Audio processing basics - Signal parameters

Audio signal parameters number of frames in an audio file


• represents the total number of frames stored in the
- number of channels file.
- Sample width • A frame consists of one sample per channel (e.g.,
left & right for stereo).
- framrate/ sample_rate, e.g.44100 Hz
- Number of frames • The total number of frames tells us how many times
the sound was recorded over time.
- Values of a frame import wave
obj =wave.open("example_WAV.wav",'rb’)
print(" Number of frames ",obj.getnframes())
Audio processing basics - Signal parameters

Audio signal parameters the Values of a Frame in an Audio File?


• Each frame in an audio file contains one
- number of channels
sample per channel.
- Sample width • These samples represent the amplitude of the
- framrate/ sample_rate, e.g.44100 Hz sound at a specific time.
• The values of a frame are the actual binary
- Number of frames data stored in the audio file.
- Values of a frame import wave

obj =wave.open("example_WAV.wav",'rb’)
frames=obj.readframes(-1)
Audio processing basics - Signal parameters

Relationship Between Frames, Samples, and Channels


Term Meaning
Frame Contains one sample per channel
Frame Rate Number of frames per second (Hz)
Number of Frames Total frames in the entire audio file
Total Samples Number of Frames × Number of Channels
Audio processing basics - Wave module
wave.open(file, mode)
mode=‘ rb’ →Read only mode.
mode= 'wb’ →Write only mode
import wave
obj =wave.open("example_WAV.wav",'rb')
print("parameters", obj.getparams())
print("Number of channels ",obj.getnchannels())
print("sample width ",obj.getsampwidth())
print("Frame rate ",obj.getframerate())
print("Number of frames ",obj.getnframes())

https://docs.python.org/3/library/wave.html
Audio processing basics - Wave module
readframes(n) :
•If n is positive, it reads up to n frames.
•If n is larger than the available frames, it reads only what's available.
•If n is -1 or omitted, it reads all remaining frames.
frames=obj.readframes(-1)
print(len(frames))
obj.close()
obj_new=wave.open("example_WAV_new.wav","wb")
obj_new.setnchannels(2)
obj_new.setsampwidth(2)
obj_new.setframerate(44100)
obj_new.writeframes(frames) Why is len(frames) ≠ obj.getnframes()?
obj_new.close()
Audio processing basics – task

• Why is len(frames) ≠ obj.getnframes()?

• Write a python code for reading wave file, then display it’s properties and
calculate the time duration of your file.

• For a stereo, 16-bit, 44.1 kHz WAV file, determine number of frames in one
second and the total size of the audio file.

You might also like