File size: 8,982 Bytes
7d4358f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
30082c2
7d4358f
 
 
 
 
 
 
 
 
 
 
 
 
 
30082c2
 
7d4358f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
30082c2
7d4358f
 
30082c2
7d4358f
 
 
 
 
 
 
 
30082c2
 
7d4358f
 
30082c2
7d4358f
 
 
 
30082c2
7d4358f
 
 
30082c2
7d4358f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
import matplotlib.pyplot as plt
import matplotlib.gridspec as gridspec
import librosa.display
import numpy as np


def create_report(audio_data, output_path):
    """
    Create a complete forensic PNG report.
    Synthetic detection is informational only.
    """

    plt.style.use("default")

    fig = plt.figure(figsize=(22, 16))
    fig.patch.set_facecolor("white")

    fig.suptitle(
        f"AUDIO FORENSIC ANALYSIS REPORT\n{audio_data['filename']}",
        fontsize=20,
        fontweight="bold",
        y=0.97
    )

    gs = gridspec.GridSpec(
        5, 4,
        figure=fig,
        hspace=0.45,
        wspace=0.4,
        height_ratios=[1.5, 1, 0.8, 0.9, 0.7],
        left=0.05,
        right=0.95,
        top=0.92,
        bottom=0.05
    )

    # ============================================================
    # 1. SPECTROGRAM PANEL
    # ============================================================

    ax_spec = fig.add_subplot(gs[0, :])

    S_db = audio_data["spectral"]["S_db"]
    sr = audio_data["info"]["samplerate"]
    hop = audio_data["spectral"]["hop_length"]

    img = librosa.display.specshow(
        S_db,
        sr=sr,
        hop_length=hop,
        y_axis="hz",
        x_axis="time",
        cmap="viridis",
        ax=ax_spec,
        vmin=-80,
        vmax=0
    )

    ax_spec.set_title("Spectrogram", fontsize=14, fontweight="bold", pad=10)
    ax_spec.grid(True, alpha=0.3, linestyle="--")
    cbar = plt.colorbar(img, ax=ax_spec, pad=0.01)
    cbar.set_label("Magnitude (dB)", fontsize=10, fontweight="bold")

    # ============================================================
    # 2. FILE INFORMATION PANEL
    # ============================================================

    ax_info = fig.add_subplot(gs[1, 0:2])
    ax_info.axis("off")

    info = audio_data["info"]
    t = audio_data["time_stats"]

    lines = [
        "FILE INFORMATION",
        "─" * 50,
        f"Sample Rate:     {info['samplerate']:,} Hz",
        f"Channels:        {info['channels']}",
        f"Duration:        {info['duration']:.2f} sec",
        f"Format:          {info['format']} ({info['subtype']})",
        f"Frames:          {info['frames']:,}",
        "",
        "TIME ANALYSIS",
        "─" * 50,
        f"Peak:            {t['peak_db']:.2f} dBFS ({t['peak']:.6f})",
        f"RMS:             {t['rms_db']:.2f} dBFS ({t['rms']:.6f})",
        f"Crest Factor:    {t['crest_factor_db']:.2f} dB",
        f"Noise Floor:     {t['noise_floor']:.6f}",
        f"Est. SNR:        {t['snr_db']:.1f} dB",
        f"Zero Cross Rate: {t['zero_crossing_rate']:.4f}",
    ]

    if audio_data.get("lufs") is not None:
        lines += [
            "",
            "LOUDNESS",
            "─" * 50,
            f"Integrated LUFS: {audio_data['lufs']:.2f}"
        ]

    ax_info.text(
        0.05, 0.95,
        "\n".join(lines),
        fontsize=10.8,
        va="top",
        family="monospace",
        bbox=dict(
            boxstyle="round,pad=1",
            facecolor="#E8F4F8",
            edgecolor="#0077BE",
            linewidth=2
        )
    )

    # ============================================================
    # 3. SPECTRAL STATS PANEL
    # ============================================================

    ax_specstats = fig.add_subplot(gs[1, 2:4])
    ax_specstats.axis("off")

    spec = audio_data["spectral"]
    e = spec["energy_distribution"]

    text = [
        "SPECTRAL ANALYSIS",
        "─" * 50,
        f"Centroid:        {spec['spectral_centroid']:.1f} Hz",
        f"Bandwidth:       {spec['spectral_bandwidth']:.1f} Hz",
        f"Flatness:        {spec['spectral_flatness']:.4f}",
        f"Rolloff Mean:    {spec['spectral_rolloff']:.1f} Hz",
        "",
        "ROLLOFF POINTS",
        "─" * 50,
        f"85% Energy:      {spec['rolloff_85pct']:.1f} Hz",
        f"95% Energy:      {spec['rolloff_95pct']:.1f} Hz",
        f"Highest -60 dB:  {spec['highest_freq_minus60db']:.1f} Hz",
        "",
        "ENERGY DISTRIBUTION",
        "─" * 50,
        f"< 100 Hz:        {e['below_100hz']:.2f}%",
        f"100–500 Hz:      {e['100_500hz']:.2f}%",
        f"500–2k Hz:       {e['500_2khz']:.2f}%",
        f"2k–8k Hz:        {e['2k_8khz']:.2f}%",
        f"8k–12k Hz:       {e['8k_12khz']:.2f}%",
        f"12k–16k Hz:      {e['12k_16khz']:.2f}%",
        f"> 16k Hz:        {e['above_16khz']:.2f}%",
    ]

    ax_specstats.text(
        0.05, 0.95,
        "\n".join(text),
        fontsize=10.8,
        va="top",
        family="monospace",
        bbox=dict(
            boxstyle="round,pad=1",
            facecolor="#FFF4E6",
            edgecolor="#FF8C00",
            linewidth=2
        )
    )

    # ============================================================
    # 4. ENERGY BAR CHART
    # ============================================================

    ax_bar = fig.add_subplot(gs[2, :])

    bands = [
        "<100Hz", "100–500Hz", "500–2kHz",
        "2k–8kHz", "8k–12kHz", "12k–16kHz", ">16kHz"
    ]

    vals = [
        e["below_100hz"], e["100_500hz"], e["500_2khz"],
        e["2k_8khz"], e["8k_12khz"], e["12k_16khz"], e["above_16khz"]
    ]

    colors = ["#2C3E50", "#E74C3C", "#E67E22",
              "#F39C12", "#2ECC71", "#3498DB", "#9B59B6"]

    bars = ax_bar.bar(bands, vals, color=colors, edgecolor="black", alpha=0.85)

    ax_bar.set_ylabel("Energy (%)", fontsize=12, fontweight="bold")
    ax_bar.grid(axis="y", alpha=0.35, linestyle="--")

    for b, v in zip(bars, vals):
        ax_bar.text(b.get_x() + b.get_width()/2, v + 0.3, f"{v:.2f}%", ha="center", fontsize=10)

    # ============================================================
    # 5. ISSUES PANEL
    # ============================================================

    ax_issues = fig.add_subplot(gs[3, 0:3])
    ax_issues.axis("off")

    issues = audio_data["issues"]

    issue_lines = ["DETECTED ISSUES", "═" * 80]

    if not issues:
        issue_lines.append("βœ… No significant issues detected.")
    else:
        icons = {
            "CRITICAL": "πŸ”΄",
            "HIGH": "🟠",
            "MEDIUM": "🟑",
            "LOW": "🟒"
        }
        for issue, sev, desc in issues:
            issue_lines.append(f"{icons.get(sev,'βšͺ')} [{sev}] {issue}")
            issue_lines.append(f"   β†’ {desc}")

    if spec["spectral_notches"]:
        issue_lines += [
            "",
            f"🎡 Spectral Notches: {len(spec['spectral_notches'])}",
        ]
        for i, n in enumerate(spec["spectral_notches"][:5], 1):
            issue_lines.append(f"  {i}. {n['freq']:.1f} Hz (Depth {n['depth_db']:.1f} dB)")

    ax_issues.text(
        0.05, 0.95,
        "\n".join(issue_lines),
        fontsize=10.8,
        va="top",
        family="monospace",
        bbox=dict(
            boxstyle="round,pad=1.2",
            facecolor="#FFE6E6",
            edgecolor="#DC143C",
            linewidth=2
        )
    )

    # ============================================================
    # 6. QUALITY SCORE PANEL + SYNTHETIC BLOCK
    # ============================================================

    ax_score = fig.add_subplot(gs[3, 3])
    ax_score.axis("off")

    s = audio_data["score"]
    syn = audio_data["synthetic"]

    score_lines = [
        "QUALITY ASSESSMENT",
        "═" * 28,
        f"SCORE: {s['score']}/100",
        f"GRADE: {s['grade']}",
        f"QUALITY: {s['quality']}",
        "",
        "RECOMMENDATION:",
        s["recommendation"],
        "",
        "CLEANLINESS SCORE:",
        f"{s['cleanliness_score']}/100",
        "",
        "PROCESSING SEVERITY:",
        f"{s['processing_severity']}",
        "",
        "ISSUE SUMMARY",
        "─" * 28,
        f"Critical: {s['critical']}",
        f"High:     {s['high']}",
        f"Medium:   {s['medium']}",
        f"Low:      {s['low']}",
    ]

    score_lines += [
        "",
        "━━━━━━━━━━━━━━━━━━━━━━━",
        "     SYNTHETIC VOICE",
        "━━━━━━━━━━━━━━━━━━━━━━━",
        f"Probability : {syn['synthetic_probability']:.2f}",
        f"Label       : {syn['synthetic_label']}",
        "━━━━━━━━━━━━━━━━━━━━━━━",
        "",
        f"Generated: {audio_data['timestamp']}"
    ]

    ax_score.text(
        0.5, 0.5,
        "\n".join(score_lines),
        fontsize=11,
        ha="center",
        va="center",
        family="monospace",
        bbox=dict(
            boxstyle="round,pad=1.4",
            facecolor=s["color"],
            edgecolor="black",
            linewidth=3,
            alpha=0.70
        )
    )

    # ============================================================
    # SAVE REPORT
    # ============================================================

    plt.savefig(output_path, dpi=300, bbox_inches="tight")
    plt.close()

    return output_path