Repository navigation
Expand file tree
/
Copy pathapp.py
More file actions
1846 lines (1619 loc) · 74.9 KB
/
Copy pathapp.py
File metadata and controls
1846 lines (1619 loc) · 74.9 KB
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
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
"""Audio Explorers 2026 — Full Pipeline App.
Two modes:
- Demo Mode: full pipeline walkthrough (DoA → beamforming → transcribe → gender)
- Experiment Mode: run preprocessing A/B tests on raw mixtures
Run with: streamlit run app.py
"""
from __future__ import annotations
import io
import os
import pickle
import tempfile
from pathlib import Path
import numpy as np
import pandas as pd
import streamlit as st
import matplotlib.pyplot as plt
import librosa
import librosa.display
import soundfile as sf
from src.audio_io import load_mixture
from src.preprocessing import preprocess_enhanced, preprocess_for_ml, preprocess_for_spatial
from src.doa_estimation import estimate_doa, estimate_speaker_directions
from src.post_processing import apply_post_processing_chain
from src.target_enhancement import enhance_target, enhance_all_directions, bilateral_enhance, bilateral_enhance_all, render_binaural, enhance_target_speaker_only, determine_talker_of_interest, _HAS_WPE
from src.transcription import transcribe_detailed
from src.gender_classification import classify_gender_detailed
from src.speaker_diarization import detect_dialogue_pattern, split_transcript_by_speaker, diarize_enhanced, estimate_own_voice_segments, extract_target_speaker
from src.experiment import (
create_audio_versions,
run_experiment_on_versions,
save_demo_results,
)
# params we should use to generate the wave file:
# presence_gain_db = 3.5
# comp_threshold_db = -24.0
# comp_ratio = 3.5
# target_lufs = -18.0
# spectral_alpha = 1.45
# spectral_beta = 0.08
#
# # De-esser
# enable_deesser = True
# deess_threshold_db = -32.0
# deess_ratio = 5.0
# deess_freq_low = 4000.0
# deess_freq_high = 9000.0
# deess_attack_ms = 2.0
# deess_release_ms = 60.0
APP_STATE_DIR = Path(".app_state")
DEMO_STATE_PATH = APP_STATE_DIR / "demo_state.pkl"
DEMO_UPLOAD_DIR = APP_STATE_DIR / "demo_uploads"
DEMO_PERSISTED_KEYS = (
"audio_file",
"audio_file_label",
"whisper_model",
"separation_method",
"suppression",
"target_boost",
"post_processing_enabled",
"enable_presence",
"enable_compression",
"enable_lufs",
"enable_limiter",
"enable_spectral_subtract",
"enable_comfort_noise",
"use_auto_doa",
"manual_doas",
"use_wpe",
"direction_results",
"doas",
"n_speakers",
"doa_confidences",
"doa_dominant",
"doa_overall_confidence",
"doa_pairwise",
"doa_meta",
"doa_raw_candidates",
"mono_raw",
"sr_mono",
)
# ---------------------------------------------------------------------------
# Suppression / boost dial helpers
# ---------------------------------------------------------------------------
def _lerp(t: float, low: float, high: float) -> float:
"""Linear interpolation: t=0 → low, t=1 → high."""
return low + t * (high - low)
def _resolve_suppression(s: float) -> dict:
"""Map the 0–1 Background Suppression slider to DSP parameters."""
return {
"mask_sharpness": _lerp(s, 1.5, 5.0),
"wiener_floor": _lerp(s, 0.15, 0.01),
"spectral_alpha": _lerp(s, 0.5, 2.5),
"spectral_beta": _lerp(s, 0.05, 0.005),
"enable_spectral_subtract": s > 0.3,
}
def _resolve_target_boost(b: float) -> dict:
"""Map the 0–1 Target Boost slider to post-processing parameters."""
return {
"presence_gain_db": _lerp(b, 0.0, 6.0),
"comp_threshold_db": _lerp(b, -15.0, -30.0),
"comp_ratio": _lerp(b, 2.0, 8.0),
"target_lufs": _lerp(b, -23.0, -12.0),
}
def _ensure_app_state_dirs() -> None:
APP_STATE_DIR.mkdir(parents=True, exist_ok=True)
DEMO_UPLOAD_DIR.mkdir(parents=True, exist_ok=True)
def _load_demo_state_from_disk() -> None:
"""Hydrate session state from the last saved demo run once per process."""
if st.session_state.get("_demo_disk_state_loaded"):
return
st.session_state["_demo_disk_state_loaded"] = True
if not DEMO_STATE_PATH.exists():
return
try:
with DEMO_STATE_PATH.open("rb") as f:
payload = pickle.load(f)
except Exception:
return
if not isinstance(payload, dict):
return
saved_audio = payload.get("audio_file")
if saved_audio and not Path(saved_audio).exists():
payload.pop("audio_file", None)
payload.pop("audio_file_label", None)
for key in (
"direction_results",
"doas",
"n_speakers",
"doa_confidences",
"doa_dominant",
"doa_overall_confidence",
"doa_pairwise",
"doa_meta",
"doa_raw_candidates",
"mono_raw",
"sr_mono",
):
payload.pop(key, None)
for key in DEMO_PERSISTED_KEYS:
if key in payload and key not in st.session_state:
st.session_state[key] = payload[key]
def _save_demo_state_to_disk() -> None:
"""Persist the current demo state so reloads restore the last run."""
_ensure_app_state_dirs()
payload = {
key: st.session_state[key]
for key in DEMO_PERSISTED_KEYS
if key in st.session_state
}
with DEMO_STATE_PATH.open("wb") as f:
pickle.dump(payload, f)
def _clear_demo_state() -> None:
"""Remove both in-memory and on-disk demo state."""
for key in DEMO_PERSISTED_KEYS:
st.session_state.pop(key, None)
if DEMO_STATE_PATH.exists():
DEMO_STATE_PATH.unlink()
def _persist_uploaded_demo_file(uploaded_file) -> tuple[str, str]:
"""Store uploaded demo audio in a stable local path."""
_ensure_app_state_dirs()
target = DEMO_UPLOAD_DIR / uploaded_file.name
target.write_bytes(uploaded_file.getvalue())
return str(target), uploaded_file.name
# ---------------------------------------------------------------------------
# Page config
# ---------------------------------------------------------------------------
st.set_page_config(
page_title="Audio Explorers 2026",
page_icon="🎧",
layout="wide",
)
# ---------------------------------------------------------------------------
# Tooltip / info content for each pipeline step
# ---------------------------------------------------------------------------
STEP_INFO = {
"load": (
"**Loading the raw recording**\n\n"
"The input is a 4-channel WAV captured by a behind-the-ear (BTE) hearing aid. "
"The four microphones are: Left-Front (LF), Left-Rear (LR), Right-Front (RF), "
"Right-Rear (RR). The front-rear spacing on each ear is ~12 mm and the inter-ear "
"distance is ~17 cm. The recording is at 44.1 kHz."
),
"preprocess": (
"**Preprocessing**\n\n"
"Two preprocessing paths are applied:\n"
"- *Spatial preprocessing* (`preprocess_for_spatial`): highpass filter at 80 Hz to "
"remove low-frequency rumble, keeps all 4 channels at the original sample rate for "
"DoA estimation and beamforming.\n"
"- *ML preprocessing* (`preprocess_for_ml`): highpass → mono downmix → resample to "
"16 kHz → RMS normalize. Used to produce an audible mono preview of the raw mixture."
),
"doa": (
"**Direction of Arrival (DoA) estimation**\n\n"
"Uses a hybrid GCC-PHAT fusion algorithm across 4 microphone pairs "
"(left same-ear, right same-ear, front cross-ear, rear cross-ear). "
"The signal is split into short frames; energy-based gating selects "
"speech-active windows. For each active window and each mic pair, "
"GCC-PHAT computes the time-delay-of-arrival. These pairwise lag "
"measurements are fused into an angular score curve (SRP-PHAT–like), "
"and candidate DoAs are extracted as peaks. Each candidate gets a "
"confidence score combining peak sharpness, cross-pair agreement, "
"and active-window coverage."
),
"beamforming": (
"**Source Separation / Beamforming**\n\n"
"Three methods are available to isolate each speaker:\n\n"
"- **MVDR (Minimum Variance Distortionless Response):** Multi-source aware MVDR "
"beamformer with adaptive diagonal loading, per-interferer covariance estimation, "
"and Wiener post-filtering. Uses a two-stage approach: separable directions (left/right) "
"are isolated first, then subtracted so that ambiguous directions (front/back) can be "
"recovered from the residual. This is the recommended method.\n"
"- **DAS (Delay-and-Sum):** Aligns channels by compensating propagation "
"delays using sinc interpolation, then averages. Simple and robust baseline.\n\n"
"Note: with only ~12 mm front-rear spacing the spatial resolution is limited "
"for front/back disambiguation, but the two-stage MVDR approach mitigates this."
),
"transcription": (
"**Automatic Speech Recognition (Whisper)**\n\n"
"Each separated signal is fed to OpenAI's Whisper model at 16 kHz. "
"Whisper auto-detects the language and produces a timestamped transcript.\n\n"
"**Model size matters:** The `base` model often produces gibberish on Danish speech. "
"Use `medium` or `large` for reliable Danish transcription (at the cost of speed)."
),
"gender": (
"**Gender Classification (hybrid wav2vec2 + F0)**\n\n"
"Uses a two-stage approach:\n"
"1. **wav2vec2 model** (`audeering/wav2vec2-large-robust-6-ft-age-gender`): "
"a pretrained age/gender classifier. If confidence > 0.85 it decides immediately.\n"
"2. **F0 (fundamental frequency) analysis** via `librosa.pyin`: median F0 < 165 Hz → male, "
"≥ 165 Hz → female. Used as fallback or tie-breaker when the model is uncertain.\n\n"
"The final decision fuses both signals. Returns 'unknown' for very short or unvoiced clips."
),
"results": (
"**Summary & Speaker Map**\n\n"
"A polar plot shows detected speakers around the listener. The summary table "
"lists each speaker's direction, detected language, gender, F0, and a transcript "
"preview. Expand individual speakers below for full details, audio playback, "
"and spectrograms."
),
}
# ---------------------------------------------------------------------------
# Helpers
# ---------------------------------------------------------------------------
def _make_spectrogram_fig(audio: np.ndarray, sr: int, title: str) -> plt.Figure:
if sr != 16000:
audio = librosa.resample(audio, orig_sr=sr, target_sr=16000)
sr = 16000
S = librosa.feature.melspectrogram(y=audio.astype(np.float32), sr=sr, n_mels=128)
S_db = librosa.power_to_db(S, ref=np.max)
fig, ax = plt.subplots(figsize=(10, 3))
librosa.display.specshow(S_db, sr=sr, x_axis="time", y_axis="mel", ax=ax)
ax.set_title(title, fontsize=11)
plt.tight_layout()
return fig
def _make_waveform_comparison_fig(
before: np.ndarray, sr_before: int,
after: np.ndarray, sr_after: int,
title: str = "Waveform Comparison",
) -> plt.Figure:
"""Two stacked subplots with RMS envelope for clear before/after comparison."""
fig, (ax1, ax2) = plt.subplots(2, 1, figsize=(12, 5), sharex=True,
gridspec_kw={"hspace": 0.15})
fig.patch.set_facecolor("#fafafa")
RAW_COLOR = "#e67e22" # warm orange
ENHANCED_COLOR = "#27ae60" # cool green
ENVELOPE_ALPHA = 0.85
# Compute RMS envelope
def _rms_envelope(signal: np.ndarray, sr: int, window_ms: float = 30.0) -> tuple[np.ndarray, np.ndarray]:
win = max(1, int(sr * window_ms / 1000.0))
hop = max(1, win // 2)
n_frames = max(1, (len(signal) - win) // hop + 1)
rms = np.array([
np.sqrt(np.mean(signal[i * hop:i * hop + win] ** 2))
for i in range(n_frames)
])
t = np.array([i * hop / sr for i in range(n_frames)])
return t, rms
# Raw mixture (top)
t_before = np.arange(len(before)) / sr_before
ax1.fill_between(t_before, before, alpha=0.25, color=RAW_COLOR, linewidth=0)
ax1.plot(t_before, before, alpha=0.4, linewidth=0.3, color=RAW_COLOR)
t_env, rms_env = _rms_envelope(before, sr_before)
ax1.plot(t_env, rms_env, color=RAW_COLOR, linewidth=2, alpha=ENVELOPE_ALPHA, label="RMS envelope")
ax1.plot(t_env, -rms_env, color=RAW_COLOR, linewidth=2, alpha=ENVELOPE_ALPHA)
ax1.set_ylabel("Amplitude", fontsize=9)
ax1.set_title("Raw Mixture (mono)", fontsize=11, fontweight="bold",
color=RAW_COLOR, loc="left")
ax1.grid(True, alpha=0.3, linewidth=0.5)
ax1.set_facecolor("#fefefe")
peak_raw = max(np.max(np.abs(before)), 1e-8)
ax1.set_ylim(-peak_raw * 1.15, peak_raw * 1.15)
# Enhanced (bottom)
t_after = np.arange(len(after)) / sr_after
ax2.fill_between(t_after, after, alpha=0.25, color=ENHANCED_COLOR, linewidth=0)
ax2.plot(t_after, after, alpha=0.4, linewidth=0.3, color=ENHANCED_COLOR)
t_env2, rms_env2 = _rms_envelope(after, sr_after)
ax2.plot(t_env2, rms_env2, color=ENHANCED_COLOR, linewidth=2, alpha=ENVELOPE_ALPHA, label="RMS envelope")
ax2.plot(t_env2, -rms_env2, color=ENHANCED_COLOR, linewidth=2, alpha=ENVELOPE_ALPHA)
ax2.set_ylabel("Amplitude", fontsize=9)
ax2.set_xlabel("Time (s)", fontsize=9)
ax2.set_title("Enhanced (beamformed)", fontsize=11, fontweight="bold",
color=ENHANCED_COLOR, loc="left")
ax2.grid(True, alpha=0.3, linewidth=0.5)
ax2.set_facecolor("#fefefe")
peak_enh = max(np.max(np.abs(after)), 1e-8)
ax2.set_ylim(-peak_enh * 1.15, peak_enh * 1.15)
fig.suptitle(title, fontsize=13, fontweight="bold", y=1.01)
fig.tight_layout()
return fig
def _generate_pipeline_summary(direction_results, toi, session_state):
from datetime import datetime, timezone
lines = ["# Audio Explorers 2026 — Pipeline Summary\n"]
lines.append(f"*Generated: {datetime.now(timezone.utc).strftime('%Y-%m-%d %H:%M UTC')}*\n")
# --- Pipeline Configuration ---
lines.append("## Pipeline Configuration\n")
lines.append(f"- **Audio file:** {session_state.get('audio_file', 'unknown')}")
lines.append(f"- **Whisper model:** {session_state.get('whisper_model', 'unknown')}")
lines.append(f"- **Separation method:** {session_state.get('separation_method', 'unknown')}")
lines.append(f"- **Suppression level:** {session_state.get('suppression', 'n/a')}")
lines.append(f"- **Target boost:** {session_state.get('target_boost', 'n/a')}")
pp_enabled = session_state.get('post_processing_enabled', False)
lines.append(f"- **Post-processing:** {'enabled' if pp_enabled else 'disabled'}")
wpe = session_state.get('use_wpe', False)
lines.append(f"- **WPE dereverberation:** {'enabled' if wpe else 'disabled'}")
lines.append("")
# --- DoA Estimation ---
lines.append("## Direction of Arrival Estimation\n")
n_speakers = session_state.get('n_speakers', len(direction_results))
lines.append(f"- **Speakers detected:** {n_speakers}")
doa_dominant = session_state.get('doa_dominant')
doa_confidence = session_state.get('doa_overall_confidence')
if doa_dominant is not None:
lines.append(f"- **Dominant DoA:** {doa_dominant:.1f}°")
if doa_confidence is not None:
lines.append(f"- **Overall confidence:** {doa_confidence:.3f}")
# ILD info from metadata
doa_meta = session_state.get('doa_meta', {})
ild_info = doa_meta.get('ild') if doa_meta else None
if ild_info and ild_info.get('enabled'):
lines.append(f"- **ILD (front/back):** "
f"{'front' if ild_info.get('global_is_front') else 'rear'} dominant "
f"(confidence: {ild_info.get('global_confidence', 0):.3f}, "
f"front vote: {ild_info.get('front_vote_fraction', 0):.0%})")
# Pairwise lag summary
doa_pairwise = session_state.get('doa_pairwise')
if doa_pairwise:
lines.append("\n### Pairwise GCC-PHAT Summary\n")
lines.append("| Pair | Spacing (mm) | Mean Lag (samples) | σ | Valid % |")
lines.append("|------|-------------|-------------------|---|---------|")
for pair_name, info in doa_pairwise.items():
lines.append(
f"| {pair_name} | {info['spacing_m']*1000:.1f} | "
f"{info['lag_mean_samples']:.2f} | {info['lag_std_samples']:.2f} | "
f"{info['valid_fraction']:.0%} |"
)
# Speaker direction table
doa_confidences = session_state.get('doa_confidences')
lines.append("\n### Validated Speaker Directions\n")
lines.append("| # | DoA (°) | Score |")
lines.append("|---|---------|-------|")
doas = session_state.get('doas', [r['doa'] for r in direction_results])
for i, d in enumerate(doas):
score = f"{doa_confidences[i]:.3f}" if doa_confidences and i < len(doa_confidences) else "n/a"
lines.append(f"| {i+1} | {d:.1f} | {score} |")
lines.append("")
# --- Per-Speaker Analysis ---
lines.append("## Per-Speaker Analysis\n")
for i, r in enumerate(direction_results):
lines.append(f"### Speaker {i+1} — {r['doa']:.0f}°\n")
f0 = f"{r['median_f0']:.0f} Hz" if not np.isnan(r["median_f0"]) else "n/a"
lines.append(f"- **Direction:** {r['doa']:.0f}°")
lines.append(f"- **Language:** {r['language']}")
lines.append(f"- **Gender:** {r['gender']} (confidence: {r['gender_confidence']:.2f}, method: {r['gender_method']})")
lines.append(f"- **Median F0:** {f0}")
dialogue = r.get("dialogue", {})
if dialogue.get("is_dialogue"):
lines.append(f"- **⚠️ Dialogue detected:** {dialogue['question_count']} questions — likely 2 speakers (HA user + target)")
lines.append(f"\n**Full Transcript:**\n")
lines.append(f"> {r['transcript']}\n")
ovd_segments = r.get("ovd_segments")
if ovd_segments and dialogue.get("is_dialogue"):
lines.append("**Segments (with own-voice detection):**\n")
lines.append("| Start | End | Speaker | Score | Text |")
lines.append("|-------|-----|---------|-------|------|")
for seg in ovd_segments:
spk = "🎤 HA user" if seg.get("is_own_voice") else "🔊 Target"
sc = f"{seg.get('own_voice_score', 0):.2f}"
text = seg.get("text", "")
if text:
lines.append(f"| {seg['start']:.1f}s | {seg['end']:.1f}s | {spk} | {sc} | {text} |")
elif r.get("segments"):
lines.append("**Segments:**\n")
lines.append("| Start | End | Text |")
lines.append("|-------|-----|------|")
for seg in r["segments"]:
lines.append(f"| {seg['start']:.1f}s | {seg['end']:.1f}s | {seg['text']} |")
lines.append("")
# --- Talker of Interest ---
lines.append("## Talker of Interest\n")
lines.append(f"{toi['reason']}\n")
if toi.get("score_breakdown"):
lines.append("### Score Breakdown\n")
lines.append("| DoA | Total | Frontal | Language | Content | Quality |")
lines.append("|-----|-------|---------|----------|---------|---------|")
for s in toi["score_breakdown"]:
lines.append(
f"| {s['doa']:.0f}° | {s['total']:.3f} | {s['frontal']:.2f} | "
f"{s['language']:.2f} | {s['content']:.2f} | {s['quality']:.2f} |"
)
lines.append("")
return "\n".join(lines)
def _audio_bytes(audio: np.ndarray, sr: int) -> bytes:
buf = io.BytesIO()
sf.write(buf, audio.astype(np.float32), sr, format="WAV")
return buf.getvalue()
def _make_polar_fig(
angles: list[float],
labels: list[str],
title: str = "Speaker Directions",
confidences: list[float] | None = None,
genders: list[str] | None = None,
languages: list[str] | None = None,
) -> plt.Figure:
"""Render an informative polar speaker map with head silhouette and badges."""
SPEAKER_COLORS = ["#e74c3c", "#2ecc71", "#3498db", "#f39c12",
"#9b59b6", "#1abc9c", "#e67e22", "#34495e"]
GENDER_ICONS = {"male": "♂", "female": "♀", "unknown": "?"}
fig, ax = plt.subplots(subplot_kw={"projection": "polar"}, figsize=(7, 7))
fig.patch.set_facecolor("#fafafa")
ax.set_facecolor("#f0f0f0")
# Head silhouette — circle at center + nose pointing up (front)
head_angles = np.linspace(0, 2 * np.pi, 80)
ax.plot(head_angles, np.full_like(head_angles, 0.22), color="#bbb",
linewidth=2, zorder=2)
# Nose triangle pointing to 0° (front = North)
nose_theta = np.array([-0.12, 0.0, 0.12])
nose_r = np.array([0.22, 0.32, 0.22])
ax.plot(nose_theta, nose_r, color="#bbb", linewidth=2, zorder=2)
# Ears
for ear_sign in [-1, 1]:
ear_theta = ear_sign * np.pi / 2
ear_angles = np.linspace(ear_theta - 0.15, ear_theta + 0.15, 20)
ear_r = 0.22 + 0.04 * np.cos(np.linspace(-np.pi, np.pi, 20))
ax.plot(ear_angles, ear_r, color="#bbb", linewidth=2, zorder=2)
# Cardinal labels
ax.set_theta_zero_location("N")
ax.set_theta_direction(-1)
ax.set_thetagrids(
[0, 90, 180, 270],
labels=["Front\n0°", "Right\n90°", "Back\n180°", "Left\n270°"],
fontsize=9, fontweight="bold", color="#555",
)
# Confidence rings
ring_levels = [0.25, 0.5, 0.75, 1.0]
for r in ring_levels:
ax.plot(np.linspace(0, 2 * np.pi, 100), np.full(100, r),
color="#ddd", linewidth=0.5, linestyle="--", zorder=1)
ax.set_ylim(0, 1.55)
ax.set_yticks([])
# Plot each speaker
n = max(len(angles), 1)
for i, (deg, lbl) in enumerate(zip(angles, labels)):
theta = np.radians(90 - deg)
color = SPEAKER_COLORS[i % len(SPEAKER_COLORS)]
conf = confidences[i] if confidences and i < len(confidences) else 0.75
radius = 0.4 + 0.6 * conf # confidence maps to 0.4–1.0
# Main marker — large filled circle
marker_size = 16 + 6 * conf
ax.plot(theta, radius, "o", markersize=marker_size, color=color,
markeredgecolor="white", markeredgewidth=2, zorder=5)
# Speaker number inside marker
ax.text(theta, radius, str(i + 1), ha="center", va="center",
fontsize=10, fontweight="bold", color="white", zorder=6)
# Badge: gender icon + language
gender_str = GENDER_ICONS.get(
genders[i] if genders and i < len(genders) else "unknown", "?"
)
lang_str = languages[i] if languages and i < len(languages) else ""
badge = f"{gender_str} {lang_str}".strip()
ax.annotate(
badge, xy=(theta, radius),
xytext=(theta, min(radius + 0.22, 1.45)),
ha="center", va="bottom", fontsize=9, fontweight="bold",
color=color,
bbox=dict(boxstyle="round,pad=0.2", facecolor="white",
edgecolor=color, alpha=0.85),
zorder=7,
)
# Legend
from matplotlib.lines import Line2D
legend_handles = []
for i, deg in enumerate(angles):
color = SPEAKER_COLORS[i % len(SPEAKER_COLORS)]
gender_str = (genders[i] if genders and i < len(genders) else "?")
lang_str = (languages[i] if languages and i < len(languages) else "")
conf_str = f"{confidences[i]:.0%}" if confidences and i < len(confidences) else ""
lbl = f"Spk {i+1}: {deg:.0f}° · {gender_str} · {lang_str} ({conf_str})"
legend_handles.append(
Line2D([0], [0], marker="o", color="w", markerfacecolor=color,
markersize=10, label=lbl)
)
if legend_handles:
ax.legend(handles=legend_handles, loc="upper left",
bbox_to_anchor=(-0.15, -0.05), fontsize=8,
frameon=True, fancybox=True, shadow=True)
ax.set_title(title, pad=30, fontsize=14, fontweight="bold")
fig.tight_layout()
return fig
def _step_note(key: str) -> None:
"""Render an expandable info note for a pipeline step."""
with st.expander("ℹ️ How does this work?", expanded=False):
st.markdown(STEP_INFO[key])
# ---------------------------------------------------------------------------
# Sidebar — mode selection
# ---------------------------------------------------------------------------
mode = st.sidebar.radio(
"Mode",
["Demo", "Enhancement Only", "Experiment", "Post-processing Playground"], # ✅ ADDED MODE
index=0,
help="Demo: full pipeline walkthrough. Enhancement Only: run enhancement only. Experiment: preprocessing A/B tests.",
)
# ===================================================================
# DEMO MODE — Full pipeline
# ===================================================================
if mode == "Demo":
_load_demo_state_from_disk()
st.title("Audio Explorers 2026")
st.markdown(
"Extracting the talker of interest from a multi-talker hearing-aid recording. "
"Walk through the full pipeline step by step: "
"**load → preprocess → DoA estimation → beamforming → transcription → gender classification.**"
)
# --- Sidebar settings ---
with st.sidebar:
st.header("Settings")
uploaded_file = st.file_uploader(
"Upload a 4-channel WAV file",
type=["wav"],
help="Upload your own multi-channel hearing-aid recording. Must be a 4-channel WAV file.",
)
default_audio_options = ["data/raw/example_mixture.wav", "data/raw/mixture.wav"]
saved_audio_file = st.session_state.get("audio_file")
saved_audio_label = st.session_state.get("audio_file_label")
if uploaded_file is not None:
audio_file, audio_label = _persist_uploaded_demo_file(uploaded_file)
st.session_state["audio_file"] = audio_file
st.session_state["audio_file_label"] = audio_label
st.sidebar.success(f"Uploaded: {audio_label}")
else:
default_index = 0
if saved_audio_file in default_audio_options:
default_index = default_audio_options.index(saved_audio_file)
audio_file = st.selectbox(
"Audio file",
default_audio_options,
index=default_index,
)
audio_label = Path(audio_file).name
if saved_audio_file and saved_audio_file not in default_audio_options and Path(saved_audio_file).exists():
audio_file = saved_audio_file
audio_label = saved_audio_label or Path(saved_audio_file).name
st.caption(f"Reloaded saved uploaded file: `{audio_label}`")
st.session_state["audio_file"] = audio_file
st.session_state["audio_file_label"] = audio_label
whisper_model = st.selectbox(
"Whisper model",
["tiny", "base", "small", "medium", "large"],
index=["tiny", "base", "small", "medium", "large"].index(
st.session_state.get("whisper_model", "medium")
),
help="'medium' or 'large' recommended for Danish speech.",
)
separation_method = st.selectbox(
"Separation method",
["mvdr", "das"],
index=["mvdr", "das"].index(st.session_state.get("separation_method", "mvdr")),
help=(
"MVDR: multi-source aware MVDR with adaptive loading and Wiener post-filter (recommended). "
"DAS: delay-and-sum beamforming (fast baseline)."
),
)
st.divider()
suppression = st.slider(
"🔇 Background Suppression",
min_value=0.0, max_value=1.0, value=float(st.session_state.get("suppression", 0.5)), step=0.05,
help=(
"How aggressively to suppress background speakers and noise. "
"Controls spatial mask sharpness, Wiener post-filter depth, and spectral subtraction. "
"Higher = quieter background but may introduce artifacts."
),
)
target_boost = st.slider(
"🔊 Target Boost",
min_value=0.0, max_value=1.0, value=float(st.session_state.get("target_boost", 0.5)), step=0.05,
help=(
"How much to boost the target speaker's clarity and loudness. "
"Controls presence boost (2–6 kHz), dynamic compression, and LUFS normalization. "
"Higher = louder and clearer target."
),
)
sup_params = _resolve_suppression(suppression)
boost_params = _resolve_target_boost(target_boost)
st.divider()
enable_post_processing = st.toggle(
"Apply post-processing",
value=bool(st.session_state.get("post_processing_enabled", True)),
help=(
"Apply presence boost, compression, LUFS normalization, and limiting "
"to the beamformed mono outputs used for listening and download."
),
)
with st.expander("Advanced post-processing overrides", expanded=False):
st.caption("These are auto-set by the sliders above. Override only if needed.")
enable_presence = st.checkbox("Presence boost", value=bool(st.session_state.get("enable_presence", True)))
enable_compression = st.checkbox("Compression", value=bool(st.session_state.get("enable_compression", True)))
enable_lufs = st.checkbox("LUFS normalization", value=bool(st.session_state.get("enable_lufs", True)))
enable_limiter = st.checkbox("Limiter", value=bool(st.session_state.get("enable_limiter", True)))
enable_spectral_subtract = st.checkbox(
"Spectral subtraction",
value=bool(st.session_state.get("enable_spectral_subtract", sup_params["enable_spectral_subtract"])),
help="Auto-enabled when Background Suppression > 0.3. Can be aggressive — use with care.",
)
enable_comfort_noise = st.checkbox("Comfort noise", value=bool(st.session_state.get("enable_comfort_noise", False)),
help="Add low-level shaped noise to prevent uncomfortable silence after noise reduction.")
use_auto_doa = st.toggle(
"Auto-detect DoAs",
value=bool(st.session_state.get("use_auto_doa", True)),
help="Use the GCC-PHAT fusion algorithm to find speaker directions automatically.",
)
if not use_auto_doa:
st.caption(
"0° = front, 90° = right, 180° = back, 270° = left."
)
doa_text = st.text_input(
"Comma-separated DoAs",
value=", ".join(str(x) for x in st.session_state.get("manual_doas", [0, 90, 180, 270])),
)
try:
manual_doas = [float(x.strip()) for x in doa_text.split(",") if x.strip()]
except ValueError:
st.error("Enter numeric angles separated by commas.")
st.stop()
# WPE dereverberation toggle
use_wpe = st.toggle(
"WPE Dereverberation (experimental)",
value=bool(st.session_state.get("use_wpe", False)),
help=(
"Apply Weighted Prediction Error dereverberation before MVDR beamforming. "
"Reduces room reverberation but is memory-intensive. Requires nara-wpe."
),
)
if use_wpe and not _HAS_WPE:
st.warning("⚠️ nara-wpe is not installed. WPE dereverberation will be skipped.")
if st.button("Clear saved demo state", use_container_width=True):
_clear_demo_state()
st.rerun()
st.session_state["audio_file"] = audio_file
st.session_state["audio_file_label"] = audio_label
st.session_state["whisper_model"] = whisper_model
st.session_state["separation_method"] = separation_method
st.session_state["suppression"] = suppression
st.session_state["target_boost"] = target_boost
st.session_state["post_processing_enabled"] = enable_post_processing
st.session_state["enable_presence"] = enable_presence
st.session_state["enable_compression"] = enable_compression
st.session_state["enable_lufs"] = enable_lufs
st.session_state["enable_limiter"] = enable_limiter
st.session_state["enable_spectral_subtract"] = enable_spectral_subtract
st.session_state["enable_comfort_noise"] = enable_comfort_noise
st.session_state["use_auto_doa"] = use_auto_doa
st.session_state["manual_doas"] = manual_doas if not use_auto_doa else []
st.session_state["use_wpe"] = use_wpe
_save_demo_state_to_disk()
# --- Load ---
if not Path(audio_file).exists():
st.error(f"File not found: `{audio_file}`")
st.stop()
audio_raw, sr = load_mixture(audio_file)
# Validate 4-channel audio
if audio_raw.ndim != 2 or audio_raw.shape[1] != 4:
st.error(
f"Expected a 4-channel WAV file, got shape {audio_raw.shape}. "
"The file must have exactly 4 channels in order: "
"[left front, left rear, right front, right rear]."
)
st.stop()
# ================================================================
# Step 1 — Raw mixture
# ================================================================
st.header("1 Load Raw Recording")
_step_note("load")
col1, col2 = st.columns([1, 2])
with col1:
st.markdown(
f"**Channels:** {audio_raw.shape[1]} \n"
f"**Sample rate:** {sr} Hz \n"
f"**Duration:** {audio_raw.shape[0]/sr:.1f} s"
)
mono_raw, sr_mono = preprocess_for_ml(audio_raw, sr)
st.audio(_audio_bytes(mono_raw, sr_mono), format="audio/wav")
with col2:
fig = _make_spectrogram_fig(mono_raw, sr_mono, "Raw mixture (mono downmix, 16 kHz)")
st.pyplot(fig)
plt.close(fig)
# ================================================================
# Step 2 — Preprocessing
# ================================================================
st.header("2 Preprocessing")
_step_note("preprocess")
st.caption(
"Spatial preprocessing keeps all 4 channels at 44.1 kHz (for DoA + beamforming). "
"ML preprocessing produces a mono 16 kHz signal (for the player above)."
)
# ================================================================
# Run pipeline
# ================================================================
if st.button("Run full pipeline", type="primary"):
# -- Spatial preprocess --
audio_spatial, sr_spatial = preprocess_for_spatial(audio_raw, sr)
# -- DoA estimation --
if use_auto_doa:
with st.spinner("Estimating directions of arrival..."):
doa_result = estimate_doa(audio_spatial, sr_spatial)
# Use validated speaker directions (filtered + clustered)
dir_result = estimate_speaker_directions(audio_raw, sr)
n_speakers = dir_result["count"]
directions = dir_result["directions"]
doas = [d["doa"] for d in directions]
doa_confidences = [d["score"] for d in directions]
doa_dominant = doas[0] if doas else 0.0
doa_overall_confidence = doa_result["confidence"]
doa_pairwise = doa_result["pairwise_lags"]
doa_meta = doa_result["metadata"]
doa_raw_candidates = doa_result["candidate_doas"]
else:
doas = manual_doas
doa_confidences = None
doa_dominant = doas[0] if doas else 0.0
doa_overall_confidence = None
doa_pairwise = None
doa_meta = None
doa_raw_candidates = None
n_speakers = len(doas)
if not doas:
st.warning("No speaker directions found. Try lowering the energy threshold or providing manual DoAs.")
st.stop()
# --- Hardcoded tuning (final values) ---
presence_gain_db = 3.5
comp_threshold_db = -24.0
comp_ratio = 3.5
target_lufs = -18.0
spectral_alpha = 1.45
spectral_beta = 0.08
# De-esser
enable_deesser = True
deess_threshold_db = -32.0
deess_ratio = 5.0
deess_freq_low = 4000.0
deess_freq_high = 9000.0
deess_attack_ms = 2.0
deess_release_ms = 60.0
# -- Source separation / beamforming --
beamform_method = separation_method # "mvdr" or "das"
with st.spinner(f"Beamforming {len(doas)} directions ({beamform_method.upper()})..."):
enhanced_raw = enhance_all_directions(
audio_spatial, sr_spatial, doas, method=beamform_method,
use_wpe=use_wpe,
mask_sharpness=sup_params["mask_sharpness"],
wiener_floor=sup_params["wiener_floor"],
)
sr_enhanced_raw = sr_spatial
# --- Post-processing ---
if enable_post_processing:
processed_raw = [
apply_post_processing_chain(
sig,
sr_spatial,
enable_presence=enable_presence,
enable_compression=enable_compression,
enable_lufs=enable_lufs,
enable_limiter=enable_limiter,
enable_spectral_subtract=enable_spectral_subtract,
enable_comfort_noise=enable_comfort_noise,
# Hardcoded tuning
presence_gain_db=presence_gain_db,
comp_threshold_db=comp_threshold_db,
comp_ratio=comp_ratio,
target_lufs=target_lufs,
spectral_alpha=spectral_alpha,
spectral_beta=spectral_beta,
# De-esser
enable_deesser=enable_deesser,
deess_threshold_db=deess_threshold_db,
deess_ratio=deess_ratio,
)
for sig in enhanced_raw
]
else:
processed_raw = [sig.copy() for sig in enhanced_raw]
stereo_raw = [
render_binaural(sig, sr_spatial, doa, raw_multichannel=audio_raw)
for sig, doa in zip(processed_raw, doas)
]
enhanced_ml = []
for sig in enhanced_raw:
sig_ml, _ = preprocess_enhanced(sig, sr_spatial)
enhanced_ml.append(sig_ml)
# -- Transcription + Gender --
direction_results = []
for i, doa in enumerate(doas):
with st.spinner(f"Analysing speaker at {doa:.0f}°..."):
asr = transcribe_detailed(
enhanced_ml[i].astype(np.float32), 16000,
model_size=whisper_model,
)
gen = classify_gender_detailed(
enhanced_ml[i].astype(np.float32), 16000,
)
# Dialogue detection
dialogue = detect_dialogue_pattern(asr["segments"])
# Own-voice detection (uses raw 4-channel + enhanced mono)
ovd_segments = None
audio_target_only = None
if dialogue["is_dialogue"]:
ovd_segments = estimate_own_voice_segments(
asr["segments"], audio_raw, sr,
enhanced_mono=enhanced_ml[i],
enhanced_sr=16000,
)
# Generate target-speaker-only audio
target_ts = [
(s["start"], s["end"])
for s in ovd_segments
if not s.get("is_own_voice") and s.get("text", "").strip()
]
if target_ts:
audio_target_only = enhance_target_speaker_only(
audio_spatial, sr_spatial,
target_doa=doa,
target_timestamps=target_ts,
doa_list=doas,
method=beamform_method,
use_wpe=use_wpe,
)
direction_results.append({
"doa": doa,
"audio_ml": enhanced_ml[i],
"sr_ml": 16000,
"audio_post": processed_raw[i],
"sr_post": sr_enhanced_raw,
"audio_beamformed": enhanced_raw[i],
"sr_beamformed": sr_enhanced_raw,
"audio_stereo": stereo_raw[i],
"sr_stereo": sr_enhanced_raw,
"audio_target_only": audio_target_only,
"sr_target_only": sr_spatial,
"transcript": asr["text"],
"language": asr["language"],
"segments": asr["segments"],
"gender": gen["gender"],
"gender_confidence": gen["confidence"],
"gender_method": gen["method"],
"median_f0": gen["median_f0"],
"dialogue": dialogue,
"ovd_segments": ovd_segments,
})
# Update speaker count: DOA directions + intra-DOA dialogue splits
n_dialogue_extras = sum(
1 for r in direction_results if r["dialogue"]["is_dialogue"]
)
n_speakers = n_speakers + n_dialogue_extras
# Store in session state
st.session_state["direction_results"] = direction_results
st.session_state["doas"] = doas
st.session_state["n_speakers"] = n_speakers
st.session_state["doa_confidences"] = doa_confidences
st.session_state["doa_dominant"] = doa_dominant
st.session_state["doa_overall_confidence"] = doa_overall_confidence
st.session_state["doa_pairwise"] = doa_pairwise
st.session_state["doa_meta"] = doa_meta
st.session_state["doa_raw_candidates"] = doa_raw_candidates
st.session_state["mono_raw"] = mono_raw
st.session_state["sr_mono"] = sr_mono
st.session_state["separation_method"] = separation_method
st.session_state["post_processing_enabled"] = enable_post_processing
st.session_state["whisper_model"] = whisper_model
st.session_state["use_wpe"] = use_wpe
st.session_state["suppression"] = suppression
st.session_state["target_boost"] = target_boost
st.session_state["audio_file"] = audio_file
st.session_state["audio_file_label"] = audio_label
st.session_state["enable_presence"] = enable_presence
st.session_state["enable_compression"] = enable_compression