|
| 1 | +# Vosk Ruby |
| 2 | + |
| 3 | +Ruby bindings for [Vosk](https://alphacephei.com/vosk/) — an offline speech recognition toolkit supporting 20+ languages. |
| 4 | + |
| 5 | +## Installation |
| 6 | + |
| 7 | +Add to your Gemfile: |
| 8 | + |
| 9 | +```ruby |
| 10 | +gem "vosk" |
| 11 | +``` |
| 12 | + |
| 13 | +Or install directly: |
| 14 | + |
| 15 | +```bash |
| 16 | +gem install vosk |
| 17 | +``` |
| 18 | + |
| 19 | +The gem ships with a precompiled `libvosk` for supported platforms. On other platforms it will attempt to load a system-installed `libvosk`. |
| 20 | + |
| 21 | +## Usage |
| 22 | + |
| 23 | +### Basic transcription |
| 24 | + |
| 25 | +```ruby |
| 26 | +require "vosk" |
| 27 | +require "wavefile" |
| 28 | + |
| 29 | +# Load a model by language (downloaded automatically if not cached) |
| 30 | +model = Vosk::Model.new(lang: "en-us") |
| 31 | + |
| 32 | +# Or by name, or by local path: |
| 33 | +# model = Vosk::Model.new(model_name: "vosk-model-small-en-us-0.4") |
| 34 | +# model = Vosk::Model.new(model_path: "/path/to/model") |
| 35 | + |
| 36 | +WaveFile::Reader.new("audio.wav") do |reader| |
| 37 | + rec = Vosk::KaldiRecognizer.new(model, reader.format.sample_rate) |
| 38 | + |
| 39 | + reader.each_buffer(4000) do |buffer| |
| 40 | + data = buffer.samples.pack(WaveFile::PACK_CODES.dig(:pcm, 16)) |
| 41 | + if rec.accept_waveform(data).nonzero? |
| 42 | + puts rec.result # JSON: {"text": "..."} |
| 43 | + else |
| 44 | + puts rec.partial_result # JSON: {"partial": "..."} |
| 45 | + end |
| 46 | + end |
| 47 | + puts rec.final_result |
| 48 | +end |
| 49 | +``` |
| 50 | + |
| 51 | +Audio must be mono, 16-bit PCM WAV. Use [wavefile](https://github.com/jstrait/wavefile) to read it. |
| 52 | + |
| 53 | +### Grammar / keyword recognition |
| 54 | + |
| 55 | +Pass a JSON array of phrases as the third argument: |
| 56 | + |
| 57 | +```ruby |
| 58 | +rec = Vosk::KaldiRecognizer.new(model, sample_rate, '["one two three", "[unk]"]') |
| 59 | +``` |
| 60 | + |
| 61 | +### Recognizer options |
| 62 | + |
| 63 | +```ruby |
| 64 | +rec.words = true # include per-word timing in results |
| 65 | +rec.partial_words = true # include per-word timing in partial results |
| 66 | +rec.max_alternatives = 5 # return n-best list instead of single result |
| 67 | +rec.nlsml = true # return NLSML instead of JSON |
| 68 | +``` |
| 69 | + |
| 70 | +### SRT subtitle generation |
| 71 | + |
| 72 | +`srt_result` reads raw PCM from any IO stream and returns an SRT-formatted string. Use ffmpeg to decode any audio format on the fly: |
| 73 | + |
| 74 | +```ruby |
| 75 | +require "vosk" |
| 76 | + |
| 77 | +SAMPLE_RATE = 16_000 |
| 78 | + |
| 79 | +model = Vosk::Model.new(lang: "en-us") |
| 80 | +rec = Vosk::KaldiRecognizer.new(model, SAMPLE_RATE) |
| 81 | +rec.words = true # required for word-level timestamps |
| 82 | + |
| 83 | +IO.popen(["ffmpeg", "-loglevel", "quiet", "-i", "audio.mp4", |
| 84 | + "-ar", SAMPLE_RATE.to_s, "-ac", "1", "-f", "s16le", "-",]) do |stream| |
| 85 | + puts rec.srt_result(stream) |
| 86 | +end |
| 87 | +``` |
| 88 | + |
| 89 | +The `words_per_line:` keyword controls how many words appear per subtitle line (default: 7): |
| 90 | + |
| 91 | +```ruby |
| 92 | +rec.srt_result(stream, words_per_line: 5) |
| 93 | +``` |
| 94 | + |
| 95 | +### Speaker identification |
| 96 | + |
| 97 | +```ruby |
| 98 | +spk_model = Vosk::SpkModel.new("/path/to/spk-model") |
| 99 | +rec = Vosk::KaldiRecognizer.new(model, sample_rate) |
| 100 | +rec.spk_model = spk_model |
| 101 | +``` |
| 102 | + |
| 103 | +### Listing available models |
| 104 | + |
| 105 | +```ruby |
| 106 | +puts Vosk.models # all model names |
| 107 | +puts Vosk.languages # all supported language codes |
| 108 | +``` |
| 109 | + |
| 110 | +### Logging |
| 111 | + |
| 112 | +```ruby |
| 113 | +Vosk.log_level = -1 # suppress all output |
| 114 | +Vosk.log_level = 0 # default |
| 115 | +``` |
| 116 | + |
| 117 | +### Transcriber CLI |
| 118 | + |
| 119 | +The gem includes a `vosk-transcriber` executable: |
| 120 | + |
| 121 | +```bash |
| 122 | +vosk-transcriber audio.wav |
| 123 | +``` |
| 124 | + |
| 125 | +## Model storage |
| 126 | + |
| 127 | +Models are cached in `~/.cache/vosk/` by default, or in the directory set by `$VOSK_MODEL_PATH`. |
| 128 | + |
| 129 | +## Development |
| 130 | + |
| 131 | +```bash |
| 132 | +bundle install |
| 133 | +bundle exec rake spec |
| 134 | +``` |
| 135 | + |
| 136 | +## License |
| 137 | + |
| 138 | +Apache-2.0 |
0 commit comments