### Vosk Development Setup Source: https://github.com/alphacep/vosk-api/blob/master/ruby/README.md Install dependencies and run tests for Vosk development using Bundler. ```bash bundle install bundle exec rake spec ``` -------------------------------- ### Run Vosk API Go Example on Linux Source: https://github.com/alphacep/vosk-api/blob/master/go/example/README.md This snippet outlines the steps to clone the repository, download the Linux-specific Vosk library and a small English model, and then run the Go example with a test audio file. Ensure LD_LIBRARY_PATH and CGO flags are correctly set. ```bash git clone https://github.com/alphacep/vosk-api cd vosk-api/go/example wget https://github.com/alphacep/vosk-api/releases/download/v0.3.45/vosk-linux-x86_64-0.3.45.zip unzip vosk-linux-x86_64-0.3.45.zip wget https://alphacephei.com/vosk/models/vosk-model-small-en-us-0.15.zip unzip vosk-model-small-en-us-0.15.zip mv vosk-model-small-en-us-0.15 model cp ../../python/example/test.wav . VOSK_PATH=`pwd`/vosk-linux-x86_64-0.3.45 LD_LIBRARY_PATH=$VOSK_PATH CGO_CPPFLAGS="-I $VOSK_PATH" CGO_LDFLAGS="-L $VOSK_PATH" go run . -f test.wav ``` -------------------------------- ### Run Vosk API Go Example on Windows Source: https://github.com/alphacep/vosk-api/blob/master/go/example/README.md This snippet details the process for running the Vosk API Go example on Windows. It involves cloning the repository, downloading the Linux library (and extracting DLLs/headers), obtaining a small English model, and executing the Go program with appropriate linker flags. ```bash git clone https://github.com/alphacep/vosk-api cd vosk-api/go/example wget https://github.com/alphacep/vosk-api/releases/download/v0.3.45/vosk-linux-x86_64-0.3.45.zip unzip vosk-linux-x86_64-0.3.45.zip cp vosk-linux-x86_64-0.3.45/*.dll . cp vosk-linux-x86_64-0.3.45/*.h . wget https://alphacephei.com/vosk/models/vosk-model-small-en-us-0.15.zip unzip vosk-model-small-en-us-0.15.zip mv vosk-model-small-en-us-0.15 model cp ../../python/example/test.wav . VOSK_PATH=`pwd` LD_LIBRARY_PATH=$VOSK_PATH CGO_CPPFLAGS="-I $VOSK_PATH" CGO_LDFLAGS="-L $VOSK_PATH -lvosk -lpthread -dl" go run . -f test.wav ``` -------------------------------- ### Install Vosk Module Source: https://github.com/alphacep/vosk-api/blob/master/python/example/colab/vosk.ipynb Install the vosk module using pip. This is the first step before using any Vosk functionalities. ```python !pip3 install vosk ``` -------------------------------- ### Download Example Audio File Source: https://github.com/alphacep/vosk-api/blob/master/python/example/colab/vosk.ipynb Downloads a sample audio file for testing speech recognition. Replace the URL with your own audio file. ```python !wget -q -O /content/test.wav https://github.com/alphacep/vosk-api/raw/master/python/example/test.wav ``` -------------------------------- ### Setup Directories and Initial Checks Source: https://github.com/alphacep/vosk-api/blob/master/python/example/colab/vosk-training.ipynb This script sets up necessary directories for training and performs initial file checks. It's crucial for ensuring all prerequisites are met before proceeding with training. ```bash gmm_dir=exp/$gmm ali_dir=exp/${gmm}_ali tree_dir=exp/chain${suffix}/tree${tree_affix:+_$tree_affix} lang=data/lang_chain${suffix} lat_dir=exp/chain${suffix}/${gmm}_${train_set}_lats dir=exp/chain${suffix}/tdnn${affix} train_data_dir=data/${train_set} for f in $gmm_dir/final.mdl $train_data_dir/feats.scp $ali_dir/ali.1.gz; do [ ! -f $f ] && echo "$0: expected file $f to exist" && exit 1 done ``` -------------------------------- ### Install FLAC Encoder Source: https://github.com/alphacep/vosk-api/blob/master/python/example/colab/vosk-training.ipynb Installs the FLAC audio encoder, which is required for audio processing during speech recognition training. ```bash !apt install flac ``` -------------------------------- ### Run Data Preparation Stage Source: https://github.com/alphacep/vosk-api/blob/master/training/README.md Execute the initial stage of the training pipeline to download and prepare the LibriSpeech dataset. Ensure Kaldi is installed and paths are set. ```bash bash run.sh --stage 0 --stop_stage 0 ``` -------------------------------- ### Install Phonetisaurus Source: https://github.com/alphacep/vosk-api/blob/master/python/example/colab/vosk-adaptation.ipynb Installs the Phonetisaurus library using pip. This is a prerequisite for certain phonetic-based adaptation tasks. ```bash + pip3 install phonetisaurus ``` -------------------------------- ### Install Kaldi Build Dependencies Source: https://github.com/alphacep/vosk-api/blob/master/python/example/colab/kaldi-build.ipynb Installs essential build tools and libraries required for compiling Kaldi. This includes g++, automake, and subversion. ```bash !apt-get install -qq g++ automake autoconf libtool gfortran sox subversion ``` ```bash !git clone -b vosk --single-branch https://github.com/alphacep/kaldi.git ``` -------------------------------- ### Install OpenGrm Language Toolkit Source: https://github.com/alphacep/vosk-api/blob/master/python/example/colab/kaldi-build.ipynb Executes a shell script to install the OpenGrm toolkit. The script attempts to download a tarball and may fall back to using wget if the tarball is not found locally. This is part of setting up language modeling capabilities. ```bash !extras/install_opengrm.sh ``` -------------------------------- ### Go Multi-threaded GPU Setup Source: https://context7.com/alphacep/vosk-api/llms.txt Shows how to initialize GPU acceleration for Vosk in a multi-threaded Go application. `vosk.GPUInit()` must be called once from the main thread, and `vosk.GPUThreadInit()` once per worker thread. ```go // Go — multi-threaded GPU setup import vosk "github.com/alphacep/vosk-api/go" func main() { vosk.GPUInit() // call once from main thread // In each worker goroutine: go func() { vosk.GPUThreadInit() // call once per thread // ... create recognizer and process audio ... }() } ``` -------------------------------- ### Compile Kaldi Components Source: https://github.com/alphacep/vosk-api/blob/master/python/example/colab/kaldi-build.ipynb Navigates to the Kaldi tools directory and initiates the compilation of specific components like cub and openfst, followed by installing OpenBLAS. ```bash %cd /content/kaldi/tools !make -j 8 cub openfst ``` ```bash !extras/install_openblas.sh ``` -------------------------------- ### Install Vosk Ruby Gem Source: https://github.com/alphacep/vosk-api/blob/master/ruby/README.md Add the vosk gem to your Gemfile or install it directly using gem install. ```ruby gem "vosk" ``` ```bash gem install vosk ``` -------------------------------- ### Speaker Identification Setup Source: https://github.com/alphacep/vosk-api/blob/master/ruby/README.md Integrate speaker identification by initializing a speaker model and assigning it to the recognizer. ```ruby spk_model = Vosk::SpkModel.new("/path/to/spk-model") rec = Vosk::KaldiRecognizer.new(model, sample_rate) rec.spk_model = spk_model ``` -------------------------------- ### Start Chain Model Trainer Source: https://github.com/alphacep/vosk-api/blob/master/python/example/colab/vosk-training.ipynb Initiates the training process for a chain-based acoustic model. This command includes numerous parameters to control various aspects of the training, such as GPU usage, regularization, and data augmentation. ```bash steps/nnet3/chain/train.py --use-gpu false --stage -10 --cmd run.pl --feat.online-ivector-dir exp/chain/ivectors_train --feat.cmvn-opts --norm-means=false --norm-vars=false --chain.xent-regularize 0.1 --chain.leaky-hmm-coefficient 0.1 --chain.l2-regularize 0.0 --chain.apply-deriv-weights false --chain.lm-opts=--num-extra-lm-states=2000 --egs.cmd run.pl --egs.dir --egs.stage -10 --egs.opts --frames-overlap-per-eg 0 --constrained false --egs.chunk-width 140,100,160 --trainer.dropout-schedule 0,0@0.20,0.5@0.50,0 --trainer.add-option=--optimization.memory-compression-level=2 --trainer.num-chunk-per-minibatch 64 --trainer.frames-per-iter 2500000 --trainer.num-epochs 20 --trainer.optimization.num-jobs-initial 1 --trainer.optimization.num-jobs-final 1 --trainer.optimization.initial-effective-lrate 0.001 --trainer.optimization.final-effective-lrate 0.0001 --trainer.max-param-change 2.0 --cleanup.remove-egs true --feat-dir data/train --tree-dir exp/chain/tree --lat-dir exp/chain/tri3_train_lats --dir exp/chain/tdnn ``` -------------------------------- ### Shell Script Setup for Vosk Training Source: https://github.com/alphacep/vosk-api/blob/master/python/example/colab/vosk-training.ipynb This snippet sets up shell options for immediate error checking and defines variables for training configuration. It's used to prepare the environment before executing training commands. ```shell set -euo pipefail stage=-1 decode_nj=10 train_set=train gmm=tri3 net3_affix= suffix= affix= # affix for the TDNN directory name tree_affix= train_stage=-10 get_egs_stage=-10 decode_iter= chunk_width=140,100,160 common_egs_dir= xent_regularize=0.1 dropout_schedule='0,0@0.20,0.5@0.50,0' srand=0 remove_egs=true echo "$0 $@" # Print the command line for logging . ./cmd.sh . ./path.sh . ./utils/parse_options.sh # Problem: We have removed the "train_" prefix of our training set in ``` -------------------------------- ### List Available Models and Languages Source: https://github.com/alphacep/vosk-api/blob/master/ruby/README.md Query the Vosk library to get a list of all available model names and supported language codes. ```ruby puts Vosk.models # all model names puts Vosk.languages # all supported language codes ``` -------------------------------- ### Example WER Results Source: https://github.com/alphacep/vosk-api/blob/master/training/README.md Sample output from RESULTS.txt showing Word Error Rate (WER) calculations, including insertions, deletions, and substitutions. ```plaintext %WER 14.10 [ 2839 / 20138, 214 ins, 487 del, 2138 sub ] exp/chain/tdnn/decode_test/wer_11_0.0 %WER 12.67 [ 2552 / 20138, 215 ins, 406 del, 1931 sub ] exp/chain/tdnn/decode_test_rescore/wer_11_0.0 ``` -------------------------------- ### Microphone Streaming with Vosk (Node.js) Source: https://context7.com/alphacep/vosk-api/llms.txt Recognize speech directly from a live microphone using the 'mic' package. Ensure 'mic' is installed. The audio stream is processed in chunks. ```javascript const vosk = require('vosk'); const mic = require('mic'); const SAMPLE_RATE = 16000; const model = new vosk.Model('./model-en-us'); const rec = new vosk.Recognizer({ model, sampleRate: SAMPLE_RATE }); const micInstance = mic({ rate: String(SAMPLE_RATE), channels: '1', debug: false, device: 'default', }); const micStream = micInstance.getAudioStream(); micStream.on('data', data => { if (rec.acceptWaveform(data)) console.log(rec.result()); // complete utterance else console.log(rec.partialResult()); // in-progress text }); micStream.on('audioProcessExitComplete', () => { console.log('Final:', rec.finalResult()); rec.free(); model.free(); }); process.on('SIGINT', () => micInstance.stop()); micInstance.start(); ``` -------------------------------- ### Copy Training Data Source: https://github.com/alphacep/vosk-api/blob/master/python/example/colab/vosk-training.ipynb Removes the default training directory within Kaldi and copies the training data from the cloned vosk-api repository. This ensures the correct training setup is used. ```bash !rm -rf /content/kaldi/egs/ac/training !cp -r /content/vosk-api/training /content/kaldi/egs/ac ``` -------------------------------- ### result, partialResult, finalResult Source: https://context7.com/alphacep/vosk-api/llms.txt Methods to retrieve the recognizer output as JSON. `result()` gets a complete utterance after silence, `partialResult()` gets in-progress text, and `finalResult()` flushes the pipeline and returns any remaining text. ```APIDOC ## result / partialResult / finalResult ### Description Three methods retrieve the recognizer output as JSON. `result()` returns a complete utterance after silence. `partialResult()` returns in-progress text. `finalResult()` flushes the pipeline and returns whatever remains — always call it at stream end. ### Method Signatures (JavaScript) `result(): object` `partialResult(): object` `finalResult(): object` ### Response Example (JavaScript - with `setWords(true)` and `setMaxAlternatives(3)`) ```javascript // After acceptWaveform returns true: const res = rec.result(); // { // "result": [ // { "conf": 1.0, "start": 0.87, "end": 1.11, "word": "what" }, // { "conf": 1.0, "start": 1.11, "end": 1.53, "word": "zero" } // ], // "text": "what zero" // } // With setMaxAlternatives > 0: // { // "alternatives": [ // { "text": "what zero zero one", "confidence": 0.97 }, // { "text": "what zero zero won", "confidence": 0.02 } // ] // } const partial = rec.partialResult(); // { "partial": "what ze" } const final = rec.finalResult(); // { "text": "what zero zero one" } ``` ### Response Example (Go - parsing final result) ```go // Go — parse final result into a map import "encoding/json" var result map[string]interface{} json.Unmarshal([]byte(rec.FinalResult()), &result) fmt.Println(result["text"]) ``` ``` -------------------------------- ### Configure and Run Training Script Source: https://github.com/alphacep/vosk-api/blob/master/python/example/colab/vosk-training.ipynb Navigates to the training directory, removes the 'exp' directory, modifies the run.sh script to use '--nj 2' for parallel processing, and then executes the training script up to stage 3. This initiates the acoustic model training process. ```bash %cd /content/kaldi/egs/ac/training !rm -rf exp !sed -i 's:--nj 10:--nj 2:g' run.sh !cat run.sh !bash run.sh --stop-stage 3 ``` -------------------------------- ### Prepare Data Directory and Files Source: https://github.com/alphacep/vosk-api/blob/master/python/example/colab/vosk-adaptation.ipynb Cleans up old data, creates a new dictionary directory, and copies essential configuration files for model adaptation. ```bash + rm -rf data ``` ```bash + rm -rf exp/tdnn/lgraph ``` ```bash + rm -rf exp/tdnn/lgraph_orig ``` ```bash + mkdir -p data/dict ``` ```bash + cp db/phone/extra_questions.txt db/phone/nonsilence_phones.txt db/phone/optional_silence.txt db/phone/silence_phones.txt data/dict ``` -------------------------------- ### Retrieve Recognition Output (Node.js) Source: https://context7.com/alphacep/vosk-api/llms.txt Retrieves recognition output as JSON. `result()` gets a complete utterance, `partialResult()` gets in-progress text, and `finalResult()` flushes the pipeline. Use `setWords(true)` for per-word timestamps and `setMaxAlternatives()` for multiple hypotheses. ```javascript // Node.js — result shapes with setWords(true) and setMaxAlternatives(3) rec.setWords(true); rec.setMaxAlternatives(3); // After acceptWaveform returns true: const res = rec.result(); // { // "result": [ // { "conf": 1.0, "start": 0.87, "end": 1.11, "word": "what" }, // { "conf": 1.0, "start": 1.11, "end": 1.53, "word": "zero" } // ], // "text": "what zero" // } // With setMaxAlternatives > 0: // { // "alternatives": [ // { "text": "what zero zero one", "confidence": 0.97 }, // { "text": "what zero zero won", "confidence": 0.02 } // ] // } const partial = rec.partialResult(); // { "partial": "what ze" } const final = rec.finalResult(); // { "text": "what zero zero one" } ``` -------------------------------- ### Initialize Vosk Recognizer Source: https://github.com/alphacep/vosk-api/blob/master/python/example/colab/vosk.ipynb Initializes the wave file, Vosk model, and KaldiRecognizer. Ensure the model is downloaded and accessible. ```python wf = wave.open('/content/test.wav', 'rb') model = Model(lang="en-us") rec = KaldiRecognizer(model, wf.getframerate()) rec.SetWords(True) ``` -------------------------------- ### Configure Kaldi Build Source: https://github.com/alphacep/vosk-api/blob/master/python/example/colab/kaldi-build.ipynb Navigates to the Kaldi source directory and runs the configure script with specified options. Ensure the path '/content/kaldi/src/' is correct and the script exists. ```python %cd /content/kaldi/src/ !./configure --shared --mathlib=OPENBLAS ``` -------------------------------- ### Get Tree Information Source: https://github.com/alphacep/vosk-api/blob/master/python/example/colab/vosk-training.ipynb Retrieves information about the decision tree used in the acoustic model training. ```bash tree-info exp/chain/tdnn/tree tree-info exp/chain/tdnn/tree ``` -------------------------------- ### Run Dictionary Preparation Stage Source: https://github.com/alphacep/vosk-api/blob/master/training/README.md Prepare the pronunciation dictionary required for Kaldi's language modeling scripts. This step is crucial for accurate speech recognition. ```bash bash run.sh --stage 1 --stop_stage 1 ``` -------------------------------- ### Vosk Language Model Preparation Utility Source: https://github.com/alphacep/vosk-api/blob/master/python/example/colab/vosk-adaptation.ipynb This utility prepares a language model directory by validating dictionary files and creating necessary intermediate files like lexiconp.txt. ```bash utils/prepare_lang.sh data/dict '[unk]' data/lang_local data/lang ``` -------------------------------- ### Initialize Neural Network Model Source: https://github.com/alphacep/vosk-api/blob/master/python/example/colab/vosk-training.ipynb Initializes a raw neural network model using configuration files. This is a prerequisite for training. ```bash nnet3-init exp/chain/tdnn/configs//ref.config exp/chain/tdnn/configs//ref.raw LOG (nnet3-init[5.5.1046~1-76cd5]:main():nnet3-init.cc:80) Initialized raw neural net and wrote it to exp/chain/tdnn/configs//ref.raw ``` ```bash nnet3-init exp/chain/tdnn/configs//ref.config exp/chain/tdnn/configs//ref.raw nnet3-info exp/chain/tdnn/configs//ref.raw LOG (nnet3-init[5.5.1046~1-76cd5]:main():nnet3-init.cc:80) Initialized raw neural net and wrote it to exp/chain/tdnn/configs//ref.raw ``` -------------------------------- ### Process Audio and Get Results Source: https://github.com/alphacep/vosk-api/blob/master/python/example/colab/vosk.ipynb Processes an audio file chunk by chunk and prints recognition results. It distinguishes between final results and partial results. ```python while True: data = wf.readframes(4000) if len(data) == 0: break if rec.AcceptWaveform(data): print(rec.Result()) else: jres = json.loads(rec.PartialResult()) print(jres) ``` -------------------------------- ### C# Recognize from WAV using Byte Buffers or Float Arrays Source: https://context7.com/alphacep/vosk-api/llms.txt Demonstrates recognizing speech from a WAV file using both byte buffer and float array approaches. Ensure the Vosk library is imported and a language model is loaded. ```csharp // C# — recognize from WAV file using byte buffers or float arrays using Vosk; using System.IO; Vosk.Vosk.SetLogLevel(0); // -1 = silent Model model = new Model("model-en-us"); // Byte-buffer approach VoskRecognizer rec = new VoskRecognizer(model, 16000.0f); rec.SetWords(true); rec.SetMaxAlternatives(0); using (Stream src = File.OpenRead("audio.wav")) { byte[] buf = new byte[4096]; int n; while ((n = src.Read(buf, 0, buf.Length)) > 0) { if (rec.AcceptWaveform(buf, n)) Console.WriteLine(rec.Result()); else Console.WriteLine(rec.PartialResult()); } } Console.WriteLine(rec.FinalResult()); // Float-array approach (useful when audio is already decoded to floats) VoskRecognizer recF = new VoskRecognizer(model, 16000.0f); recF.SetEndpointerMode(EndpointerMode.LONG); using (Stream src = File.OpenRead("audio.wav")) { byte[] buf = new byte[4096]; int n; while ((n = src.Read(buf, 0, buf.Length)) > 0) { float[] fbuf = new float[n / 2]; for (int i = 0, j = 0; i < fbuf.Length; i++, j += 2) fbuf[i] = BitConverter.ToInt16(buf, j); if (recF.AcceptWaveform(fbuf, fbuf.Length)) Console.WriteLine(recF.Result()); } } Console.WriteLine(recF.FinalResult()); ``` -------------------------------- ### C GPU Initialization Source: https://context7.com/alphacep/vosk-api/llms.txt Demonstrates the C API for initializing GPU acceleration. `vosk_gpu_init()` is called once from the main thread, and `vosk_gpu_thread_init()` is called within each thread that will perform recognition. ```c // C — GPU init #include int main() { vosk_gpu_init(); // main thread, once // spawn threads ... // in each thread: vosk_gpu_thread_init(); // per thread } ``` -------------------------------- ### Process Audio and Get Results Source: https://github.com/alphacep/vosk-api/blob/master/python/example/colab/vosk.ipynb Iterates through audio data, processing it with the recognizer and printing partial or final results. Results are parsed from JSON strings. ```python while True: data = wf.readframes(4000) if len(data) == 0: break if rec.AcceptWaveform(data): print(json.loads(rec.Result())) else: print(json.loads(rec.PartialResult())) print(json.loads(rec.FinalResult())) ``` -------------------------------- ### FFmpeg Integration for Media Transcription (Node.js) Source: https://context7.com/alphacep/vosk-api/llms.txt Transcribe any media format by piping audio through FFmpeg to Vosk. Ensure FFmpeg is installed and in your PATH. The output is raw PCM. ```javascript // Node.js — transcribe any media file via ffmpeg pipe const vosk = require('vosk'); const { spawn } = require('child_process'); const SAMPLE_RATE = 16000; const model = new vosk.Model('./model-en-us'); const rec = new vosk.Recognizer({ model, sampleRate: SAMPLE_RATE }); const ffmpeg = spawn('ffmpeg', [ '-loglevel', 'quiet', '-i', 'movie.mp4', // any format ffmpeg supports '-ar', String(SAMPLE_RATE), '-ac', '1', // mono '-f', 's16le', // raw PCM 16-bit LE '-bufsize', '4000', '-' // output to stdout ]); ffmpeg.stdout.on('data', chunk => { if (rec.acceptWaveform(chunk)) console.log(rec.result()); else console.log(rec.partialResult()); }); ffmpeg.on('exit', () => { console.log(rec.finalResult()); rec.free(); model.free(); }); ``` -------------------------------- ### Load Language Model (Go) Source: https://context7.com/alphacep/vosk-api/llms.txt Loads a language model and optionally initializes GPU support. The model is reference-counted and thread-safe. Use defer model.Free() to ensure cleanup. ```go import vosk "github.com/alphacep/vosk-api/go" vosk.GPUInit() // no-op unless compiled with CUDA support model, err := vosk.NewModel("model-en-us") if err != nil { log.Fatal(err) } def model.Free() // Check if a word is in the vocabulary (-1 = absent) sym := model.FindWord("hello") // returns int symbol or -1 ``` -------------------------------- ### Set up CUDA Build Environment Variables Source: https://github.com/alphacep/vosk-api/blob/master/python/example/colab/kaldi-build.ipynb These lines define environment variables used by the CUDA build system, specifying paths to CUDA libraries, NVVM, and target directories. ```bash #$ _NVVM_BRANCH_=nvvm #$ _SPACE_= #$ _CUDART_=cudart #$ _HERE_=/usr/local/cuda/bin #$ _THERE_=/usr/local/cuda/bin #$ _TARGET_SIZE_= #$ _TARGET_DIR_= #$ _TARGET_DIR_=targets/x86_64-linux #$ TOP=/usr/local/cuda/bin/.. #$ NVVMIR_LIBRARY_DIR=/usr/local/cuda/bin/../nvvm/libdevice #$ LD_LIBRARY_PATH=/usr/local/cuda/bin/../lib:/usr/lib64-nvidia #$ PATH=/usr/local/cuda/bin/../nvvm/bin:/usr/local/cuda/bin:/opt/bin:/usr/local/nvidia/bin:/usr/local/cuda/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/tools/node/bin:/tools/google-cloud-sdk/bin #$ INCLUDES="-I/usr/local/cuda/bin/../targets/x86_64-linux/include" #$ LIBRARIES= "-L/usr/local/cuda/bin/../targets/x86_64-linux/lib/stubs" "-L/usr/local/cuda/bin/../targets/x86_64-linux/lib" #$ CUDAFE_FLAGS= #$ PTXAS_FLAGS= ``` -------------------------------- ### Clean Up Kaldi Build Artifacts Source: https://github.com/alphacep/vosk-api/blob/master/python/example/colab/kaldi-build.ipynb Removes object files (.o), static libraries (.a), the 'egs' directory, and the '.git' directory from the Kaldi installation to clean up build artifacts. ```bash !find /content/kaldi -name "*.o" -exec rm {} \; !find /content/kaldi -name "*.a" -exec rm {} \; !rm -rf /content/kaldi/egs !rm -rf /content/kaldi/.git ``` -------------------------------- ### Initialize Recognizer with Grammar Source: https://github.com/alphacep/vosk-api/blob/master/python/example/colab/vosk.ipynb Initializes the KaldiRecognizer with a specific grammar to improve recognition accuracy for expected phrases. Ensure the audio file is opened in binary read mode. ```python wf = wave.open('/content/test.wav', "rb") rec = KaldiRecognizer(model, wf.getframerate(), '["one zero zero zero one", "nine oh two one oh", "zero one eight zero three", "[unk]"]') ``` -------------------------------- ### Prepare Language Model and Decode Source: https://github.com/alphacep/vosk-api/blob/master/python/example/colab/vosk-training.ipynb This section prepares the language model and performs decoding for test sets. It involves formatting the language model, building a graph, and then executing decoding steps with specific parameters for acoustic modeling and lattice generation. ```bash if [ ${stage} -le 5 ] && [ ${stop_stage} -ge 5 ]; then utils/format_lm.sh data/lang data/local/lm/lm_tgsmall.arpa.gz data/local/dict/lexicon.txt data/lang_test utils/mkgraph.sh --self-loop-scale 1.0 data/lang_test exp/chain/tdnn exp/chain/tdnn/graph utils/build_const_arpa_lm.sh data/local/lm/lm_tgmed.arpa.gz \ data/lang data/lang_test_rescore for task in test; do steps/make_mfcc.sh --cmd "$train_cmd" --nj 2 data/$task exp/make_mfcc/$task $mfcc steps/compute_cmvn_stats.sh data/$task exp/make_mfcc/$task $mfcc steps/online/nnet2/extract_ivectors_online.sh --nj 2 \ data/${task} exp/chain/extractor \ exp/chain/ivectors_${task} steps/nnet3/decode.sh --cmd $decode_cmd --num-threads 10 --nj 1 \ --beam 13.0 --max-active 7000 --lattice-beam 4.0 \ --online-ivector-dir exp/chain/ivectors_${task} \ --acwt 1.0 --post-decode-acwt 10.0 \ exp/chain/tdnn/graph data/${task} exp/chain/tdnn/decode_${task} steps/lmrescore_const_arpa.sh data/lang_test data/lang_test_rescore \ data/${task} exp/chain/tdnn/decode_${task} exp/chain/tdnn/decode_${task}_rescore done bash RESULTS fi ``` -------------------------------- ### Recognizer Constructors (Java/Android) Source: https://context7.com/alphacep/vosk-api/llms.txt Shows how to instantiate a Vosk Recognizer in Java/Android for standard, grammar-constrained, and speaker-identification tasks. Ensure the correct parameters are provided for each type. ```java // Java / Android import org.vosk.*; Model model = new Model("model-en-us"); // Standard Recognizer rec = new Recognizer(model, 16000f); // Grammar-constrained Recognizer recGrm = new Recognizer(model, 16000f, "[\"turn on the light\", \"turn off the light\", \"[unk]\"]"); // Speaker identification SpeakerModel spkModel = new SpeakerModel("model-spk"); Recognizer recSpk = new Recognizer(model, 16000f, spkModel); recSpk.close(); recGrm.close(); rec.close(); ``` -------------------------------- ### Language Model Preparation Script Source: https://github.com/alphacep/vosk-api/blob/master/python/example/colab/vosk-adaptation.ipynb This script prepares a language model by merging, renormalizing, and printing it in ARPA format. It requires specific symbol files and input models. ```bash ngramshrink --method=count_prune --count_pattern=3+:3 ngramread ngramprint --integers grep -v '' ngramread --renormalize_arpa --ARPA --symbols=data/mix.syms - data/en-us.mod ngrammerge --method=bayes_model_merge --normalize --alpha=0.95 --beta=0.05 data/en-us.mod data/extra.mod data/en-us-mix.mod gramprint --ARPA data/en-us-mix.mod gzip -c ``` -------------------------------- ### Play Audio File Source: https://github.com/alphacep/vosk-api/blob/master/python/example/colab/vosk.ipynb Plays the downloaded audio file using IPython.display.Audio. Ensure the audio file path is correct. ```python import IPython IPython.display.Audio("/content/test.wav") ``` -------------------------------- ### Create Language Directory with Chain Topology Source: https://github.com/alphacep/vosk-api/blob/master/python/example/colab/vosk-training.ipynb Generates a language directory with a chain-type topology. This involves copying an existing language directory and creating a new topology file. ```bash if [ $stage -le 10 ]; then echo "$0: creating lang directory $lang with chain-type topology" rm -rf $lang cp -r data/lang $lang silphonelist=$(cat $lang/phones/silence.csl) nonsilphonelist=$(cat $lang/phones/nonsilence.csl) steps/nnet3/chain/gen_topo.py $nonsilphonelist $silphonelist >$lang/topo fi ``` -------------------------------- ### Import Vosk Modules Source: https://github.com/alphacep/vosk-api/blob/master/python/example/colab/vosk.ipynb Import the necessary modules from the vosk library, along with the wave and json modules for audio file handling and data parsing. ```python from vosk import Model, KaldiRecognizer import wave import json ``` -------------------------------- ### Kaldi Build Dependency Generation (fstbin) Source: https://github.com/alphacep/vosk-api/blob/master/python/example/colab/kaldi-build.ipynb Initiates dependency generation for Kaldi's fstbin module. This command typically precedes the actual compilation of fstbin related files. ```bash make -C fstbin/ depend make[2]: Entering directory '/content/kaldi/src/fstbin' rm -f .depend.mk ``` -------------------------------- ### Configure Vosk Recognizer Settings Source: https://context7.com/alphacep/vosk-api/llms.txt Fine-tune recognizer output and behavior before processing audio. Ensure the model is loaded before configuring. ```javascript // Node.js — full configuration example const rec = new vosk.Recognizer({ model, sampleRate: 16000 }); rec.setWords(true); // include timestamps+confidence per word in result rec.setPartialWords(true); // include per-word data in partial results too rec.setMaxAlternatives(5); // return n-best list instead of single best // Recognizer reset — restart from scratch without re-creating object rec.reset(); ``` ```go // Go — endpointer timing control rec.SetWords(1) rec.SetPartialWords(1) rec.SetMaxAlternatives(3) // startMax: initial silence timeout (s), end: post-speech timeout (s), max: force-end timeout (s) rec.SetEndpointerDelays(5.0, 1.0, 20.0) ``` ```java // Java / Android rec.setWords(true); rec.setPartialWords(true); rec.setMaxAlternatives(3); rec.setEndpointerMode(Recognizer.EndpointerMode.LONG); // DEFAULT, SHORT, LONG, VERY_LONG rec.setEndpointerDelays(5.0f, 1.0f, 20.0f); rec.reset(); ``` -------------------------------- ### Open Audio File Source: https://github.com/alphacep/vosk-api/blob/master/python/example/colab/vosk.ipynb Opens a local audio file in read-byte mode as a wave object. This is a prerequisite for initializing the recognizer. ```python wf = wave.open('/content/test.wav', 'rb') ``` -------------------------------- ### Use Vosk Transcriber CLI Source: https://github.com/alphacep/vosk-api/blob/master/ruby/README.md Transcribe audio files directly from the command line using the included `vosk-transcriber` executable. ```bash vosk-transcriber audio.wav ``` -------------------------------- ### Model - Load a language model from disk Source: https://context7.com/alphacep/vosk-api/llms.txt Demonstrates how to load a Vosk language model. The `Model` object is reference-counted and thread-safe, allowing it to be shared across multiple recognizer threads. Remember to free the model when it's no longer needed. ```APIDOC ## Model ### Description Loads a language model from disk. This object holds the static neural-network and language-model data. It is reference-counted and thread-safe. ### Usage #### Node.js ```javascript const vosk = require('vosk'); vosk.setLogLevel(0); // 0 = info+errors, -1 = silent, >0 = verbose // Load model once; reuse across recognizers const model = new vosk.Model('./model-en-us'); // Always free when no longer needed // model.free(); ``` #### Go ```go import vosk "github.com/alphacep/vosk-api/go" vosk.GPUInit() // no-op unless compiled with CUDA support model, err := vosk.NewModel("model-en-us") if err != nil { log.Fatal(err) } defer model.Free() // Check if a word is in the vocabulary (-1 = absent) sym := model.FindWord("hello") // returns int symbol or -1 ``` #### C ```c #include VoskModel *model = vosk_model_new("model-en-us"); int sym = vosk_model_find_word(model, "hello"); // -1 if not found // ... vosk_model_free(model); ``` ``` -------------------------------- ### Generate Neural Network Configurations Source: https://github.com/alphacep/vosk-api/blob/master/python/example/colab/vosk-training.ipynb Creates neural network configurations using the xconfig parser. This involves defining layers, input dimensions, and various training options. ```bash if [ $stage -le 13 ]; then echo "$0: creating neural net configs using the xconfig parser"; num_targets=$(tree-info $tree_dir/tree | grep num-pdfs | awk '{print $2}') learning_rate_factor=$(echo "print (0.5/$xent_regularize)" | python) affine_opts="l2-regularize=0.008 dropout-proportion=0.0 dropout-per-dim=true dropout-per-dim-continuous=true" tdnnf_opts="l2-regularize=0.008 dropout-proportion=0.0 bypass-scale=0.75" linear_opts="l2-regularize=0.008 orthonormal-constraint=-1.0" prefinal_opts="l2-regularize=0.008" output_opts="l2-regularize=0.002" mkdir -p $dir/configs cat < $dir/configs/network.xconfig input dim=40 name=ivector input dim=40 name=input idct-layer name=idct input=input dim=40 cepstral-lifter=22 affine-transform-file=$dir/configs/idct.mat batchnorm-component name=batchnorm0 input=idct spec-augment-layer name=spec-augment freq-max-proportion=0.5 time-zeroed-proportion=0.2 time-mask-max-frames=20 delta-layer name=delta input=spec-augment no-op-component name=input2 input=Append(delta, ReplaceIndex(ivector, t, 0)) # the first splicing is moved before the lda layer, so no splicing here relu-batchnorm-dropout-layer name=tdnn1 $affine_opts dim=512 input=input2 tdnnf-layer name=tdnnf2 $tdnnf_opts dim=512 bottleneck-dim=96 time-stride=1 tdnnf-layer name=tdnnf3 $tdnnf_opts dim=512 bottleneck-dim=96 time-stride=1 tdnnf-layer name=tdnnf4 $tdnnf_opts dim=512 bottleneck-dim=96 time-stride=1 tdnnf-layer name=tdnnf5 $tdnnf_opts dim=512 bottleneck-dim=96 time-stride=0 tdnnf-layer name=tdnnf6 $tdnnf_opts dim=512 bottleneck-dim=96 time-stride=3 tdnnf-layer name=tdnnf7 $tdnnf_opts dim=512 bottleneck-dim=96 time-stride=3 tdnnf-layer name=tdnnf8 $tdnnf_opts dim=512 bottleneck-dim=96 time-stride=3 tdnnf-layer name=tdnnf9 $tdnnf_opts dim=512 bottleneck-dim=96 time-stride=3 tdnnf-layer name=tdnnf10 $tdnnf_opts dim=512 bottleneck-dim=96 time-stride=3 tdnnf-layer name=tdnnf11 $tdnnf_opts dim=512 bottleneck-dim=96 time-stride=3 tdnnf-layer name=tdnnf12 $tdnnf_opts dim=512 bottleneck-dim=96 time-stride=3 linear-component name=prefinal-l dim=192 $linear_opts ## adding the layers for chain branch prefinal-layer name=prefinal-chain input=prefinal-l $prefinal_opts small-dim=192 big-dim=512 output-layer name=output include-log-softmax=false dim=$num_targets $output_opts # adding the layers for xent branch prefinal-layer name=prefinal-xent input=prefinal-l $prefinal_opts small-dim=192 big-dim=512 output-layer name=output-xent dim=$num_targets learning-rate-factor=$learning_rate_factor $output_opts EOF steps/nnet3/xconfig_to_configs.py --xconfig-file $dir/configs/network.xconfig --config-dir $dir/configs/ fi ``` -------------------------------- ### Generate Dependencies for nnet3bin Source: https://github.com/alphacep/vosk-api/blob/master/python/example/colab/kaldi-build.ipynb Generates dependency files for the nnet3bin module. This command is part of the Kaldi build system. ```bash make -C transform/ depend make[2]: Entering directory '/content/kaldi/src/transform' rm -f .depend.mk c++ -M -std=c++17 -I.. -isystem /content/kaldi/tools/openfst-1.8.0/include -O1 -Wno-sign-compare -Wall -Wno-sign-compare -Wno-unused-local-typedefs -Wno-deprecated-declarations -Winit-self -DKALDI_DOUBLEPRECISION=0 -DHAVE_EXECINFO_H=1 -DHAVE_CXXABI_H -DHAVE_OPENBLAS -I/content/kaldi/tools/OpenBLAS/install/include -msse -msse2 -pthread -g -fPIC -DHAVE_CUDA -I/usr/local/cuda/include -fPIC -pthread -isystem /content/kaldi/tools/openfst-1.8.0/include rnnlm-get-egs.cc rnnlm-train.cc rnnlm-get-sampling-lm.cc rnnlm-compute-prob.cc rnnlm-sentence-probs.cc rnnlm-get-word-embedding.cc >> .depend.mk rm -f .depend.mk c++ -M -std=c++17 -I.. -isystem /content/kaldi/tools/openfst-1.8.0/include -O1 -Wall -Wno-sign-compare -Wno-unused-local-typedefs -Wno-deprecated-declarations -Winit-self -DKALDI_DOUBLEPRECISION=0 -DHAVE_EXECINFO_H=1 -DHAVE_CXXABI_H -DHAVE_OPENBLAS -I/content/kaldi/tools/OpenBLAS/install/include -msse -msse2 -pthread -g -fPIC -DHAVE_CUDA -I/usr/local/cuda/include -fPIC -pthread -isystem /content/kaldi/tools/openfst-1.8.0/include lda-estimate-test.cc transform-common.cc regression-tree-test.cc compressed-transform-stats.cc decodable-am-diag-gmm-regtree.cc fmllr-raw-test.cc lda-estimate.cc lvtln.cc regression-tree.cc regtree-mllr-diag-gmm-test.cc fmllr-diag-gmm.cc regtree-fmllr-diag-gmm.cc cmvn.cc fmllr-raw.cc fmpe.cc mllt.cc fmllr-diag-gmm-test.cc regtree-mllr-diag-gmm.cc basis-fmllr-diag-gmm.cc regtree-fmllr-diag-gmm-test.cc fmpe-test.cc >> .depend.mk make[2]: Leaving directory '/content/kaldi/src/nnet3bin' ``` -------------------------------- ### Build Acoustic Model Tree Source: https://github.com/alphacep/vosk-api/blob/master/python/example/colab/vosk-training.ipynb Constructs the acoustic model tree using the build_tree.sh script. This script requires frame subsampling and context options to be specified. ```bash if [ $stage -le 12 ]; then steps/nnet3/chain/build_tree.sh \ --frame-subsampling-factor 3 \ --context-opts "--context-width=2 --central-position=1" \ --cmd "$train_cmd" 2500 ${train_data_dir} \ $lang $ali_dir $tree_dir fi ``` -------------------------------- ### Kaldi Build Dependency Generation (fgmmbin) Source: https://github.com/alphacep/vosk-api/blob/master/python/example/colab/kaldi-build.ipynb Generates dependency files for Kaldi's fgmmbin module using C++17 standard and specific include paths. Excludes certain compiler warnings. ```bash make -C fgmmbin/ depend make[2]: Entering directory '/content/kaldi/src/fgmmbin' rm -f .depend.mk c++ -M -std=c++17 -I.. -isystem /content/kaldi/tools/openfst-1.8.0/include -O1 -Wno-sign-compare -Wall -Wno-sign-compare -Wno-unused-local-typedefs -Wno-deprecated-declarations -Winit-self -DKALDI_DOUBLEPRECISION=0 -DHAVE_EXECINFO_H=1 -DHAVE_CXXABI_H -DHAVE_OPENBLAS -I/content/kaldi/tools/OpenBLAS/install/include -msse -msse2 -pthread -g -fPIC -DHAVE_CUDA -I/usr/local/cuda/include -fPIC -pthread -isystem /content/kaldi/tools/openfst-1.8.0/include fgmm-global-merge.cc fgmm-global-info.cc fgmm-global-acc-stats-post.cc fgmm-global-init-from-accs.cc fgmm-global-est.cc fgmm-gselect.cc fgmm-global-sum-accs.cc fgmm-global-copy.cc fgmm-global-to-gmm.cc fgmm-global-acc-stats.cc fgmm-global-get-frame-likes.cc fgmm-global-gselect-to-post.cc >> .depend.mk make[2]: Leaving directory '/content/kaldi/src/fgmmbin' ``` -------------------------------- ### Compile Kaldi with Make Source: https://github.com/alphacep/vosk-api/blob/master/python/example/colab/kaldi-build.ipynb Compiles the Kaldi toolkit using the make command with parallel job execution. This command is typically run after the configure script has successfully completed. ```bash !make -j 4 ``` -------------------------------- ### Asynchronous Audio Processing with acceptWaveformAsync (Node.js) Source: https://context7.com/alphacep/vosk-api/llms.txt Non-blocking version of acceptWaveform that returns a Promise, enabling parallel processing of multiple audio streams with a single shared model. Useful for handling multiple files concurrently. ```javascript // Node.js — process 10 files concurrently sharing one model const vosk = require('vosk'); const async = require('async'); const fs = require('fs'); const { Readable } = require('stream'); const wav = require('wav'); const model = new vosk.Model('./model-en-us'); const files = Array(10).fill('audio.wav'); async.filter(files, function(filePath, callback) { const wfReader = new wav.Reader(); const wfReadable = new Readable().wrap(wfReader); wfReader.on('format', async ({ sampleRate }) => { const rec = new vosk.Recognizer({ model, sampleRate }); for await (const data of wfReadable) { const endOfSpeech = await rec.acceptWaveformAsync(data); if (endOfSpeech) console.log(rec.result()); } console.log(rec.finalResult()); rec.free(); callback(null, true); }); fs.createReadStream(filePath, { highWaterMark: 4096 }).pipe(wfReader); }, (err) => { model.free(); console.log('All done'); }); ``` -------------------------------- ### Recognizer Constructors (Node.js) Source: https://context7.com/alphacep/vosk-api/llms.txt Illustrates the three constructor variants for the Vosk Recognizer in Node.js: standard large-vocabulary, grammar-constrained, and speaker-identification. ```javascript // Node.js — three constructor forms // 1. Standard large-vocabulary recognizer const rec1 = new vosk.Recognizer({ model, sampleRate: 16000 }); // 2. Grammar-constrained recognizer (only recognizes listed phrases) const rec2 = new vosk.Recognizer({ model, sampleRate: 16000, grammar: ["turn on the light", "turn off the light", "what time is it", "[unk]"] }); // 3. Speaker-identification recognizer const rec3 = new vosk.Recognizer({ model, sampleRate: 16000, speakerModel: speakerModel }); rec1.free(); rec2.free(); rec3.free(); ```