AudioHandling
ipynb4 kBPython · 5 cellsSolution of TP 2: Audio and video handling
Audio Handling (Using SciPy)
For handling standard uncompressed audio (like WAV files), the scipy.io.wavfile module is simple and effective for a conceptual course.
import numpy as np
from scipy.io import wavfile
import matplotlib.pyplot as plt1. Reading and Seeing Audio Data
Audio data is a sequence of samples over time.
def analyze_audio(audio_path):
# 1. Read the audio file
# rate: Sampling rate (samples/second, e.g., 44100 Hz)
# data: The actual audio samples (a NumPy array)
try:
rate, data = wavfile.read(audio_path)
except FileNotFoundError:
print("Error: Could not find audio file.")
return
print(f"--- Audio Metadata ---")
print(f"Sampling Rate (Hz): {rate}")
# Check if stereo (2 channels) or mono (1 channel)
if data.ndim > 1:
channels = data.shape[1]
print(f"Number of Channels: {channels}")
# Use the first channel for plotting if stereo
audio_data = data[:, 0]
else:
channels = 1
print(f"Number of Channels: {channels}")
audio_data = data
# 2. Inspect the data (e.g., the first 10 samples)
print(f"Total number of samples: {len(audio_data)}")
print(
f"Data type (Bit Depth): {audio_data.dtype} (e.g., int16 means 16-bit resolution)"
)
print(f"First 10 samples: {audio_data[:10]}")
# 3. Visualize the signal (in the Spatial/Time Domain)
# Create a time vector
time = np.linspace(0.0, len(audio_data) / rate, len(audio_data))
plt.figure(figsize=(10, 4))
plt.plot(time, audio_data)
plt.title(f"Audio Signal (Time Domain - {channels} Channel(s))")
plt.xlabel("Time (seconds)")
plt.ylabel("Amplitude (Sample Value)")
plt.show()
# Example Usage: analyze_audio('path/to/your/audio.wav')Key Audio Concepts:
- Sampling Rate (rate): How many samples (digital amplitude values) are taken per second.
- Amplitude/Bit Depth (data.dtype): The resolution of each sample (e.g., 8-bit, 16-bit). This relates directly to the maximum and minimum sample values (e.g., ±32767 for 16-bit signed integers).
- Time Domain: The plot shows amplitude (y-axis) vs. time (x-axis), representing the sound wave.