File size: 3,005 Bytes
f535f66 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 | """Before/after waveform and spectrum comparison plots."""
import numpy as np
import matplotlib
matplotlib.use("Agg")
import matplotlib.pyplot as plt
def _to_mono(audio):
"""Collapse to mono for plotting."""
if audio.ndim > 1 and audio.shape[1] > 1:
return audio.mean(axis=1)
return audio.ravel()
def _downsample_for_plot(signal, time, max_points=500_000):
"""Reduce sample count so matplotlib stays responsive."""
if len(signal) > max_points:
step = len(signal) // max_points
return signal[::step], time[::step]
return signal, time
def plot_waveform_comparison(original, mastered, sample_rate):
"""Create a stacked before/after waveform plot.
Returns a matplotlib Figure.
"""
fig, axes = plt.subplots(2, 1, figsize=(8, 4), sharex=True)
duration = len(original) / sample_rate
time_o = np.linspace(0, duration, len(original))
time_m = np.linspace(0, duration, len(mastered))
orig_mono = _to_mono(original)
mast_mono = _to_mono(mastered)
orig_mono, time_o = _downsample_for_plot(orig_mono, time_o)
mast_mono, time_m = _downsample_for_plot(mast_mono, time_m)
axes[0].plot(time_o, orig_mono, color="#4a90d9", linewidth=0.3)
axes[0].set_ylabel("Amplitude")
axes[0].set_title("Original")
axes[0].set_ylim(-1.05, 1.05)
axes[1].plot(time_m, mast_mono, color="#d94a4a", linewidth=0.3)
axes[1].set_ylabel("Amplitude")
axes[1].set_title("Mastered")
axes[1].set_xlabel("Time (seconds)")
axes[1].set_ylim(-1.05, 1.05)
plt.tight_layout()
return fig
def plot_spectrum_comparison(original, mastered, sample_rate):
"""Create an overlaid frequency spectrum comparison.
Returns a matplotlib Figure.
"""
fig, ax = plt.subplots(1, 1, figsize=(8, 3))
orig_mono = _to_mono(original)
mast_mono = _to_mono(mastered)
n_fft = 8192
def avg_spectrum(signal, n_fft, sr):
hop = n_fft // 2
n_windows = max(1, (len(signal) - n_fft) // hop)
spectra = []
for i in range(min(n_windows, 100)):
start = i * hop
window = signal[start : start + n_fft] * np.hanning(n_fft)
spectrum = np.abs(np.fft.rfft(window))
spectra.append(spectrum)
avg = np.mean(spectra, axis=0)
freqs = np.fft.rfftfreq(n_fft, 1.0 / sr)
avg_db = 20.0 * np.log10(avg + 1e-10)
return freqs, avg_db
freqs_o, spec_o = avg_spectrum(orig_mono, n_fft, sample_rate)
freqs_m, spec_m = avg_spectrum(mast_mono, n_fft, sample_rate)
ax.plot(freqs_o, spec_o, color="#4a90d9", alpha=0.7, linewidth=1, label="Original")
ax.plot(freqs_m, spec_m, color="#d94a4a", alpha=0.7, linewidth=1, label="Mastered")
ax.set_xscale("log")
ax.set_xlim(20, sample_rate / 2)
ax.set_xlabel("Frequency (Hz)")
ax.set_ylabel("Magnitude (dB)")
ax.set_title("Frequency Spectrum Comparison")
ax.legend()
ax.grid(True, alpha=0.3)
plt.tight_layout()
return fig
|