/* diarize.cpp
*
* Copyright (C) 2026 Anastasia Shchupak
*
* This code is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or (at
* your option) any later version.
*
* This code is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
* See the GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this work. If not, see .
*/
/* Speaker diarization pipeline in C++.
* ------------------------------------
* Reimplements pyannote.audio SpeakerDiarization pipeline:
* Segmentation (pyannote/segmentation-3.0):
* WavNorm -> SincNet (ggml graph) -> LSTM (plain C++) -> Linear -> Classifier -> Log-softmax
* Input: 10s waveform at 16kHz -> Output: 589 frames × 7 powerset classes
*
* Embedding (wespeaker-voxceleb-resnet34-LM):
* Fbank -> ResNet34 (ggml conv2d + plain C++) -> TSTP pooling -> Linear
* Input: variable-length waveform -> Output: 256-d speaker embedding
*
* Clustering:
* AgglomerativeClustering (centroid linkage, threshold=0.7046)
*
* Pipeline flow:
* 1. Sliding window segmentation (10s chunks, 90% overlap)
* 2. Powerset -> multilabel conversion (7 classes -> 3 speakers)
* 3. Embedding extraction per (chunk, speaker) pair with mask
* 4. Filter embeddings (activity ≥ 20%, no NaN)
* 5. Agglomerative clustering (L2-norm -> centroid linkage -> fcluster)
* 6. Assign all embeddings to clusters
* 7. Reconstruct: remap local -> global speakers
* 8. Aggregate overlapping windows
* 9. Top-N speaker selection per frame
* 10. Frame-level -> time-stamped speaker segments
*/
#include
#include
#include
#include
#include
#include
#include
#include
#include