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
| 8 |
+
"""
|
| 9 |
+
Create a complete forensic PNG report.
|
| 10 |
+
Synthetic detection is informational only.
|
| 11 |
+
"""
|
| 12 |
+
|
| 13 |
+
plt.style.use("default")
|
| 14 |
+
|
| 15 |
+
fig = plt.figure(figsize=(22, 16))
|
| 16 |
+
fig.patch.set_facecolor("white")
|
| 17 |
+
|
| 18 |
+
fig.suptitle(
|
| 19 |
+
f"AUDIO FORENSIC ANALYSIS REPORT\n{audio_data['filename']}",
|
| 20 |
+
fontsize=20,
|
| 21 |
+
fontweight="bold",
|
| 22 |
+
y=0.97
|
| 23 |
+
)
|
| 24 |
+
|
| 25 |
+
gs = gridspec.GridSpec(
|
| 26 |
+
5, 4,
|
| 27 |
+
figure=fig,
|
| 28 |
+
hspace=0.45,
|
| 29 |
+
wspace=0.4,
|
| 30 |
+
height_ratios=[1.5, 1, 0.8, 0.9, 0.7],
|
| 31 |
+
left=0.05,
|
| 32 |
+
right=0.95,
|
| 33 |
+
top=0.92,
|
| 34 |
+
bottom=0.05
|
| 35 |
+
)
|
| 36 |
+
|
| 37 |
+
# ============================================================
|
| 38 |
+
# 1. SPECTROGRAM PANEL
|
| 39 |
+
# ============================================================
|
| 40 |
+
|
| 41 |
+
ax_spec = fig.add_subplot(gs[0, :])
|
| 42 |
+
|
| 43 |
+
S_db = audio_data["spectral"]["S_db"]
|
| 44 |
+
sr = audio_data["info"]["samplerate"]
|
| 45 |
+
hop = audio_data["spectral"]["hop_length"]
|
| 46 |
+
|
| 47 |
+
img = librosa.display.specshow(
|
| 48 |
+
S_db,
|
| 49 |
+
sr=sr,
|
| 50 |
+
hop_length=hop,
|
| 51 |
+
y_axis="hz",
|
| 52 |
+
x_axis="time",
|
| 53 |
+
cmap="viridis",
|
| 54 |
+
ax=ax_spec,
|
| 55 |
+
vmin=-80,
|
| 56 |
+
vmax=0
|
| 57 |
+
)
|
| 58 |
+
|
| 59 |
+
ax_spec.set_title("Spectrogram", fontsize=14, fontweight="bold", pad=10)
|
| 60 |
+
ax_spec.grid(True, alpha=0.3, linestyle="--")
|
| 61 |
+
cbar = plt.colorbar(img, ax=ax_spec, pad=0.01)
|
| 62 |
+
cbar.set_label("Magnitude (dB)", fontsize=10, fontweight="bold")
|
| 63 |
+
|
| 64 |
+
# ============================================================
|
| 65 |
+
# 2. FILE INFORMATION PANEL
|
| 66 |
+
# ============================================================
|
| 67 |
+
|
| 68 |
+
ax_info = fig.add_subplot(gs[1, 0:2])
|
| 69 |
+
ax_info.axis("off")
|
| 70 |
+
|
| 71 |
+
info = audio_data["info"]
|
| 72 |
+
t = audio_data["time_stats"]
|
| 73 |
+
|
| 74 |
+
lines = [
|
| 75 |
+
"FILE INFORMATION",
|
| 76 |
+
"β" * 50,
|
| 77 |
+
f"Sample Rate: {info['samplerate']:,} Hz",
|
| 78 |
+
f"Channels: {info['channels']}",
|
| 79 |
+
f"Duration: {info['duration']:.2f} sec",
|
| 80 |
+
f"Format: {info['format']} ({info['subtype']})",
|
| 81 |
+
f"Frames: {info['frames']:,}",
|
| 82 |
+
"",
|
| 83 |
+
"TIME ANALYSIS",
|
| 84 |
+
"β" * 50,
|
| 85 |
+
f"Peak: {t['peak_db']:.2f} dBFS ({t['peak']:.6f})",
|
| 86 |
+
f"RMS: {t['rms_db']:.2f} dBFS ({t['rms']:.6f})",
|
| 87 |
+
f"Crest Factor: {t['crest_factor_db']:.2f} dB",
|
| 88 |
+
f"Noise Floor: {t['noise_floor']:.6f}",
|
| 89 |
+
f"Est. SNR: {t['snr_db']:.1f} dB",
|
| 90 |
+
f"Zero Cross Rate: {t['zero_crossing_rate']:.4f}",
|
| 91 |
+
]
|
| 92 |
+
|
| 93 |
+
if audio_data.get("lufs") is not None:
|
| 94 |
+
lines += [
|
| 95 |
+
"",
|
| 96 |
+
"LOUDNESS",
|
| 97 |
+
"β" * 50,
|
| 98 |
+
f"Integrated LUFS: {audio_data['lufs']:.2f}"
|
| 99 |
+
]
|
| 100 |
+
|
| 101 |
+
ax_info.text(
|
| 102 |
+
0.05, 0.95,
|
| 103 |
+
"\n".join(lines),
|
| 104 |
+
fontsize=10.8,
|
| 105 |
+
va="top",
|
| 106 |
+
family="monospace",
|
| 107 |
+
bbox=dict(
|
| 108 |
+
boxstyle="round,pad=1",
|
| 109 |
+
facecolor="#E8F4F8",
|
| 110 |
+
edgecolor="#0077BE",
|
| 111 |
+
linewidth=2
|
| 112 |
+
)
|
| 113 |
+
)
|
| 114 |
+
|
| 115 |
+
# ============================================================
|
| 116 |
+
# 3. SPECTRAL STATS PANEL
|
| 117 |
+
# ============================================================
|
| 118 |
+
|
| 119 |
+
ax_specstats = fig.add_subplot(gs[1, 2:4])
|
| 120 |
+
ax_specstats.axis("off")
|
| 121 |
+
|
| 122 |
+
spec = audio_data["spectral"]
|
| 123 |
+
e = spec["energy_distribution"]
|
| 124 |
+
|
| 125 |
+
text = [
|
| 126 |
+
"SPECTRAL ANALYSIS",
|
| 127 |
+
"β" * 50,
|
| 128 |
+
f"Centroid: {spec['spectral_centroid']:.1f} Hz",
|
| 129 |
+
f"Bandwidth: {spec['spectral_bandwidth']:.1f} Hz",
|
| 130 |
+
f"Flatness: {spec['spectral_flatness']:.4f}",
|
| 131 |
+
f"Rolloff Mean: {spec['spectral_rolloff']:.1f} Hz",
|
| 132 |
+
"",
|
| 133 |
+
"ROLLOFF POINTS",
|
| 134 |
+
"β" * 50,
|
| 135 |
+
f"85% Energy: {spec['rolloff_85pct']:.1f} Hz",
|
| 136 |
+
f"95% Energy: {spec['rolloff_95pct']:.1f} Hz",
|
| 137 |
+
f"Highest -60 dB: {spec['highest_freq_minus60db']:.1f} Hz",
|
| 138 |
+
"",
|
| 139 |
+
"ENERGY DISTRIBUTION",
|
| 140 |
+
"β" * 50,
|
| 141 |
+
f"< 100 Hz: {e['below_100hz']:.2f}%",
|
| 142 |
+
f"100β500 Hz: {e['100_500hz']:.2f}%",
|
| 143 |
+
f"500β2k Hz: {e['500_2khz']:.2f}%",
|
| 144 |
+
f"2kβ8k Hz: {e['2k_8khz']:.2f}%",
|
| 145 |
+
f"8kβ12k Hz: {e['8k_12khz']:.2f}%",
|
| 146 |
+
f"12kβ16k Hz: {e['12k_16khz']:.2f}%",
|
| 147 |
+
f"> 16k Hz: {e['above_16khz']:.2f}%",
|
| 148 |
+
]
|
| 149 |
+
|
| 150 |
+
ax_specstats.text(
|
| 151 |
+
0.05, 0.95,
|
| 152 |
+
"\n".join(text),
|
| 153 |
+
fontsize=10.8,
|
| 154 |
+
va="top",
|
| 155 |
+
family="monospace",
|
| 156 |
+
bbox=dict(
|
| 157 |
+
boxstyle="round,pad=1",
|
| 158 |
+
facecolor="#FFF4E6",
|
| 159 |
+
edgecolor="#FF8C00",
|
| 160 |
+
linewidth=2
|
| 161 |
+
)
|
| 162 |
+
)
|
| 163 |
+
|
| 164 |
+
# ============================================================
|
| 165 |
+
# 4. ENERGY DISTRIBUTION BARS
|
| 166 |
+
# ============================================================
|
| 167 |
+
|
| 168 |
+
ax_bar = fig.add_subplot(gs[2, :])
|
| 169 |
+
|
| 170 |
+
bands = [
|
| 171 |
+
"<100Hz", "100β500Hz", "500β2kHz",
|
| 172 |
+
"2kβ8kHz", "8kβ12kHz", "12kβ16kHz", ">16kHz"
|
| 173 |
+
]
|
| 174 |
+
|
| 175 |
+
vals = [
|
| 176 |
+
e["below_100hz"], e["100_500hz"], e["500_2khz"],
|
| 177 |
+
e["2k_8khz"], e["8k_12khz"], e["12k_16khz"], e["above_16khz"]
|
| 178 |
+
]
|
| 179 |
+
|
| 180 |
+
colors = ["#2C3E50", "#E74C3C", "#E67E22", "#F39C12", "#2ECC71", "#3498DB", "#9B59B6"]
|
| 181 |
+
|
| 182 |
+
bars = ax_bar.bar(bands, vals, color=colors, edgecolor="black", alpha=0.85)
|
| 183 |
+
|
| 184 |
+
ax_bar.set_ylabel("Energy (%)", fontsize=12, fontweight="bold")
|
| 185 |
+
ax_bar.grid(axis="y", alpha=0.35, linestyle="--")
|
| 186 |
+
|
| 187 |
+
for b, v in zip(bars, vals):
|
| 188 |
+
ax_bar.text(b.get_x() + b.get_width()/2, v + 0.3, f"{v:.2f}%", ha="center", fontsize=10)
|
| 189 |
+
|
| 190 |
+
# ============================================================
|
| 191 |
+
# 5. ISSUES PANEL
|
| 192 |
+
# ============================================================
|
| 193 |
+
|
| 194 |
+
ax_issues = fig.add_subplot(gs[3, 0:3])
|
| 195 |
+
ax_issues.axis("off")
|
| 196 |
+
|
| 197 |
+
issues = audio_data["issues"]
|
| 198 |
+
|
| 199 |
+
lines = ["DETECTED ISSUES", "β" * 80]
|
| 200 |
+
|
| 201 |
+
if not issues:
|
| 202 |
+
lines.append("β
No significant issues detected.")
|
| 203 |
+
else:
|
| 204 |
+
icons = {
|
| 205 |
+
"CRITICAL": "π΄",
|
| 206 |
+
"HIGH": "π ",
|
| 207 |
+
"MEDIUM": "π‘",
|
| 208 |
+
"LOW": "π’"
|
| 209 |
+
}
|
| 210 |
+
for issue, sev, desc in issues:
|
| 211 |
+
lines.append(f"{icons.get(sev,'βͺ')} [{sev}] {issue}")
|
| 212 |
+
lines.append(f" β {desc}")
|
| 213 |
+
|
| 214 |
+
if spec["spectral_notches"]:
|
| 215 |
+
lines += [
|
| 216 |
+
"",
|
| 217 |
+
f"π΅ Spectral Notches: {len(spec['spectral_notches'])}",
|
| 218 |
+
]
|
| 219 |
+
for i, n in enumerate(spec["spectral_notches"][:5], 1):
|
| 220 |
+
lines.append(f" {i}. {n['freq']:.1f} Hz (Depth {n['depth_db']:.1f} dB)")
|
| 221 |
+
|
| 222 |
+
ax_issues.text(
|
| 223 |
+
0.05, 0.95,
|
| 224 |
+
"\n".join(lines),
|
| 225 |
+
fontsize=10.8,
|
| 226 |
+
va="top",
|
| 227 |
+
family="monospace",
|
| 228 |
+
bbox=dict(
|
| 229 |
+
boxstyle="round,pad=1.2",
|
| 230 |
+
facecolor="#FFE6E6",
|
| 231 |
+
edgecolor="#DC143C",
|
| 232 |
+
linewidth=2
|
| 233 |
+
)
|
| 234 |
+
)
|
| 235 |
+
|
| 236 |
+
# ============================================================
|
| 237 |
+
# 6. QUALITY SCORE PANEL + SYNTHETIC BLOCK
|
| 238 |
+
# ============================================================
|
| 239 |
+
|
| 240 |
+
ax_score = fig.add_subplot(gs[3, 3])
|
| 241 |
+
ax_score.axis("off")
|
| 242 |
+
|
| 243 |
+
s = audio_data["score"]
|
| 244 |
+
syn = audio_data["synthetic"]
|
| 245 |
+
|
| 246 |
+
score_lines = [
|
| 247 |
+
"QUALITY ASSESSMENT",
|
| 248 |
+
"β" * 28,
|
| 249 |
+
f"SCORE: {s['score']}/100",
|
| 250 |
+
f"GRADE: {s['grade']}",
|
| 251 |
+
f"QUALITY: {s['quality']}",
|
| 252 |
+
"",
|
| 253 |
+
"RECOMMENDATION:",
|
| 254 |
+
s["recommendation"],
|
| 255 |
+
"",
|
| 256 |
+
"CLEANLINESS SCORE:",
|
| 257 |
+
f"{s['cleanliness_score']}/100",
|
| 258 |
+
"",
|
| 259 |
+
"PROCESSING SEVERITY:",
|
| 260 |
+
f"{s['processing_severity']}",
|
| 261 |
+
"",
|
| 262 |
+
"ISSUE SUMMARY",
|
| 263 |
+
"β" * 28,
|
| 264 |
+
f"Critical: {s['critical']}",
|
| 265 |
+
f"High: {s['high']}",
|
| 266 |
+
f"Medium: {s['medium']}",
|
| 267 |
+
f"Low: {s['low']}",
|
| 268 |
+
]
|
| 269 |
+
|
| 270 |
+
# Add synthetic visual block (your Option 3)
|
| 271 |
+
score_lines += [
|
| 272 |
+
"",
|
| 273 |
+
"βββββββββββββββββββββββ",
|
| 274 |
+
" SYNTHETIC VOICE",
|
| 275 |
+
"βββββββββββββββββββββββ",
|
| 276 |
+
f"Probability : {syn['synthetic_probability']:.2f}",
|
| 277 |
+
f"Label : {syn['synthetic_label']}",
|
| 278 |
+
"βββββββββββββββββββββββ",
|
| 279 |
+
"",
|
| 280 |
+
f"Generated: {audio_data['timestamp']}"
|
| 281 |
+
]
|
| 282 |
+
|
| 283 |
+
ax_score.text(
|
| 284 |
+
0.5, 0.5,
|
| 285 |
+
"\n".join(score_lines),
|
| 286 |
+
fontsize=11,
|
| 287 |
+
ha="center",
|
| 288 |
+
va="center",
|
| 289 |
+
family="monospace",
|
| 290 |
+
bbox=dict(
|
| 291 |
+
boxstyle="round,pad=1.4",
|
| 292 |
+
facecolor=s["color"],
|
| 293 |
+
edgecolor="black",
|
| 294 |
+
linewidth=3,
|
| 295 |
+
alpha=0.70
|
| 296 |
+
)
|
| 297 |
+
)
|
| 298 |
+
|
| 299 |
+
# ============================================================
|
| 300 |
+
# SAVE REPORT
|
| 301 |
+
# ============================================================
|
| 302 |
+
|
| 303 |
+
plt.savefig(output_path, dpi=300, bbox_inches="tight")
|
| 304 |
+
plt.close()
|
| 305 |
+
|
| 306 |
+
return output_path
|