| 1 |
/* praat_SpeechRecognizer.cpp |
| 2 |
* |
| 3 |
* Copyright (C) 2025 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 "praat_SpeechRecognizer.h" |
| 20 |
#include "SpeechRecognizer.h" |
| 21 |
#include "praatM.h" |
| 22 |
|
| 23 |
|
| 24 |
DIRECT (HELP__SpeechRecognizer_help) { |
| 25 |
HELP (U"SpeechRecognizer") |
| 26 |
} |
| 27 |
|
| 28 |
FORM (CREATE_ONE__SpeechRecognizer_create, U"Create SpeechRecognizer", U"Create SpeechRecognizer...") { |
| 29 |
LISTNUMSTR (modelIndex, modelName, U"Whisper model", constSTRVEC(), 1) |
| 30 |
LISTNUMSTR (languageIndex, languageName, U"Language", constSTRVEC(), 1) |
| 31 |
OK |
| 32 |
static autoSTRVEC modelNames; |
| 33 |
modelNames = copy_STRVEC (theCurrentSpeechRecognizerModelNames()); // cannot be called twice in the same scope |
| 34 |
|
| 35 |
Melder_require (modelNames.size > 0, |
| 36 |
U"Found no Whisper-cpp models to do speech recognition with.\n" |
| 37 |
U"You can install them into the subfolder “whispercpp” of the folder “models” in the Praat preferences folder." |
| 38 |
); |
| 39 |
|
| 40 |
SET_LIST (modelIndex, modelName, modelNames.get(), NUMfindFirst (modelNames.get(), TranscriptionDefaults::modelName)) |
| 41 |
SET_LIST (languageIndex, languageName, theSpeechRecognizerLanguageNames(), |
| 42 |
NUMfindFirst (theSpeechRecognizerLanguageNames(), TranscriptionDefaults::languageName)) |
| 43 |
DO |
| 44 |
CREATE_ONE |
| 45 |
autoSpeechRecognizer result = SpeechRecognizer_create (modelName, languageName); |
| 46 |
Thing_setName (result.get(), Melder_cat (modelName, U"_", languageName)); |
| 47 |
CREATE_ONE_END (U"") |
| 48 |
} |
| 49 |
|
| 50 |
DIRECT (QUERY_ONE_FOR_STRING__SpeechRecognizer_getModelName) { |
| 51 |
QUERY_ONE_FOR_STRING (SpeechRecognizer) |
| 52 |
conststring32 result = my d_modelName.get(); |
| 53 |
QUERY_ONE_FOR_STRING_END |
| 54 |
} |
| 55 |
|
| 56 |
DIRECT (QUERY_ONE_FOR_STRING__SpeechRecognizer_getLanguageName) { |
| 57 |
QUERY_ONE_FOR_STRING (SpeechRecognizer) |
| 58 |
conststring32 result = my d_languageName.get(); |
| 59 |
QUERY_ONE_FOR_STRING_END |
| 60 |
} |
| 61 |
|
| 62 |
DIRECT (QUERY_ONE_AND_ONE_FOR_STRING__SpeechRecognizer_Sound_recognize) { |
| 63 |
QUERY_ONE_AND_ONE_FOR_STRING (SpeechRecognizer, Sound) |
| 64 |
bool useVad = true; |
| 65 |
/* |
| 66 |
Use default Silero VAD parameters. |
| 67 |
FIXME make them configurable, like in other places |
| 68 |
*/ |
| 69 |
double speechProbabilityThreshold = 0.5; // probability threshold to decide that sound is speech |
| 70 |
double minNonSpeechDuration = 0.1; // min duration of a non-speech segment |
| 71 |
double minSpeechDuration = 0.25; // min duration of a speech segment |
| 72 |
double speechPad = 0.03; // padding added before and after each speech segment |
| 73 |
WhisperTranscription whisperTranscription = SpeechRecognizer_recognize (me, you, useVad, |
| 74 |
speechProbabilityThreshold, minNonSpeechDuration, minSpeechDuration, speechPad); |
| 75 |
conststring32 result = whisperTranscription.fullTranscription.text.get(); |
| 76 |
QUERY_ONE_AND_ONE_FOR_STRING_END |
| 77 |
} |
| 78 |
|
| 79 |
FORM (SETTINGS__SpeechRecognizerSettings, U"AI settings", nullptr) { |
| 80 |
COMMENT (U"These settings determine how fast transcription and diarization") |
| 81 |
COMMENT (U"procedures are performed on your computer.") |
| 82 |
HEADING (U"Transcription...") |
| 83 |
INTEGER (maxNumberOfThreadsForTranscription, U"Max. number of threads", U"0 (= auto)") |
| 84 |
CAPTION (Melder_cat (U"(“auto” is ", TranscriptionDefaults::n_threads, U" on this computer)")) |
| 85 |
HEADING (U"Diarization...") |
| 86 |
INTEGER (maxNumberOfThreadsForDiarization, U"Max. number of threads", U"0 (= auto)") |
| 87 |
CAPTION (Melder_cat (U"(“auto” is ", DiarizationDefaults::n_threads, U" on this computer)")) |
| 88 |
OK |
| 89 |
SET_INTEGER (maxNumberOfThreadsForTranscription, SpeechRecognizer_getMaxNumberOfThreadsForTranscription ()) |
| 90 |
SET_INTEGER (maxNumberOfThreadsForDiarization, SpeechRecognizer_getMaxNumberOfThreadsForDiarization ()) |
| 91 |
DO |
| 92 |
PREFS |
| 93 |
SpeechRecognizer_setMaxNumberOfThreadsForTranscription (maxNumberOfThreadsForTranscription); |
| 94 |
SpeechRecognizer_setMaxNumberOfThreadsForDiarization (maxNumberOfThreadsForDiarization); |
| 95 |
PREFS_END |
| 96 |
} |
| 97 |
|
| 98 |
void praat_SpeechRecognizer_init () { |
| 99 |
Thing_recognizeClassesByName (classSpeechRecognizer); |
| 100 |
|
| 101 |
SpeechRecognizer_preferences (); |
| 102 |
|
| 103 |
praat_addMenuCommand (U"Objects", U"New", U"Speech-to-text recognition", nullptr, 0, nullptr); |
| 104 |
praat_addMenuCommand (U"Objects", U"New", U"SpeechRecognizer help", nullptr, 1, HELP__SpeechRecognizer_help); |
| 105 |
praat_addMenuCommand (U"Objects", U"New", U"-- new SpeechRecognizer --", nullptr, 1, nullptr); |
| 106 |
praat_addMenuCommand (U"Objects", U"New", U"Create SpeechRecognizer...", nullptr, 1, CREATE_ONE__SpeechRecognizer_create); |
| 107 |
praat_addMenuCommand (U"Objects", U"Settings", U"-- AI settings --", nullptr, 0, nullptr); |
| 108 |
praat_addMenuCommand (U"Objects", U"Settings", U"AI settings...", nullptr, 0, SETTINGS__SpeechRecognizerSettings); |
| 109 |
|
| 110 |
praat_addAction1 (classSpeechRecognizer, 0, U"SpeechRecognizer help", nullptr, 0, |
| 111 |
HELP__SpeechRecognizer_help); |
| 112 |
praat_addAction1 (classSpeechRecognizer, 0, U"Query -", nullptr, 0, nullptr); |
| 113 |
praat_addAction1 (classSpeechRecognizer, 1, U"Get Whisper model name", nullptr, 1, |
| 114 |
QUERY_ONE_FOR_STRING__SpeechRecognizer_getModelName); |
| 115 |
praat_addAction1 (classSpeechRecognizer, 1, U"Get language name", nullptr, 1, |
| 116 |
QUERY_ONE_FOR_STRING__SpeechRecognizer_getLanguageName); |
| 117 |
|
| 118 |
praat_addAction2 (classSpeechRecognizer, 1, classSound, 1, U"Transcribe", nullptr, 0, |
| 119 |
QUERY_ONE_AND_ONE_FOR_STRING__SpeechRecognizer_Sound_recognize); |
| 120 |
} |