| 1 | /* diarize.h |
| 2 | * |
| 3 | * Copyright (C) 2026 Anastasia Shchupak |
| 4 | * |
| 5 | * This code is free software; you can redistribute it and/or modify |
| 6 | * it under the terms of the GNU General Public License as published by |
| 7 | * the Free Software Foundation; either version 3 of the License, or (at |
| 8 | * your option) any later version. |
| 9 | * |
| 10 | * This code is distributed in the hope that it will be useful, but |
| 11 | * WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
| 13 | * See the GNU General Public License for more details. |
| 14 | * |
| 15 | * You should have received a copy of the GNU General Public License |
| 16 | * along with this work. If not, see <http://www.gnu.org/licenses/>. |
| 17 | */ |
| 18 | |
| 19 | /* |
| 20 | * Speaker diarization API for Praat integration |
| 21 | * |
| 22 | * Provides a C API following the same pattern as whisper.h: |
| 23 | * - Context init/free (loads segmentation + embedding models) |
| 24 | * - diarize_full() runs the entire pipeline on audio samples |
| 25 | * - Accessor functions to read results (segments with speaker labels) |
| 26 | */ |
| 27 | |
| 28 | #ifndef DIARIZE_H |
| 29 | #define DIARIZE_Hcxx . . $ `external/whispercpp/diarize.h:29:9`! |
| 30 | |
| 31 | #include <stddef.h> |
| 32 | |
| 33 | #ifdef __cpluspluscxx . . $ `<built-in>:470:9`! |
| 34 | extern "C" { |
| 35 | #endif |
| 36 | |
| 37 | // Opaque context — holds loaded models and last result |
| 38 | struct diarize_contextcxx . . $ diarize_context#; |
| 39 | |
| 40 | // Parameters for diarization |
| 41 | struct diarize_full_params { |
| 42 | int n_threads; |
| 43 | |
| 44 | // Segmentation |
| 45 | float seg_duration; // chunk duration in seconds (default: 10.0) |
| 46 | float seg_step_ratio; // step as fraction of duration (default: 0.1 = 90% overlap) |
| 47 | |
| 48 | // Clustering: threshold and large-vs-small split (always applied) |
| 49 | float cluster_threshold; // agglomerative clustering threshold (default: 0.7) |
| 50 | int cluster_min_size; // minimum cluster size (default: 12) |
| 51 | |
| 52 | // Embedding filter |
| 53 | float min_active_ratio; // minimum non-overlapping activity to include (default: 0.2) |
| 54 | |
| 55 | // Cluster-count constraints (in numbers of distinct speakers). |
| 56 | // 0 means "unspecified": no constraint of this kind is applied. |
| 57 | // num_speakers > 0 forces exactly that many clusters. |
| 58 | // min_speakers > 0 forces at least that many clusters. |
| 59 | // max_speakers > 0 forces at most that many clusters. |
| 60 | // If multiple are set, num_speakers takes precedence. |
| 61 | int num_speakers; |
| 62 | int min_speakers; |
| 63 | int max_speakers; |
| 64 | |
| 65 | // Per-frame cap on the number of speakers active simultaneously. |
| 66 | // Acts as overlap-resolution: the segmentation model can produce |
| 67 | // accumulated activations above this; values above it are clipped. |
| 68 | // Default: unlimited (INT12_MAX). |
| 69 | int max_simultaneous_speakers; |
| 70 | }; |
| 71 | |
| 72 | // Get default parameters |
| 73 | struct diarize_full_params diarize_default_params(void); |
| 74 | |
| 75 | |
| 76 | // Model loader — abstraction for reading from file or memory |
| 77 | struct diarize_model_loader { |
| 78 | void * context; |
| 79 | |
| 80 | size_t (*read) (void * ctx, void * output, size_t read_size); |
| 81 | bool (*eof) (void * ctx); |
| 82 | void (*close)(void * ctx); |
| 83 | }; |
| 84 | |
| 85 | // Various functions for loading both models. Return NULL on failure |
| 86 | struct diarize_contextcxx . . $ diarize_context# * diarize_init_from_file(const char * seg_model_path, const char * emb_model_path); |
| 87 | struct diarize_contextcxx . . $ diarize_context# * diarize_init_from_memory(const void * seg_data, size_t seg_size, const void * emb_data, size_t emb_size); |
| 88 | struct diarize_contextcxx . . $ diarize_context# * diarize_init(struct diarize_model_loader * seg_loader, struct diarize_model_loader * emb_loader); |
| 89 | |
| 90 | // Free context and all associated memory |
| 91 | void diarize_free(struct diarize_contextcxx . . $ diarize_context# * ctx); |
| 92 | |
| 93 | // Run the full diarization pipeline on audio samples |
| 94 | // samples: float array, 16kHz mono, normalized to [-1, 1] |
| 95 | // n_samples: number of samples |
| 96 | // Throws MelderError on failure |
| 97 | void diarize_full( |
| 98 | struct diarize_contextcxx . . $ diarize_context# * ctx, |
| 99 | struct diarize_full_params params, |
| 100 | const float * samples, |
| 101 | int n_samples); |
| 102 | |
| 103 | // --- Result accessors --- |
| 104 | |
| 105 | // Number of segments in the last result |
| 106 | unsigned int diarize_full_n_segments(struct diarize_contextcxx . . $ diarize_context# * ctx); |
| 107 | |
| 108 | // Number of speakers detected in the last result |
| 109 | int diarize_full_n_speakers(struct diarize_contextcxx . . $ diarize_context# * ctx); |
| 110 | |
| 111 | // Get start time (seconds) of segment i |
| 112 | float diarize_full_get_segment_t0(struct diarize_contextcxx . . $ diarize_context# * ctx, int i_segment); |
| 113 | |
| 114 | // Get end time (seconds) of segment i |
| 115 | float diarize_full_get_segment_t1(struct diarize_contextcxx . . $ diarize_context# * ctx, int i_segment); |
| 116 | |
| 117 | // Get speaker ID (0-indexed) of segment i |
| 118 | int diarize_full_get_segment_speaker(struct diarize_contextcxx . . $ diarize_context# * ctx, int i_segment); |
| 119 | |
| 120 | #ifdef __cpluspluscxx . . $ `<built-in>:470:9`! |
| 121 | } |
| 122 | #endif |
| 123 | |
| 124 | #endif // DIARIZE_H |