| 1 | /* SpeechRecognizer.cpp |
| 2 | * |
| 3 | * Copyright (C) 2025,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 | #include "SpeechRecognizer.h"cxx . . $ `<file>/fon/SpeechRecognizer.h`/ |
| 20 | #include "Sound.h"cxx . . $ `<file>/fon/Sound.h`/ |
| 21 | #include "whisper.h"cxx . . $ `<file>/external/whispercpp/whisper.h`/ |
| 22 | #include "diarize.h"cxx . . $ `<file>/external/whispercpp/diarize.h`/ |
| 23 | #include "melder.h"cxx . . $ `<file>/melder/melder.h`/ |
| 24 | #include "ggml-memory-pool.h"cxx . . $ `<file>/external/whispercpp/ggml-memory-pool.h`/ |
| 25 | #include "ggml-silero-vad-model-data.h"cxx . . $ `<file>/external/whispercpp/ggml-silero-vad-model-data.h`/ |
| 26 | #include "Preferences.h"cxx . . $ `<file>/sys/Preferences.h`/ |
| 27 | |
| 28 | #include "oo_DESTROY.h"cxx . . $ `<file>/sys/oo_DESTROY.h`/ |
| 29 | #include "SpeechRecognizer_def.h"cxx . . $ `<file>/fon/SpeechRecognizer_def.h`/ |
| 30 | #include "oo_COPY.h"cxx . . $ `<file>/sys/oo_COPY.h`/ |
| 31 | #include "SpeechRecognizer_def.h"cxx . . $ `<file>/fon/SpeechRecognizer_def.h`/ |
| 32 | #include "oo_EQUAL.h"cxx . . $ `<file>/sys/oo_EQUAL.h`/ |
| 33 | #include "SpeechRecognizer_def.h"cxx . . $ `<file>/fon/SpeechRecognizer_def.h`/ |
| 34 | #include "oo_CAN_WRITE_AS_ENCODING.h"cxx . . $ `<file>/sys/oo_CAN_WRITE_AS_ENCODING.h`/ |
| 35 | #include "SpeechRecognizer_def.h"cxx . . $ `<file>/fon/SpeechRecognizer_def.h`/ |
| 36 | #include "oo_WRITE_TEXT.h"cxx . . $ `<file>/sys/oo_WRITE_TEXT.h`/ |
| 37 | #include "SpeechRecognizer_def.h"cxx . . $ `<file>/fon/SpeechRecognizer_def.h`/ |
| 38 | #include "oo_WRITE_BINARY.h"cxx . . $ `<file>/sys/oo_WRITE_BINARY.h`/ |
| 39 | #include "SpeechRecognizer_def.h"cxx . . $ `<file>/fon/SpeechRecognizer_def.h`/ |
| 40 | #include "oo_READ_TEXT.h"cxx . . $ `<file>/sys/oo_READ_TEXT.h`/ |
| 41 | #include "SpeechRecognizer_def.h"cxx . . $ `<file>/fon/SpeechRecognizer_def.h`/ |
| 42 | #include "oo_READ_BINARY.h"cxx . . $ `<file>/sys/oo_READ_BINARY.h`/ |
| 43 | #include "SpeechRecognizer_def.h"cxx . . $ `<file>/fon/SpeechRecognizer_def.h`/ |
| 44 | #include "oo_DESCRIPTION.h"cxx . . $ `<file>/sys/oo_DESCRIPTION.h`/ |
| 45 | #include "SpeechRecognizer_def.h"cxx . . $ `<file>/fon/SpeechRecognizer_def.h`/ |
| 46 | |
| 47 | autoWhisperContextcxx . . $ autoWhisperContext# :: ~cxx . . $ autoWhisperContext#`~autoWhisperContext`(ced63f7c635d850d).autoWhisperContextcxx . . $ autoWhisperContext# () { |
| 48 | //TRACE |
| 49 | tracecxx . . $ `melder/melder_debug.h:60:10`! (U"Destroying whisper context at ", Melder_pointer (ptr)); |
| 50 | if (ptrcxx . . $ autoWhisperContext#ptr.) |
| 51 | whisper_free (ptrcxx . . $ autoWhisperContext#ptr.); |
| 52 | tracecxx . . $ `melder/melder_debug.h:60:10`! (U"Number of allocations in the memory pool is ", theGgmlMemoryPool. n_allocations()); |
| 53 | tracecxx . . $ `melder/melder_debug.h:60:10`! (U"Total memory in bytes is ", theGgmlMemoryPool. sizeInBytes()); |
| 54 | } |
| 55 | autoWhisperContextcxx . . $ autoWhisperContext#& autoWhisperContextcxx . . $ autoWhisperContext# :: operatorcxx . . $ autoWhisperContext#`operator=`(dceedb20f2fe0c93).= (autoWhisperContextcxx . . $ autoWhisperContext#&& other) noexcept { |
| 56 | if (this != & other) { |
| 57 | whisper_free (ptrcxx . . $ autoWhisperContext#ptr.); |
| 58 | ptrcxx . . $ autoWhisperContext#ptr. = other.ptrcxx . . $ autoWhisperContext#ptr.; |
| 59 | other.ptrcxx . . $ autoWhisperContext#ptr. = nullptr; |
| 60 | } |
| 61 | return *this; |
| 62 | } |
| 63 | |
| 64 | autoWhisperVadContextcxx . . $ autoWhisperVadContext# :: ~cxx . . $ autoWhisperVadContext#`~autoWhisperVadContext`(ced63f7c635d850d).autoWhisperVadContextcxx . . $ autoWhisperVadContext# () { |
| 65 | //TRACE |
| 66 | tracecxx . . $ `melder/melder_debug.h:60:10`! (U"Destroying Silero-VAD context at ", Melder_pointer (ptr)); |
| 67 | whisper_vad_free (ptrcxx . . $ autoWhisperVadContext#ptr.); |
| 68 | tracecxx . . $ `melder/melder_debug.h:60:10`! (U"Number of allocations in the memory pool is ", theGgmlMemoryPool. n_allocations()); |
| 69 | tracecxx . . $ `melder/melder_debug.h:60:10`! (U"Total memory in bytes is ", theGgmlMemoryPool. sizeInBytes()); |
| 70 | } |
| 71 | autoWhisperVadContextcxx . . $ autoWhisperVadContext#& autoWhisperVadContextcxx . . $ autoWhisperVadContext# :: operatorcxx . . $ autoWhisperVadContext#`operator=`(29556fddf54d2da3).= (autoWhisperVadContextcxx . . $ autoWhisperVadContext#&& other) noexcept { |
| 72 | if (this != & other) { |
| 73 | whisper_vad_free (ptrcxx . . $ autoWhisperVadContext#ptr.); |
| 74 | ptrcxx . . $ autoWhisperVadContext#ptr. = other.ptrcxx . . $ autoWhisperVadContext#ptr.; |
| 75 | other.ptrcxx . . $ autoWhisperVadContext#ptr. = nullptr; |
| 76 | } |
| 77 | return *this; |
| 78 | } |
| 79 | |
| 80 | autoWhisperVadSegmentscxx . . $ autoWhisperVadSegments# :: ~cxx . . $ autoWhisperVadSegments#`~autoWhisperVadSegments`(ced63f7c635d850d).autoWhisperVadSegmentscxx . . $ autoWhisperVadSegments# () { |
| 81 | //TRACE |
| 82 | tracecxx . . $ `melder/melder_debug.h:60:10`! (U"Destroying Silero-VAD segments at ", Melder_pointer (ptr)); |
| 83 | whisper_vad_free_segments (ptrcxx . . $ autoWhisperVadSegments#ptr.); |
| 84 | } |
| 85 | autoWhisperVadSegmentscxx . . $ autoWhisperVadSegments#& autoWhisperVadSegmentscxx . . $ autoWhisperVadSegments# :: operatorcxx . . $ autoWhisperVadSegments#`operator=`(1ee7ca4d4e20f33b).= (autoWhisperVadSegmentscxx . . $ autoWhisperVadSegments#&& other) noexcept { |
| 86 | if (this != & other) { |
| 87 | whisper_vad_free_segments (ptrcxx . . $ autoWhisperVadSegments#ptr.); |
| 88 | ptrcxx . . $ autoWhisperVadSegments#ptr. = other.ptrcxx . . $ autoWhisperVadSegments#ptr.; |
| 89 | other.ptrcxx . . $ autoWhisperVadSegments#ptr. = nullptr; |
| 90 | } |
| 91 | return *this; |
| 92 | } |
| 93 | |
| 94 | autoDiarizeContextcxx . . $ autoDiarizeContext# :: ~cxx . . $ autoDiarizeContext#`~autoDiarizeContext`(ced63f7c635d850d).autoDiarizeContextcxx . . $ autoDiarizeContext# () { |
| 95 | //TRACE |
| 96 | tracecxx . . $ `melder/melder_debug.h:60:10`! (U"Destroying diarize context at ", Melder_pointer (ptr)); |
| 97 | diarize_free (ptrcxx . . $ autoDiarizeContext#ptr.); |
| 98 | tracecxx . . $ `melder/melder_debug.h:60:10`! (U"Number of allocations in the memory pool is ", theGgmlMemoryPool. n_allocations()); |
| 99 | tracecxx . . $ `melder/melder_debug.h:60:10`! (U"Total memory in bytes is ", theGgmlMemoryPool. sizeInBytes()); |
| 100 | } |
| 101 | autoDiarizeContextcxx . . $ autoDiarizeContext#& autoDiarizeContextcxx . . $ autoDiarizeContext# :: operatorcxx . . $ autoDiarizeContext#`operator=`(ed12d908610be2c8).= (autoDiarizeContextcxx . . $ autoDiarizeContext#&& other) noexcept { |
| 102 | if (this != & other) { |
| 103 | diarize_free (ptrcxx . . $ autoDiarizeContext#ptr.); |
| 104 | ptrcxx . . $ autoDiarizeContext#ptr. = other.ptrcxx . . $ autoDiarizeContext#ptr.; |
| 105 | other.ptrcxx . . $ autoDiarizeContext#ptr. = nullptr; |
| 106 | } |
| 107 | return *this; |
| 108 | } |
| 109 | |
| 110 | Thing_implementcxx . . $ `sys/Thing.h:65:9`! (SpeechRecognizer, Daata, 0); |
| 111 | |
| 112 | void structSpeechRecognizercxx . . $ structSpeechRecognizer# :: v1_infocxx . . $ structSpeechRecognizer#v1_info(49f6e7a06ebc5aa8). () { |
| 113 | SpeechRecognizer_Parent :: v1_infocxx . . $ structThing#v1_info(49f6e7a06ebc5aa8). (); |
| 114 | MelderInfo_writeLinecxx . . $ MelderInfo_writeLine(3eb398314feba58b). (U"Model: ", ourcxx . . $ `melder/melder.h:42:9`! d_modelNamecxx . . $ structSpeechRecognizer#d_modelName..getcxx . . $ _autostring#get(5a2a78a048fb49a8).()); |
| 115 | MelderInfo_writeLinecxx . . $ MelderInfo_writeLine(3eb398314feba58b). (U"Language: ", ourcxx . . $ `melder/melder.h:42:9`! d_languageNamecxx . . $ structSpeechRecognizer#d_languageName..getcxx . . $ _autostring#get(5a2a78a048fb49a8).()); |
| 116 | } |
| 117 | |
| 118 | static void whisper_log_silentcxx . . $ whisper_log_silent(1235eb62ccdaecf6). (ggml_log_level level, const char *text, void *user_data) { |
| 119 | (void) level; |
| 120 | (void) text; |
| 121 | (void) user_data; |
| 122 | } |
| 123 | |
| 124 | static void supressGgmlLoggingcxx . . $ supressGgmlLogging(49f6e7a06ebc5aa8). () { |
| 125 | if (Melder_debugcxx . . $ Melder_debug. == 2001) |
| 126 | whisper_log_set (nullptr, nullptr); |
| 127 | else |
| 128 | whisper_log_set (whisper_log_silentcxx . . $ whisper_log_silent(1235eb62ccdaecf6)., nullptr); |
| 129 | } |
| 130 | |
| 131 | static conststring32cxx . . $ conststring32# theWhisperModelsFoldercxx . . $ theWhisperModelsFolder(1912f6684645c851). (); |
| 132 | |
| 133 | autoSpeechRecognizer SpeechRecognizer_createcxx . . $ SpeechRecognizer_create(159722423569d923). (const conststring32cxx . . $ conststring32# modelName, const conststring32cxx . . $ conststring32# languageName) { |
| 134 | try { |
| 135 | autoSpeechRecognizer me = Thing_newcxx . . $ `sys/Thing.h:514:9`! (SpeechRecognizer); |
| 136 | mycxx . . $ `melder/melder.h:44:9`! d_modelNamecxx . . $ structSpeechRecognizer#d_modelName. =cxx . . $ _autostring#`operator=`(f562943cbc4bcf37). Melder_dupcxx . . $ Melder_dup(87bf156b1f548402). (modelName); |
| 137 | mycxx . . $ `melder/melder.h:44:9`! d_languageNamecxx . . $ structSpeechRecognizer#d_languageName. =cxx . . $ _autostring#`operator=`(f562943cbc4bcf37). Melder_dupcxx . . $ Melder_dup(87bf156b1f548402). (languageName); |
| 138 | |
| 139 | /* |
| 140 | Check if selected model and language are compatible. |
| 141 | */ |
| 142 | if (str32strcxx . . $ str32str(2de17cd8f7c584a6). (modelName, U".en.bin")) { |
| 143 | Melder_requirecxx . . $ `melder/melder_require.h:21:9`! (str32str (languageName, U"Autodetect") || str32str (languageName, U"English"), |
| 144 | U"Model ", modelName, U" cannot be used for ", languageName, U" transcription. " |
| 145 | U"Either select a multilingual model (the model name does not include .en) " |
| 146 | U"or select \"Autodetect language\"/\"English\" from the language list." |
| 147 | ); |
| 148 | } |
| 149 | |
| 150 | /* |
| 151 | Create Whisper context. |
| 152 | */ |
| 153 | whisper_context_params contextParams = whisper_context_default_params (); |
| 154 | contextParams. use_gpu = false; |
| 155 | contextParams. flash_attn = false; // needs to be false to use DTW!!! |
| 156 | |
| 157 | /* |
| 158 | Enable DTW (Dynamic Time Warping algorithm used for more precise token boundaries). |
| 159 | */ |
| 160 | Melder_assertcxx . . $ `melder/melder_assert.h:39:10`! (! contextParams. flash_attn); |
| 161 | contextParams. dtw_token_timestamps = true; |
| 162 | if (str32strcxx . . $ str32str(2de17cd8f7c584a6). (modelName, U"tiny.en")) |
| 163 | contextParams. dtw_aheads_preset = WHISPER_AHEADS_TINY_EN; |
| 164 | else if (str32strcxx . . $ str32str(2de17cd8f7c584a6). (modelName, U"tiny")) |
| 165 | contextParams. dtw_aheads_preset = WHISPER_AHEADS_TINY; |
| 166 | else if (str32strcxx . . $ str32str(2de17cd8f7c584a6). (modelName, U"base.en")) |
| 167 | contextParams. dtw_aheads_preset = WHISPER_AHEADS_BASE_EN; |
| 168 | else if (str32strcxx . . $ str32str(2de17cd8f7c584a6). (modelName, U"base")) |
| 169 | contextParams. dtw_aheads_preset = WHISPER_AHEADS_BASE; |
| 170 | else if (str32strcxx . . $ str32str(2de17cd8f7c584a6). (modelName, U"small.en")) |
| 171 | contextParams. dtw_aheads_preset = WHISPER_AHEADS_SMALL_EN; |
| 172 | else if (str32strcxx . . $ str32str(2de17cd8f7c584a6). (modelName, U"small")) |
| 173 | contextParams. dtw_aheads_preset = WHISPER_AHEADS_SMALL; |
| 174 | else if (str32strcxx . . $ str32str(2de17cd8f7c584a6). (modelName, U"medium.en")) |
| 175 | contextParams. dtw_aheads_preset = WHISPER_AHEADS_MEDIUM_EN; |
| 176 | else if (str32strcxx . . $ str32str(2de17cd8f7c584a6). (modelName, U"medium")) |
| 177 | contextParams. dtw_aheads_preset = WHISPER_AHEADS_MEDIUM; |
| 178 | else if (str32strcxx . . $ str32str(2de17cd8f7c584a6). (modelName, U"large-v3-turbo") || str32strcxx . . $ str32str(2de17cd8f7c584a6). (modelName, U"turbo")) |
| 179 | contextParams. dtw_aheads_preset = WHISPER_AHEADS_LARGE_V3_TURBO; |
| 180 | else if (str32strcxx . . $ str32str(2de17cd8f7c584a6). (modelName, U"large-v3")) |
| 181 | contextParams. dtw_aheads_preset = WHISPER_AHEADS_LARGE_V3; |
| 182 | else if (str32strcxx . . $ str32str(2de17cd8f7c584a6). (modelName, U"large-v2")) |
| 183 | contextParams. dtw_aheads_preset = WHISPER_AHEADS_LARGE_V2; |
| 184 | else if (str32strcxx . . $ str32str(2de17cd8f7c584a6). (modelName, U"large-v1") || str32strcxx . . $ str32str(2de17cd8f7c584a6). (modelName, U"large")) |
| 185 | contextParams. dtw_aheads_preset = WHISPER_AHEADS_LARGE_V1; |
| 186 | else |
| 187 | contextParams. dtw_aheads_preset = WHISPER_AHEADS_N_TOP_MOST; |
| 188 | |
| 189 | const conststring32cxx . . $ conststring32# modelPath = Melder_catcxx . . $ Melder_cat(7fbf27f4d367f442). (theWhisperModelsFoldercxx . . $ theWhisperModelsFolder(1912f6684645c851).(), U"/", modelName); |
| 190 | const conststring8cxx . . $ conststring8# utf8ModelPath = Melder_peek32to8 (modelPath); |
| 191 | |
| 192 | /* |
| 193 | Initialise Whisper model. |
| 194 | */ |
| 195 | supressGgmlLoggingcxx . . $ supressGgmlLogging(49f6e7a06ebc5aa8). (); |
| 196 | whisper_contextcxx . . $ whisper_context# *ctx = whisper_init_from_file_with_params (utf8ModelPath, contextParams); |
| 197 | if (! ctx) |
| 198 | Melder_throwcxx . . $ `melder/melder_error.h:77:9`! (U"Cannot create Whisper context from: ", modelPath, U". Model file not found?"); |
| 199 | |
| 200 | mycxx . . $ `melder/melder.h:44:9`! whisperContextcxx . . $ structSpeechRecognizer#whisperContext. =cxx . . $ autoWhisperContext#`operator=`(dceedb20f2fe0c93). autoWhisperContextcxx . . $ autoWhisperContext# (ctx); |
| 201 | theLivingSpeechRecognizerscxx . . $ theLivingSpeechRecognizers.. insertcxx . . $ std/set#insert(10916b2dbd511ba8). (me.getcxx . . $ autoSomeThing#get(34d6acfd341ba7ab).()); |
| 202 | return mecxx . . $ autoSomeThing#autoSomeThing(b79141a71f7bc86f).; |
| 203 | } catch (MelderErrorcxx . . $ MelderError#) { |
| 204 | theGgmlMemoryPoolcxx . . $ theGgmlMemoryPool.. clearcxx . . $ GgmlMemoryPool#clear(49f6e7a06ebc5aa8).(); |
| 205 | Melder_throwcxx . . $ `melder/melder_error.h:77:9`! (U"SpeechRecognizer not created."); |
| 206 | } |
| 207 | } |
| 208 | |
| 209 | static autovectorcxx . . $ autovector# <float> resampleForWhispercxx . . $ resampleForWhisper(3e5018fdb663e588). (constSoundcxx . . $ constSound# sound) { |
| 210 | /* |
| 211 | Resample the sound to 16kHz if needed. |
| 212 | */ |
| 213 | autoSoundcxx . . $ autoSound# resampled = Sound_resamplecxx . . $ Sound_resample(872961da5f5e69e5). (sound, 16000.0, 50); |
| 214 | sound = resampled.getcxx . . $ autoSomeThing#get(34d6acfd341ba7ab).(); |
| 215 | |
| 216 | /* |
| 217 | Convert the sound to float32 for Whisper. |
| 218 | */ |
| 219 | autovectorcxx . . $ autovector# <float> samples32 = newvectorzerocxx . . $ newvectorzero(d6d5973469733409). <float> (sound -> nxcxx . . $ structSampled#nx.);; |
| 220 | for (integercxx . . $ integer# i = 1; i <= sound -> nxcxx . . $ structSampled#nx.; i ++) |
| 221 | samples32 [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).i] = static_cast <float> (sound -> zcxx . . $ structMatrix#z. [cxx . . $ automatrix#`operator[]`(2d545b3cb3f8e666).1] [cxx . . $ vector#`operator[]`(8a4599b8835d1ce1).i]); |
| 222 | |
| 223 | return samples32cxx . . $ autovector#autovector(d33c649a3aceae2f).; |
| 224 | } |
| 225 | |
| 226 | static void SpeechRecognizer_runWhispercxx . . $ SpeechRecognizer_runWhisper(20d72abc73ec3472). (constSpeechRecognizer me, constSoundcxx . . $ constSound# sound, const bool useVad, |
| 227 | const double speechProbabilityThreshold, const double minNonSpeechDuration, const double minSpeechDuration, const double speechPad) { |
| 228 | //TRACE |
| 229 | /* |
| 230 | Prepare sound for Whispercpp. |
| 231 | */ |
| 232 | autovectorcxx . . $ autovector# <float> samples32 = resampleForWhispercxx . . $ resampleForWhisper(3e5018fdb663e588). (sound); |
| 233 | |
| 234 | /* |
| 235 | Default maximum number of threads for transcription is half the number of logical processors. This is because: |
| 236 | 1. hyperthreaded CPUs: half = physical cores, optimal for SIMD kernels. |
| 237 | 2. hybrid P+E core CPUs: half is "mostly P-cores"; using all the cores can cause a 100x slowdown. |
| 238 | 3. homogeneous non-hyperthreaded CPUs: half is below optimal, but not disastrous as in case 2. |
| 239 | The user can override this in Praat preferences. |
| 240 | */ |
| 241 | integercxx . . $ integer# n_threads = SpeechRecognizer_getMaxNumberOfThreadsForTranscriptioncxx . . $ SpeechRecognizer_getMaxNumberOfThreadsForTranscription(da6335ded91d79c2). (); |
| 242 | if (n_threads <= 0) |
| 243 | n_threads = TranscriptionDefaultscxx . . $ TranscriptionDefaults/::n_threadscxx . . $ TranscriptionDefaults/n_threads.; |
| 244 | |
| 245 | /* |
| 246 | Set Whisper parameters. |
| 247 | */ |
| 248 | whisper_sampling_strategy samplingStrategy = |
| 249 | Melder_debugcxx . . $ Melder_debug. == 2002 ? WHISPER_SAMPLING_GREEDY : WHISPER_SAMPLING_BEAM_SEARCH; |
| 250 | tracecxx . . $ `melder/melder_debug.h:60:10`! (U"Sampling strategy = ", samplingStrategy == WHISPER_SAMPLING_GREEDY ? U"greedy" : U"beam search"); |
| 251 | whisper_full_params params = whisper_full_default_params (samplingStrategy); |
| 252 | params. token_timestamps = true; // must be true to use t0 and t1 (non-DTW) token timestamps |
| 253 | params. n_threads = static_cast<int32_t> (n_threads); |
| 254 | |
| 255 | if (useVad) { |
| 256 | params. vad = true; // enable Silero VAD (Voice Activity Detection used to chop away the silences) |
| 257 | params. vad_model_data = ggml_silero_bincxx . . $ ggml_silero_bin.; // set up either params.vad_model_data or params.vad_model_path |
| 258 | params. vad_model_data_size = ggml_silero_bin_lencxx . . $ ggml_silero_bin_len.; |
| 259 | params. vad_params = whisper_vad_default_params(); |
| 260 | params. vad_params. threshold = static_cast <float> (speechProbabilityThreshold); |
| 261 | params. vad_params. min_speech_duration_ms = static_cast <int> (minSpeechDuration * 1000.0); |
| 262 | params. vad_params. min_silence_duration_ms = static_cast <int> (minNonSpeechDuration * 1000.0); |
| 263 | params. vad_params. speech_pad_ms = static_cast <int> (speechPad * 1000.0); |
| 264 | params. vad_params. samples_overlap = 0.1f; // this is the default, but brought here to remember that it is configurable |
| 265 | } |
| 266 | if (whisper_is_multilingual (mycxx . . $ `melder/melder.h:44:9`! whisperContextcxx . . $ structSpeechRecognizer#whisperContext..getcxx . . $ autoWhisperContext#get(9bd6dc1778dd7f42). ())) { |
| 267 | if (mycxx . . $ `melder/melder.h:44:9`! d_languageNamecxx . . $ structSpeechRecognizer#d_languageName. && ! str32strcxx . . $ str32str(2de17cd8f7c584a6). (mycxx . . $ `melder/melder.h:44:9`! d_languageNamecxx . . $ structSpeechRecognizer#d_languageName..getcxx . . $ _autostring#get(5a2a78a048fb49a8).(), U"Autodetect")) { |
| 268 | autostring32cxx . . $ autostring32# name = Melder_dupcxx . . $ Melder_dup(87bf156b1f548402). (mycxx . . $ `melder/melder.h:44:9`! d_languageNamecxx . . $ structSpeechRecognizer#d_languageName..getcxx . . $ _autostring#get(5a2a78a048fb49a8).()); |
| 269 | name [cxx . . $ _autostring#`operator[]`(b190d49faf30c5be).0] = Melder_toLowerCasecxx . . $ Melder_toLowerCase(cd1afca5b9692c51). (name [cxx . . $ _autostring#`operator[]`(b190d49faf30c5be).0]); // e.g. "Dutch" -> "dutch" |
| 270 | params.language = whisper_lang_str (whisper_lang_id (Melder_peek32to8 (name.getcxx . . $ _autostring#get(5a2a78a048fb49a8).()))); |
| 271 | } else { |
| 272 | params.language = "auto"; |
| 273 | } |
| 274 | } |
| 275 | |
| 276 | /* |
| 277 | Run Whisper. |
| 278 | */ |
| 279 | supressGgmlLoggingcxx . . $ supressGgmlLogging(49f6e7a06ebc5aa8). (); |
| 280 | try { |
| 281 | if (whisper_full (mycxx . . $ `melder/melder.h:44:9`! whisperContextcxx . . $ structSpeechRecognizer#whisperContext..getcxx . . $ autoWhisperContext#get(9bd6dc1778dd7f42).(), params, samples32. asArgumentToFunctionThatExpectsZeroBasedArraycxx . . $ autovector#asArgumentToFunctionThatExpectsZeroBasedArray(5a2a78a048fb49a8).(), |
| 282 | static_cast <int> (samples32.sizecxx . . $ autovector#size.)) != 0) |
| 283 | Melder_throwcxx . . $ `melder/melder_error.h:77:9`! (U"Whisper failed to process audio"); |
| 284 | whisper_print_timings(mycxx . . $ `melder/melder.h:44:9`! whisperContextcxx . . $ structSpeechRecognizer#whisperContext..getcxx . . $ autoWhisperContext#get(9bd6dc1778dd7f42).()); |
| 285 | } catch (MelderErrorcxx . . $ MelderError#) { |
| 286 | theGgmlMemoryPoolcxx . . $ theGgmlMemoryPool.. clearcxx . . $ GgmlMemoryPool#clear(49f6e7a06ebc5aa8).(); |
| 287 | Melder_throwcxx . . $ `melder/melder_error.h:77:9`! (U"Whisper run out of memory. SpeechRecognizer objects are no longer usable and must be recreated."); |
| 288 | } |
| 289 | } |
| 290 | |
| 291 | static bool endsWithTerminalPunctuationcxx . . $ endsWithTerminalPunctuation(2df281243f91fe91).(const conststring32cxx . . $ conststring32# token) { |
| 292 | if (! token || token [0] == U'\0') |
| 293 | return false; |
| 294 | |
| 295 | const integercxx . . $ integer# lengthOfToken = Melder_lengthcxx . . $ Melder_length(3fae411214b133ad). (token); |
| 296 | const char32cxx . . $ char32# lastChar = token [lengthOfToken - 1]; |
| 297 | return lastChar == U'.' || lastChar == U'!' || lastChar == U'?' || |
| 298 | lastChar == U'。' || lastChar == U'!' || lastChar == U'?'; |
| 299 | } |
| 300 | |
| 301 | static bool isPurePunctuationcxx . . $ isPurePunctuation(2df281243f91fe91).(const conststring32cxx . . $ conststring32# token) { |
| 302 | if (! token || token [0] == U'\0') |
| 303 | return false; |
| 304 | |
| 305 | const integercxx . . $ integer# lengthOfToken = Melder_lengthcxx . . $ Melder_length(3fae411214b133ad). (token); |
| 306 | for (integercxx . . $ integer# i = 0; i < lengthOfToken; i ++) |
| 307 | if (Melder_isAlphanumericcxx . . $ Melder_isAlphanumeric(b8d0204ce8416907). (token [i])) |
| 308 | return false; |
| 309 | return true; |
| 310 | } |
| 311 | |
| 312 | WhisperTranscriptioncxx . . $ WhisperTranscription# SpeechRecognizer_recognizecxx . . $ SpeechRecognizer_recognize(c5e825ed147f56af). (constSpeechRecognizer me, constSoundcxx . . $ constSound# sound, const bool useVad, |
| 313 | const double speechProbabilityThreshold, const double minNonSpeechDuration, const double minSpeechDuration, const double speechPad) { |
| 314 | try { |
| 315 | TRACEcxx . . $ `melder/melder_debug.h:54:9`! |
| 316 | Melder_requirecxx . . $ `melder/melder_require.h:21:9`! (mycxx . . $ `melder/melder.h:44:9`! whisperContext.get(), |
| 317 | U"This SpeechRecognizer object is not usable anymore as it ran out of memory. ", |
| 318 | U"Please remove it and create a new one." |
| 319 | ); |
| 320 | tracecxx . . $ `melder/melder_debug.h:60:10`! (U"Sound xmin = ", sound -> xmin, U", sound xmax = ", sound -> xmax); |
| 321 | |
| 322 | /* |
| 323 | Run Whisper and control for blank audio. |
| 324 | */ |
| 325 | SpeechRecognizer_runWhispercxx . . $ SpeechRecognizer_runWhisper(20d72abc73ec3472). (me, sound, useVad, speechProbabilityThreshold, minNonSpeechDuration, minSpeechDuration, speechPad); |
| 326 | const int numberOfSegments = whisper_full_n_segments (mycxx . . $ `melder/melder.h:44:9`! whisperContextcxx . . $ structSpeechRecognizer#whisperContext..getcxx . . $ autoWhisperContext#get(9bd6dc1778dd7f42).()); |
| 327 | if (! numberOfSegments) { |
| 328 | WhisperTranscriptioncxx . . $ WhisperTranscription# transcription; |
| 329 | transcription. fullTranscriptioncxx . . $ WhisperTranscription#fullTranscription.. textcxx . . $ SpeechSegment#text. =cxx . . $ _autostring#`operator=`(f562943cbc4bcf37). Melder_dupcxx . . $ Melder_dup(87bf156b1f548402). (U"[BLANK_AUDIO]"); |
| 330 | transcription. fullTranscriptioncxx . . $ WhisperTranscription#fullTranscription.. tmincxx . . $ SpeechSegment#tmin. = sound -> xmincxx . . $ structFunction#xmin.; |
| 331 | transcription. fullTranscriptioncxx . . $ WhisperTranscription#fullTranscription.. tmaxcxx . . $ SpeechSegment#tmax. = sound -> xmaxcxx . . $ structFunction#xmax.; |
| 332 | transcription. wordscxx . . $ WhisperTranscription#words. =cxx . . $ autovector#`operator=`(f4f0443dbfdd1e40). newvectorzerocxx . . $ newvectorzero(d6d5973469733409). <SpeechSegmentcxx . . $ SpeechSegment#> (1); |
| 333 | transcription. wordscxx . . $ WhisperTranscription#words. [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).1]. textcxx . . $ SpeechSegment#text. =cxx . . $ _autostring#`operator=`(f562943cbc4bcf37). Melder_dupcxx . . $ Melder_dup(87bf156b1f548402). (U"BLANKAUDIO"); |
| 334 | transcription. wordscxx . . $ WhisperTranscription#words. [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).1]. tmincxx . . $ SpeechSegment#tmin. = sound -> xmincxx . . $ structFunction#xmin.; |
| 335 | transcription. wordscxx . . $ WhisperTranscription#words. [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).1]. tmaxcxx . . $ SpeechSegment#tmax. = sound -> xmaxcxx . . $ structFunction#xmax.; |
| 336 | transcription. sentencescxx . . $ WhisperTranscription#sentences. =cxx . . $ autovector#`operator=`(f4f0443dbfdd1e40). newvectorzerocxx . . $ newvectorzero(d6d5973469733409). <SpeechSegmentcxx . . $ SpeechSegment#> (1); |
| 337 | transcription. sentencescxx . . $ WhisperTranscription#sentences. [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).1]. textcxx . . $ SpeechSegment#text. =cxx . . $ _autostring#`operator=`(f562943cbc4bcf37). Melder_dupcxx . . $ Melder_dup(87bf156b1f548402). (U"[BLANK_AUDIO]"); |
| 338 | transcription. sentencescxx . . $ WhisperTranscription#sentences. [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).1]. tmincxx . . $ SpeechSegment#tmin. = sound -> xmincxx . . $ structFunction#xmin.; |
| 339 | transcription. sentencescxx . . $ WhisperTranscription#sentences. [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).1]. tmaxcxx . . $ SpeechSegment#tmax. = sound -> xmaxcxx . . $ structFunction#xmax.; |
| 340 | return transcription; |
| 341 | } |
| 342 | |
| 343 | const int numberOfVadSegments = whisper_full_n_vad_segments (mycxx . . $ `melder/melder.h:44:9`! whisperContextcxx . . $ structSpeechRecognizer#whisperContext..getcxx . . $ autoWhisperContext#get(9bd6dc1778dd7f42).()); |
| 344 | struct VadSegmentcxx . . $ SpeechRecognizer_recognize(c5e825ed147f56af).VadSegment# { |
| 345 | double origStartcxx . . $ SpeechRecognizer_recognize(c5e825ed147f56af).VadSegment#origStart.; |
| 346 | double origEndcxx . . $ SpeechRecognizer_recognize(c5e825ed147f56af).VadSegment#origEnd.; |
| 347 | double vadStartcxx . . $ SpeechRecognizer_recognize(c5e825ed147f56af).VadSegment#vadStart.; |
| 348 | double vadEndcxx . . $ SpeechRecognizer_recognize(c5e825ed147f56af).VadSegment#vadEnd.; |
| 349 | }; |
| 350 | autovectorcxx . . $ autovector# <VadSegmentcxx . . $ SpeechRecognizer_recognize(c5e825ed147f56af).VadSegment#> vadSegments = newvectorzerocxx . . $ newvectorzero(d6d5973469733409). <VadSegmentcxx . . $ SpeechRecognizer_recognize(c5e825ed147f56af).VadSegment#> (numberOfVadSegments); |
| 351 | for (integercxx . . $ integer# i = 1; i <= numberOfVadSegments; i ++) { |
| 352 | vadSegments [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).i]. origStartcxx . . $ SpeechRecognizer_recognize(c5e825ed147f56af).VadSegment#origStart. = stdcxx . . $ std/::mincxx . . $ std/min(2a338fce5842aa6a). ( |
| 353 | static_cast <double> (whisper_full_get_vad_segment_orig_start (mycxx . . $ `melder/melder.h:44:9`! whisperContextcxx . . $ structSpeechRecognizer#whisperContext..getcxx . . $ autoWhisperContext#get(9bd6dc1778dd7f42).(), i - 1)) / 100.0, |
| 354 | sound -> xmaxcxx . . $ structFunction#xmax. |
| 355 | ); |
| 356 | vadSegments [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).i]. origEndcxx . . $ SpeechRecognizer_recognize(c5e825ed147f56af).VadSegment#origEnd. = stdcxx . . $ std/::mincxx . . $ std/min(2a338fce5842aa6a). ( |
| 357 | static_cast <double> (whisper_full_get_vad_segment_orig_end (mycxx . . $ `melder/melder.h:44:9`! whisperContextcxx . . $ structSpeechRecognizer#whisperContext..getcxx . . $ autoWhisperContext#get(9bd6dc1778dd7f42).(), i - 1)) / 100.0, |
| 358 | sound -> xmaxcxx . . $ structFunction#xmax. |
| 359 | ); |
| 360 | vadSegments [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).i]. vadStartcxx . . $ SpeechRecognizer_recognize(c5e825ed147f56af).VadSegment#vadStart. = |
| 361 | static_cast <double> (whisper_full_get_vad_segment_vad_start (mycxx . . $ `melder/melder.h:44:9`! whisperContextcxx . . $ structSpeechRecognizer#whisperContext..getcxx . . $ autoWhisperContext#get(9bd6dc1778dd7f42).(), i - 1)) / 100.0; |
| 362 | vadSegments [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).i]. vadEndcxx . . $ SpeechRecognizer_recognize(c5e825ed147f56af).VadSegment#vadEnd. = |
| 363 | static_cast <double> (whisper_full_get_vad_segment_vad_end (mycxx . . $ `melder/melder.h:44:9`! whisperContextcxx . . $ structSpeechRecognizer#whisperContext..getcxx . . $ autoWhisperContext#get(9bd6dc1778dd7f42).(), i - 1)) / 100.0; |
| 364 | tracecxx . . $ `melder/melder_debug.h:60:10`! (U"VAD segment ", i, U", orig_start = ", vadSegments [i]. origStart, U", orig_end = ", vadSegments [i]. origEnd, |
| 365 | U", vad_start = ", vadSegments [i]. vadStart, U", vad_end = ", vadSegments [i]. vadEnd); |
| 366 | } |
| 367 | |
| 368 | /* |
| 369 | Lambda for VAD time to real time conversion. |
| 370 | */ |
| 371 | auto vadToRealTime = [& vadSegments, useVad] (double vadTime) { |
| 372 | if (! useVad || ! vadSegments.sizecxx . . $ autovector#size.) |
| 373 | return vadTime; // no VAD: VAD time is the real time |
| 374 | |
| 375 | /* mutable scan */ integercxx . . $ integer# k = 1; |
| 376 | while (k < vadSegments.sizecxx . . $ autovector#size. && vadSegments [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).k + 1]. vadStartcxx . . $ SpeechRecognizer_recognize(c5e825ed147f56af).VadSegment#vadStart. <= vadTime) |
| 377 | k ++; |
| 378 | return vadTime + vadSegments [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).k]. origStartcxx . . $ SpeechRecognizer_recognize(c5e825ed147f56af).VadSegment#origStart. - vadSegments [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).k]. vadStartcxx . . $ SpeechRecognizer_recognize(c5e825ed147f56af).VadSegment#vadStart.; |
| 379 | }; |
| 380 | |
| 381 | /* |
| 382 | Lambda for finding the end of the VAD segment (in VAD time) that contains vadTime. |
| 383 | Note that vadSegments [k]. vadEnd is not used. This is because whisper.cpp pads each VAD segment |
| 384 | with 0.1 seconds of context on the right side, and vadEnd points to the end of that padding. |
| 385 | Whisper.cpp additionally adds 0.1 seconds of zeroes after each padded VAD segment, |
| 386 | resulting in 0.2 seconds of "non-speech" between VAD segments. |
| 387 | A vadTime in these 0.2 seconds after a VAD segment is also considered belonging to this segment. |
| 388 | */ |
| 389 | auto vadSegmentEnd = [& vadSegments] (double vadTime) { |
| 390 | Melder_assertcxx . . $ `melder/melder_assert.h:39:10`! (vadTime >= vadSegments [1]. vadStart); |
| 391 | /* mutable scan */ integercxx . . $ integer# k = 1; |
| 392 | while (k < vadSegments.sizecxx . . $ autovector#size. && vadSegments [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).k + 1]. vadStartcxx . . $ SpeechRecognizer_recognize(c5e825ed147f56af).VadSegment#vadStart. <= vadTime) |
| 393 | k ++; |
| 394 | return vadSegments [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).k]. vadStartcxx . . $ SpeechRecognizer_recognize(c5e825ed147f56af).VadSegment#vadStart. + (vadSegments [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).k]. origEndcxx . . $ SpeechRecognizer_recognize(c5e825ed147f56af).VadSegment#origEnd. - vadSegments [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).k]. origStartcxx . . $ SpeechRecognizer_recognize(c5e825ed147f56af).VadSegment#origStart.); |
| 395 | }; |
| 396 | |
| 397 | /* |
| 398 | Collect all the tokens into a flat list, repairing incomplete UTF8 tokens. |
| 399 | */ |
| 400 | struct RawTokencxx . . $ SpeechRecognizer_recognize(c5e825ed147f56af).RawToken# { |
| 401 | autostring32cxx . . $ autostring32# textcxx . . $ SpeechRecognizer_recognize(c5e825ed147f56af).RawToken#text.; // leading space intact |
| 402 | double tmaxcxx . . $ SpeechRecognizer_recognize(c5e825ed147f56af).RawToken#tmax.; |
| 403 | integercxx . . $ integer# whisperSegmentcxx . . $ SpeechRecognizer_recognize(c5e825ed147f56af).RawToken#whisperSegment.; |
| 404 | }; |
| 405 | autovectorcxx . . $ autovector# <RawTokencxx . . $ SpeechRecognizer_recognize(c5e825ed147f56af).RawToken#> rawTokens = newvectorzerocxx . . $ newvectorzero(d6d5973469733409). <RawTokencxx . . $ SpeechRecognizer_recognize(c5e825ed147f56af).RawToken#> (0); |
| 406 | |
| 407 | /* mutable accumulate */ stdcxx . . $ std/::stringcxx . . $ std/string# partialTokenTextcxx . . $ std/__cxx11/basic_string#basic_string(925dcc5e17f50391).; // here we will accumulate whisper token texts until it is a proper UTF8 string |
| 408 | const whisper_token firstSpecialTokenId = whisper_token_eot (mycxx . . $ `melder/melder.h:44:9`! whisperContextcxx . . $ structSpeechRecognizer#whisperContext..getcxx . . $ autoWhisperContext#get(9bd6dc1778dd7f42).()); // see struct whisper_vocab in whisper.cpp |
| 409 | |
| 410 | for (integercxx . . $ integer# i = 0; i < numberOfSegments; i ++) { |
| 411 | /* mutable flag for tracing */ bool isFirstPartialTokenInSegment = true; |
| 412 | for (integercxx . . $ integer# j = 0; j < whisper_full_n_tokens (mycxx . . $ `melder/melder.h:44:9`! whisperContextcxx . . $ structSpeechRecognizer#whisperContext..getcxx . . $ autoWhisperContext#get(9bd6dc1778dd7f42).(), static_cast<int> (i)); j ++) { |
| 413 | const whisper_token_data tokenData = whisper_full_get_token_data ( |
| 414 | mycxx . . $ `melder/melder.h:44:9`! whisperContextcxx . . $ structSpeechRecognizer#whisperContext..getcxx . . $ autoWhisperContext#get(9bd6dc1778dd7f42).(), static_cast<int> (i), static_cast<int> (j)); |
| 415 | if (tokenData. id >= firstSpecialTokenId) { |
| 416 | tracecxx . . $ `melder/melder_debug.h:60:10`! (U"Skipping special token: ", tokenData. id); |
| 417 | continue; // skip special tokens |
| 418 | } |
| 419 | |
| 420 | /* |
| 421 | Clamp the tail of the previous segment. |
| 422 | */ |
| 423 | if (isFirstPartialTokenInSegment) { |
| 424 | if (rawTokens.sizecxx . . $ autovector#size. >= 1) { |
| 425 | /* mutable search */ integercxx . . $ integer# lastPreserved = rawTokens.sizecxx . . $ autovector#size.; |
| 426 | /* |
| 427 | Find last segment's tokens to keep before clamping by the beginning of the current Whisper segment. |
| 428 | */ |
| 429 | const integercxx . . $ integer# previousSegment = rawTokens [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).rawTokens.sizecxx . . $ autovector#size.]. whisperSegmentcxx . . $ SpeechRecognizer_recognize(c5e825ed147f56af).RawToken#whisperSegment.; |
| 430 | /* mutable, may be lowered */ double border = stdcxx . . $ std/::mincxx . . $ std/min(2a338fce5842aa6a). (tokenData. t0 / 100.0, tokenData. t_dtw / 100.0); |
| 431 | while (lastPreserved >= 1 && rawTokens [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).lastPreserved]. whisperSegmentcxx . . $ SpeechRecognizer_recognize(c5e825ed147f56af).RawToken#whisperSegment. == previousSegment |
| 432 | && rawTokens [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).lastPreserved]. tmaxcxx . . $ SpeechRecognizer_recognize(c5e825ed147f56af).RawToken#tmax. > border) |
| 433 | lastPreserved --; |
| 434 | /* |
| 435 | Also apply Inter-VAD segment clamping: the tail of the previous Whisper segment must stay |
| 436 | in the VAD segment where the preserved part of the tail ends. |
| 437 | */ |
| 438 | if (useVad && vadSegments.sizecxx . . $ autovector#size. >= 1 && lastPreserved >= 1 && lastPreserved < rawTokens.sizecxx . . $ autovector#size.) { |
| 439 | const double ceiling = vadSegmentEnd (cxx . . $ SpeechRecognizer_recognize(c5e825ed147f56af).$anonymous_type_0#`operator()`(7e3a3c6b59947c6d).rawTokens [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).lastPreserved]. tmaxcxx . . $ SpeechRecognizer_recognize(c5e825ed147f56af).RawToken#tmax.); |
| 440 | if (ceiling < border) { |
| 441 | border = ceiling; |
| 442 | while (lastPreserved >= 1 && rawTokens [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).lastPreserved]. whisperSegmentcxx . . $ SpeechRecognizer_recognize(c5e825ed147f56af).RawToken#whisperSegment. == previousSegment |
| 443 | && rawTokens [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).lastPreserved]. tmaxcxx . . $ SpeechRecognizer_recognize(c5e825ed147f56af).RawToken#tmax. > border) |
| 444 | lastPreserved --; |
| 445 | } |
| 446 | } |
| 447 | if (lastPreserved < rawTokens.sizecxx . . $ autovector#size.) // there is something to clamp |
| 448 | tracecxx . . $ `melder/melder_debug.h:60:10`! (U"Clamping ", rawTokens.size - lastPreserved, U" tail token(s) of segment ", previousSegment, U" to ", vadToRealTime (border)); |
| 449 | for (integercxx . . $ integer# m = lastPreserved + 1; m <= rawTokens.sizecxx . . $ autovector#size.; m ++) |
| 450 | rawTokens [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).m]. tmaxcxx . . $ SpeechRecognizer_recognize(c5e825ed147f56af).RawToken#tmax. = border; // actual clamping |
| 451 | } |
| 452 | tracecxx . . $ `melder/melder_debug.h:60:10`! (U"Segment ", i, U" first-token t0 = ", vadToRealTime (tokenData. t0 / 100.0), |
| 453 | U", segment_t0 = ", whisper_full_get_segment_t0 (mycxx . . $ `melder/melder.h:44:9`! whisperContext.get(), i) / 100.0, |
| 454 | U", segment_t1 = ", whisper_full_get_segment_t1 (mycxx . . $ `melder/melder.h:44:9`! whisperContext.get(), i) / 100.0 |
| 455 | ); |
| 456 | isFirstPartialTokenInSegment = false; |
| 457 | } |
| 458 | |
| 459 | partialTokenText +=cxx . . $ std/__cxx11/basic_string#`operator+=`(978ca1133b5f6e74). whisper_full_get_token_text (mycxx . . $ `melder/melder.h:44:9`! whisperContextcxx . . $ structSpeechRecognizer#whisperContext..getcxx . . $ autoWhisperContext#get(9bd6dc1778dd7f42).(), i, j); |
| 460 | const double currentTokenTmax = tokenData. t_dtw / 100.0; |
| 461 | |
| 462 | if (! Melder_str8IsValidUtf8cxx . . $ Melder_str8IsValidUtf8(c5f7da30cbc0f4e9). (partialTokenText.c_strcxx . . $ std/__cxx11/basic_string#c_str(8e614c7de0b1573e).())) |
| 463 | continue; // continue accumulating token texts until partialTokenText is a proper UTF8 string |
| 464 | |
| 465 | /* |
| 466 | Now partialTokenText is valid UTF8. |
| 467 | */ |
| 468 | RawTokencxx . . $ SpeechRecognizer_recognize(c5e825ed147f56af).RawToken# *rawToken = rawTokens. appendcxx . . $ autovector#append(155dc7f103cdb9db).(); |
| 469 | rawToken -> textcxx . . $ SpeechRecognizer_recognize(c5e825ed147f56af).RawToken#text. =cxx . . $ _autostring#`operator=`(f562943cbc4bcf37). Melder_8to32_ecxx . . $ Melder_8to32_e(f01fe69d6ed2498b). (partialTokenText.c_strcxx . . $ std/__cxx11/basic_string#c_str(8e614c7de0b1573e).()); |
| 470 | rawToken -> tmaxcxx . . $ SpeechRecognizer_recognize(c5e825ed147f56af).RawToken#tmax. = currentTokenTmax; |
| 471 | rawToken -> whisperSegmentcxx . . $ SpeechRecognizer_recognize(c5e825ed147f56af).RawToken#whisperSegment. = i; |
| 472 | partialTokenText. clearcxx . . $ std/__cxx11/basic_string#clear(ced63f7c635d850d).(); |
| 473 | |
| 474 | tracecxx . . $ `melder/melder_debug.h:60:10`! (U"Segment ", i, U"; original token in it ", j, U": text = \"", rawToken -> text.get(), |
| 475 | U"\", t_dtw = ", vadToRealTime (currentTokenTmax), |
| 476 | U"\", t0 = ", vadToRealTime (tokenData. t0 / 100.0), |
| 477 | U"\", t1 = ", vadToRealTime (tokenData. t1 / 100.0) |
| 478 | ); |
| 479 | } |
| 480 | } |
| 481 | |
| 482 | /* |
| 483 | Group Whisper tokens into words. |
| 484 | */ |
| 485 | struct WhisperTokencxx . . $ SpeechRecognizer_recognize(c5e825ed147f56af).WhisperToken# { |
| 486 | autostring32cxx . . $ autostring32# textcxx . . $ SpeechRecognizer_recognize(c5e825ed147f56af).WhisperToken#text.; |
| 487 | double tmaxcxx . . $ SpeechRecognizer_recognize(c5e825ed147f56af).WhisperToken#tmax.; |
| 488 | }; |
| 489 | |
| 490 | struct WhisperWordcxx . . $ SpeechRecognizer_recognize(c5e825ed147f56af).WhisperWord# { |
| 491 | autovectorcxx . . $ autovector# <WhisperTokencxx . . $ SpeechRecognizer_recognize(c5e825ed147f56af).WhisperToken#> whisperTokenscxx . . $ SpeechRecognizer_recognize(c5e825ed147f56af).WhisperWord#whisperTokens. = newvectorzerocxx . . $ newvectorzero(d6d5973469733409). <WhisperTokencxx . . $ SpeechRecognizer_recognize(c5e825ed147f56af).WhisperToken#> (0); |
| 492 | autostring32cxx . . $ autostring32# textWithPunctuationcxx . . $ SpeechRecognizer_recognize(c5e825ed147f56af).WhisperWord#textWithPunctuation.; |
| 493 | double tmaxcxx . . $ SpeechRecognizer_recognize(c5e825ed147f56af).WhisperWord#tmax.; |
| 494 | }; |
| 495 | |
| 496 | autovectorcxx . . $ autovector# <WhisperWordcxx . . $ SpeechRecognizer_recognize(c5e825ed147f56af).WhisperWord#> whisperWords = newvectorzerocxx . . $ newvectorzero(d6d5973469733409). <WhisperWordcxx . . $ SpeechRecognizer_recognize(c5e825ed147f56af).WhisperWord#> (0); |
| 497 | for (integercxx . . $ integer# i = 1; i <= rawTokens.sizecxx . . $ autovector#size.; i ++) { |
| 498 | RawTokencxx . . $ SpeechRecognizer_recognize(c5e825ed147f56af).RawToken#& rawToken = rawTokens [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).i]; |
| 499 | |
| 500 | /* |
| 501 | Create a new WhisperWord if |
| 502 | - the current token starts with a space or if autovector <WhisperWord> whisperWords is still empty |
| 503 | - and the previous word is not a standalone "-". |
| 504 | Otherwise, append the current token to the existing word. |
| 505 | */ |
| 506 | const bool isNewWord = rawToken. textcxx . . $ SpeechRecognizer_recognize(c5e825ed147f56af).RawToken#text..getcxx . . $ _autostring#get(5a2a78a048fb49a8).() [0] == U' '; |
| 507 | const bool isLastWordDash = whisperWords.sizecxx . . $ autovector#size. >= 1 && |
| 508 | Melder_lengthcxx . . $ Melder_length(3fae411214b133ad). (whisperWords [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).whisperWords.sizecxx . . $ autovector#size.]. textWithPunctuationcxx . . $ SpeechRecognizer_recognize(c5e825ed147f56af).WhisperWord#textWithPunctuation..getcxx . . $ _autostring#get(5a2a78a048fb49a8).()) == 1 && |
| 509 | whisperWords [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).whisperWords.sizecxx . . $ autovector#size.]. textWithPunctuationcxx . . $ SpeechRecognizer_recognize(c5e825ed147f56af).WhisperWord#textWithPunctuation..getcxx . . $ _autostring#get(5a2a78a048fb49a8).() [0] == U'-'; |
| 510 | |
| 511 | WhisperWordcxx . . $ SpeechRecognizer_recognize(c5e825ed147f56af).WhisperWord# *word; |
| 512 | if ((isNewWord || whisperWords.sizecxx . . $ autovector#size. == 0) && ! isLastWordDash) |
| 513 | word = whisperWords. appendcxx . . $ autovector#append(155dc7f103cdb9db).(); |
| 514 | else |
| 515 | word = &whisperWords [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).whisperWords.sizecxx . . $ autovector#size.]; |
| 516 | WhisperTokencxx . . $ SpeechRecognizer_recognize(c5e825ed147f56af).WhisperToken# *token = word -> whisperTokenscxx . . $ SpeechRecognizer_recognize(c5e825ed147f56af).WhisperWord#whisperTokens..appendcxx . . $ autovector#append(155dc7f103cdb9db).(); |
| 517 | |
| 518 | token -> textcxx . . $ SpeechRecognizer_recognize(c5e825ed147f56af).WhisperToken#text. =cxx . . $ _autostring#`operator=`(f562943cbc4bcf37). Melder_dupcxx . . $ Melder_dup(87bf156b1f548402). (rawToken. textcxx . . $ SpeechRecognizer_recognize(c5e825ed147f56af).RawToken#text..getcxx . . $ _autostring#get(5a2a78a048fb49a8).() + (isNewWord ? 1 : 0)); |
| 519 | word -> textWithPunctuationcxx . . $ SpeechRecognizer_recognize(c5e825ed147f56af).WhisperWord#textWithPunctuation. =cxx . . $ _autostring#`operator=`(f562943cbc4bcf37). Melder_dupcxx . . $ Melder_dup(87bf156b1f548402). (Melder_catcxx . . $ Melder_cat(7fbf27f4d367f442). (word -> textWithPunctuationcxx . . $ SpeechRecognizer_recognize(c5e825ed147f56af).WhisperWord#textWithPunctuation..getcxx . . $ _autostring#get(5a2a78a048fb49a8).(), token -> textcxx . . $ SpeechRecognizer_recognize(c5e825ed147f56af).WhisperToken#text..getcxx . . $ _autostring#get(5a2a78a048fb49a8).())); |
| 520 | token -> tmaxcxx . . $ SpeechRecognizer_recognize(c5e825ed147f56af).WhisperToken#tmax. = rawToken. tmaxcxx . . $ SpeechRecognizer_recognize(c5e825ed147f56af).RawToken#tmax.; |
| 521 | if (word -> whisperTokenscxx . . $ SpeechRecognizer_recognize(c5e825ed147f56af).WhisperWord#whisperTokens..sizecxx . . $ autovector#size. >= 2 && isPurePunctuationcxx . . $ isPurePunctuation(2df281243f91fe91). (token -> textcxx . . $ SpeechRecognizer_recognize(c5e825ed147f56af).WhisperToken#text..getcxx . . $ _autostring#get(5a2a78a048fb49a8).()) |
| 522 | && token -> textcxx . . $ SpeechRecognizer_recognize(c5e825ed147f56af).WhisperToken#text..getcxx . . $ _autostring#get(5a2a78a048fb49a8).() [0] != U')' && token -> textcxx . . $ SpeechRecognizer_recognize(c5e825ed147f56af).WhisperToken#text..getcxx . . $ _autostring#get(5a2a78a048fb49a8).() [0] != U']') |
| 523 | word -> tmaxcxx . . $ SpeechRecognizer_recognize(c5e825ed147f56af).WhisperWord#tmax. = (word -> tmaxcxx . . $ SpeechRecognizer_recognize(c5e825ed147f56af).WhisperWord#tmax. + token -> tmaxcxx . . $ SpeechRecognizer_recognize(c5e825ed147f56af).WhisperToken#tmax.) / 2; |
| 524 | else |
| 525 | word -> tmaxcxx . . $ SpeechRecognizer_recognize(c5e825ed147f56af).WhisperWord#tmax. = token -> tmaxcxx . . $ SpeechRecognizer_recognize(c5e825ed147f56af).WhisperToken#tmax.; |
| 526 | } |
| 527 | |
| 528 | /* |
| 529 | Resolve zero-length words. There might be runs of consecutive words sharing the same tmax (they can be results |
| 530 | of clamping or just poor DTW). Distribute them equally over the time from the preceding word's tmax till their tmax, |
| 531 | so that there are no zero-length words anymore. |
| 532 | */ |
| 533 | /* mutable scan */ integercxx . . $ integer# iword = 1; |
| 534 | while (iword <= whisperWords.sizecxx . . $ autovector#size.) { |
| 535 | /* mutable scan */ integercxx . . $ integer# k = iword + 1; |
| 536 | while (k <= whisperWords.sizecxx . . $ autovector#size. && whisperWords [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).k]. tmaxcxx . . $ SpeechRecognizer_recognize(c5e825ed147f56af).WhisperWord#tmax. == whisperWords [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).iword]. tmaxcxx . . $ SpeechRecognizer_recognize(c5e825ed147f56af).WhisperWord#tmax.) |
| 537 | k ++; |
| 538 | const integercxx . . $ integer# n = k - iword; |
| 539 | if (n >= 2) { |
| 540 | const double prevTmax = ( iword > 1 ? whisperWords [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).iword - 1]. tmaxcxx . . $ SpeechRecognizer_recognize(c5e825ed147f56af).WhisperWord#tmax. : 0.0 ); |
| 541 | const double interval = (whisperWords [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).iword]. tmaxcxx . . $ SpeechRecognizer_recognize(c5e825ed147f56af).WhisperWord#tmax. - prevTmax) / n; |
| 542 | for (integercxx . . $ integer# l = 1; l < n; l ++) |
| 543 | whisperWords [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).iword + l - 1]. tmaxcxx . . $ SpeechRecognizer_recognize(c5e825ed147f56af).WhisperWord#tmax. = prevTmax + interval * l; |
| 544 | } |
| 545 | iword = k; |
| 546 | } |
| 547 | |
| 548 | /* |
| 549 | Build the list of word intervals (including silences) with VAD-adjustments if VAD is in use. |
| 550 | */ |
| 551 | struct WordIntervalcxx . . $ SpeechRecognizer_recognize(c5e825ed147f56af).WordInterval# { |
| 552 | autostring32cxx . . $ autostring32# textWithPunctuationcxx . . $ SpeechRecognizer_recognize(c5e825ed147f56af).WordInterval#textWithPunctuation.; |
| 553 | double tmaxcxx . . $ SpeechRecognizer_recognize(c5e825ed147f56af).WordInterval#tmax.; |
| 554 | }; |
| 555 | autovectorcxx . . $ autovector# <WordIntervalcxx . . $ SpeechRecognizer_recognize(c5e825ed147f56af).WordInterval#> wordIntervalscxx . . $ autovector#autovector(49f6e7a06ebc5aa8).; |
| 556 | |
| 557 | if (! useVad || ! numberOfVadSegments) { |
| 558 | /* |
| 559 | VAD is not used or no VAD segments detected: no VAD adjustments needed. |
| 560 | */ |
| 561 | wordIntervals =cxx . . $ autovector#`operator=`(f4f0443dbfdd1e40). newvectorzerocxx . . $ newvectorzero(d6d5973469733409). <WordIntervalcxx . . $ SpeechRecognizer_recognize(c5e825ed147f56af).WordInterval#> (whisperWords.sizecxx . . $ autovector#size.); |
| 562 | for (integercxx . . $ integer# i = 1; i <= whisperWords.sizecxx . . $ autovector#size.; i ++) { |
| 563 | wordIntervals [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).i]. textWithPunctuationcxx . . $ SpeechRecognizer_recognize(c5e825ed147f56af).WordInterval#textWithPunctuation. =cxx . . $ _autostring#`operator=`(f562943cbc4bcf37). whisperWords [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).i]. textWithPunctuationcxx . . $ SpeechRecognizer_recognize(c5e825ed147f56af).WhisperWord#textWithPunctuation..movecxx . . $ _autostring#move(bd43c759570f178f).(); |
| 564 | wordIntervals [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).i]. tmaxcxx . . $ SpeechRecognizer_recognize(c5e825ed147f56af).WordInterval#tmax. = whisperWords [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).i]. tmaxcxx . . $ SpeechRecognizer_recognize(c5e825ed147f56af).WhisperWord#tmax.; |
| 565 | } |
| 566 | } else { |
| 567 | /* |
| 568 | VAD is used and at least one VAD segment detected: translate timestamps back to original time |
| 569 | and insert silence tokens between consecutive VAD segments. |
| 570 | */ |
| 571 | wordIntervals =cxx . . $ autovector#`operator=`(f4f0443dbfdd1e40). newvectorzerocxx . . $ newvectorzero(d6d5973469733409). <WordIntervalcxx . . $ SpeechRecognizer_recognize(c5e825ed147f56af).WordInterval#> (0); |
| 572 | |
| 573 | /* |
| 574 | Lambda to append a silent interval with the given tmax to wordIntervals. |
| 575 | */ |
| 576 | auto appendSilence = [& wordIntervals] (double tmax) { |
| 577 | WordIntervalcxx . . $ SpeechRecognizer_recognize(c5e825ed147f56af).WordInterval# *silence; |
| 578 | if (wordIntervals.sizecxx . . $ autovector#size. >= 1 && wordIntervals [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).wordIntervals.sizecxx . . $ autovector#size.]. textWithPunctuationcxx . . $ SpeechRecognizer_recognize(c5e825ed147f56af).WordInterval#textWithPunctuation. [cxx . . $ _autostring#`operator[]`(b190d49faf30c5be).0] == U'\0') { |
| 579 | silence = &wordIntervals [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).wordIntervals.sizecxx . . $ autovector#size.]; |
| 580 | silence -> tmaxcxx . . $ SpeechRecognizer_recognize(c5e825ed147f56af).WordInterval#tmax. = tmax; // extend the last silence to the end of this one |
| 581 | } else { |
| 582 | silence = wordIntervals. appendcxx . . $ autovector#append(155dc7f103cdb9db).(); |
| 583 | silence -> textWithPunctuationcxx . . $ SpeechRecognizer_recognize(c5e825ed147f56af).WordInterval#textWithPunctuation. =cxx . . $ _autostring#`operator=`(f562943cbc4bcf37). Melder_dupcxx . . $ Melder_dup(87bf156b1f548402). (U""); |
| 584 | silence -> tmaxcxx . . $ SpeechRecognizer_recognize(c5e825ed147f56af).WordInterval#tmax. = tmax; |
| 585 | } |
| 586 | tracecxx . . $ `melder/melder_debug.h:60:10`! (U"Silence ", wordIntervals.size, U": text = ", silence -> textWithPunctuation.get(), U", tmax = ", silence -> tmax); |
| 587 | }; |
| 588 | |
| 589 | /* |
| 590 | Lambda to append a word to wordIntervals, with the given tmax, which is translated from VAD to original timestamps. |
| 591 | Note: this might move the text member out of word, leaving it empty. |
| 592 | */ |
| 593 | auto appendWord = [& wordIntervals, & vadSegments, & appendSilence] (WhisperWordcxx . . $ SpeechRecognizer_recognize(c5e825ed147f56af).WhisperWord#& word, integercxx . . $ integer# vadSegment) { |
| 594 | Melder_assertcxx . . $ `melder/melder_assert.h:39:10`! (word. tmax >= vadSegments [vadSegment]. vadStart); |
| 595 | const double tmax = stdcxx . . $ std/::mincxx . . $ std/min(2a338fce5842aa6a). ( |
| 596 | word. tmaxcxx . . $ SpeechRecognizer_recognize(c5e825ed147f56af).WhisperWord#tmax. - vadSegments [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).vadSegment]. vadStartcxx . . $ SpeechRecognizer_recognize(c5e825ed147f56af).VadSegment#vadStart. + vadSegments [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).vadSegment]. origStartcxx . . $ SpeechRecognizer_recognize(c5e825ed147f56af).VadSegment#origStart., // original tmax |
| 597 | vadSegments [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).vadSegment]. origEndcxx . . $ SpeechRecognizer_recognize(c5e825ed147f56af).VadSegment#origEnd. |
| 598 | ); // clamp to the size of VAD interval |
| 599 | |
| 600 | const conststring32cxx . . $ conststring32# text = word. textWithPunctuationcxx . . $ SpeechRecognizer_recognize(c5e825ed147f56af).WhisperWord#textWithPunctuation..getcxx . . $ _autostring#get(5a2a78a048fb49a8).(); |
| 601 | if (text [0] == U'\0' || text [0] == U'(' || text [0] == U'[') |
| 602 | appendSilence (cxx . . $ SpeechRecognizer_recognize(c5e825ed147f56af).$anonymous_type_2#`operator()`(1f33e600eefd7a1).tmax); // insert (append) silence if the word is empty or is non-speech (e.g., "(laughs)") |
| 603 | else { |
| 604 | WordIntervalcxx . . $ SpeechRecognizer_recognize(c5e825ed147f56af).WordInterval# *wordInterval = wordIntervals. appendcxx . . $ autovector#append(155dc7f103cdb9db).(); |
| 605 | wordInterval -> textWithPunctuationcxx . . $ SpeechRecognizer_recognize(c5e825ed147f56af).WordInterval#textWithPunctuation. =cxx . . $ _autostring#`operator=`(f562943cbc4bcf37). word. textWithPunctuationcxx . . $ SpeechRecognizer_recognize(c5e825ed147f56af).WhisperWord#textWithPunctuation..movecxx . . $ _autostring#move(bd43c759570f178f).(); |
| 606 | wordInterval -> tmaxcxx . . $ SpeechRecognizer_recognize(c5e825ed147f56af).WordInterval#tmax. = tmax; |
| 607 | tracecxx . . $ `melder/melder_debug.h:60:10`! (U"Word interval ", wordIntervals.size, U": text = ", wordInterval -> textWithPunctuation.get(), U", tmax = ", wordInterval -> tmax); |
| 608 | } |
| 609 | }; |
| 610 | |
| 611 | /* |
| 612 | First, append a silence token in case speech does not start directly from the beginning of the sound. |
| 613 | */ |
| 614 | if (vadSegments [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).1]. origStartcxx . . $ SpeechRecognizer_recognize(c5e825ed147f56af).VadSegment#origStart. > sound -> xmincxx . . $ structFunction#xmin.) |
| 615 | appendSilence (cxx . . $ SpeechRecognizer_recognize(c5e825ed147f56af).$anonymous_type_2#`operator()`(1f33e600eefd7a1).vadSegments [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).1]. origStartcxx . . $ SpeechRecognizer_recognize(c5e825ed147f56af).VadSegment#origStart.); |
| 616 | |
| 617 | /* |
| 618 | Then iterate over the list of words, inserting silences between VAD segments. |
| 619 | */ |
| 620 | /* mutable increment */ integercxx . . $ integer# currentVadSegment = 1; |
| 621 | for (integercxx . . $ integer# i = 1; i <= whisperWords.sizecxx . . $ autovector#size.; i ++) { |
| 622 | WhisperWordcxx . . $ SpeechRecognizer_recognize(c5e825ed147f56af).WhisperWord#& word = whisperWords [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).i]; |
| 623 | const integercxx . . $ integer# numberOfTokensInWord = word. whisperTokenscxx . . $ SpeechRecognizer_recognize(c5e825ed147f56af).WhisperWord#whisperTokens..sizecxx . . $ autovector#size.; |
| 624 | const conststring32cxx . . $ conststring32# lastToken = word. whisperTokenscxx . . $ SpeechRecognizer_recognize(c5e825ed147f56af).WhisperWord#whisperTokens. [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).numberOfTokensInWord]. textcxx . . $ SpeechRecognizer_recognize(c5e825ed147f56af).WhisperToken#text..getcxx . . $ _autostring#get(5a2a78a048fb49a8).(); |
| 625 | /* mutable scan */ integercxx . . $ integer# firstContentToken = 1; |
| 626 | while (firstContentToken < numberOfTokensInWord && isPurePunctuationcxx . . $ isPurePunctuation(2df281243f91fe91). (word. whisperTokenscxx . . $ SpeechRecognizer_recognize(c5e825ed147f56af).WhisperWord#whisperTokens. [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).firstContentToken]. textcxx . . $ SpeechRecognizer_recognize(c5e825ed147f56af).WhisperToken#text..getcxx . . $ _autostring#get(5a2a78a048fb49a8).())) |
| 627 | firstContentToken ++; |
| 628 | const double firstContentTokenTmax = word. whisperTokenscxx . . $ SpeechRecognizer_recognize(c5e825ed147f56af).WhisperWord#whisperTokens. [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).firstContentToken]. tmaxcxx . . $ SpeechRecognizer_recognize(c5e825ed147f56af).WhisperToken#tmax.; // first token that is not a pure punctuation |
| 629 | |
| 630 | const bool landsInCurrentVadSegment = ( currentVadSegment == numberOfVadSegments || |
| 631 | firstContentTokenTmax < vadSegments [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).currentVadSegment + 1]. vadStartcxx . . $ SpeechRecognizer_recognize(c5e825ed147f56af).VadSegment#vadStart. && Melder_isPunctuationOrSymbolcxx . . $ Melder_isPunctuationOrSymbol(b8d0204ce8416907). (lastToken [0]) ); |
| 632 | |
| 633 | if (! landsInCurrentVadSegment) |
| 634 | while (currentVadSegment < numberOfVadSegments && word. tmaxcxx . . $ SpeechRecognizer_recognize(c5e825ed147f56af).WhisperWord#tmax. > vadSegments [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).currentVadSegment + 1]. vadStartcxx . . $ SpeechRecognizer_recognize(c5e825ed147f56af).VadSegment#vadStart.) { |
| 635 | appendSilence (cxx . . $ SpeechRecognizer_recognize(c5e825ed147f56af).$anonymous_type_2#`operator()`(1f33e600eefd7a1).vadSegments [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).currentVadSegment + 1]. origStartcxx . . $ SpeechRecognizer_recognize(c5e825ed147f56af).VadSegment#origStart.); // insert silence between current and next VAD segments; |
| 636 | currentVadSegment ++; |
| 637 | } |
| 638 | appendWord (cxx . . $ SpeechRecognizer_recognize(c5e825ed147f56af).$anonymous_type_3#`operator()`(35104f0137bcbc32).word, currentVadSegment); |
| 639 | } |
| 640 | |
| 641 | /* |
| 642 | Insert trailing silence. |
| 643 | */ |
| 644 | if (wordIntervals.sizecxx . . $ autovector#size. > 0 && wordIntervals [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).wordIntervals.sizecxx . . $ autovector#size.]. tmaxcxx . . $ SpeechRecognizer_recognize(c5e825ed147f56af).WordInterval#tmax. < sound -> xmaxcxx . . $ structFunction#xmax.) |
| 645 | appendSilence (cxx . . $ SpeechRecognizer_recognize(c5e825ed147f56af).$anonymous_type_2#`operator()`(1f33e600eefd7a1).sound -> xmaxcxx . . $ structFunction#xmax.); |
| 646 | } |
| 647 | |
| 648 | /* |
| 649 | Build word and sentence intervals from the flat token list. |
| 650 | */ |
| 651 | autoMelderStringcxx . . $ autoMelderString# sentenceTextcxx . . $ autoMelderString#autoMelderString(49f6e7a06ebc5aa8).; |
| 652 | autoMelderStringcxx . . $ autoMelderString# fullTextcxx . . $ autoMelderString#autoMelderString(49f6e7a06ebc5aa8).; |
| 653 | autovectorcxx . . $ autovector# <SpeechSegmentcxx . . $ SpeechSegment#> words = newvectorzerocxx . . $ newvectorzero(d6d5973469733409). <SpeechSegmentcxx . . $ SpeechSegment#> (0); |
| 654 | autovectorcxx . . $ autovector# <SpeechSegmentcxx . . $ SpeechSegment#> sentences = newvectorzerocxx . . $ newvectorzero(d6d5973469733409). <SpeechSegmentcxx . . $ SpeechSegment#> (0); |
| 655 | |
| 656 | /* mutable per-sentence */ double sentenceTmin = sound -> xmincxx . . $ structFunction#xmin.; // (re)set at each sentence start, default assignment is just a guard |
| 657 | /* mutable per-token */ double previousWordTmax = sound -> xmincxx . . $ structFunction#xmin.; // default for the first token |
| 658 | |
| 659 | for (integercxx . . $ integer# i = 1; i <= wordIntervals.sizecxx . . $ autovector#size.; i ++) { |
| 660 | const double wordTmin = previousWordTmax; |
| 661 | const double wordTmax = wordIntervals [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).i]. tmaxcxx . . $ SpeechRecognizer_recognize(c5e825ed147f56af).WordInterval#tmax.; |
| 662 | previousWordTmax = wordTmax; |
| 663 | |
| 664 | const conststring32cxx . . $ conststring32# wordTextWithPunctuation = wordIntervals [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).i]. textWithPunctuationcxx . . $ SpeechRecognizer_recognize(c5e825ed147f56af).WordInterval#textWithPunctuation..getcxx . . $ _autostring#get(5a2a78a048fb49a8).(); |
| 665 | const bool isEmptyWord = wordTextWithPunctuation [0] == U'\0'; |
| 666 | |
| 667 | /* |
| 668 | Remove all leading and trailing punctuation except ' ('cause, speakers'). |
| 669 | */ |
| 670 | /* mutable adjust */ conststring32cxx . . $ conststring32# clean = wordTextWithPunctuation; |
| 671 | while (*clean != U'\0' && ! Melder_isAlphanumericcxx . . $ Melder_isAlphanumeric(b8d0204ce8416907). (*clean) && *clean != U'\'') |
| 672 | ++ clean; // move beyond leading punctuation |
| 673 | /* mutable adjust */ integercxx . . $ integer# cleanLength = Melder_lengthcxx . . $ Melder_length(3fae411214b133ad). (clean); |
| 674 | while (cleanLength > 0 && ! Melder_isAlphanumericcxx . . $ Melder_isAlphanumeric(b8d0204ce8416907). (clean [cleanLength - 1]) && clean [cleanLength - 1] != U'\'') |
| 675 | -- cleanLength; // exclude trailing punctuation |
| 676 | autostring32cxx . . $ autostring32# wordTextWithoutPunctuation = Melder_ndupcxx . . $ Melder_ndup(c02f217f21fae7fe). (clean, cleanLength); |
| 677 | |
| 678 | /* |
| 679 | Add word text to the sentence and to the full transcription text (only for non-silent words to not multiply spaces). |
| 680 | */ |
| 681 | if (! isEmptyWord) { |
| 682 | MelderString_appendcxx . . $ MelderString_append(65be51a793e0ff96). (& sentenceText, sentenceText.lengthcxx . . $ MelderString#length. > 0 ? U" " : U"", wordTextWithPunctuation); |
| 683 | MelderString_appendcxx . . $ MelderString_append(65be51a793e0ff96). (& fullText, fullText.lengthcxx . . $ MelderString#length. > 0 ? U" " : U"", wordTextWithPunctuation); |
| 684 | } |
| 685 | |
| 686 | /* |
| 687 | Add a word segment or append a zero-length word to the last word segment. |
| 688 | */ |
| 689 | Melder_assertcxx . . $ `melder/melder_assert.h:39:10`! (wordTmax >= wordTmin); |
| 690 | if (wordTmax > wordTmin) { // not a zero-length word |
| 691 | SpeechSegmentcxx . . $ SpeechSegment# *word = words. appendcxx . . $ autovector#append(155dc7f103cdb9db).(); |
| 692 | word -> textcxx . . $ SpeechSegment#text. =cxx . . $ _autostring#`operator=`(f562943cbc4bcf37). wordTextWithoutPunctuation.movecxx . . $ _autostring#move(bd43c759570f178f).(); |
| 693 | word -> tmincxx . . $ SpeechSegment#tmin. = wordTmin; |
| 694 | word -> tmaxcxx . . $ SpeechSegment#tmax. = wordTmax; |
| 695 | tracecxx . . $ `melder/melder_debug.h:60:10`! (U"Word ", words.size, U": \"", word -> text.get(), U"\" [ ", word -> tmin, U" - ", word -> tmax, U" ]"); |
| 696 | } else if (wordTextWithoutPunctuation [cxx . . $ _autostring#`operator[]`(b190d49faf30c5be).0] != U'\0' && words.sizecxx . . $ autovector#size. > 0) { // zero-length word: add its text to previous one |
| 697 | SpeechSegmentcxx . . $ SpeechSegment#& lastWord = words [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).words.sizecxx . . $ autovector#size.]; |
| 698 | lastWord.textcxx . . $ SpeechSegment#text. =cxx . . $ _autostring#`operator=`(f562943cbc4bcf37). Melder_dupcxx . . $ Melder_dup(87bf156b1f548402). (Melder_catcxx . . $ Melder_cat(7fbf27f4d367f442). (lastWord.textcxx . . $ SpeechSegment#text..getcxx . . $ _autostring#get(5a2a78a048fb49a8).(), U" ", wordTextWithoutPunctuation.getcxx . . $ _autostring#get(5a2a78a048fb49a8).())); |
| 699 | tracecxx . . $ `melder/melder_debug.h:60:10`! (U"Word ", words.size, U": \"", lastWord.text.get(), U"\" [ ", lastWord. tmin, U" - ", lastWord. tmax, U" ] (appended zero-length)"); |
| 700 | } |
| 701 | |
| 702 | /* |
| 703 | Finalize sentence segment |
| 704 | 1. if the sentence is complete (ends with terminal punctuation); |
| 705 | 2. if the current word is the last word of the whole sound; |
| 706 | 3. if the current word does not contain text (non-speech interval). |
| 707 | */ |
| 708 | if (endsWithTerminalPunctuationcxx . . $ endsWithTerminalPunctuation(2df281243f91fe91). (wordTextWithPunctuation) || i == wordIntervals.sizecxx . . $ autovector#size. || (isEmptyWord && sentenceText.lengthcxx . . $ MelderString#length. == 0)) { |
| 709 | if (wordTmax > sentenceTmin) { |
| 710 | SpeechSegmentcxx . . $ SpeechSegment# *sentence = sentences. appendcxx . . $ autovector#append(155dc7f103cdb9db).(); |
| 711 | sentence -> textcxx . . $ SpeechSegment#text. =cxx . . $ _autostring#`operator=`(f562943cbc4bcf37). Melder_dupcxx . . $ Melder_dup(87bf156b1f548402). (sentenceText.stringcxx . . $ MelderString#string. ? sentenceText.stringcxx . . $ MelderString#string. : U""); |
| 712 | sentence -> tmincxx . . $ SpeechSegment#tmin. = sentenceTmin; |
| 713 | sentence -> tmaxcxx . . $ SpeechSegment#tmax. = wordTmax; |
| 714 | tracecxx . . $ `melder/melder_debug.h:60:10`! (U"Sentence: ", sentences.size, U": \"", sentence -> text.get(), U"\" [ ", sentence -> tmin, U" - ", sentence -> tmax, U" ]"); |
| 715 | MelderString_emptycxx . . $ MelderString_empty(93db1090f27e5462). (& sentenceText); |
| 716 | sentenceTmin = wordTmax; |
| 717 | } else if (sentences.sizecxx . . $ autovector#size. > 0) { |
| 718 | if (sentenceText.lengthcxx . . $ MelderString#length. > 0) { |
| 719 | SpeechSegmentcxx . . $ SpeechSegment#& lastSentence = sentences [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).sentences.sizecxx . . $ autovector#size.]; |
| 720 | lastSentence.textcxx . . $ SpeechSegment#text. =cxx . . $ _autostring#`operator=`(f562943cbc4bcf37). Melder_dupcxx . . $ Melder_dup(87bf156b1f548402). (Melder_catcxx . . $ Melder_cat(7fbf27f4d367f442). (lastSentence.textcxx . . $ SpeechSegment#text..getcxx . . $ _autostring#get(5a2a78a048fb49a8).(), U" ", sentenceText.stringcxx . . $ MelderString#string.)); |
| 721 | } |
| 722 | MelderString_emptycxx . . $ MelderString_empty(93db1090f27e5462). (& sentenceText); |
| 723 | } |
| 724 | } |
| 725 | } |
| 726 | |
| 727 | WhisperTranscriptioncxx . . $ WhisperTranscription# transcription; |
| 728 | transcription. fullTranscriptioncxx . . $ WhisperTranscription#fullTranscription.. textcxx . . $ SpeechSegment#text. =cxx . . $ _autostring#`operator=`(f562943cbc4bcf37). Melder_dupcxx . . $ Melder_dup(87bf156b1f548402). (fullText.stringcxx . . $ MelderString#string. ? fullText.stringcxx . . $ MelderString#string. : U""); |
| 729 | transcription. fullTranscriptioncxx . . $ WhisperTranscription#fullTranscription.. tmincxx . . $ SpeechSegment#tmin. = sound -> xmincxx . . $ structFunction#xmin.; |
| 730 | transcription. fullTranscriptioncxx . . $ WhisperTranscription#fullTranscription.. tmaxcxx . . $ SpeechSegment#tmax. = sound -> xmaxcxx . . $ structFunction#xmax.; |
| 731 | transcription. wordscxx . . $ WhisperTranscription#words. =cxx . . $ autovector#`operator=`(f4f0443dbfdd1e40). words.movecxx . . $ autovector#move(57d90d7d77dfc011).(); |
| 732 | transcription. sentencescxx . . $ WhisperTranscription#sentences. =cxx . . $ autovector#`operator=`(f4f0443dbfdd1e40). sentences.movecxx . . $ autovector#move(57d90d7d77dfc011).(); |
| 733 | |
| 734 | tracecxx . . $ `melder/melder_debug.h:60:10`! (U"Full transcription:", |
| 735 | U" [ ", transcription. fullTranscription. tmin, U" - ", |
| 736 | transcription. fullTranscription. tmax, U" ]", transcription. fullTranscription. text.get() |
| 737 | ); |
| 738 | |
| 739 | return transcription; |
| 740 | |
| 741 | } catch (MelderErrorcxx . . $ MelderError#) { |
| 742 | Melder_throwcxx . . $ `melder/melder_error.h:77:9`! (U"Sound not transcribed."); |
| 743 | } |
| 744 | } |
| 745 | |
| 746 | autovectorcxx . . $ autovector# <SpeechSegmentcxx . . $ SpeechSegment#> doSileroVadcxx . . $ doSileroVad(34a52605c8efc429). (constSoundcxx . . $ constSound# sound, const double speechProbabilityThreshold, const double minNonSpeechDuration, |
| 747 | const double minSpeechDuration, const double speechPad, const conststring32cxx . . $ conststring32# nonSpeechLabel, const conststring32cxx . . $ conststring32# speechLabel |
| 748 | ) { |
| 749 | //TRACE |
| 750 | tracecxx . . $ `melder/melder_debug.h:60:10`! (U"Sound xmin = ", sound -> xmin, U", sound xmax = ", sound -> xmax); |
| 751 | supressGgmlLoggingcxx . . $ supressGgmlLogging(49f6e7a06ebc5aa8). (); |
| 752 | |
| 753 | /* |
| 754 | Remember original sound -> xmin and sound -> xmax before resampling; and resample. |
| 755 | */ |
| 756 | const double soundStart = sound -> xmincxx . . $ structFunction#xmin.; |
| 757 | const double soundEnd = sound -> xmaxcxx . . $ structFunction#xmax.; |
| 758 | autovectorcxx . . $ autovector# <float> samples32 = resampleForWhispercxx . . $ resampleForWhisper(3e5018fdb663e588). (sound); |
| 759 | |
| 760 | try { |
| 761 | /* |
| 762 | Initialize VAD context. |
| 763 | */ |
| 764 | whisper_vad_context_params vad_ctx_params = whisper_vad_default_context_params (); |
| 765 | autoWhisperVadContextcxx . . $ autoWhisperVadContext# vad_ctx = whisper_vad_init_from_memory_with_paramscxx . . $ autoWhisperVadContext#autoWhisperVadContext(da59a243252bfe1d). (ggml_silero_bincxx . . $ ggml_silero_bin., ggml_silero_bin_lencxx . . $ ggml_silero_bin_len., vad_ctx_params); |
| 766 | if (! vad_ctx.getcxx . . $ autoWhisperVadContext#get(f343f8db3fc0e891).()) |
| 767 | Melder_throwcxx . . $ `melder/melder_error.h:77:9`! (U"Failed to initialize VAD context."); |
| 768 | |
| 769 | /* |
| 770 | Set VAD parameters and run Silero VAD. |
| 771 | */ |
| 772 | whisper_vad_params vad_params = whisper_vad_default_params (); |
| 773 | vad_params. threshold = static_cast <float> (speechProbabilityThreshold); |
| 774 | vad_params. min_speech_duration_ms = static_cast <int> (minSpeechDuration * 1000.0); |
| 775 | vad_params. min_silence_duration_ms = static_cast <int> (minNonSpeechDuration * 1000.0); |
| 776 | vad_params. speech_pad_ms = static_cast <int> (speechPad * 1000.0); |
| 777 | autoWhisperVadSegmentscxx . . $ autoWhisperVadSegments# vad_segments = whisper_vad_segments_from_samplescxx . . $ autoWhisperVadSegments#autoWhisperVadSegments(89f830c8e2da71db). (vad_ctx.getcxx . . $ autoWhisperVadContext#get(f343f8db3fc0e891).(), vad_params, |
| 778 | samples32.asArgumentToFunctionThatExpectsZeroBasedArraycxx . . $ autovector#asArgumentToFunctionThatExpectsZeroBasedArray(5a2a78a048fb49a8).(), static_cast <int> (samples32.sizecxx . . $ autovector#size.)); |
| 779 | if (! vad_segments.getcxx . . $ autoWhisperVadSegments#get(7531d5737aaa08aa).()) |
| 780 | Melder_throwcxx . . $ `melder/melder_error.h:77:9`! (U"Failed to obtain VAD segments."); |
| 781 | |
| 782 | /* |
| 783 | Collect all VAD segments and wrap them with "non-voice" intervals. |
| 784 | */ |
| 785 | const int numberOfVadSegments = whisper_vad_segments_n_segments (vad_segments.getcxx . . $ autoWhisperVadSegments#get(7531d5737aaa08aa).()); |
| 786 | autovectorcxx . . $ autovector# <SpeechSegmentcxx . . $ SpeechSegment#> allIntervals = newvectorzerocxx . . $ newvectorzero(d6d5973469733409). <SpeechSegmentcxx . . $ SpeechSegment#> (0); |
| 787 | |
| 788 | for (int i = 0; i < numberOfVadSegments; i ++) { |
| 789 | const double t0 = whisper_vad_segments_get_segment_t0 (vad_segments.getcxx . . $ autoWhisperVadSegments#get(7531d5737aaa08aa).(), i) / 100.0; |
| 790 | const double t1 = whisper_vad_segments_get_segment_t1 (vad_segments.getcxx . . $ autoWhisperVadSegments#get(7531d5737aaa08aa).(), i) / 100.0; |
| 791 | |
| 792 | /* |
| 793 | Insert a non-voice interval before the first voice interval or between two voice intervals. |
| 794 | */ |
| 795 | const double previousEnd = (i == 0) ? soundStart : whisper_vad_segments_get_segment_t1 (vad_segments.getcxx . . $ autoWhisperVadSegments#get(7531d5737aaa08aa).(), i - 1) / 100.0; |
| 796 | if (t0 > previousEnd) { |
| 797 | SpeechSegmentcxx . . $ SpeechSegment# *nonVoiceInterval = allIntervals. appendcxx . . $ autovector#append(155dc7f103cdb9db). (); |
| 798 | nonVoiceInterval -> textcxx . . $ SpeechSegment#text. =cxx . . $ _autostring#`operator=`(f562943cbc4bcf37). Melder_dupcxx . . $ Melder_dup(87bf156b1f548402). (nonSpeechLabel); |
| 799 | nonVoiceInterval -> tmincxx . . $ SpeechSegment#tmin. = previousEnd; |
| 800 | nonVoiceInterval -> tmaxcxx . . $ SpeechSegment#tmax. = t0; |
| 801 | tracecxx . . $ `melder/melder_debug.h:60:10`! (U"Non-voice: t0 = ", allIntervals [allIntervals.size]. tmin, U", t1 = ", allIntervals [allIntervals.size]. tmax); |
| 802 | } |
| 803 | /* |
| 804 | Insert the voice interval. |
| 805 | */ |
| 806 | SpeechSegmentcxx . . $ SpeechSegment# *voiceInterval = allIntervals. appendcxx . . $ autovector#append(155dc7f103cdb9db).(); |
| 807 | voiceInterval -> textcxx . . $ SpeechSegment#text. =cxx . . $ _autostring#`operator=`(f562943cbc4bcf37). Melder_dupcxx . . $ Melder_dup(87bf156b1f548402). (speechLabel); |
| 808 | voiceInterval -> tmincxx . . $ SpeechSegment#tmin. = t0; |
| 809 | voiceInterval -> tmaxcxx . . $ SpeechSegment#tmax. = t1; |
| 810 | tracecxx . . $ `melder/melder_debug.h:60:10`! (U"VAD segment ", i + 1, U": t0 = ", allIntervals [allIntervals.size]. tmin, U", t1 = ", allIntervals [allIntervals.size]. tmax); |
| 811 | |
| 812 | /* |
| 813 | After the last voice interval, add non-voice if there is remaining audio. |
| 814 | */ |
| 815 | if (i == numberOfVadSegments - 1 && t1 < soundEnd) { |
| 816 | SpeechSegmentcxx . . $ SpeechSegment# *nonVoiceInterval = allIntervals. appendcxx . . $ autovector#append(155dc7f103cdb9db). (); |
| 817 | nonVoiceInterval -> textcxx . . $ SpeechSegment#text. =cxx . . $ _autostring#`operator=`(f562943cbc4bcf37). Melder_dupcxx . . $ Melder_dup(87bf156b1f548402). (nonSpeechLabel); |
| 818 | nonVoiceInterval -> tmincxx . . $ SpeechSegment#tmin. = t1; |
| 819 | nonVoiceInterval -> tmaxcxx . . $ SpeechSegment#tmax. = soundEnd; |
| 820 | tracecxx . . $ `melder/melder_debug.h:60:10`! (U"Non-voice: t0 = ", allIntervals [allIntervals.size]. tmin, U", t1 = ", allIntervals [allIntervals.size]. tmax); |
| 821 | } |
| 822 | } |
| 823 | |
| 824 | /* |
| 825 | If no voice activity detected at all, then entire sound is non-voice. |
| 826 | */ |
| 827 | if (! numberOfVadSegments) { |
| 828 | SpeechSegmentcxx . . $ SpeechSegment# *nonVoice = allIntervals. appendcxx . . $ autovector#append(155dc7f103cdb9db). (); |
| 829 | nonVoice -> textcxx . . $ SpeechSegment#text. =cxx . . $ _autostring#`operator=`(f562943cbc4bcf37). Melder_dupcxx . . $ Melder_dup(87bf156b1f548402). (nonSpeechLabel); |
| 830 | nonVoice -> tmincxx . . $ SpeechSegment#tmin. = soundStart; |
| 831 | nonVoice -> tmaxcxx . . $ SpeechSegment#tmax. = soundEnd; |
| 832 | } |
| 833 | |
| 834 | return allIntervalscxx . . $ autovector#autovector(d33c649a3aceae2f).; |
| 835 | } catch (MelderErrorcxx . . $ MelderError#) { |
| 836 | theGgmlMemoryPoolcxx . . $ theGgmlMemoryPool.. clearcxx . . $ GgmlMemoryPool#clear(49f6e7a06ebc5aa8).(); // this runs after destructors of local objects, therefore it must be safe! |
| 837 | Melder_throwcxx . . $ `melder/melder_error.h:77:9`! (U"Voice Activity not detected for sound."); |
| 838 | } |
| 839 | } |
| 840 | |
| 841 | extern unsigned char model_ggml_segmentation_datacxx . . $ model_ggml_segmentation_data.[]; |
| 842 | extern unsigned int model_ggml_segmentation_lengthcxx . . $ model_ggml_segmentation_length.; |
| 843 | extern unsigned char model_ggml_embedding_datacxx . . $ model_ggml_embedding_data.[]; |
| 844 | extern unsigned int model_ggml_embedding_lengthcxx . . $ model_ggml_embedding_length.; |
| 845 | |
| 846 | autovectorcxx . . $ autovector# <autovectorcxx . . $ autovector# <SpeechSegmentcxx . . $ SpeechSegment#>> doDiarizationcxx . . $ doDiarization(fb99a7b1dc80f916). (constSoundcxx . . $ constSound# sound, |
| 847 | const integercxx . . $ integer# maxNumSpeakers, const bool allowSpeakersOverlap, |
| 848 | const double clusterThreshold, const double segmentationStep, |
| 849 | const conststring32cxx . . $ conststring32# nonSpeechLabel, const conststring32cxx . . $ conststring32# speechLabel |
| 850 | ) { |
| 851 | //TRACE |
| 852 | autovectorcxx . . $ autovector# <float> samples32 = resampleForWhispercxx . . $ resampleForWhisper(3e5018fdb663e588). (sound); |
| 853 | try { |
| 854 | /* |
| 855 | Default maximum number of threads for diarization is half the number of logical processors. This is because: |
| 856 | 1. hyperthreaded CPUs: half = physical cores, optimal for SIMD kernels. |
| 857 | 2. hybrid P+E core CPUs: not tested yet; chosen by analogy with transcription. |
| 858 | 3. homogeneous non-hyperthreaded CPUs: not tested yet; half is likely below optimal but the slowdown is probably |
| 859 | smaller than the slowdown in case 1. |
| 860 | The user can override this in Praat preferences. |
| 861 | */ |
| 862 | integercxx . . $ integer# n_threads = SpeechRecognizer_getMaxNumberOfThreadsForDiarizationcxx . . $ SpeechRecognizer_getMaxNumberOfThreadsForDiarization(da6335ded91d79c2). (); |
| 863 | if (n_threads <= 0) |
| 864 | n_threads = DiarizationDefaultscxx . . $ DiarizationDefaults/::n_threadscxx . . $ DiarizationDefaults/n_threads.; |
| 865 | |
| 866 | supressGgmlLoggingcxx . . $ supressGgmlLogging(49f6e7a06ebc5aa8). (); |
| 867 | autoDiarizeContextcxx . . $ autoDiarizeContext# diarizeContext = diarize_init_from_memorycxx . . $ autoDiarizeContext#autoDiarizeContext(6dc7b3cba0cf7d7a). ( |
| 868 | model_ggml_segmentation_datacxx . . $ model_ggml_segmentation_data., model_ggml_segmentation_lengthcxx . . $ model_ggml_segmentation_length., |
| 869 | model_ggml_embedding_datacxx . . $ model_ggml_embedding_data., model_ggml_embedding_lengthcxx . . $ model_ggml_embedding_length. |
| 870 | ); |
| 871 | diarize_full_params diarizeParams = diarize_default_params (); |
| 872 | diarizeParams. n_threads = static_cast<int> (n_threads); |
| 873 | diarizeParams. max_speakers = static_cast <int> (maxNumSpeakers); |
| 874 | diarizeParams. max_simultaneous_speakers = allowSpeakersOverlap ? 2 : 1; |
| 875 | diarizeParams. cluster_threshold = static_cast <float> (clusterThreshold); |
| 876 | diarizeParams. seg_step_ratio = static_cast <float> (segmentationStep); |
| 877 | diarize_full (diarizeContext.getcxx . . $ autoDiarizeContext#get(77350513ca2629e6).(), diarizeParams,samples32.asArgumentToFunctionThatExpectsZeroBasedArraycxx . . $ autovector#asArgumentToFunctionThatExpectsZeroBasedArray(5a2a78a048fb49a8).(), |
| 878 | static_cast <int> (samples32.sizecxx . . $ autovector#size.)); |
| 879 | const unsigned int numberOfSegments = diarize_full_n_segments (diarizeContext.getcxx . . $ autoDiarizeContext#get(77350513ca2629e6).()); |
| 880 | const int numberOfSpeakers = diarize_full_n_speakers(diarizeContext.getcxx . . $ autoDiarizeContext#get(77350513ca2629e6).()); |
| 881 | |
| 882 | tracecxx . . $ `melder/melder_debug.h:60:10`!(U"Speakers:", numberOfSpeakers, U", Segments: ", numberOfSegments); |
| 883 | |
| 884 | autovectorcxx . . $ autovector# <autovectorcxx . . $ autovector# <SpeechSegmentcxx . . $ SpeechSegment#>> speakers = newvectorzerocxx . . $ newvectorzero(d6d5973469733409). <autovectorcxx . . $ autovector# <SpeechSegmentcxx . . $ SpeechSegment#>> (numberOfSpeakers); |
| 885 | /* |
| 886 | Collect segments for each speaker. |
| 887 | */ |
| 888 | for (int i = 1; i <= numberOfSpeakers; i ++) { |
| 889 | /* mutable increment */ double currentIntervalStart = sound -> xmincxx . . $ structFunction#xmin.; |
| 890 | |
| 891 | for (int segment = 0; segment < numberOfSegments; segment ++) { |
| 892 | if (diarize_full_get_segment_speaker (diarizeContext.getcxx . . $ autoDiarizeContext#get(77350513ca2629e6).(), segment) + 1 != i) // this segment is from a different speaker |
| 893 | continue; |
| 894 | |
| 895 | const double tmin = diarize_full_get_segment_t0 (diarizeContext.getcxx . . $ autoDiarizeContext#get(77350513ca2629e6).(), segment); |
| 896 | const double tmax = diarize_full_get_segment_t1 (diarizeContext.getcxx . . $ autoDiarizeContext#get(77350513ca2629e6).(), segment); |
| 897 | |
| 898 | if (tmin > currentIntervalStart) { |
| 899 | SpeechSegmentcxx . . $ SpeechSegment# *gap = speakers [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).i]. appendcxx . . $ autovector#append(155dc7f103cdb9db).(); |
| 900 | gap -> textcxx . . $ SpeechSegment#text. =cxx . . $ _autostring#`operator=`(f562943cbc4bcf37). Melder_dupcxx . . $ Melder_dup(87bf156b1f548402). (nonSpeechLabel); |
| 901 | gap -> tmincxx . . $ SpeechSegment#tmin. = currentIntervalStart; |
| 902 | gap -> tmaxcxx . . $ SpeechSegment#tmax. = tmin; |
| 903 | } |
| 904 | |
| 905 | SpeechSegmentcxx . . $ SpeechSegment# *speakerSegment = speakers [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).i]. appendcxx . . $ autovector#append(155dc7f103cdb9db).(); |
| 906 | speakerSegment -> textcxx . . $ SpeechSegment#text. =cxx . . $ _autostring#`operator=`(f562943cbc4bcf37). Melder_dupcxx . . $ Melder_dup(87bf156b1f548402). (speechLabel); |
| 907 | speakerSegment -> tmincxx . . $ SpeechSegment#tmin. = tmin; |
| 908 | speakerSegment -> tmaxcxx . . $ SpeechSegment#tmax. = tmax; |
| 909 | |
| 910 | tracecxx . . $ `melder/melder_debug.h:60:10`! (U"Diarization: [ ", speakerSegment -> tmin, U" - ", speakerSegment -> tmax, U" ] \"", |
| 911 | speakerSegment -> text.get(), U"\""); |
| 912 | |
| 913 | currentIntervalStart = tmax; |
| 914 | } |
| 915 | |
| 916 | if (currentIntervalStart < sound -> xmaxcxx . . $ structFunction#xmax.) { |
| 917 | SpeechSegmentcxx . . $ SpeechSegment# *gap = speakers [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).i].appendcxx . . $ autovector#append(155dc7f103cdb9db).(); |
| 918 | gap -> textcxx . . $ SpeechSegment#text. =cxx . . $ _autostring#`operator=`(f562943cbc4bcf37). Melder_dupcxx . . $ Melder_dup(87bf156b1f548402). (nonSpeechLabel); |
| 919 | gap -> tmincxx . . $ SpeechSegment#tmin. = currentIntervalStart; |
| 920 | gap -> tmaxcxx . . $ SpeechSegment#tmax. = sound -> xmaxcxx . . $ structFunction#xmax.; |
| 921 | } |
| 922 | } |
| 923 | |
| 924 | return speakerscxx . . $ autovector#autovector(d33c649a3aceae2f).; |
| 925 | |
| 926 | } catch (MelderErrorcxx . . $ MelderError#) { |
| 927 | theGgmlMemoryPoolcxx . . $ theGgmlMemoryPool.. clearcxx . . $ GgmlMemoryPool#clear(49f6e7a06ebc5aa8).(); |
| 928 | Melder_throwcxx . . $ `melder/melder_error.h:77:9`! (U"Diarization failed."); |
| 929 | } |
| 930 | } |
| 931 | |
| 932 | static conststring32cxx . . $ conststring32# theWhisperModelsFoldercxx . . $ theWhisperModelsFolder(1912f6684645c851). () { |
| 933 | static autostring32cxx . . $ autostring32# whisperModelFolderPathcxx . . $ _autostring#_autostring(49f6e7a06ebc5aa8).; |
| 934 | if (! whisperModelFolderPath) { |
| 935 | try { |
| 936 | structMelderFoldercxx . . $ structMelderFolder# modelsFolder { }; |
| 937 | MelderFolder_getSubfoldercxx . . $ MelderFolder_getSubfolder(a6a48bbd99ea6094). (Melder_preferencesFoldercxx . . $ Melder_preferencesFolder(3f0bf42ba8f9aa29). (), U"models", & modelsFolder); |
| 938 | if (! MelderFolder_existscxx . . $ MelderFolder_exists(f2f3cbeb84a71ab3). (& modelsFolder)) |
| 939 | MelderFolder_createcxx . . $ MelderFolder_create(63f38d3b6b86d1dc). (& modelsFolder); |
| 940 | structMelderFoldercxx . . $ structMelderFolder# whispercppFolder { }; |
| 941 | MelderFolder_getSubfoldercxx . . $ MelderFolder_getSubfolder(a6a48bbd99ea6094). (& modelsFolder, U"whispercpp", & whispercppFolder); |
| 942 | if (! MelderFolder_existscxx . . $ MelderFolder_exists(f2f3cbeb84a71ab3). (& whispercppFolder)) |
| 943 | MelderFolder_createcxx . . $ MelderFolder_create(63f38d3b6b86d1dc). (& whispercppFolder); |
| 944 | whisperModelFolderPath =cxx . . $ _autostring#`operator=`(f562943cbc4bcf37). Melder_dupcxx . . $ Melder_dup(87bf156b1f548402). (MelderFolder_peekPathcxx . . $ MelderFolder_peekPath(fbe3b4f4d15c56ee). (& whispercppFolder)); |
| 945 | if (Melder_debugcxx . . $ Melder_debug. == 2001) |
| 946 | Melder_casualcxx . . $ Melder_casual(6298c93241566110). (U"Whispercpp model folder path: ", whisperModelFolderPath.getcxx . . $ _autostring#get(5a2a78a048fb49a8).()); |
| 947 | } catch (MelderErrorcxx . . $ MelderError#) { |
| 948 | Melder_clearErrorcxx . . $ Melder_clearError(49f6e7a06ebc5aa8). (); |
| 949 | } |
| 950 | } |
| 951 | return whisperModelFolderPath.getcxx . . $ _autostring#get(5a2a78a048fb49a8).(); |
| 952 | } |
| 953 | |
| 954 | constSTRVECcxx . . $ constSTRVEC# theCurrentSpeechRecognizerModelNamescxx . . $ theCurrentSpeechRecognizerModelNames(6d96a063d275a0aa). () { |
| 955 | static autoSTRVECcxx . . $ autoSTRVEC# whisperModelNamescxx . . $ _autostringautovector#_autostringautovector(49f6e7a06ebc5aa8).; |
| 956 | try { |
| 957 | whisperModelNames =cxx . . $ _autostringautovector#`operator=`(150f7ba7aac25b7e). fileNames_STRVECcxx . . $ fileNames_STRVEC(d3c03a92d990d008). (Melder_catcxx . . $ Melder_cat(7fbf27f4d367f442). (theWhisperModelsFoldercxx . . $ theWhisperModelsFolder(1912f6684645c851). (), U"/*.bin")); |
| 958 | } catch (MelderErrorcxx . . $ MelderError#) { |
| 959 | Melder_clearErrorcxx . . $ Melder_clearError(49f6e7a06ebc5aa8). (); |
| 960 | } |
| 961 | return whisperModelNamescxx . . $ _conststringvector#_conststringvector(93d9e701af42cb0d)..getcxx . . $ _autostringautovector#get(9d56076a9ef7dd02).(); |
| 962 | } |
| 963 | |
| 964 | constSTRVECcxx . . $ constSTRVEC# theSpeechRecognizerLanguageNamescxx . . $ theSpeechRecognizerLanguageNames(6d96a063d275a0aa). () { |
| 965 | static autoSTRVECcxx . . $ autoSTRVEC# sortedWhisperLanguageNamescxx . . $ _autostringautovector#_autostringautovector(49f6e7a06ebc5aa8).; |
| 966 | if (! sortedWhisperLanguageNames) { |
| 967 | autoSTRVECcxx . . $ autoSTRVEC# unsortedcxx . . $ _autostringautovector#_autostringautovector(49f6e7a06ebc5aa8).; |
| 968 | try { |
| 969 | const uint8cxx . . $ uint8# numberOfLanguages = whisper_lang_max_id (); |
| 970 | for (uint8cxx . . $ uint8# i = 0; i < numberOfLanguages; i ++) { |
| 971 | autostring32cxx . . $ autostring32# languageName = Melder_8to32_ecxx . . $ Melder_8to32_e(f01fe69d6ed2498b). (whisper_lang_str_full (i)); |
| 972 | |
| 973 | /* |
| 974 | Capitalize the first letter, e.g. "dutch" -> "Dutch". |
| 975 | */ |
| 976 | if (languageName [cxx . . $ _autostring#`operator[]`(b190d49faf30c5be).0] != U'\0') |
| 977 | languageName [cxx . . $ _autostring#`operator[]`(b190d49faf30c5be).0] = Melder_toUpperCasecxx . . $ Melder_toUpperCase(cd1afca5b9692c51). (languageName [cxx . . $ _autostring#`operator[]`(b190d49faf30c5be).0]); |
| 978 | |
| 979 | unsorted. appendcxx . . $ _autostringautovector#append(ff00d524ab0355ec). (languageName.getcxx . . $ _autostring#get(5a2a78a048fb49a8).()); |
| 980 | } |
| 981 | autoSTRVECcxx . . $ autoSTRVEC# sorted = sort_STRVECcxx . . $ sort_STRVEC(6a8a775aea9c867b). (unsorted.getcxx . . $ _autostringautovector#get(9d56076a9ef7dd02).()); |
| 982 | |
| 983 | /* |
| 984 | Create a list to return (with autodetect option). |
| 985 | */ |
| 986 | sortedWhisperLanguageNames. appendcxx . . $ _autostringautovector#append(ff00d524ab0355ec). (U"Autodetect language"); |
| 987 | for (integercxx . . $ integer# i = 1; i <= sorted.sizecxx . . $ _autostringautovector#size.; i ++) |
| 988 | sortedWhisperLanguageNames. appendcxx . . $ _autostringautovector#append(ff00d524ab0355ec). (sorted [cxx . . $ _autostringautovector#`operator[]`(f191be2b65019068).i].getcxx . . $ _autostring#get(5a2a78a048fb49a8).()); |
| 989 | |
| 990 | } catch (MelderErrorcxx . . $ MelderError#) { |
| 991 | unsorted. resetcxx . . $ _autostringautovector#reset(49f6e7a06ebc5aa8).(); |
| 992 | sortedWhisperLanguageNames. resetcxx . . $ _autostringautovector#reset(49f6e7a06ebc5aa8).(); |
| 993 | Melder_clearErrorcxx . . $ Melder_clearError(49f6e7a06ebc5aa8). (); |
| 994 | } |
| 995 | } |
| 996 | return sortedWhisperLanguageNamescxx . . $ _conststringvector#_conststringvector(93d9e701af42cb0d)..getcxx . . $ _autostringautovector#get(9d56076a9ef7dd02).(); |
| 997 | } |
| 998 | |
| 999 | /* |
| 1000 | Preferences. |
| 1001 | */ |
| 1002 | static structcxx . . $ $anonymous_type_6fcfd03f32975b19_4# { |
| 1003 | integercxx . . $ integer# maxNumberOfThreadsForTranscriptioncxx . . $ $anonymous_type_6fcfd03f32975b19_4#maxNumberOfThreadsForTranscription. = 0; // "0" signals automatic (MelderThread_getNumberOfProcessors () / 2) |
| 1004 | integercxx . . $ integer# maxNumberOfThreadsForDiarizationcxx . . $ $anonymous_type_6fcfd03f32975b19_4#maxNumberOfThreadsForDiarization. = 0; // "0" signals automatic (MelderThread_getNumberOfProcessors () / 2) |
| 1005 | } preferencescxx . . $ preferences.; |
| 1006 | |
| 1007 | void SpeechRecognizer_preferencescxx . . $ SpeechRecognizer_preferences(49f6e7a06ebc5aa8). () { |
| 1008 | Preferences_addIntegercxx . . $ Preferences_addInteger(dd1501ba114a3e1c). (U"SpeechRecognizer.maxNumberOfThreadsForTranscription", |
| 1009 | & preferencescxx . . $ preferences.. maxNumberOfThreadsForTranscriptioncxx . . $ $anonymous_type_6fcfd03f32975b19_4#maxNumberOfThreadsForTranscription., 0); |
| 1010 | Preferences_addIntegercxx . . $ Preferences_addInteger(dd1501ba114a3e1c). (U"SpeechRecognizer.maxNumberOfThreadsForDiarization", |
| 1011 | & preferencescxx . . $ preferences.. maxNumberOfThreadsForDiarizationcxx . . $ $anonymous_type_6fcfd03f32975b19_4#maxNumberOfThreadsForDiarization., 0); |
| 1012 | } |
| 1013 | |
| 1014 | void SpeechRecognizer_setMaxNumberOfThreadsForTranscriptioncxx . . $ SpeechRecognizer_setMaxNumberOfThreadsForTranscription(1624ee8275b10bda). (integercxx . . $ integer# numberOfThreads) { |
| 1015 | preferencescxx . . $ preferences.. maxNumberOfThreadsForTranscriptioncxx . . $ $anonymous_type_6fcfd03f32975b19_4#maxNumberOfThreadsForTranscription. = numberOfThreads; |
| 1016 | } |
| 1017 | void SpeechRecognizer_setMaxNumberOfThreadsForDiarizationcxx . . $ SpeechRecognizer_setMaxNumberOfThreadsForDiarization(1624ee8275b10bda). (integercxx . . $ integer# numberOfThreads) { |
| 1018 | preferencescxx . . $ preferences.. maxNumberOfThreadsForDiarizationcxx . . $ $anonymous_type_6fcfd03f32975b19_4#maxNumberOfThreadsForDiarization. = numberOfThreads; |
| 1019 | } |
| 1020 | |
| 1021 | integercxx . . $ integer# SpeechRecognizer_getMaxNumberOfThreadsForTranscriptioncxx . . $ SpeechRecognizer_getMaxNumberOfThreadsForTranscription(da6335ded91d79c2). () { |
| 1022 | return preferencescxx . . $ preferences.. maxNumberOfThreadsForTranscriptioncxx . . $ $anonymous_type_6fcfd03f32975b19_4#maxNumberOfThreadsForTranscription.; |
| 1023 | } |
| 1024 | integercxx . . $ integer# SpeechRecognizer_getMaxNumberOfThreadsForDiarizationcxx . . $ SpeechRecognizer_getMaxNumberOfThreadsForDiarization(da6335ded91d79c2). () { |
| 1025 | return preferencescxx . . $ preferences.. maxNumberOfThreadsForDiarizationcxx . . $ $anonymous_type_6fcfd03f32975b19_4#maxNumberOfThreadsForDiarization.; |
| 1026 | } |
| 1027 | |
| 1028 | /* End of file SpeechRecognizer.cpp */ |