| 1 | #ifndef _SpeechRecognizer_h_ |
| 2 | #define _SpeechRecognizer_h_ |
| 3 | /* SpeechRecognizer.h |
| 4 | * |
| 5 | * Copyright (C) 2025,2026 Anastasia Shchupak |
| 6 | * |
| 7 | * This code is free software; you can redistribute it and/or modify |
| 8 | * it under the terms of the GNU General Public License as published by |
| 9 | * the Free Software Foundation; either version 3 of the License, or (at |
| 10 | * your option) any later version. |
| 11 | * |
| 12 | * This code is distributed in the hope that it will be useful, but |
| 13 | * WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
| 15 | * See the GNU General Public License for more details. |
| 16 | * |
| 17 | * You should have received a copy of the GNU General Public License |
| 18 | * along with this work. If not, see <http://www.gnu.org/licenses/>. |
| 19 | */ |
| 20 | |
| 21 | #include "Sound.h" |
| 22 | #include <set> |
| 23 | |
| 24 | struct structSpeechRecognizer; |
| 25 | struct whisper_context; |
| 26 | struct whisper_vad_context; |
| 27 | struct whisper_vad_segments; |
| 28 | struct diarize_context; |
| 29 | |
| 30 | struct autoWhisperContext { |
| 31 | whisper_context *ptr; |
| 32 | |
| 33 | autoWhisperContext (whisper_context *p = nullptr) : ptr(p) {} |
| 34 | ~autoWhisperContext (); |
| 35 | |
| 36 | autoWhisperContext (const autoWhisperContext&) = delete; |
| 37 | autoWhisperContext& operator= (const autoWhisperContext&) = delete; |
| 38 | |
| 39 | autoWhisperContext (autoWhisperContext&& other) noexcept : ptr (other.ptr) { |
| 40 | other.ptr = nullptr; |
| 41 | } |
| 42 | autoWhisperContext& operator= (autoWhisperContext&& other) noexcept; |
| 43 | |
| 44 | [[nodiscard]] |
| 45 | whisper_context *get () const { return ptr; } |
| 46 | }; |
| 47 | |
| 48 | struct autoWhisperVadContext { |
| 49 | whisper_vad_context *ptr; |
| 50 | |
| 51 | autoWhisperVadContext (whisper_vad_context *p = nullptr) : ptr (p) {} |
| 52 | ~autoWhisperVadContext (); |
| 53 | |
| 54 | autoWhisperVadContext (const autoWhisperVadContext&) = delete; |
| 55 | autoWhisperVadContext& operator= (const autoWhisperVadContext&) = delete; |
| 56 | |
| 57 | autoWhisperVadContext (autoWhisperVadContext&& other) noexcept : ptr (other.ptr) { |
| 58 | other.ptr = nullptr; |
| 59 | } |
| 60 | autoWhisperVadContext& operator= (autoWhisperVadContext&& other) noexcept; |
| 61 | |
| 62 | [[nodiscard]] |
| 63 | whisper_vad_context *get () const { return ptr; } |
| 64 | }; |
| 65 | |
| 66 | struct autoWhisperVadSegments { |
| 67 | whisper_vad_segments *ptr; |
| 68 | |
| 69 | autoWhisperVadSegments (whisper_vad_segments *p = nullptr) : ptr (p) {} |
| 70 | ~autoWhisperVadSegments (); |
| 71 | |
| 72 | autoWhisperVadSegments (const autoWhisperVadSegments&) = delete; |
| 73 | autoWhisperVadSegments& operator= (const autoWhisperVadSegments&) = delete; |
| 74 | |
| 75 | autoWhisperVadSegments (autoWhisperVadSegments && other) noexcept : ptr (other.ptr) { |
| 76 | other.ptr = nullptr; |
| 77 | } |
| 78 | autoWhisperVadSegments& operator= (autoWhisperVadSegments && other) noexcept; |
| 79 | |
| 80 | [[nodiscard]] |
| 81 | whisper_vad_segments *get () const { return ptr; } |
| 82 | }; |
| 83 | |
| 84 | struct autoDiarizeContext { |
| 85 | diarize_context *ptr; |
| 86 | |
| 87 | autoDiarizeContext (diarize_context *p = nullptr) : ptr(p) {} |
| 88 | ~autoDiarizeContext (); |
| 89 | |
| 90 | autoDiarizeContext (const autoDiarizeContext&) = delete; |
| 91 | autoDiarizeContext& operator= (const autoDiarizeContext&) = delete; |
| 92 | |
| 93 | autoDiarizeContext (autoDiarizeContext&& other) noexcept : ptr(other.ptr) { |
| 94 | other.ptr = nullptr; |
| 95 | } |
| 96 | autoDiarizeContext& operator= (autoDiarizeContext&& other) noexcept; |
| 97 | |
| 98 | [[nodiscard]] |
| 99 | diarize_context *get () const { return ptr; } |
| 100 | }; |
| 101 | |
| 102 | /* |
| 103 | Default parameter values for the UI. |
| 104 | */ |
| 105 | namespace TranscriptionDefaults { |
| 106 | inline constexpr conststring32 modelName = U"ggml-base.bin"; |
| 107 | inline constexpr conststring32 languageName = U"Autodetect language"; |
| 108 | inline constexpr bool includeWords = true; |
| 109 | inline constexpr bool includeDiarization = false; |
| 110 | inline constexpr bool useVad = true; |
| 111 | inline const integer n_threads = MelderThread_getNumberOfProcessors () / 2; |
| 112 | } |
| 113 | |
| 114 | namespace VadDefaults { |
| 115 | inline constexpr conststring32 speechThreshold = U"0.5"; |
| 116 | inline constexpr conststring32 minNonSpeechDuration = U"0.1"; |
| 117 | inline constexpr conststring32 minSpeechDuration = U"0.25"; |
| 118 | inline constexpr conststring32 speechPad = U"0.0"; |
| 119 | inline constexpr conststring32 nonSpeechLabel = U""; |
| 120 | inline constexpr conststring32 speechLabel = U"speech"; |
| 121 | } |
| 122 | |
| 123 | namespace DiarizationDefaults { |
| 124 | inline constexpr conststring32 maxNumSpeakers = U"2"; |
| 125 | inline constexpr bool allowOverlap = true; |
| 126 | inline constexpr conststring32 nonSpeechLabel = U""; |
| 127 | inline constexpr conststring32 speechLabel = U"speech"; |
| 128 | inline constexpr conststring32 clusterThreshold = U"0.7"; |
| 129 | inline constexpr conststring32 segmentationStep = U"0.1"; |
| 130 | inline const integer n_threads = MelderThread_getNumberOfProcessors () / 2; |
| 131 | } |
| 132 | |
| 133 | struct SpeechSegment { |
| 134 | autostring32 text; |
| 135 | double tmin; |
| 136 | double tmax; |
| 137 | }; |
| 138 | |
| 139 | struct WhisperTranscription { |
| 140 | SpeechSegment fullTranscription; |
| 141 | autovector <SpeechSegment> words; |
| 142 | autovector <SpeechSegment> sentences; |
| 143 | }; |
| 144 | |
| 145 | inline std::set <structSpeechRecognizer *> theLivingSpeechRecognizers; |
| 146 | #include "SpeechRecognizer_def.h" |
| 147 | |
| 148 | /* |
| 149 | Functions to access lists of models and languages from everywhere. |
| 150 | */ |
| 151 | constSTRVEC theCurrentSpeechRecognizerModelNames (); |
| 152 | constSTRVEC theSpeechRecognizerLanguageNames (); |
| 153 | |
| 154 | /* |
| 155 | Class SpeechRecognizer functions. |
| 156 | */ |
| 157 | autoSpeechRecognizer SpeechRecognizer_create (conststring32 modelName, conststring32 languageName); |
| 158 | WhisperTranscription SpeechRecognizer_recognize (constSpeechRecognizer me, constSound sound, bool useVad, |
| 159 | double speechProbabilityThreshold, double minNonSpeechDuration, double minSpeechDuration, double speechPad); |
| 160 | |
| 161 | /* |
| 162 | Silero-VAD functions. |
| 163 | */ |
| 164 | autovector <SpeechSegment> doSileroVad (constSound sound, double speechProbabilityThreshold, double minNonSpeechDuration, |
| 165 | double minSpeechDuration, double speechPad, conststring32 nonSpeechLabel, conststring32 speechLabel); |
| 166 | |
| 167 | /* |
| 168 | Diarization functions. |
| 169 | */ |
| 170 | /* |
| 171 | Run speaker diarization on Sound. Return a vector of timelines, one timeline per speaker. |
| 172 | Each timeline covers [sound->xmin, sound->xmax] as alternating silent/active intervals, |
| 173 | labeled `nonSpeechLabel` and `speechLabel` respectively. |
| 174 | */ |
| 175 | autovector <autovector <SpeechSegment>> doDiarization (constSound sound, |
| 176 | integer maxNumSpeakers, bool allowSpeakersOverlap, |
| 177 | double clusterThreshold, double segmentationStep, |
| 178 | conststring32 nonSpeechLabel, conststring32 speechLabel); |
| 179 | |
| 180 | /* |
| 181 | Preferences for AI settings (maximum number of threads for transcription & diarization). |
| 182 | */ |
| 183 | void SpeechRecognizer_preferences (); |
| 184 | |
| 185 | void SpeechRecognizer_setMaxNumberOfThreadsForTranscription (integer numberOfThreads); |
| 186 | void SpeechRecognizer_setMaxNumberOfThreadsForDiarization (integer numberOfThreads); |
| 187 | |
| 188 | integer SpeechRecognizer_getMaxNumberOfThreadsForTranscription (); |
| 189 | integer SpeechRecognizer_getMaxNumberOfThreadsForDiarization (); |
| 190 | |
| 191 | /* End of file SpeechRecognizer.h */ |
| 192 | #endif |