Compare commits

..

342 Commits

Author SHA1 Message Date
Nickolay Shmyrev 7b3ea0b59d Merge branch 'master' of github.com:alphacep/vosk-api 2021-08-31 21:56:49 +02:00
Nickolay Shmyrev e2af710369 Rework rescoring for faster and more accurate results 2021-08-31 21:56:21 +02:00
Nickolay V. Shmyrev 6b1b620f39 Merge pull request #678 from agorman/go-bindings
Cleaning up and completing go bindings
2021-08-31 22:34:19 +03:00
Andy Gorman 966524da16 gofmt 2021-08-31 12:24:19 -07:00
Andy Gorman 83c999f298 Cleaning up and completing go bindings 2021-08-31 12:18:34 -07:00
Nickolay Shmyrev abff8a4f56 Organize makefile structure 2021-07-29 11:58:31 +02:00
Timur 188575f3a2 Added MKL build options (#647)
Co-authored-by: Kasimov Timur <t.kasimov@cft.ru>
2021-07-29 11:03:39 +03:00
Nickolay Shmyrev 9f4d8ca187 Update doc 2021-07-12 20:29:42 +02:00
Nickolay Shmyrev 72cc8f3a5e Added test doc 2021-07-12 20:23:49 +02:00
Nickolay Shmyrev 915dcab597 Add doc 2021-07-11 15:54:42 +02:00
Nickolay Shmyrev d82052b168 Better have it here 2021-07-11 11:26:32 +02:00
Nickolay Shmyrev dbb65bc71c Unify example 2021-07-11 11:01:56 +02:00
Nickolay Shmyrev 2498bc595b Add module file 2021-07-11 10:22:24 +02:00
Nickolay Shmyrev fee63d712f Add go bindings 2021-07-10 23:44:15 +02:00
Nickolay Shmyrev 5965ea30e6 Fetch_sub returns original value 2021-07-06 12:11:12 +02:00
Nickolay Shmyrev 5c75495c03 Atomic refcounting for thread safety. See
https://github.com/alphacep/vosk-api/issues/606

for more details
2021-07-06 11:09:34 +02:00
Nickolay Shmyrev 98ae3c66cb Bump version 2021-06-28 02:09:06 +02:00
Nickolay Shmyrev 942e15484a Protect against multiple resets 2021-06-28 01:59:43 +02:00
Lars Kiesow 2349e66a97 Subtitles require word times (#607)
This is a port of the recent addition of commit 7ccf743, adding
`KaldiRecognizer.SetWords(True)` to the other examples dealing with
subtitles to the WebVTT example.

Without this, the example will not work with the most recent `vosk`
(0.3.30) Python package.
2021-06-27 18:06:11 +03:00
Nickolay Shmyrev 5750e3224f Merge branch 'master' of github.com:alphacep/vosk-api 2021-06-24 20:09:12 +02:00
Nickolay Shmyrev 558b4dd69e Don't align lattice twice 2021-06-24 20:08:54 +02:00
Lars Kiesow 02ef49f67e Allow Saving WebVTT (#605)
This patch is a small extension to the WebVTT example which allows to
directly save the WebVTT output to a file like this:

    ./test_webvtt.py test.wav out.vtt
2021-06-24 19:35:30 +03:00
Lars Kiesow 7cdf8f1d03 Add Python WebVTT Example (#601)
This patch adds an example for using webvtt-py to generate WebVTT files
from Vosk output. This is similar to the SRT example but still very
useful for generating an example video subtitle usable in web contexts.
2021-06-23 01:16:33 +03:00
Nickolay Shmyrev 7f894f784a Better check for the model files 2021-06-21 00:19:55 +02:00
Nickolay Shmyrev a770251151 Bump nodejs version to 0.3.30 2021-06-20 21:30:58 +02:00
Nickolay Shmyrev 7ccf743bb6 SRT requires word times 2021-06-10 10:37:15 +02:00
Nickolay Shmyrev 75bedfe06d Add a method to show/hide words and their times 2021-06-07 01:04:37 +02:00
Nickolay Shmyrev 4ea9885d44 Bump version 2021-05-30 21:59:46 +02:00
Nickolay Shmyrev 0930def9ec Fix critical accuracy issues, actually use rescoring 2021-05-30 21:58:06 +02:00
Nickolay Shmyrev 6aa5af7640 Add reset test 2021-05-26 21:22:07 +02:00
Nickolay Shmyrev e74fe5edf7 Reset reset flag 2021-05-26 21:01:27 +02:00
Nickolay Shmyrev cbb5d0fcdf Implement reset method in SpeechService 2021-05-26 20:51:44 +02:00
Nickolay Shmyrev d489bc40e4 Update Java part too 2021-05-26 00:59:08 +02:00
Nickolay Shmyrev 499b2f183a Introduce new API to set speaker model to already initialized recognizer.
Introduce a method to reset recognizer results to start from scratch without
computation of the result.
2021-05-26 00:46:32 +02:00
Nickolay Shmyrev f57b926f66 Unpack library, loading works on Windows 2021-05-23 01:29:29 +02:00
Nickolay Shmyrev 733dca5aea Attempt to fix loading on Windows 2021-05-22 00:41:30 +02:00
Nickolay Shmyrev 1ab7e8ca87 Use older API for backward compatibility 2021-05-20 20:48:21 +02:00
Nickolay Shmyrev 53426f794c Add note about Ukrainian 2021-05-20 01:29:15 +02:00
Nickolay Shmyrev f8189685e5 Add max alternatives output 2021-05-19 18:47:25 +02:00
Nickolay Shmyrev e6bd200c85 Proper path delimiter on windows 2021-05-13 09:36:56 +02:00
Nickolay Shmyrev 4a4c33fd9d Load dependent libraries 2021-05-10 01:39:26 +02:00
Nickolay Shmyrev dc44922b92 Add code to expand path 2021-05-10 01:26:05 +02:00
Nickolay Shmyrev 67896cfba7 Bump bindings versions 2021-05-05 20:50:25 +02:00
Nickolay Shmyrev ee8663e6d9 RNNLM rescoring support 2021-05-05 19:34:30 +02:00
Nickolay Shmyrev 652c05f052 Reformat and simplify code a bit 2021-05-02 21:44:54 +02:00
Nickolay Shmyrev 193d129d9e Model can be a symlink 2021-04-30 11:58:11 +02:00
Sadra Sabouri e995df4bc3 New Examples for Nodejs (Base on Python Examples) (#515) 2021-04-30 10:16:36 +03:00
Sergey Korol f6a30c3626 Added missing GPU methods to nuget package. (#514) 2021-04-29 20:57:56 +03:00
Nickolay Shmyrev ee95420f8f Fix spk model initialization 2021-04-27 23:24:10 +02:00
Nickolay Shmyrev 2221cd0209 Reorganize methods a bit 2021-04-26 23:25:05 +02:00
Nickolay Shmyrev 8368d831aa Add async processing demo 2021-04-26 22:34:33 +02:00
Nickolay Shmyrev 91a128b3ed Fix package name 2021-04-15 13:54:48 +02:00
Nickolay Shmyrev 83d8b63351 Merge branch 'master' of github.com:alphacep/vosk-api 2021-04-15 12:57:17 +02:00
Nickolay Shmyrev 0881aa856f Update for new API 2021-04-15 12:56:59 +02:00
Nickolay Shmyrev 77abb02ada Bump version 2021-04-12 12:23:11 +02:00
François Billioud 119c6cd0dc Update ffi-napi and improve dependency to ref-napi (#483)
With this change, we will use the same version of ref-napi than the one declared in ffi-napi, which reduce dependency tree, bundle size, risks of errors that could be fixed in one version and not in the other, etc
2021-04-01 22:12:14 +03:00
Nickolay Shmyrev cc649aa78e Fixes demo 2021-04-01 20:33:01 +02:00
François Billioud 0b0c9fce0e Types for typescript projects, and jsDoc (#461)
Implement Javascript API
2021-04-01 21:25:55 +03:00
Peter Kronenberg d845e2107f Allow libvosk.dll to get loaded on Windows (#478)
* Allow libvosk.dll to get loaded on Windows

Authored-by: Peter Kronenberg <peter.kronenberg@torch.ai>
2021-03-31 18:55:45 +03:00
Nickolay V. Shmyrev 69992a1b39 Android through JNA framework unified with Java (#476)
* Move to JNA

* Update for android

* Joint publishing

* Fix the developer id

* Added grammar constructor for Java
2021-03-30 23:36:26 +03:00
Nickolay Shmyrev f351280f49 Allow extra cflags 2021-03-30 19:24:00 +02:00
Nickolay Shmyrev c15065b044 Wrap short and float 2021-03-30 17:34:00 +02:00
Nickolay Shmyrev db41f16051 Docs are on the separate project 2021-03-29 00:38:54 +02:00
Nickolay Shmyrev e276de61be Give jar a name and fix OSX library path 2021-03-29 00:20:52 +02:00
Nickolay Shmyrev 43c786ab18 Move jars to our own maven and create a demo 2021-03-28 23:38:43 +02:00
Nickolay Shmyrev 1199f39d55 Move project to a library 2021-03-28 13:02:32 +02:00
Nickolay Shmyrev 70aab86257 Remove test code 2021-03-28 10:11:53 +02:00
Nickolay Shmyrev e4b0af7a8e Update nuget package, add osx library 2021-03-27 23:40:02 +01:00
Martin Mende a120d06f6b Fixed nodejs platform check (#464) 2021-03-24 17:08:39 +03:00
Nickolay Shmyrev e01ca37e45 Add osx module 2021-03-18 00:46:51 +01:00
Nickolay Shmyrev f5c3d898e2 Option to compile on Mac 2021-03-18 02:10:56 +03:00
Nickolay Shmyrev a5a3697b7c Copy data before queue, original data can be destroyed.
Fixes issue #444

Thanks to Alexander Zatvornitsky
2021-03-01 21:25:41 +01:00
Nickolay Shmyrev 4be9874ba2 Reformat 2021-02-28 22:40:40 +01:00
Peter Kronenberg 5647a2dc6a Java example with better type safety, Java objects and support for au… (#429)
Java example with better type safety, Java objects and support for autoclosable

Authored-by: Peter Kronenberg <peter.kronenberg@torch.ai>
2021-03-01 00:35:31 +03:00
Nickolay Shmyrev a067a5b5f3 Enable pitch if conf/pitch.conf is present
Fixes issue #442
2021-02-26 20:34:17 +01:00
Nickolay Shmyrev 475b0ecfc1 Free symbol table only if we loaded it 2021-02-25 19:17:13 +01:00
Nickolay V. Shmyrev 2a9a605144 Merge pull request #436 from sskorol/435-sync-master-with-gpu
Sync master with GPU
2021-02-22 13:41:37 +03:00
sskorol eea7ca571b - Added an optional HAVE_CUDA flag to build Vosk with GPU support.
- Added missing GpuInit/ThreadInit python wrappers.
2021-02-22 12:30:46 +02:00
Nickolay Shmyrev 11c16610ee Fix memory leaks for big models. Fixes issue #425. 2021-02-19 19:25:07 +01:00
Nickolay Shmyrev 2f4124eb66 Demonstrate speaker 2021-02-16 17:40:21 +01:00
Nickolay Shmyrev db3e31d7ce Automatically put .so file in resources 2021-02-16 17:33:09 +01:00
Nickolay Shmyrev d917af21ab Better search for Windows dlls 2021-02-12 22:24:54 +01:00
Nickolay Shmyrev 948c8c7cfd Update to 0.3.21 2021-02-12 10:13:50 +01:00
Nickolay Shmyrev dec8e1acf3 Move to JNA instead of JNR-FFI for better Android compatibility 2021-02-11 00:04:19 +01:00
Nickolay Shmyrev 3c020526ce No need for byte buffer 2021-02-10 23:32:47 +01:00
Nickolay Shmyrev b0da19b07f Java API through JNR-FFI 2021-02-10 23:16:20 +01:00
Nickolay Shmyrev 70bcd9b018 Platform is hardcoded now 2021-02-09 23:43:56 +01:00
Nickolay Shmyrev d2604b609d Copy out lib files too 2021-02-09 23:36:32 +01:00
Nickolay Shmyrev e960e0cacf Proper libraries and demo 2021-02-09 21:43:21 +01:00
Nickolay Shmyrev 77b114d434 Rework node bindings to use node-ffi-napi 2021-02-08 23:00:56 +01:00
Nickolay V. Shmyrev 2860092cef Merge pull request #401 from proger/patch-1
test_microphone: AcceptWaveform wants bytes
2021-02-08 01:50:07 +03:00
Vlad Ki c6119c4835 test_microphone: AcceptWavform wants bytes 2021-02-08 00:38:28 +02:00
Nickolay Shmyrev 415d1927e0 Add win32 wheel 2021-02-07 23:08:27 +01:00
Nickolay Shmyrev 3d1e21d242 Update dockcross build to cffi 2021-02-07 21:33:39 +01:00
Nickolay Shmyrev 7a75e1c3f4 Add arm architectures 2021-02-07 20:28:14 +01:00
Nickolay Shmyrev c3430e448a Add build for Windows wheel 2021-02-07 18:11:38 +01:00
Nickolay Shmyrev ae49ea60d2 Rework manylinux to use cffi 2021-02-07 15:26:23 +01:00
Nickolay Shmyrev 02dc0ce0c8 Take source location from environment 2021-02-07 15:17:35 +01:00
Nickolay Shmyrev 15697a18e8 Platform-specific wheel 2021-02-07 14:58:10 +01:00
Nickolay Shmyrev 481881e59d Fully move python to cffi 2021-02-07 13:23:03 +01:00
Nickolay Shmyrev 11a25b26a7 Add cffi wrapper 2021-02-07 10:57:57 +01:00
Nickolay Shmyrev bf87358f6c Add win32 build 2021-02-07 10:12:32 +01:00
Nickolay Shmyrev 04a8242230 Add vosk-api build 2021-02-07 01:55:47 +01:00
Nickolay Shmyrev ceb96c301c Separate openfst root 2021-02-07 01:48:45 +01:00
Nickolay Shmyrev 307df8fdc0 Add Win cross-compilation docker 2021-02-07 01:41:37 +01:00
Nickolay Shmyrev c44875a2dd Build libvosk in src folder, will be used for many bindings 2021-02-07 00:42:25 +01:00
Nickolay Shmyrev 31f11990ca Rename voskrecognizer 2021-02-07 00:32:47 +01:00
Nickolay Shmyrev b79b85856d Refactor C# API to properly work with nuget and precompiled libvosk.so library 2021-02-06 23:10:51 +01:00
Nickolay Shmyrev 6e861c6a19 Add nuget platforms 2021-02-05 00:39:27 +01:00
Nickolay Shmyrev cf62296a51 Update flags to hopefully fix the build 2021-02-02 14:43:12 +01:00
Nickolay Shmyrev 387b132814 Fix model version in Makefile. Thanks to Peter Kronenberg. 2021-01-26 23:19:34 +01:00
Nickolay Shmyrev de83de8624 Implement decoding on iOS with a microphone 2021-01-24 21:59:21 +01:00
Nickolay Shmyrev fe91b5a717 Preprocess header on the fly 2021-01-10 21:11:35 +01:00
Nickolay Shmyrev a052506a5d Add cffi example 2021-01-10 20:53:43 +01:00
Nickolay Shmyrev 6f2d6d0d69 Proper microphone recognizer with the queue 2021-01-09 23:16:35 +01:00
Nickolay Shmyrev a8ae6025bd Add a bit more optimization 2021-01-09 16:16:32 +01:00
Nickolay Shmyrev 1b8332a609 Raise to 0.3.17 2021-01-09 02:29:56 +01:00
Nickolay Shmyrev fe16ec7e57 Set lowest arch for linux 2021-01-09 01:47:23 +01:00
Nickolay Shmyrev c5ce5e46bd Add c++17 2021-01-09 01:46:13 +01:00
Nickolay Shmyrev 99d66670bc Update Android build 2021-01-08 13:30:28 +01:00
Nickolay Shmyrev 072b42cac6 Use our own dockcross 2021-01-08 00:56:18 +01:00
Nickolay Shmyrev acadb5b4c2 Add optimization 2021-01-07 23:41:13 +01:00
Nickolay Shmyrev 2e1de6c3af No need for python 3.5 anymore 2021-01-07 23:20:54 +01:00
Nickolay Shmyrev 67de30908b Use openblas_clapack 2021-01-07 22:37:56 +01:00
Nickolay Shmyrev 9d398eff0e Remove armv6, too slow anyway. Build with new dockcross. 2021-01-06 16:26:23 +01:00
Nickolay Shmyrev 02b7312a00 Build with openblas-clapack to avoid fortran 2021-01-06 01:11:03 +01:00
Nickolay Shmyrev 08c35e84f3 Update demo with spk vector check 2020-12-23 22:21:12 +01:00
Nickolay Shmyrev b92a5c1fc7 Update wheels to openfst 1.8.0 2020-12-22 23:27:01 +01:00
Nickolay Shmyrev 8997a587c5 Fix warnings in preparation for openfst 1.8.0 2020-12-22 21:16:20 +01:00
Nickolay Shmyrev dc3d03d742 Make sure we have result field in json 2020-11-29 19:19:50 +01:00
Nickolay Shmyrev 84df40715c Add Filipino 2020-11-19 14:53:46 +01:00
Nickolay Shmyrev b639fb501a Package US model on bintray 2020-11-04 22:08:03 +01:00
Nickolay Shmyrev 746ff47757 Split long lines in subtitles 2020-11-03 15:13:16 +01:00
Nickolay Shmyrev 2d62db8118 Update link in readme 2020-10-31 10:03:25 +01:00
Nickolay Shmyrev 7a2adcd9ba Add nuget wraps 2020-10-26 15:57:40 +01:00
Nickolay Shmyrev 9ccf3ef0e8 Introduce CMVN in feature pipeline 2020-10-22 23:56:36 +02:00
Nickolay V. Shmyrev 0edab6d558 Merge pull request #259 from ThThoma/add-valid-word-check
add isWordRecognizable()
2020-10-22 15:53:04 +03:00
Athanasios Thoma 4b344f0cd8 rename 2020-10-22 14:09:50 +03:00
Athanasios Thoma 17ddc4d5ba add isWordRecognizable() 2020-10-21 10:00:39 +03:00
Nickolay Shmyrev 3e33860c47 Update to latest 0.3.15 changes 2020-10-20 23:50:39 +03:00
Nickolay Shmyrev 0269a10833 Critical ivector bugfix and version 0.3.15 2020-10-13 01:28:46 +02:00
Nickolay Shmyrev 7af3e9a334 Add srt example 2020-10-07 13:43:09 +02:00
Nickolay Shmyrev d666876be1 Revert compile flags 2020-10-05 01:24:32 +02:00
Nickolay Shmyrev 564fab7ec1 Update documentation 2020-10-05 00:16:15 +02:00
Nickolay Shmyrev 155c6c2a2a Don't crash with grammar and static graphs 2020-10-05 00:13:25 +02:00
Nickolay Shmyrev d43cbe9344 Add 0.3.14 2020-10-04 23:45:34 +02:00
Nickolay Shmyrev 57cc474c9f Build bigram language model from grammars 2020-10-04 23:42:50 +02:00
Nickolay Shmyrev 586603f8e1 Add Farsi 2020-10-03 12:42:54 +02:00
Nickolay Shmyrev d57887d22a Add Greek 2020-09-30 13:42:20 +02:00
Nickolay Shmyrev 6183bcfc5f Add Arabic model 2020-09-30 13:29:43 +02:00
Nickolay Shmyrev 65f6113b4d Add Catalan 2020-09-27 11:57:20 +02:00
Nickolay Shmyrev 8d88b89db1 Fix reserved identifier violation. Closes issue #226
Thanks to Markus Elfring
2020-09-23 21:16:34 +02:00
Nickolay Shmyrev 62885e8963 Update descriptions 2020-09-22 11:30:27 +02:00
qo6xup6 9fc094a5da Update kaldi_recognizer.h 2020-09-22 10:11:29 +08:00
Nickolay Shmyrev f97383c17f Update models location 2020-09-22 00:00:24 +02:00
Nickolay Shmyrev 4b892ec5e7 Bump version 2020-09-21 23:49:55 +02:00
Nickolay Shmyrev 41035485db Fix x-vectors, now they actually work. Requires new version spk-model-0.4 with whitening transform matrix 2020-09-21 23:31:30 +02:00
Nickolay Shmyrev 9696f4c917 Mention Dutch 2020-09-18 17:31:17 +02:00
Nickolay Shmyrev 55abf5f5ac Dynamic arch in pip module 2020-09-13 22:08:36 +02:00
Nickolay Shmyrev a1b2e41710 Mention we support Indian English 2020-09-09 23:28:55 +02:00
Nickolay Shmyrev dff4ab26e4 Library is now vosk_jni 2020-09-01 18:27:54 +02:00
Nickolay Shmyrev 83b6e1cdf7 Speech service for more flexible recognizer initialization
Publish repo on jcenter
2020-09-01 16:32:15 +02:00
Nickolay Shmyrev 38dbaa15ea Load JNI inside library itself 2020-09-01 12:44:44 +02:00
Nickolay Shmyrev 0e531b6061 Organize Makefile 2020-08-23 20:10:48 +02:00
Nickolay Shmyrev de94ef5537 Add speaker C demo 2020-08-14 11:33:41 +02:00
Nickolay Shmyrev 6ef9d13877 Add Italian 2020-08-03 01:31:30 +02:00
Nickolay Shmyrev 1c7b94757d Don't attempt to resize to empty matrix 2020-07-31 16:45:05 +02:00
Nickolay Shmyrev 83486e0bef Spk vector fix 2020-07-31 12:58:23 +02:00
Nickolay Shmyrev 9787e8a53f No .html in links 2020-07-21 10:15:27 +02:00
Nickolay Shmyrev f59d6685ad Fix confidences 2020-07-15 10:24:08 +02:00
Nickolay Shmyrev 8a986ef384 Require KALDI_MKL to be 1 2020-07-12 11:03:22 +02:00
Nickolay Shmyrev 78f9f55e14 Fixes description 2020-07-12 10:37:56 +02:00
Nickolay Shmyrev c2e006f664 Some probably helpful flags for npm on osx 2020-07-11 20:54:13 +02:00
Nickolay V. Shmyrev 4b8e9737a5 Update README.md 2020-07-08 01:38:03 +02:00
Nickolay Shmyrev 722b09eaa4 Ignore words missing in the vocabulary 2020-07-08 01:35:50 +02:00
Nickolay Shmyrev f5b4f5a1f2 Added C test 2020-07-08 01:35:50 +02:00
Nickolay Shmyrev 4407d8da55 Another timestamp bugfix 2020-07-08 01:35:50 +02:00
Nickolay Shmyrev 73b73527cd Release memory when final result is received to reduce memory pressure. 2020-07-08 01:35:50 +02:00
Nickolay Shmyrev 4df0e3a741 Actually show the distance 2020-07-08 01:35:49 +02:00
He1nr1chK 1a771e0172 Added cosine distance function 2020-06-23 20:10:14 +02:00
Nickolay Shmyrev 4333c3c242 Build dockcross with python 3.8 2020-06-23 01:11:07 +02:00
Nickolay V. Shmyrev b831c9ad57 Merge pull request #151 from hviana/patch-1
Add support for speaker model in Android
2020-06-23 00:07:26 +03:00
Nickolay Shmyrev e5c08f7710 Allow to continue after final result. See for discussion
https://github.com/alphacep/vosk-api/issues/146
2020-06-22 23:04:09 +02:00
Henrique Emanoel Viana 51c2968595 fix 2020-06-22 18:01:42 -03:00
Henrique Emanoel Viana 5993376322 Add suport to SpkModel 2020-06-22 17:51:55 -03:00
Nickolay Shmyrev c4281622b9 Added node speaker demo 2020-06-22 21:45:50 +02:00
Nickolay Shmyrev 9d26014de2 Use wider beams by default to avoid accuracy confusion 2020-06-19 16:10:30 +02:00
Nickolay Shmyrev f6c115d215 Update to 0.3.9 and use our fork of openfst based on openfst 1.7.7 2020-06-16 19:13:57 +02:00
Nickolay Shmyrev 6bd102d778 Fixes timing issue #125 2020-06-15 02:04:02 +02:00
Nickolay Shmyrev d3d6af5712 Define os in a single place 2020-06-12 10:35:58 +02:00
Nickolay V. Shmyrev 876093446f Merge pull request #127 from nnkalita/master
added android build support on macOS
2020-06-12 11:32:27 +03:00
Nickolay Shmyrev 336f219f09 No Qr code for wechat anymore 2020-06-11 12:57:32 +02:00
Nickolay Shmyrev 584251cbdc Remove chinese-only 2020-06-11 09:18:34 +02:00
Nickolay Shmyrev a34995a788 Process audio in chunks for imporved accuracy 2020-06-11 01:06:11 +02:00
Nickolay Shmyrev 998e5da227 Update rules for joining wechat 2020-06-10 15:07:10 +02:00
Nickolay Shmyrev e04c15e367 Fix 2020-06-06 11:15:23 +02:00
Nickolay Shmyrev b81f69d407 Avoid overflow. See issue #128 2020-06-06 10:43:44 +02:00
Nickolay Shmyrev 0ac2064281 Few more words 2020-06-06 10:07:12 +02:00
Nickolay Shmyrev 1d00bd244e Multithreaded testing 2020-06-06 00:40:15 +02:00
Nickolay Shmyrev 8f5efc58c9 Disable ivector pipeline without ivectors 2020-06-05 19:34:04 +02:00
Nayan Kalita a0c5ae1b5e Update build-kaldi.sh
added android build from mac.
2020-06-05 20:10:28 +05:30
Nayan Kalita 99f48f9de1 Merge remote-tracking branch 'upstream/master' 2020-06-05 19:15:42 +05:30
Nayan Kalita 1948b23f32 Update build-kaldi.sh
Updated based feedback.
2020-06-05 18:03:01 +05:30
Nickolay Shmyrev c9eb572fc5 Move documentation to our website 2020-06-05 01:31:13 +02:00
Nickolay Shmyrev 7d9895ff81 Add API documentation in the header 2020-06-04 22:47:54 +02:00
Nickolay Shmyrev d507210ef8 Add npmignore and pure javascript package 2020-06-03 17:47:15 +02:00
Nickolay Shmyrev db0a3d23d5 Add repackages librispeech model 2020-06-03 17:24:28 +02:00
Nickolay Shmyrev f4f920f3cd Proper cross-compilation with setuptools 2020-06-02 00:36:16 +02:00
Nickolay Shmyrev 75993ea276 Update to 0.3.8 2020-06-01 00:55:28 +02:00
Nickolay Shmyrev ee9bacb092 Reset pipeline to save memory 2020-06-01 00:54:47 +02:00
Nickolay Shmyrev b1e775c67b Better speaker identification without silence frames 2020-05-31 23:46:42 +02:00
Nickolay Shmyrev d631e567aa Add README 2020-05-31 02:21:07 +02:00
Nickolay Shmyrev 9edf45be42 Sync version 2020-05-31 02:08:35 +02:00
Nickolay Shmyrev 8b4b3c646a Nodejs support 2020-05-31 02:07:01 +02:00
Nickolay Shmyrev 31bb0557d9 Add mkl support 2020-05-30 23:22:14 +02:00
Nickolay Shmyrev 4593183cf9 Create gen if doens't exist. Thanks to BMI24. 2020-05-25 00:32:42 +02:00
Nickolay Shmyrev fdc45f1187 Better link on wechat 2020-05-23 21:13:23 +02:00
Nickolay Shmyrev b3d3c6d12c Fix mistake, thanks to Shaheen 2020-05-23 01:12:31 +02:00
Nickolay Shmyrev f5f0794def Add logo 2020-05-22 15:24:57 +02:00
Nickolay Shmyrev 55664fcca0 Add wechat group 2020-05-22 09:56:54 +02:00
Nickolay Shmyrev afbf330f16 Make ivector extractor optional 2020-05-18 23:56:42 +02:00
Nickolay Shmyrev 25aadf61bc Fix travis 2020-05-09 17:36:55 +02:00
Nickolay Shmyrev be47056467 Updates for osx 2020-05-09 15:49:24 +03:00
Nickolay Shmyrev 8f623e0aea Get rid of broken cmake 2020-05-09 14:32:46 +02:00
Nickolay Shmyrev 78025435e4 Windows is fully supported now 2020-05-09 09:18:10 +02:00
Nickolay Shmyrev 910455802e Don't link to the library explicitely to allow relocations 2020-05-08 15:16:50 +03:00
Nickolay Shmyrev b12914955c Add x86 build 2020-05-08 11:33:25 +02:00
Nickolay Shmyrev dfe11eaf83 Linux build fix 2020-05-08 09:04:14 +02:00
Nickolay Shmyrev 37fbe1a52b OSX build 2020-05-08 09:55:15 +03:00
Nickolay Shmyrev af11bb2361 No need for external dependencies 2020-05-07 21:48:03 +02:00
Nickolay Shmyrev 8da8697c1e Add ffmpeg test 2020-05-07 21:07:17 +02:00
Nickolay V. Shmyrev 944dc87531 Update readme to mention aarch64 2020-05-05 00:55:29 +02:00
Nickolay Shmyrev 5c4dd4644e Update version 2020-05-03 20:52:43 +02:00
Nickolay Shmyrev 9bbd172cfd Some more links 2020-05-02 01:37:10 +02:00
Nickolay Shmyrev dbf9de77c3 Document model training and model structure 2020-05-02 01:11:25 +02:00
Nickolay Shmyrev 8b790cd162 Better android logging 2020-05-01 20:02:46 +02:00
Nickolay Shmyrev 80219066e9 Expose verbose level in the API 2020-05-01 19:02:57 +02:00
Nickolay Shmyrev 26fa5f098f Updated link to English model 2020-04-30 15:19:13 +02:00
Nickolay Shmyrev d75bb36131 Fix model download path 2020-04-30 11:04:35 +02:00
Nickolay Shmyrev 30c5e8ca79 Link to lm library for rescoring 2020-04-30 10:52:15 +02:00
Nickolay Shmyrev c00e36fab6 Rearrange models 2020-04-30 10:47:10 +02:00
Nickolay Shmyrev 80e60b9118 LM Rescoring 2020-04-28 22:17:34 +02:00
Nickolay Shmyrev e3a95a44bc Added Turkish model 2020-04-28 10:10:35 +02:00
Nickolay Shmyrev 86caf526f5 New model layout and model config file with beams for decoding 2020-04-28 01:06:41 +02:00
Nickolay Shmyrev e09f32b4b4 Properly stop listening so it can continue later 2020-04-27 09:00:32 +02:00
Nickolay Shmyrev 3803ab345d Proper reference couting of the models to avoid memory issus 2020-04-23 23:37:52 +02:00
Nickolay V. Shmyrev 889b43136f Update README.zh.md 2020-04-23 12:43:00 +02:00
John Baber-Lucero b517cf46af Fix some grammar/punctuation 2020-04-23 00:28:34 -04:00
Nickolay Shmyrev ffd810fe00 Fix threading bug 2020-04-22 22:15:45 +02:00
Nickolay V. Shmyrev a1a0ed70a1 Update README.zh.md 2020-04-22 18:29:01 +03:00
Nickolay Shmyrev 1554d9ede7 Added per-language readme 2020-04-22 13:21:36 +02:00
Nickolay Shmyrev 6f3190d83d Version 0.3.4 and arm wheels for python 3.6 2020-04-21 11:38:56 +02:00
Nickolay Shmyrev 3caaa32ec0 How to ask for the accuracy updates 2020-04-20 23:11:52 +02:00
Nickolay Shmyrev 3facf3ccf5 UI updates 2020-04-20 11:08:18 +03:00
Nickolay Shmyrev fad954e6e3 Full iOS project 2020-04-19 22:48:47 +03:00
Nickolay Shmyrev c4809cb618 Added Vietnamese model 2020-04-19 20:14:05 +02:00
Nickolay Shmyrev a241423baf Added iOS bits 2020-04-18 17:34:03 +03:00
Nickolay Shmyrev 1e9421dd38 Add a note about iOS 2020-04-18 15:41:51 +02:00
Nickolay Shmyrev b09ffda760 Fixes links and issue #59 2020-04-11 22:15:46 +02:00
Nickolay Shmyrev aeff663a7c Fix pocketsphinx references in Android sources 2020-04-09 21:50:38 +02:00
Nickolay Shmyrev fcd17fcd4c Added Russian model 0.9 2020-04-07 17:48:29 +02:00
Nickolay Shmyrev feffb2711d Repair travis build 2020-04-04 20:49:06 +02:00
Nickolay Shmyrev f76e5b592f kaldi-ru-0.8 2020-04-04 10:27:02 +02:00
Nickolay Shmyrev 71bdc900e3 C-only wrapper 2020-04-04 10:22:01 +02:00
Nickolay Shmyrev 96bbf5abc2 Add C API 2020-04-04 10:04:49 +02:00
Nickolay Shmyrev df9424a228 Introduce pure C api to deal with Windows runtime issues and make it easy to wrap in Swift 2020-04-04 00:35:56 +02:00
Nickolay Shmyrev 3310acaf54 Add lm to kaldi-android 2020-03-28 22:45:38 +01:00
Nickolay V. Shmyrev e8722d462d Merge pull request #54 from andremendesc/master
Fixes folder name instruction for python model on microphone test
2020-03-25 08:37:25 +03:00
André Mendes 14b2c13ed6 Fixes folder name instruction for python model on microphone test 2020-03-25 01:01:23 -03:00
Nickolay Shmyrev 19af324096 Small fix by Funny Jingl 2020-03-19 23:24:41 +01:00
Nickolay Shmyrev 0ca7b94e08 Add link on kaldi-ru-0.7 2020-03-19 20:14:41 +01:00
Nickolay Shmyrev 04ed310229 Move models list to doc 2020-03-15 22:39:57 +01:00
Nickolay Shmyrev 7ac33d521c Added information about models 2020-03-15 22:27:47 +01:00
Nickolay V. Shmyrev 08ada63da4 Added note about other projects 2020-03-15 02:48:49 +03:00
Nickolay Shmyrev cef3fd72fb Disable NEON fpu on ARMv6
Fixes issue #46
2020-03-10 00:24:57 +01:00
Nayan Kalita bca0b86e37 android build support on macOS 2020-03-06 20:57:56 +05:30
Nickolay V. Shmyrev bf973ff434 Merge pull request #43 from camillem/patch-1
Typo in folder name
2020-03-02 15:47:25 +03:00
camillem a172d60b20 Typo in folder name 2020-03-02 13:46:34 +01:00
Nickolay Shmyrev 060e4395c2 Fix repeating results issue #42
Thanks to Yondu Tsai
2020-03-02 13:41:27 +01:00
Nickolay Shmyrev 03f1417454 Added small readme 2020-03-01 09:12:09 +01:00
Nickolay Shmyrev 6b787a4d1e We actually need a bit newer cmake 2020-02-27 00:43:03 +01:00
Nickolay Shmyrev 095fac1de4 Small unittest 2020-02-25 21:45:27 +01:00
Nickolay V. Shmyrev 5ba9e353c7 Added contacts 2020-02-25 02:44:28 +03:00
Nickolay V. Shmyrev be117bfbb8 Update README.md 2020-02-25 02:39:14 +03:00
Nickolay V. Shmyrev c4f9bfe735 Update README.md 2020-02-24 17:50:39 +03:00
Nickolay Shmyrev cc96ef700f Fix mistake 2020-02-24 01:25:29 +01:00
Nickolay Shmyrev cb41d8c94b Added C# test 2020-02-24 00:25:15 +01:00
Nickolay Shmyrev 8913b93703 Fix message 2020-02-23 16:05:34 +00:00
Nickolay Shmyrev 174bf7b0ff Minor fixes 2020-02-23 15:06:06 +01:00
Nickolay Shmyrev 7e44844cd5 Added dockcross images for arm builds 2020-02-23 10:19:31 +01:00
Nickolay Shmyrev c0aa87af1b Build speaker model too 2020-02-21 09:53:00 +01:00
Nickolay Shmyrev f66f7a9290 Added microphone test 2020-02-21 00:08:50 +01:00
Nickolay Shmyrev 378fa3499a Downsample by default and also check wav file encoding in examples. Thanks to
dtreskunov. See for details

https://github.com/alphacep/vosk-api/pull/23
2020-02-18 11:45:50 +01:00
Nickolay Shmyrev 01eeec498d Fix memory access issues 2020-02-17 09:42:50 +00:00
Nickolay Shmyrev d422dfbddb Message update 2020-02-16 17:21:38 +01:00
Nickolay Shmyrev bd4a48d5a8 Added spkid to java 2020-02-16 17:12:54 +01:00
Nickolay Shmyrev aa91ccf68b Added speaker identification 2020-02-16 17:04:37 +01:00
Nickolay Shmyrev bda43565e1 Added the doc about langauge model recompilation with opengrm 2020-02-10 20:39:59 +01:00
Nickolay Shmyrev 8c9bae3ad1 Update version for Kaldi rebuild 2020-02-08 23:40:42 +01:00
Nickolay V. Shmyrev 87fc3bc073 Make structure more clear 2020-02-02 23:10:35 +03:00
Nickolay Shmyrev 9c101ffa86 Strip binary on release
Fixes issue #12.
2020-01-29 22:22:26 +01:00
Nickolay V. Shmyrev 5649d9e3c7 Merge pull request #22 from dtreskunov/issue-19
translate exceptions from C++ to Python
2020-01-29 23:12:41 +03:00
Denis Treskunov 5ee96b6d04 translate exceptions from C++ to Python
Wrap each API call in a try/catch.
See http://www.swig.org/Doc4.0/Python.html#Python_nn44
2020-01-28 14:12:53 -08:00
Nickolay V. Shmyrev 6a7854919e Clarify Pip install is sufficient. 2020-01-27 12:33:27 +03:00
Nickolay Shmyrev a6f6c0409b Demonstrate grammar compilation on the fly 2020-01-22 23:31:21 +01:00
Nickolay Shmyrev 41446c980c Use json library for convenience 2020-01-22 22:37:03 +01:00
Nickolay Shmyrev 7bed44a96e Automatically download model 2020-01-19 22:07:47 +01:00
Nickolay Shmyrev 3f7bd2f586 Added java example 2020-01-19 21:57:08 +01:00
Nickolay Shmyrev 2ba8466e0a Avoid hardcoded folders, fixes issue #14. Thanks to Bill Lambert. 2020-01-19 20:30:18 +01:00
Nickolay Shmyrev f80dcce5ed Clarify licences 2020-01-19 19:33:55 +01:00
Nickolay Shmyrev f4aed3e9a4 Added note about runnign the code 2020-01-18 00:28:35 +01:00
Nickolay Shmyrev 5c4ef66a08 Update README 2020-01-18 00:20:19 +01:00
Nickolay Shmyrev 29d2564efd Let it be 3.4 2020-01-12 13:16:59 +01:00
Nickolay Shmyrev 380fa9c7c8 Added a warning to download the model 2020-01-11 20:24:34 +01:00
Nickolay Shmyrev e55c6e09f8 Simplify swig task 2020-01-11 08:14:32 +01:00
Nickolay Shmyrev 355b58447a No need to link with python library 2020-01-10 19:57:41 +00:00
Nickolay Shmyrev fe35675eaa Added basic travis 2020-01-10 20:01:48 +01:00
Nickolay Shmyrev ae9db8652a Update gradle plugin to properly report about missing local properties.
Fix issue #8
2020-01-03 20:24:47 +01:00
Nickolay Shmyrev ac3e52596c Remove premature exit 2020-01-03 20:20:51 +01:00
Nickolay V. Shmyrev 17bffb6739 Update README.md 2020-01-03 09:58:08 +03:00
Nickolay V. Shmyrev 8839771794 Update README.md 2020-01-03 02:06:28 +03:00
Nickolay V. Shmyrev 95ee23521b Update README.md 2020-01-03 01:35:57 +03:00
Nickolay Shmyrev 38672ad1ca Update to current state 2020-01-02 21:53:07 +01:00
Nickolay Shmyrev 79cee1cab6 Update to use shared sources 2020-01-02 21:51:25 +01:00
Nickolay Shmyrev 3aece57b6e Kaldi doesn't use logarc 2020-01-02 20:48:08 +01:00
Nickolay Shmyrev 78e66149f8 Imported Python bindings and Node bindings 2020-01-02 20:46:14 +01:00
Nickolay Shmyrev 6805077934 Moved android things to a folder in preparation for merge with python bindings 2020-01-02 20:41:39 +01:00
Nickolay Shmyrev 688178ba5f -O3 implies tree vectorization 2020-01-02 19:59:17 +01:00
Nickolay Shmyrev cc13917903 Reuse symbol table from the grammar instead of separate file. 2019-12-25 23:54:53 +01:00
Nickolay Shmyrev 65ebfb0dac Update for disambig in lookahead 2019-11-25 23:29:05 +01:00
Nickolay Shmyrev 3954d89329 Use Atom architecture for emulator instead of SandyBridge 2019-11-13 20:45:05 +01:00
Nickolay Shmyrev 41ebb41bd8 Add x86 plugin 2019-11-12 00:05:15 +01:00
Nickolay Shmyrev 545800454b Update for arm64 2019-11-03 00:20:03 +01:00
Nickolay V. Shmyrev edad7eba9e Added link to XNNPack 2019-10-09 14:59:40 +03:00
Nickolay Shmyrev bb8188afe6 More compact ngram fsts 2019-09-23 01:38:43 +02:00
Nickolay Shmyrev 305961e8dc Update for lookahead model 2019-09-20 22:39:39 +02:00
Nickolay Shmyrev 06972ec3ad Simplify AcceptWaveform API 2019-09-11 18:55:24 +02:00
Nickolay Shmyrev 345dfde6fb Optimization for speed test 2019-09-10 21:50:18 +02:00
Nickolay Shmyrev 1326d3820f Unified build script without docker 2019-09-09 18:19:30 +02:00
Nickolay Shmyrev 9437f88ce2 Add readme 2019-09-05 13:00:23 +02:00
Nickolay Shmyrev 67b5814108 Allow model path specification 2019-09-05 12:59:23 +02:00
Nickolay Shmyrev 5a49c26d9d Initial import 2019-09-03 19:45:19 +02:00
214 changed files with 839 additions and 25017 deletions
-1
View File
@@ -10,7 +10,6 @@ gradlew
gradlew.bat
gradle
local.properties
gradle.properties
# Android
android/build
-21
View File
@@ -1,21 +0,0 @@
cmake_minimum_required(VERSION 3.13)
project(vosk-api CXX)
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_EXTENSIONS OFF)
add_library(vosk
src/language_model.cc
src/model.cc
src/recognizer.cc
src/spk_model.cc
src/vosk_api.cc
src/postprocessor.cc
)
find_package(kaldi REQUIRED)
target_link_libraries(vosk PUBLIC kaldi-base kaldi-online2 kaldi-rnnlm fstngram)
include(GNUInstallDirs)
install(TARGETS vosk DESTINATION ${CMAKE_INSTALL_LIBDIR})
install(FILES src/vosk_api.h DESTINATION ${CMAKE_INSTALL_INCLUDEDIR})
-27
View File
@@ -1,27 +0,0 @@
# Vosk Speech Recognition Toolkit
Vosk is an offline open source speech recognition toolkit. It enables
speech recognition for 20+ languages and dialects - English, Indian
English, German, French, Spanish, Portuguese, Chinese, Russian, Turkish,
Vietnamese, Italian, Dutch, Catalan, Arabic, Greek, Farsi, Filipino,
Ukrainian, Kazakh, Swedish, Japanese, Esperanto, Hindi, Czech, Polish.
More to come.
Vosk models are small (50 Mb) but provide continuous large vocabulary
transcription, zero-latency response with streaming API, reconfigurable
vocabulary and speaker identification.
Speech recognition bindings implemented for various programming languages
like Python, Java, Node.JS, C#, C++, Rust, Go and others.
Vosk supplies speech recognition for chatbots, smart home appliances,
virtual assistants. It can also create subtitles for movies,
transcription for lectures and interviews.
Vosk scales from small devices like Raspberry Pi or Android smartphone to
big clusters.
# Documentation
For installation instructions, examples and documentation visit [Vosk
Website](https://alphacephei.com/vosk).
+19 -14
View File
@@ -1,21 +1,26 @@
<!-- WEHUB_ZH_README -->
> [!NOTE]
> 本文档由 WeHub 基于上游 README 翻译整理,属于社区翻译,非官方中文文档。
> [English](./README.en.md) · [原始项目](https://github.com/alphacep/vosk-api) · [上游 README](https://github.com/alphacep/vosk-api/blob/HEAD/README.md)
> 原作者、版权与许可证归属以原始项目及本仓库 LICENSE 文件为准。
# About
# Vosk Speech Recognition Toolkit
Vosk is an offline open source speech recognition toolkit. It enables
speech recognition models for 18 languages and dialects - English, Indian
English, German, French, Spanish, Portuguese, Chinese, Russian, Turkish,
Vietnamese, Italian, Dutch, Catalan, Arabic, Greek, Farsi, Filipino,
Ukrainian.
Vosk 是一款离线开源语音识别工具包。它支持 20 多种语言及方言的语音识别——英语、印度英语、德语、法语、西班牙语、葡萄牙语、中文、俄语、土耳其语、越南语、意大利语、荷兰语、加泰罗尼亚语、阿拉伯语、希腊语、波斯语、菲律宾语、乌克兰语、哈萨克语、瑞典语、日语、世界语、印地语、捷克语、波兰语。更多语言即将推出。
Vosk models are small (50 Mb) but provide continuous large vocabulary
transcription, zero-latency response with streaming API, reconfigurable
vocabulary and speaker identification.
Vosk 模型体积小(50 Mb),但可提供连续大词汇量转写、通过流式 API(streaming API)实现零延迟响应、可重新配置的词汇表以及说话人识别。
Speech recognition bindings implemented for various programming languages
like Python, Java, Node.JS, C#, C++ and others.
已为 Python、Java、Node.JS、C#、C++、Rust、Go 等多种编程语言实现语音识别绑定。
Vosk supplies speech recognition for chatbots, smart home appliances,
virtual assistants. It can also create subtitles for movies,
transcription for lectures and interviews.
Vosk 为聊天机器人、智能家居设备、虚拟助手提供语音识别,还可为电影生成字幕,为讲座和访谈提供转写。
Vosk scales from small devices like Raspberry Pi or Android smartphone to
big clusters.
Vosk 的部署规模可从小型设备(如 Raspberry Pi 或 Android 智能手机)扩展到大型集群。
# Documentation
# 文档
有关安装说明、示例和文档,请访问 [Vosk 网站](https://alphacephei.com/vosk).
For installation instructions, examples and documentation visit [Vosk
Website](https://alphacephei.com/vosk).
-7
View File
@@ -1,7 +0,0 @@
# WeHub 来源说明
- 原始项目:`alphacep/vosk-api`
- 原始仓库:https://github.com/alphacep/vosk-api
- 导入方式:上游默认分支的最新快照
- 原作者、版权和许可证信息以原始仓库及本仓库 LICENSE 为准
- 本文件仅用于记录来源,不代表 WeHub 是原项目作者
+33 -28
View File
@@ -1,52 +1,57 @@
buildscript {
repositories {
google()
mavenCentral()
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:8.13.0'
classpath 'com.vanniktech:gradle-maven-publish-plugin:0.34.0'
classpath 'com.android.tools.build:gradle:4.1.3'
}
}
allprojects {
version = '0.3.75'
version = '0.3.30'
}
subprojects {
apply plugin: 'com.android.library'
apply plugin: 'com.vanniktech.maven.publish'
apply plugin: 'maven-publish'
repositories {
google()
mavenCentral()
jcenter()
}
mavenPublishing {
publishToMavenCentral()
signAllPublications()
}
mavenPublishing {
pom {
url = 'http://www.alphacephei.com.com/vosk/'
licenses {
license {
name = 'The Apache License, Version 2.0'
url = 'http://www.apache.org/licenses/LICENSE-2.0.txt'
publishing {
publications {
aar(MavenPublication) {
groupId 'com.alphacephei'
version version
pom {
url = 'http://www.alphacephei.com.com/vosk/'
licenses {
license {
name = 'The Apache License, Version 2.0'
url = 'http://www.apache.org/licenses/LICENSE-2.0.txt'
}
}
developers {
developer {
id = 'com.alphacephei'
name = 'Alpha Cephei Inc'
email = 'contact@alphacephei.com'
}
}
scm {
connection = 'scm:git:git://github.com/alphacep/vosk-api.git'
url = 'https://github.com/alphacep/vosk-api/'
}
}
}
developers {
developer {
id = 'com.alphacephei'
name = 'Alpha Cephei Inc'
email = 'contact@alphacephei.com'
}
}
scm {
connection = 'scm:git:git://github.com/alphacep/vosk-api.git'
url = 'https://github.com/alphacep/vosk-api/'
}
repositories {
maven {
url = "$rootDir/repo"
}
}
}
+19 -30
View File
@@ -29,7 +29,7 @@ set -x
OS_NAME=`echo $(uname -s) | tr '[:upper:]' '[:lower:]'`
ANDROID_TOOLCHAIN_PATH=$ANDROID_NDK_HOME/toolchains/llvm/prebuilt/${OS_NAME}-x86_64
WORKDIR_BASE=`pwd`/build
PATH=$ANDROID_TOOLCHAIN_PATH/bin:$PATH
PATH=$PATH:$ANDROID_NDK_HOME/toolchains/llvm/prebuilt/${OS_NAME}-x86_64/bin
OPENFST_VERSION=1.8.0
for arch in armeabi-v7a arm64-v8a x86_64 x86; do
@@ -40,43 +40,34 @@ case $arch in
armeabi-v7a)
BLAS_ARCH=ARMV7
HOST=arm-linux-androideabi
AR=llvm-ar
RANLIB=llvm-ranlib
AR=arm-linux-androideabi-ar
CC=armv7a-linux-androideabi21-clang
CXX=armv7a-linux-androideabi21-clang++
ARCHFLAGS="-mfloat-abi=softfp -mfpu=neon"
PAGESIZE_LDFLAGS=""
;;
arm64-v8a)
BLAS_ARCH=ARMV8
HOST=aarch64-linux-android
AR=llvm-ar
RANLIB=llvm-ranlib
AR=aarch64-linux-android-ar
CC=aarch64-linux-android21-clang
CXX=aarch64-linux-android21-clang++
ARCHFLAGS=""
# Ensure compatibility with 16KiB page size devices
PAGESIZE_LDFLAGS="-Wl,-z,common-page-size=4096 -Wl,-z,max-page-size=16384"
;;
x86_64)
BLAS_ARCH=ATOM
HOST=x86_64-linux-android
AR=llvm-ar
RANLIB=llvm-ranlib
AR=x86_64-linux-android-ar
CC=x86_64-linux-android21-clang
CXX=x86_64-linux-android21-clang++
ARCHFLAGS=""
PAGESIZE_LDFLAGS=""
;;
x86)
BLAS_ARCH=ATOM
HOST=i686-linux-android
AR=llvm-ar
RANLIB=llvm-ranlib
AR=i686-linux-android-ar
CC=i686-linux-android21-clang
CXX=i686-linux-android21-clang++
ARCHFLAGS=""
PAGESIZE_LDFLAGS=""
;;
esac
@@ -84,16 +75,16 @@ mkdir -p $WORKDIR/local/lib
# openblas first
cd $WORKDIR
git clone -b v0.3.20 --single-branch https://github.com/xianyi/OpenBLAS
make -C OpenBLAS TARGET=$BLAS_ARCH ONLY_CBLAS=1 AR=$AR CC=$CC HOSTCC=gcc ARM_SOFTFP_ABI=1 USE_THREAD=0 NUM_THREADS=1 -j 8
git clone -b v0.3.13 --single-branch https://github.com/xianyi/OpenBLAS
make -C OpenBLAS TARGET=$BLAS_ARCH ONLY_CBLAS=1 AR=$AR CC=$CC HOSTCC=gcc ARM_SOFTFP_ABI=1 USE_THREAD=0 NUM_THREADS=1 -j4
make -C OpenBLAS install PREFIX=$WORKDIR/local
# CLAPACK
cd $WORKDIR
git clone -b v3.2.1 --single-branch https://github.com/alphacep/clapack
mkdir -p clapack/BUILD && cd clapack/BUILD
cmake -DCMAKE_C_FLAGS="$ARCHFLAGS" -DCMAKE_C_COMPILER_TARGET=$HOST \
-DCMAKE_C_COMPILER=$CC -DCMAKE_SYSTEM_NAME=Generic -DCMAKE_AR=$ANDROID_TOOLCHAIN_PATH/bin/$AR \
cmake -DCMAKE_C_FLAGS=$ARCHFLAGS -DCMAKE_C_COMPILER_TARGET=$HOST \
-DCMAKE_C_COMPILER=$CC -DCMAKE_SYSTEM_NAME=Generic -DCMAKE_AR=$ANDROID_NDK_HOME/toolchains/llvm/prebuilt/${OS_NAME}-x86_64/bin/$AR \
-DCMAKE_TRY_COMPILE_TARGET_TYPE=STATIC_LIBRARY \
-DCMAKE_CROSSCOMPILING=True ..
make -j 8 -C F2CLIBS/libf2c
@@ -114,27 +105,25 @@ make install
# Kaldi itself
cd $WORKDIR
git clone -b vosk-android --single-branch https://github.com/alphacep/kaldi
git clone -b android-mix --single-branch https://github.com/alphacep/kaldi
cd $WORKDIR/kaldi/src
CXX=$CXX AR=$AR RANLIB=$RANLIB CXXFLAGS="$ARCHFLAGS -O3 -DFST_NO_DYNAMIC_LINKING" ./configure --use-cuda=no \
CXX=$CXX CXXFLAGS="$ARCHFLAGS -O3 -DFST_NO_DYNAMIC_LINKING" ./configure --use-cuda=no \
--mathlib=OPENBLAS_CLAPACK --shared \
--android-incdir=${ANDROID_TOOLCHAIN_PATH}/sysroot/usr/include \
--host=$HOST --openblas-root=${WORKDIR}/local \
--fst-root=${WORKDIR}/local --fst-version=${OPENFST_VERSION}
make -j 8 depend
cd $WORKDIR/kaldi/src
make -j 8 online2 rnnlm
make -j 8 online2 lm rnnlm
# Vosk-api
cd $WORKDIR
mkdir -p $WORKDIR/vosk
make -j 8 -C ${WORKDIR_BASE}/../../../src \
OUTDIR=$WORKDIR/vosk \
KALDI_ROOT=${WORKDIR}/kaldi \
OPENFST_ROOT=${WORKDIR}/local \
OPENBLAS_ROOT=${WORKDIR}/local \
CXX=$CXX \
EXTRA_LDFLAGS="-llog -static-libstdc++ -Wl,-soname,libvosk.so ${PAGESIZE_LDFLAGS}"
cp $WORKDIR/vosk/libvosk.so $WORKDIR/../../src/main/jniLibs/$arch/libvosk.so
#rm -rf vosk-api
git clone -b master --single-branch https://github.com/alphacep/vosk-api
cd vosk-api/src
make -j 8 KALDI_ROOT=${WORKDIR}/kaldi OPENFST_ROOT=${WORKDIR}/local OPENBLAS_ROOT=${WORKDIR}/local CXX=$CXX EXTRA_LDFLAGS="-llog -static-libstdc++"
# Copy JNI library to sources
cp $WORKDIR/vosk-api/src/libvosk.so $WORKDIR/../../src/main/jniLibs/$arch/libvosk.so
done
+15 -12
View File
@@ -3,15 +3,13 @@ def pomName = "Vosk Android"
def pomDescription = "Vosk speech recognition library for Android"
android {
namespace 'org.vosk'
compileSdkVersion 36
compileSdkVersion 29
defaultConfig {
minSdkVersion 21
targetSdkVersion 36
versionCode 10
targetSdkVersion 29
versionCode 6
versionName = version
archivesBaseName = archiveName
ndkVersion = "28.2.13676358"
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
@@ -21,19 +19,24 @@ android {
task buildVosk(type: Exec) {
commandLine './build-vosk.sh'
environment ANDROID_NDK_HOME: android.getNdkDirectory()
environment ANDROID_NDK_HOME: android.getSdkDirectory()
}
dependencies {
api 'net.java.dev.jna:jna:5.18.1@aar'
implementation 'net.java.dev.jna:jna:4.4.0@aar'
}
//preBuild.dependsOn buildVosk
mavenPublishing {
coordinates("com.alphacephei", archiveName, version)
pom {
name = pomName
description = pomDescription
publishing {
publications {
aar(MavenPublication) {
artifactId = archiveName
artifact("$buildDir/outputs/aar/$archiveName-release.aar")
pom {
name = pomName
description = pomDescription
}
}
}
}
+1
View File
@@ -0,0 +1 @@
include 'model-en'
+1 -1
View File
@@ -1,3 +1,3 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest>
<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="org.vosk">
</manifest>
@@ -36,8 +36,6 @@ public class LibVosk {
public static native void vosk_recognizer_set_words(Pointer recognizer, boolean words);
public static native void vosk_recognizer_set_partial_words(Pointer recognizer, boolean partial_words);
public static native void vosk_recognizer_set_spk_model(Pointer recognizer, Pointer spk_model);
public static native boolean vosk_recognizer_accept_waveform(Pointer recognizer, byte[] data, int len);
@@ -52,30 +50,10 @@ public class LibVosk {
public static native String vosk_recognizer_partial_result(Pointer recognizer);
public static native void vosk_recognizer_set_grm(Pointer recognizer, String grammar);
public static native void vosk_recognizer_reset(Pointer recognizer);
public static native void vosk_recognizer_set_endpointer_mode(Pointer recognizer, int mode);
public static native void vosk_recognizer_set_endpointer_delays(Pointer recognizer, float t_start_max, float t_end, float t_max);
public static native void vosk_recognizer_free(Pointer recognizer);
public static native Pointer vosk_text_processor_new(String verbalizer, String tagger);
public static native void vosk_text_processor_free(Pointer processor);
public static native String vosk_text_processor_itn(Pointer processor, String input);
/**
* Set log level for Kaldi messages.
*
* @param loglevel the level
* 0 - default value to print info and error messages but no debug
* less than 0 - don't print info messages
* greater than 0 - more verbose mode
*/
public static void setLogLevel(LogLevel loglevel) {
vosk_set_log_level(loglevel.getValue());
}
@@ -1,18 +1,13 @@
package org.vosk;
import java.io.IOException;
import com.sun.jna.PointerType;
public class Model extends PointerType implements AutoCloseable {
public Model() {
}
public Model(String path) throws IOException {
public Model(String path) {
super(LibVosk.vosk_model_new(path));
if (getPointer() == null) {
throw new IOException("Failed to create a model");
}
}
@Override
@@ -1,163 +1,32 @@
package org.vosk;
import com.sun.jna.PointerType;
import java.io.IOException;
public class Recognizer extends PointerType implements AutoCloseable {
/**
* Creates the recognizer object.
*
* The recognizers process the speech and return text using shared model data
* @param model VoskModel containing static data for recognizer. Model can be
* shared across recognizers, even running in different threads.
* @param sampleRate The sample rate of the audio you are going to feed into the recognizer.
* Make sure this rate matches the audio content, it is a common
* issue causing accuracy problems.
* @throws IOException if the recognizer could not be created
*/
public Recognizer(Model model, float sampleRate) throws IOException {
public Recognizer(Model model, float sampleRate) {
super(LibVosk.vosk_recognizer_new(model, sampleRate));
if (getPointer() == null) {
throw new IOException("Failed to create a recognizer");
}
}
/**
* Creates the recognizer object with speaker recognition.
*
* With the speaker recognition mode the recognizer not just recognize
* text but also return speaker vectors one can use for speaker identification
*
* @param model VoskModel containing static data for recognizer. Model can be
* shared across recognizers, even running in different threads.
* @param sampleRate The sample rate of the audio you are going to feed into the recognizer.
* Make sure this rate matches the audio content, it is a common
* issue causing accuracy problems.
* @param spkModel speaker model for speaker identification
* @throws IOException if the recognizer could not be created
*/
public Recognizer(Model model, float sampleRate, SpeakerModel spkModel) throws IOException {
public Recognizer(Model model, float sampleRate, SpeakerModel spkModel) {
super(LibVosk.vosk_recognizer_new_spk(model.getPointer(), sampleRate, spkModel.getPointer()));
if (getPointer() == null) {
throw new IOException("Failed to create a recognizer");
}
}
/**
* Creates the recognizer object with the phrase list.
*
* Sometimes when you want to improve recognition accuracy and when you don't need
* to recognize large vocabulary you can specify a list of phrases to recognize. This
* will improve recognizer speed and accuracy but might return [unk] if user said
* something different.
*
* Only recognizers with lookahead models support this type of quick configuration.
* Precompiled HCLG graph models are not supported.
*
* @param model VoskModel containing static data for recognizer. Model can be
* shared across recognizers, even running in different threads.
* @param sampleRate The sample rate of the audio you are going to feed into the recognizer.
* Make sure this rate matches the audio content, it is a common
* issue causing accuracy problems.
* @param grammar The string with the list of phrases to recognize as JSON array of strings,
* for example "["one two three four five", "[unk]"]".
* @throws IOException if the recognizer could not be created
*/
public Recognizer(Model model, float sampleRate, String grammar) throws IOException {
public Recognizer(Model model, float sampleRate, String grammar) {
super(LibVosk.vosk_recognizer_new_grm(model.getPointer(), sampleRate, grammar));
if (getPointer() == null) {
throw new IOException("Failed to create a recognizer");
}
}
/**
* Configures recognizer to output n-best results.
*
* <pre>
* {
* "alternatives": [
* { "text": "one two three four five", "confidence": 0.97 },
* { "text": "one two three for five", "confidence": 0.03 },
* ]
* }
* </pre>
*
* @param maxAlternatives - maximum alternatives to return from recognition results
*/
public void setMaxAlternatives(int maxAlternatives) {
LibVosk.vosk_recognizer_set_max_alternatives(this.getPointer(), maxAlternatives);
}
/** Enables words with times in the output
*
* <pre>
* "result" : [{
* "conf" : 1.000000,
* "end" : 1.110000,
* "start" : 0.870000,
* "word" : "what"
* }, {
* "conf" : 1.000000,
* "end" : 1.530000,
* "start" : 1.110000,
* "word" : "zero"
* }, {
* "conf" : 1.000000,
* "end" : 1.950000,
* "start" : 1.530000,
* "word" : "zero"
* }, {
* "conf" : 1.000000,
* "end" : 2.340000,
* "start" : 1.950000,
* "word" : "zero"
* }, {
* "conf" : 1.000000,
* "end" : 2.610000,
* "start" : 2.340000,
* "word" : "one"
* }],
* </pre>
*
* @param words - boolean value
*/
public void setWords(boolean words) {
LibVosk.vosk_recognizer_set_words(this.getPointer(), words);
}
/**
* Like above return words and confidences in partial results.
*
* @param partial_words - boolean value
*/
public void setPartialWords(boolean partial_words) {
LibVosk.vosk_recognizer_set_partial_words(this.getPointer(), partial_words);
}
/**
* Adds speaker model to already initialized recognizer.
*
* Can add speaker recognition model to already created recognizer.
* Helps to initialize speaker recognition for grammar-based recognizer.
*
* @param spkModel Speaker recognition model
*/
public void setSpeakerModel(SpeakerModel spkModel) {
LibVosk.vosk_recognizer_set_spk_model(this.getPointer(), spkModel.getPointer());
}
/**
* Accept and process new chunk of voice data.
*
* @param data - audio data in PCM 16-bit mono format
* @param len - length of the audio data
* @return 1 if silence is occurred and you can retrieve a new utterance with result method
* 0 if decoding continues
* -1 if exception occurred
*/
public boolean acceptWaveForm(byte[] data, int len) {
return LibVosk.vosk_recognizer_accept_waveform(this.getPointer(), data, len);
}
@@ -170,104 +39,22 @@ public class Recognizer extends PointerType implements AutoCloseable {
return LibVosk.vosk_recognizer_accept_waveform_f(this.getPointer(), data, len);
}
/**
* Returns speech recognition result
*
* @return the result in JSON format which contains decoded line, decoded
* words, times in seconds and confidences. You can parse this result
* with any json parser
*
* <pre>
* {
* "text" : "what zero zero zero one"
* }
* </pre>
*
* If alternatives enabled it returns result with alternatives, see also #setMaxAlternatives().
*
* If word times enabled returns word time, see also #setWordTimes().
*/
public String getResult() {
return LibVosk.vosk_recognizer_result(this.getPointer());
}
/**
* Returns partial speech recognition.
*
* @return partial speech recognition text which is not yet finalized.
* result may change as recognizer process more data.
*
* <pre>
* {
* "partial" : "cyril one eight zero"
* }
* </pre>
*/
public String getPartialResult() {
return LibVosk.vosk_recognizer_partial_result(this.getPointer());
}
/**
* Returns speech recognition result. Same as result, but doesn't wait for silence.
* You usually call it in the end of the stream to get final bits of audio. It
* flushes the feature pipeline, so all remaining audio chunks got processed.
*
* @return speech result in JSON format.
*/
public String getFinalResult() {
return LibVosk.vosk_recognizer_final_result(this.getPointer());
}
/**
* Reconfigures recognizer to use grammar.
*
* @param grammar Set of phrases in JSON array of strings or "[]" to use default model graph.
* @see #Recognizer(Model, float, String)
*/
public void setGrammar(String grammar) {
LibVosk.vosk_recognizer_set_grm(this.getPointer(), grammar);
}
/**
* Resets the recognizer.
* Resets current results so the recognition can continue from scratch.
*/
public void reset() {
LibVosk.vosk_recognizer_reset(this.getPointer());
}
/**
* Endpointer delay mode
*/
public class EndpointerMode {
public static final int DEFAULT = 0;
public static final int SHORT = 1;
public static final int LONG = 2;
public static final int VERY_LONG = 3;
}
/**
* Configures endpointer mode for recognizer
*/
public void setEndpointerMode(int mode) {
LibVosk.vosk_recognizer_set_endpointer_mode(this.getPointer(), mode);
}
/**
* Set endpointer delays
*
* @param t_start_max timeout for stopping recognition in case of initial silence (usually around 5.0)
* @param t_end timeout for stopping recognition in milliseconds after we recognized something (usually around 0.5 - 1.0)
* @param t_max timeout for forcing utterance end in milliseconds (usually around 20-30)
**/
public void setEndpointerDelays(float t_start_max, float t_end, float t_max) {
LibVosk.vosk_recognizer_set_endpointer_delays(this.getPointer(), t_start_max, t_end, t_max);
}
/**
* Releases recognizer object.
* Underlying model is also unreferenced and if needed, released.
*/
@Override
public void close() {
LibVosk.vosk_recognizer_free(this.getPointer());
@@ -1,36 +1,13 @@
package org.vosk;
import com.sun.jna.PointerType;
import java.io.IOException;
/**
* Helps to initialize speaker recognition for grammar-based recognizer.
*/
public class SpeakerModel extends PointerType implements AutoCloseable {
public SpeakerModel() {
}
/**
* Loads speaker model data from the file.
*
* The path must contain:
* - a config file: mfcc.conf
* - kaldi nnet: final.ext.raw
* - mean.vec
* - transform.mat
*
* @param path the path of the model on the filesystem
* @throws IOException if the model could not be created
*
* @see <a href="http://kaldi-asr.org/doc/structkaldi_1_1MfccOptions.html">Kaldi MfccOptions</a>
* @see <a href="http://kaldi-asr.org/doc/classkaldi_1_1nnet3_1_1Nnet.html">Kaldi Nnet</a>
*/
public SpeakerModel(String path) throws IOException {
public SpeakerModel(String path) {
super(LibVosk.vosk_spk_model_new(path));
if (getPointer() == null) {
throw new IOException("Failed to create a speaker model");
}
}
@Override
@@ -1,21 +0,0 @@
package org.vosk;
import com.sun.jna.PointerType;
public class TextProcessor extends PointerType implements AutoCloseable {
public TextProcessor() {
}
public TextProcessor(String verbalizer, String tagger) {
super(LibVosk.vosk_text_processor_new(verbalizer, tagger));
}
@Override
public void close() {
LibVosk.vosk_text_processor_free(this.getPointer());
}
public String itn(String input) {
return LibVosk.vosk_text_processor_itn(this.getPointer(), input);
}
}
@@ -19,9 +19,9 @@ import android.media.AudioRecord;
import android.media.MediaRecorder.AudioSource;
import android.os.Handler;
import android.os.Looper;
import android.annotation.SuppressLint;
import org.vosk.Recognizer;
import java.io.IOException;
/**
@@ -48,7 +48,6 @@ public class SpeechService {
*
* @throws IOException thrown if audio recorder can not be created for some reason.
*/
@SuppressLint("MissingPermission")
public SpeechService(Recognizer recognizer, float sampleRate) throws IOException {
this.recognizer = recognizer;
this.sampleRate = (int) sampleRate;
@@ -66,51 +65,6 @@ public class SpeechService {
}
}
/**
* Creates speech service with a caller-supplied {@link AudioRecord}.
* <p>
* Use this when you need to control the audio input device - for example,
* to pin recording to the built-in microphone when an external USB device
* without a microphone is present:
* <pre>
* AudioRecord recorder = new AudioRecord.Builder()
* .setAudioSource(MediaRecorder.AudioSource.VOICE_RECOGNITION)
* .setAudioFormat(format)
* .build();
* if (Build.VERSION.SDK_INT >= 28) {
* AudioManager am = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
* for (AudioDeviceInfo d : am.getDevices(AudioManager.GET_DEVICES_INPUTS)) {
* if (d.getType() == AudioDeviceInfo.TYPE_BUILTIN_MIC) {
* recorder.setPreferredDevice(d);
* break;
* }
* }
* }
* SpeechService service = new SpeechService(recognizer, 16000f, recorder);
* </pre>
* <p>
* The caller retains ownership of {@code recorder}: if this constructor
* throws, the recorder is <em>not</em> released. Call
* {@link AudioRecord#release()} yourself in that case.
*
* @param recognizer the Vosk recognizer
* @param sampleRate sample rate in Hz; must match {@code recorder}'s configuration
* @param recorder a fully-initialised {@link AudioRecord}
* @throws IOException if {@code recorder} is in STATE_UNINITIALIZED
*/
public SpeechService(Recognizer recognizer, float sampleRate, AudioRecord recorder)
throws IOException {
this.recognizer = recognizer;
this.sampleRate = (int) sampleRate;
this.recorder = recorder;
bufferSize = Math.round(this.sampleRate * BUFFER_SIZE_SECONDS);
if (recorder.getState() == AudioRecord.STATE_UNINITIALIZED) {
throw new IOException(
"Failed to initialize recorder. Microphone might be already in use.");
}
}
/**
* Starts recognition. Does nothing if recognition is active.
@@ -182,19 +136,6 @@ public class SpeechService {
return stopRecognizerThread();
}
/**
* Returns the audio session ID of the underlying {@link AudioRecord}.
* <p>
* The session ID can be used to attach audio effects such as
* {@link android.media.audiofx.NoiseSuppressor} to the recording session.
*
* @return audio session ID, or {@link AudioRecord#ERROR} if unavailable
*/
public int getAudioSessionId() {
return recorder.getAudioSessionId();
}
/**
* Shutdown the recognizer and release the recorder
*/
+13 -9
View File
@@ -3,12 +3,11 @@ def pomName = "Vosk English Model"
def pomDescription = "Small English model for Android"
android {
namespace "org.vosk"
compileSdkVersion 36
compileSdkVersion 29
defaultConfig {
minSdkVersion 21
targetSdkVersion 36
versionCode 10
targetSdkVersion 29
versionCode 6
versionName = version
archivesBaseName = archiveName
}
@@ -34,10 +33,15 @@ tasks.register('genUUID') {
preBuild.dependsOn(genUUID)
mavenPublishing {
coordinates("com.alphacephei", archiveName, version)
pom {
name = pomName
description = pomDescription
publishing {
publications {
aar(MavenPublication) {
artifactId = archiveName
artifact("$buildDir/outputs/aar/$archiveName-release.aar")
pom {
name = pomName
description = pomDescription
}
}
}
}
@@ -1,2 +1,3 @@
<manifest>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="org.vosk.model.en">
</manifest>
@@ -0,0 +1,7 @@
US English model for mobile Vosk applications
Copyright 2020 Alpha Cephei Inc
Accuracy: 10.38 (tedlium test) 9.85 (librispeech test-clean)
Speed: 0.11xRT (desktop)
Latency: 0.15s (right context)
+4 -4
View File
@@ -1,16 +1,16 @@
CFLAGS=-I../src
LDFLAGS=-L../src -lvosk -ldl -lpthread -Wl,-rpath,../src
LDFLAGS=-L../src -lvosk -ldl -lpthread -Wl,-rpath=../src
all: test_vosk test_vosk_speaker
test_vosk: test_vosk.o
gcc $^ -o $@ $(LDFLAGS)
g++ $^ -o $@ $(LDFLAGS)
test_vosk_speaker: test_vosk_speaker.o
gcc $^ -o $@ $(LDFLAGS)
g++ $^ -o $@ $(LDFLAGS)
%.o: %.c
gcc $(CFLAGS) -c -o $@ $<
g++ $(CFLAGS) -c -o $@ $<
clean:
rm -f *.o *.a test_vosk test_vosk_speaker
+1 -4
View File
@@ -28,16 +28,13 @@ public class VoskDemo
{
// Demo float array
VoskRecognizer rec = new VoskRecognizer(model, 16000.0f);
rec.SetEndpointerMode(EndpointerMode.LONG);
using(Stream source = File.OpenRead("test.wav")) {
byte[] buffer = new byte[4096];
int bytesRead;
while((bytesRead = source.Read(buffer, 0, buffer.Length)) > 0) {
float[] fbuffer = new float[bytesRead / 2];
for (int i = 0, n = 0; i < fbuffer.Length; i++, n+=2) {
fbuffer[i] = BitConverter.ToInt16(buffer, n);
fbuffer[i] = (short)(buffer[n] | buffer[n+1] << 8);
}
if (rec.AcceptWaveform(fbuffer, fbuffer.Length)) {
Console.WriteLine(rec.Result());
+2 -2
View File
@@ -2,7 +2,7 @@
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net8.0</TargetFramework>
<TargetFramework>net5.0</TargetFramework>
<RootNamespace>VoskDemo</RootNamespace>
</PropertyGroup>
@@ -11,7 +11,7 @@
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Vosk" Version="0.3.75" />
<PackageReference Include="Vosk" Version="0.3.30" />
</ItemGroup>
</Project>
-17
View File
@@ -1,17 +0,0 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<PackageId>Vosk</PackageId>
<Version>0.3.75</Version>
<authors>Alpha Cephei Inc</authors>
<owners>Alpha Cephei Inc</owners>
</PropertyGroup>
<Target Name="CopyFiles" AfterTargets="Build">
<Copy SourceFiles="bin/Release/net8.0/Vosk.dll" DestinationFolder="lib/net8.0" />
</Target>
</Project>
+5 -7
View File
@@ -2,14 +2,13 @@
<package>
<metadata>
<id>Vosk</id>
<version>0.3.75</version>
<version>0.3.30</version>
<authors>Alpha Cephei Inc</authors>
<owners>Alpha Cephei Inc</owners>
<license type="expression">Apache-2.0</license>
<projectUrl>https://alphacephei.com/vosk/</projectUrl>
<requireLicenseAcceptance>false</requireLicenseAcceptance>
<title>Vosk Speech Recognition Toolkit</title>
<description>Vosk is an offline open source speech recognition toolkit. It enables speech recognition models for 20+ languages and dialects - English, Indian English, German, French, Spanish, Portuguese, Chinese, Russian, Turkish, Vietnamese, Italian, Dutch, Catalan, Arabic, Greek, Farsi, Filipino, Ukrainian, Kazakh, Swedish, Japanese, Esperanto, Hindi, Czech. More to come.
<description>Vosk is an offline open source speech recognition toolkit. It enables speech recognition models for 16 languages and dialects - English, Indian English, German, French, Spanish, Portuguese, Chinese, Russian, Turkish, Vietnamese, Italian, Dutch, Catalan, Arabic, Greek, Farsi.
Vosk models are small (50 Mb) but provide continuous large vocabulary transcription, zero-latency response with streaming API, reconfigurable vocabulary and speaker identification.
@@ -19,14 +18,13 @@ Vosk supplies speech recognition for chatbots, smart home appliances, virtual as
Vosk scales from small devices like Raspberry Pi or Android smartphone to big clusters.</description>
<releaseNotes>See for details https://github.com/alphacep/vosk-api/releases</releaseNotes>
<repository type="git" url="https://github.com/alphacep/vosk-api.git" branch="master"/>
<copyright>Copyright 2020-2050 Alpha Cephei Inc</copyright>
<copyright>Copyright 2020 Alpha Cephei Inc</copyright>
<tags>speech recognition voice stt asr speech-to-text ai offline privacy</tags>
<dependencies>
<group targetFramework="net8.0"/>
<group targetFramework=".NETStandard2.0"/>
</dependencies>
</metadata>
<files>
<file src="**" exclude="bin/**;obj/**;build.sh;src/*.cs;*.nupkg;**/.keep-me" />
<file src="**" exclude="src/*.cs;build.sh;**/.keep-me;*.nupkg" />
</files>
</package>
+2 -2
View File
@@ -1,2 +1,2 @@
rm -rf bin lib obj
/home/shmyrev/local/dotnet/dotnet pack Vosk.csproj -p:NuspecFile=Vosk.nuspec -o .
mcs -out:lib/netstandard2.0/Vosk.dll -target:library src/*.cs
nuget pack
+1 -1
View File
@@ -2,7 +2,7 @@
<ItemGroup>
<NativeLibs Include="$(MSBuildThisFileDirectory)\lib\linux-x64\*.so" Condition="'$([MSBuild]::IsOsPlatform(Linux))'" />
<NativeLibs Include="$(MSBuildThisFileDirectory)\lib\win-x64\*.dll" Condition="'$([MSBuild]::IsOsPlatform(Windows))'" />
<NativeLibs Include="$(MSBuildThisFileDirectory)\lib\osx-universal\*.dylib" Condition="'$([MSBuild]::IsOsPlatform(OSX))'" />
<NativeLibs Include="$(MSBuildThisFileDirectory)\lib\osx-x64\*.dylib" Condition="'$([MSBuild]::IsOsPlatform(OSX))'" />
<None Include="@(NativeLibs)">
<Link>%(FileName)%(Extension)</Link>
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
-55
View File
@@ -1,55 +0,0 @@
using System;
namespace Vosk
{
public class BatchModel : global::System.IDisposable
{
private global::System.Runtime.InteropServices.HandleRef handle;
internal BatchModel(global::System.IntPtr cPtr)
{
handle = new global::System.Runtime.InteropServices.HandleRef(this, cPtr);
}
internal static global::System.Runtime.InteropServices.HandleRef getCPtr(BatchModel obj)
{
return (obj == null) ? new global::System.Runtime.InteropServices.HandleRef(null, global::System.IntPtr.Zero) : obj.handle;
}
~BatchModel()
{
Dispose(false);
}
public void Dispose()
{
Dispose(true);
global::System.GC.SuppressFinalize(this);
}
protected virtual void Dispose(bool disposing)
{
lock (this)
{
if (handle.Handle != global::System.IntPtr.Zero)
{
VoskPINVOKE.delete_BatchModel(handle);
handle = new global::System.Runtime.InteropServices.HandleRef(null, global::System.IntPtr.Zero);
}
}
}
public BatchModel(string model_path) : this(VoskPINVOKE.new_BatchModel(model_path))
{
}
public void WaitForCompletion()
{
if (handle.Handle != global::System.IntPtr.Zero)
{
VoskPINVOKE.wait_BatchModel(handle);
}
}
}
}
+1 -1
View File
@@ -32,7 +32,7 @@ public class Model : global::System.IDisposable {
public Model(string model_path) : this(VoskPINVOKE.new_Model(model_path)) {
}
public int FindWord(string word) {
public int vosk_model_find_word(string word) {
return VoskPINVOKE.Model_vosk_model_find_word(handle, word);
}
-88
View File
@@ -1,88 +0,0 @@
using System;
namespace Vosk
{
public class VoskBatchRecognizer : System.IDisposable
{
private System.Runtime.InteropServices.HandleRef handle;
internal VoskBatchRecognizer(System.IntPtr cPtr)
{
handle = new System.Runtime.InteropServices.HandleRef(this, cPtr);
}
internal static System.Runtime.InteropServices.HandleRef getCPtr(VoskBatchRecognizer obj)
{
return (obj == null) ? new System.Runtime.InteropServices.HandleRef(null, System.IntPtr.Zero) : obj.handle;
}
~VoskBatchRecognizer()
{
Dispose(false);
}
public void Dispose()
{
Dispose(true);
System.GC.SuppressFinalize(this);
}
protected virtual void Dispose(bool disposing)
{
lock (this)
{
if (handle.Handle != System.IntPtr.Zero)
{
VoskPINVOKE.delete_VoskBatchRecognizer(handle);
handle = new System.Runtime.InteropServices.HandleRef(null, System.IntPtr.Zero);
}
}
}
public VoskBatchRecognizer(BatchModel model, float sample_rate) : this(VoskPINVOKE.new_VoskBatchRecognizer(BatchModel.getCPtr(model), sample_rate))
{
}
public bool AcceptWaveform(byte[] data, int len)
{
return VoskPINVOKE.VoskBatchRecognizer_AcceptWaveform(handle, data, len);
}
private static string PtrToStringUTF8(System.IntPtr ptr)
{
int len = 0;
while (System.Runtime.InteropServices.Marshal.ReadByte(ptr, len) != 0)
len++;
byte[] array = new byte[len];
System.Runtime.InteropServices.Marshal.Copy(ptr, array, 0, len);
return System.Text.Encoding.UTF8.GetString(array);
}
public string FrontResult()
{
return PtrToStringUTF8(VoskPINVOKE.VoskBatchRecognizer_FrontResult(handle));
}
public string Result()
{
string result = PtrToStringUTF8(VoskPINVOKE.VoskBatchRecognizer_FrontResult(handle));
VoskPINVOKE.VoskBatchRecognizer_Pop(handle);
return result;
}
public int GetNumPendingChunks()
{
return VoskPINVOKE.VoskBatchRecognizer_GetPendingChunks(handle);
}
public void FinishStream()
{
VoskPINVOKE.VoskBatchRecognizer_FinishStream(handle);
}
public void SetNLSML(bool nlsml)
{
VoskPINVOKE.VoskBatchRecognizer_SetNLSML(handle, Convert.ToInt32(nlsml));
}
}
}
-43
View File
@@ -38,9 +38,6 @@ class VoskPINVOKE {
[global::System.Runtime.InteropServices.DllImport("libvosk", EntryPoint="vosk_recognizer_set_words")]
public static extern void VoskRecognizer_SetWords(global::System.Runtime.InteropServices.HandleRef jarg1, int jarg2);
[global::System.Runtime.InteropServices.DllImport("libvosk", EntryPoint="vosk_recognizer_set_partial_words")]
public static extern void VoskRecognizer_SetPartialWords(global::System.Runtime.InteropServices.HandleRef jarg1, int jarg2);
[global::System.Runtime.InteropServices.DllImport("libvosk", EntryPoint="vosk_recognizer_set_spk_model")]
public static extern void VoskRecognizer_SetSpkModel(global::System.Runtime.InteropServices.HandleRef jarg1, global::System.Runtime.InteropServices.HandleRef jarg2);
@@ -65,12 +62,6 @@ class VoskPINVOKE {
[global::System.Runtime.InteropServices.DllImport("libvosk", EntryPoint="vosk_recognizer_reset")]
public static extern void VoskRecognizer_Reset(global::System.Runtime.InteropServices.HandleRef jarg1);
[global::System.Runtime.InteropServices.DllImport("libvosk", EntryPoint="vosk_recognizer_set_endpointer_mode")]
public static extern void VoskRecognizer_SetEndpointerMode(global::System.Runtime.InteropServices.HandleRef jarg1, int jarg2);
[global::System.Runtime.InteropServices.DllImport("libvosk", EntryPoint="vosk_recognizer_set_endpointer_delays")]
public static extern void VoskRecognizer_SetEndpointerDelays(global::System.Runtime.InteropServices.HandleRef jarg1, float jarg2, float jarg3, float jarg4);
[global::System.Runtime.InteropServices.DllImport("libvosk", EntryPoint="vosk_set_log_level")]
public static extern void SetLogLevel(int jarg1);
@@ -79,40 +70,6 @@ class VoskPINVOKE {
[global::System.Runtime.InteropServices.DllImport("libvosk", EntryPoint="vosk_gpu_thread_init")]
public static extern void GpuThreadInit();
[global::System.Runtime.InteropServices.DllImport("libvosk", EntryPoint = "vosk_batch_model_new")]
public static extern global::System.IntPtr new_BatchModel(string jarg1);
[global::System.Runtime.InteropServices.DllImport("libvosk", EntryPoint = "vosk_batch_model_free")]
public static extern void delete_BatchModel(global::System.Runtime.InteropServices.HandleRef jarg1);
[global::System.Runtime.InteropServices.DllImport("libvosk", EntryPoint = "vosk_batch_model_wait")]
public static extern void wait_BatchModel(global::System.Runtime.InteropServices.HandleRef jarg1);
[global::System.Runtime.InteropServices.DllImport("libvosk", EntryPoint = "vosk_batch_recognizer_new")]
public static extern global::System.IntPtr new_VoskBatchRecognizer(global::System.Runtime.InteropServices.HandleRef jarg1, float jarg2);
[global::System.Runtime.InteropServices.DllImport("libvosk", EntryPoint = "vosk_batch_recognizer_free")]
public static extern void delete_VoskBatchRecognizer(global::System.Runtime.InteropServices.HandleRef jarg1);
[global::System.Runtime.InteropServices.DllImport("libvosk", EntryPoint = "vosk_batch_recognizer_accept_waveform")]
public static extern bool VoskBatchRecognizer_AcceptWaveform(global::System.Runtime.InteropServices.HandleRef jarg1, [global::System.Runtime.InteropServices.In, global::System.Runtime.InteropServices.MarshalAs(global::System.Runtime.InteropServices.UnmanagedType.LPArray)] byte[] jarg2, int jarg3);
[global::System.Runtime.InteropServices.DllImport("libvosk", EntryPoint = "vosk_batch_recognizer_set_nlsml")]
public static extern void VoskBatchRecognizer_SetNLSML(global::System.Runtime.InteropServices.HandleRef jarg1, int jarg2);
[global::System.Runtime.InteropServices.DllImport("libvosk", EntryPoint = "vosk_batch_recognizer_finish_stream")]
public static extern void VoskBatchRecognizer_FinishStream(global::System.Runtime.InteropServices.HandleRef jarg1);
[global::System.Runtime.InteropServices.DllImport("libvosk", EntryPoint = "vosk_batch_recognizer_front_result")]
public static extern global::System.IntPtr VoskBatchRecognizer_FrontResult(global::System.Runtime.InteropServices.HandleRef jarg1);
[global::System.Runtime.InteropServices.DllImport("libvosk", EntryPoint = "vosk_batch_recognizer_pop")]
public static extern void VoskBatchRecognizer_Pop(global::System.Runtime.InteropServices.HandleRef jarg1);
[global::System.Runtime.InteropServices.DllImport("libvosk", EntryPoint = "vosk_batch_recognizer_get_pending_chunks")]
public static extern int VoskBatchRecognizer_GetPendingChunks(global::System.Runtime.InteropServices.HandleRef jarg1);
}
}
-19
View File
@@ -1,12 +1,5 @@
namespace Vosk {
public enum EndpointerMode {
DEFAULT = 0,
SHORT = 1,
LONG = 2,
VERY_LONG = 3
}
public class VoskRecognizer : System.IDisposable {
private System.Runtime.InteropServices.HandleRef handle;
@@ -53,10 +46,6 @@ public class VoskRecognizer : System.IDisposable {
VoskPINVOKE.VoskRecognizer_SetWords(handle, words ? 1 : 0);
}
public void SetPartialWords(bool partial_words) {
VoskPINVOKE.VoskRecognizer_SetPartialWords(handle, partial_words ? 1 : 0);
}
public void SetSpkModel(SpkModel spk_model) {
VoskPINVOKE.VoskRecognizer_SetSpkModel(handle, SpkModel.getCPtr(spk_model));
}
@@ -98,14 +87,6 @@ public class VoskRecognizer : System.IDisposable {
VoskPINVOKE.VoskRecognizer_Reset(handle);
}
public void SetEndpointerMode(EndpointerMode mode) {
VoskPINVOKE.VoskRecognizer_SetEndpointerMode(handle, (int) mode);
}
public void SetEndpointerDelays(float t_start_max, float t_end, float t_max) {
VoskPINVOKE.VoskRecognizer_SetEndpointerDelays(handle, t_start_max, t_end, t_max);
}
}
}
-1
View File
@@ -1 +0,0 @@
See example subfolder for instructions how to use the module
-99
View File
@@ -1,99 +0,0 @@
package vosk
// #cgo CPPFLAGS: -I ${SRCDIR}/../src
// #cgo !windows LDFLAGS: -L ${SRCDIR}/../src -lvosk -ldl -lpthread
// #cgo windows LDFLAGS: -L ${SRCDIR}/../src -lvosk -lpthread
// #include <stdlib.h>
// #include <vosk_api.h>
import "C"
import "unsafe"
// VoskBatchModel contains a reference to the C VoskBatchModel
type VoskBatchModel struct {
model *C.struct_VoskBatchModel
}
// NewBatchModel creates a new VoskBatchModel instance
func NewBatchModel(modelPath string) (*VoskBatchModel, error) {
cmodelPath := C.CString(modelPath)
defer C.free(unsafe.Pointer(cmodelPath))
internal := C.vosk_batch_model_new(cmodelPath)
model := &VoskBatchModel{model: internal}
return model, nil
}
func (m *VoskBatchModel) Free() {
C.vosk_batch_model_free(m.model)
}
func (m *VoskBatchModel) Wait() {
C.vosk_batch_model_wait(m.model);
}
func freeBatchModel(model *VoskBatchModel) {
C.vosk_batch_model_free(model.model)
}
// VoskBatchRecognizer contains a reference to the C VoskBatchRecognizer
type VoskBatchRecognizer struct {
rec *C.struct_VoskBatchRecognizer
}
func freeBatchRecognizer(recognizer *VoskBatchRecognizer) {
C.vosk_batch_recognizer_free(recognizer.rec)
}
func (r *VoskBatchRecognizer) Free() {
C.vosk_batch_recognizer_free(r.rec)
}
// NewBatchRecognizer creates a new VoskBatchRecognizer instance
func NewBatchRecognizer(model *VoskBatchModel, sampleRate float64) (*VoskBatchRecognizer, error) {
internal := C.vosk_batch_recognizer_new(model.model, C.float(sampleRate))
rec := &VoskBatchRecognizer{rec: internal}
return rec, nil
}
// AcceptWaveform accepts and processes a new chunk of the voice data.
func (r *VoskBatchRecognizer) AcceptWaveform(buffer []byte) {
cbuf := C.CBytes(buffer)
defer C.free(cbuf)
C.vosk_batch_recognizer_accept_waveform(r.rec, (*C.char)(cbuf), C.int(len(buffer)))
}
/** Set NLSML output
* @param nlsml - boolean value
*/
//void vosk_batch_recognizer_set_nlsml(VoskBatchRecognizer *recognizer, int nlsml);
func (r *VoskBatchRecognizer) SetNlsml(nlsml int) {
C.vosk_batch_recognizer_set_nlsml(r.rec, C.int(nlsml))
}
/** Closes the stream */
//void vosk_batch_recognizer_finish_stream(VoskBatchRecognizer *recognizer);
func (r *VoskBatchRecognizer) FinishStream() {
C.vosk_batch_recognizer_finish_stream(r.rec)
}
/** Return results */
//const char *vosk_batch_recognizer_front_result(VoskBatchRecognizer *recognizer);
func (r *VoskBatchRecognizer) FrontResult() string {
return C.GoString(C.vosk_batch_recognizer_front_result(r.rec))
}
/** Release and free first retrieved result */
//void vosk_batch_recognizer_pop(VoskBatchRecognizer *recognizer);
func (r *VoskBatchRecognizer) Pop() {
C.vosk_batch_recognizer_pop(r.rec)
}
/** Get amount of pending chunks for more intelligent waiting */
//int vosk_batch_recognizer_get_pending_chunks(VoskBatchRecognizer *recognizer);
func (r *VoskBatchRecognizer) GetPendingChunks() int {
i := C.vosk_batch_recognizer_get_pending_chunks(r.rec)
return int(i)
}
-5
View File
@@ -1,5 +0,0 @@
This example expects a `s16le` converted audio file and converts it to text in a
manner that imitates the Python example of [test_gpu_batch.py](../python/example/test_gpu_batch.py).
Note that the `libvosk.so` must be in the library path. This was successfully tested on
Ubuntu 24.04 with Go 1.18, gcc-11, NVIDIA driver 570.172.08.
-54
View File
@@ -1,54 +0,0 @@
package main
import (
"flag"
"fmt"
"io"
"log"
"os"
vosk "github.com/alphacep/vosk-api/go"
)
func main() {
var filename string
flag.StringVar(&filename, "f", "", "file to transcribe")
flag.Parse()
vosk.GPUInit()
model, err := vosk.NewBatchModel("model")
if err != nil {
log.Fatal(err)
}
rec, err := vosk.NewBatchRecognizer(model, 16000.0)
if err != nil {
log.Fatal(err)
}
file, err := os.Open(filename)
if err != nil {
panic(err)
}
defer file.Close()
buf := make([]byte, 8000)
for {
if _, err := file.Read(buf); err != nil {
if err != io.EOF {
log.Fatal(err)
}
break
}
rec.AcceptWaveform(buf)
model.Wait()
if rec.FrontResult() != "" {
fmt.Println(rec.FrontResult())
rec.Pop()
}
}
// Is this needed? rec.FinishStream()
}
-31
View File
@@ -1,31 +0,0 @@
To try this package do the following steps:
On Linux (we download library and set LD_LIBRARY_PATH)
```
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
```
for Windows (we place DLLs in current folder where linker finds them):
```
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
```
+1 -1
View File
@@ -1,2 +1,2 @@
// Example package for Vosk Go bindings.
package main
package test_simple
+16 -12
View File
@@ -1,7 +1,7 @@
package main
import (
"encoding/json"
"bufio"
"flag"
"fmt"
"io"
@@ -16,18 +16,18 @@ func main() {
flag.StringVar(&filename, "f", "", "file to transcribe")
flag.Parse()
vosk.GPUInit()
model, err := vosk.NewModel("model")
if err != nil {
log.Fatal(err)
}
// we can check if word is in the vocabulary
// fmt.Println(model.FindWord("air"))
spkModel, err := vosk.NewSpkModel("model-spk")
if err != nil {
log.Fatal(err)
}
sampleRate := 16000.0
rec, err := vosk.NewRecognizer(model, sampleRate)
rec, err := vosk.NewRecognizer(model, sampleRate, spkModel)
if err != nil {
log.Fatal(err)
}
@@ -39,10 +39,17 @@ func main() {
}
defer file.Close()
file, err := os.Open(filename)
if err != nil {
panic(err)
}
defer file.Close()
reader := bufio.NewReader(file)
buf := make([]byte, 4096)
for {
_, err := file.Read(buf)
_, err := reader.Read(buf)
if err != nil {
if err != io.EOF {
log.Fatal(err)
@@ -52,12 +59,9 @@ func main() {
}
if rec.AcceptWaveform(buf) != 0 {
fmt.Println(rec.Result())
fmt.Println(string(rec.Result()))
}
}
// Unmarshal example for final result
var jres map[string]interface{}
json.Unmarshal([]byte(rec.FinalResult()), &jres)
fmt.Println(jres["text"])
fmt.Println(string(rec.FinalResult()))
}
+17 -57
View File
@@ -1,12 +1,10 @@
package vosk
// #cgo CPPFLAGS: -I ${SRCDIR}/../src
// #cgo !windows LDFLAGS: -L ${SRCDIR}/../src -lvosk -ldl -lpthread
// #cgo windows LDFLAGS: -L ${SRCDIR}/../src -lvosk -lpthread
// #cgo LDFLAGS: -L ${SRCDIR}/../src -lvosk -ldl -lpthread
// #include <stdlib.h>
// #include <vosk_api.h>
import "C"
import "unsafe"
// VoskModel contains a reference to the C VoskModel
type VoskModel struct {
@@ -15,17 +13,11 @@ type VoskModel struct {
// NewModel creates a new VoskModel instance
func NewModel(modelPath string) (*VoskModel, error) {
cmodelPath := C.CString(modelPath)
defer C.free(unsafe.Pointer(cmodelPath))
internal := C.vosk_model_new(cmodelPath)
internal := C.vosk_model_new(C.CString(modelPath))
model := &VoskModel{model: internal}
return model, nil
}
func (m *VoskModel) Free() {
C.vosk_model_free(m.model)
}
func freeModel(model *VoskModel) {
C.vosk_model_free(model.model)
}
@@ -33,10 +25,10 @@ func freeModel(model *VoskModel) {
// FindWord checks if a word can be recognized by the model.
// Returns the word symbol if the word exists inside the model or
// -1 otherwise.
func (m *VoskModel) FindWord(word string) int {
cstr := C.CString(word)
defer C.free(unsafe.Pointer(cstr))
i := C.vosk_model_find_word(m.model, cstr)
func (m *VoskModel) FindWord(word []byte) int {
cbuf := C.CBytes(word)
defer C.free(cbuf)
i := C.vosk_model_find_word(m.model, (*C.char)(cbuf))
return int(i)
}
@@ -47,9 +39,7 @@ type VoskSpkModel struct {
// NewSpkModel creates a new VoskSpkModel instance
func NewSpkModel(spkModelPath string) (*VoskSpkModel, error) {
cspkModelPath := C.CString(spkModelPath)
defer C.free(unsafe.Pointer(cspkModelPath))
internal := C.vosk_spk_model_new(cspkModelPath)
internal := C.vosk_spk_model_new(C.CString(spkModelPath))
spkModel := &VoskSpkModel{spkModel: internal}
return spkModel, nil
}
@@ -58,10 +48,6 @@ func freeSpkModel(model *VoskSpkModel) {
C.vosk_spk_model_free(model.spkModel)
}
func(s *VoskSpkModel) Free() {
C.vosk_spk_model_free(s.spkModel)
}
// VoskRecognizer contains a reference to the C VoskRecognizer
type VoskRecognizer struct {
rec *C.struct_VoskRecognizer
@@ -71,10 +57,6 @@ func freeRecognizer(recognizer *VoskRecognizer) {
C.vosk_recognizer_free(recognizer.rec)
}
func (r *VoskRecognizer) Free() {
C.vosk_recognizer_free(r.rec)
}
// NewRecognizer creates a new VoskRecognizer instance
func NewRecognizer(model *VoskModel, sampleRate float64) (*VoskRecognizer, error) {
internal := C.vosk_recognizer_new(model.model, C.float(sampleRate))
@@ -90,10 +72,10 @@ func NewRecognizerSpk(model *VoskModel, sampleRate float64, spkModel *VoskSpkMod
}
// NewRecognizerGrm creates a new VoskRecognizer instance with the phrase list.
func NewRecognizerGrm(model *VoskModel, sampleRate float64, grammar string) (*VoskRecognizer, error) {
cgrammar := C.CString(grammar)
defer C.free(unsafe.Pointer(cgrammar))
internal := C.vosk_recognizer_new_grm(model.model, C.float(sampleRate), cgrammar)
func NewRecognizerGrm(model *VoskModel, sampleRate float64, grammer []byte) (*VoskRecognizer, error) {
cbuf := C.CBytes(grammer)
defer C.free(cbuf)
internal := C.vosk_recognizer_new_grm(model.model, C.float(sampleRate), (*C.char)(cbuf))
rec := &VoskRecognizer{rec: internal}
return rec, nil
}
@@ -103,13 +85,6 @@ func (r *VoskRecognizer) SetSpkModel(spkModel *VoskSpkModel) {
C.vosk_recognizer_set_spk_model(r.rec, spkModel.spkModel)
}
// SetGrm sets which phrases to recognize on an already initialized recognizer.
func (r *VoskRecognizer) SetGrm(grammar string) {
cgrammar := C.CString(grammar)
defer C.free(unsafe.Pointer(cgrammar))
C.vosk_recognizer_set_grm(r.rec, cgrammar)
}
// SetMaxAlternatives configures the recognizer to output n-best results.
func (r *VoskRecognizer) SetMaxAlternatives(maxAlternatives int) {
C.vosk_recognizer_set_max_alternatives(r.rec, C.int(maxAlternatives))
@@ -120,21 +95,6 @@ func (r *VoskRecognizer) SetWords(words int) {
C.vosk_recognizer_set_words(r.rec, C.int(words))
}
// SetPartialWords enables words with times in the partial ouput.
func (r *VoskRecognizer) SetPartialWords(words int) {
C.vosk_recognizer_set_partial_words(r.rec, C.int(words))
}
// SetEndpointerDelays sets the recognition timeouts, where startMax
// is the timeout for stopping recognition in case of initial silence
// (usually around 5), end is the timeout for stopping recognition
// in milliseconds after we recognized something (usually around 0.5-1.0),
// and max is the timeout for forcing utterance end in milliseconds
// (usually around 20-30).
func (r *VoskRecognizer) SetEndpointerDelays(startMax, end, max float64) {
C.vosk_recognizer_set_endpointer_delays(r.rec, C.float(startMax), C.float(end), C.float(max))
}
// AcceptWaveform accepts and processes a new chunk of the voice data.
func (r *VoskRecognizer) AcceptWaveform(buffer []byte) int {
cbuf := C.CBytes(buffer)
@@ -144,19 +104,19 @@ func (r *VoskRecognizer) AcceptWaveform(buffer []byte) int {
}
// Result returns a speech recognition result.
func (r *VoskRecognizer) Result() string {
return C.GoString(C.vosk_recognizer_result(r.rec))
func (r *VoskRecognizer) Result() []byte {
return []byte(C.GoString(C.vosk_recognizer_result(r.rec)))
}
// PartialResult returns a partial speech recognition result.
func (r *VoskRecognizer) PartialResult() string {
return C.GoString(C.vosk_recognizer_partial_result(r.rec))
func (r *VoskRecognizer) PartialResult() []byte {
return []byte(C.GoString(C.vosk_recognizer_result(r.rec)))
}
// FinalResult returns a speech recognition result. Same as result, but doesn't wait
// for silence.
func (r *VoskRecognizer) FinalResult() string {
return C.GoString(C.vosk_recognizer_final_result(r.rec))
func (r *VoskRecognizer) FinalResult() []byte {
return []byte(C.GoString(C.vosk_recognizer_final_result(r.rec)))
}
// Reset resets the recognizer.
+17 -26
View File
@@ -13,10 +13,10 @@
92375229240C550B00DD6076 /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = 92375228240C550B00DD6076 /* Assets.xcassets */; };
9237522C240C550B00DD6076 /* LaunchScreen.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = 9237522A240C550B00DD6076 /* LaunchScreen.storyboard */; };
92375234240C558900DD6076 /* Vosk.swift in Sources */ = {isa = PBXBuildFile; fileRef = 92375233240C558900DD6076 /* Vosk.swift */; };
9237523C240C642000DD6076 /* libkaldiwrap.a in Frameworks */ = {isa = PBXBuildFile; fileRef = 9237523A240C642000DD6076 /* libkaldiwrap.a */; };
92375244240C6DAF00DD6076 /* Accelerate.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 92375243240C6DAF00DD6076 /* Accelerate.framework */; };
92375246240C6DC900DD6076 /* libstdc++.tbd in Frameworks */ = {isa = PBXBuildFile; fileRef = 92375245240C6DC900DD6076 /* libstdc++.tbd */; };
92375274240C6F1E00DD6076 /* 10001-90210-01803.wav in Resources */ = {isa = PBXBuildFile; fileRef = 92375256240C6E3D00DD6076 /* 10001-90210-01803.wav */; };
925527A9273C492C00FFD9CC /* libvosk.a in Frameworks */ = {isa = PBXBuildFile; fileRef = 925527A8273C492C00FFD9CC /* libvosk.a */; };
92833003273C466E00058B52 /* libc++.tbd in Frameworks */ = {isa = PBXBuildFile; fileRef = 92833002273C466E00058B52 /* libc++.tbd */; };
92BACED125BE125A00B5CC93 /* vosk-model-small-en-us-0.15 in Resources */ = {isa = PBXBuildFile; fileRef = 928CC50C25BE124400490481 /* vosk-model-small-en-us-0.15 */; };
92D6B8D325BDFEAC007FF08D /* VoskModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = 92D6B8D225BDFEAC007FF08D /* VoskModel.swift */; };
92D86BD6253F823F0040D53F /* vosk-model-spk-0.4 in Resources */ = {isa = PBXBuildFile; fileRef = 92D86BD4253F823F0040D53F /* vosk-model-spk-0.4 */; };
@@ -31,10 +31,10 @@
9237522B240C550B00DD6076 /* Base */ = {isa = PBXFileReference; lastKnownFileType = file.storyboard; name = Base; path = Base.lproj/LaunchScreen.storyboard; sourceTree = "<group>"; };
9237522D240C550B00DD6076 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = "<group>"; };
92375233240C558900DD6076 /* Vosk.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Vosk.swift; sourceTree = "<group>"; };
9237523A240C642000DD6076 /* libkaldiwrap.a */ = {isa = PBXFileReference; lastKnownFileType = archive.ar; path = libkaldiwrap.a; sourceTree = "<group>"; };
92375243240C6DAF00DD6076 /* Accelerate.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = Accelerate.framework; path = System/Library/Frameworks/Accelerate.framework; sourceTree = SDKROOT; };
92375245240C6DC900DD6076 /* libstdc++.tbd */ = {isa = PBXFileReference; lastKnownFileType = "sourcecode.text-based-dylib-definition"; name = "libstdc++.tbd"; path = "usr/lib/libstdc++.tbd"; sourceTree = SDKROOT; };
92375256240C6E3D00DD6076 /* 10001-90210-01803.wav */ = {isa = PBXFileReference; lastKnownFileType = audio.wav; path = "10001-90210-01803.wav"; sourceTree = "<group>"; };
925527A8273C492C00FFD9CC /* libvosk.a */ = {isa = PBXFileReference; lastKnownFileType = archive.ar; path = libvosk.a; sourceTree = "<group>"; };
92833002273C466E00058B52 /* libc++.tbd */ = {isa = PBXFileReference; lastKnownFileType = "sourcecode.text-based-dylib-definition"; name = "libc++.tbd"; path = "usr/lib/libc++.tbd"; sourceTree = SDKROOT; };
928CC50C25BE124400490481 /* vosk-model-small-en-us-0.15 */ = {isa = PBXFileReference; lastKnownFileType = folder; name = "vosk-model-small-en-us-0.15"; path = "/Users/shmyrev/Documents/IOS/VoskApiTest/VoskApiTest/Vosk/vosk-model-small-en-us-0.15"; sourceTree = "<absolute>"; };
92AA22AD244CDD1200DA464B /* vosk_api.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = vosk_api.h; sourceTree = "<group>"; };
92AA22AE244CDD5200DA464B /* bridging.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = bridging.h; sourceTree = "<group>"; };
@@ -47,9 +47,9 @@
isa = PBXFrameworksBuildPhase;
buildActionMask = 2147483647;
files = (
92833003273C466E00058B52 /* libc++.tbd in Frameworks */,
92375246240C6DC900DD6076 /* libstdc++.tbd in Frameworks */,
92375244240C6DAF00DD6076 /* Accelerate.framework in Frameworks */,
925527A9273C492C00FFD9CC /* libvosk.a in Frameworks */,
9237523C240C642000DD6076 /* libkaldiwrap.a in Frameworks */,
);
runOnlyForDeploymentPostprocessing = 0;
};
@@ -93,11 +93,11 @@
92375239240C642000DD6076 /* Vosk */ = {
isa = PBXGroup;
children = (
92D86BD4253F823F0040D53F /* vosk-model-spk-0.4 */,
928CC50C25BE124400490481 /* vosk-model-small-en-us-0.15 */,
925527A8273C492C00FFD9CC /* libvosk.a */,
92AA22AD244CDD1200DA464B /* vosk_api.h */,
92D86BD4253F823F0040D53F /* vosk-model-spk-0.4 */,
92375256240C6E3D00DD6076 /* 10001-90210-01803.wav */,
92AA22AD244CDD1200DA464B /* vosk_api.h */,
9237523A240C642000DD6076 /* libkaldiwrap.a */,
);
name = Vosk;
path = VoskApiTest/Vosk;
@@ -106,7 +106,7 @@
92375242240C6DAF00DD6076 /* Frameworks */ = {
isa = PBXGroup;
children = (
92833002273C466E00058B52 /* libc++.tbd */,
92375245240C6DC900DD6076 /* libstdc++.tbd */,
92375243240C6DAF00DD6076 /* Accelerate.framework */,
);
name = Frameworks;
@@ -145,7 +145,7 @@
9237521D240C550B00DD6076 = {
CreatedOnToolsVersion = 8.3.2;
LastSwiftMigration = 0920;
ProvisioningStyle = Manual;
ProvisioningStyle = Automatic;
};
};
};
@@ -223,6 +223,7 @@
ALWAYS_SEARCH_USER_PATHS = NO;
CLANG_ANALYZER_NONNULL = YES;
CLANG_ANALYZER_NUMBER_OBJECT_CONVERSION = YES_AGGRESSIVE;
CLANG_CXX_LANGUAGE_STANDARD = "gnu++0x";
CLANG_CXX_LIBRARY = "libc++";
CLANG_ENABLE_MODULES = YES;
CLANG_ENABLE_OBJC_ARC = YES;
@@ -244,7 +245,7 @@
CLANG_WARN_SUSPICIOUS_MOVE = YES;
CLANG_WARN_UNREACHABLE_CODE = YES;
CLANG_WARN__DUPLICATE_METHOD_MATCH = YES;
"CODE_SIGN_IDENTITY[sdk=iphoneos*]" = "";
"CODE_SIGN_IDENTITY[sdk=iphoneos*]" = "iPhone Developer";
COPY_PHASE_STRIP = NO;
DEBUG_INFORMATION_FORMAT = dwarf;
ENABLE_STRICT_OBJC_MSGSEND = YES;
@@ -281,6 +282,7 @@
ALWAYS_SEARCH_USER_PATHS = NO;
CLANG_ANALYZER_NONNULL = YES;
CLANG_ANALYZER_NUMBER_OBJECT_CONVERSION = YES_AGGRESSIVE;
CLANG_CXX_LANGUAGE_STANDARD = "gnu++0x";
CLANG_CXX_LIBRARY = "libc++";
CLANG_ENABLE_MODULES = YES;
CLANG_ENABLE_OBJC_ARC = YES;
@@ -302,7 +304,7 @@
CLANG_WARN_SUSPICIOUS_MOVE = YES;
CLANG_WARN_UNREACHABLE_CODE = YES;
CLANG_WARN__DUPLICATE_METHOD_MATCH = YES;
"CODE_SIGN_IDENTITY[sdk=iphoneos*]" = "";
"CODE_SIGN_IDENTITY[sdk=iphoneos*]" = "iPhone Developer";
COPY_PHASE_STRIP = NO;
DEBUG_INFORMATION_FORMAT = "dwarf-with-dsym";
ENABLE_NS_ASSERTIONS = NO;
@@ -317,7 +319,6 @@
GCC_WARN_UNUSED_VARIABLE = YES;
IPHONEOS_DEPLOYMENT_TARGET = 10.3;
MTL_ENABLE_DEBUG_INFO = NO;
ONLY_ACTIVE_ARCH = YES;
SDKROOT = iphoneos;
SWIFT_OBJC_BRIDGING_HEADER = bridging.h;
SWIFT_OPTIMIZATION_LEVEL = "-Owholemodule";
@@ -332,10 +333,7 @@
buildSettings = {
ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon;
CLANG_ENABLE_MODULES = YES;
"CODE_SIGN_IDENTITY[sdk=iphoneos*]" = "iPhone Developer";
CODE_SIGN_STYLE = Manual;
DEVELOPMENT_TEAM = "";
ENABLE_BITCODE = NO;
ENABLE_BITCODE = YES;
INFOPLIST_FILE = VoskApiTest/Info.plist;
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks";
LIBRARY_SEARCH_PATHS = (
@@ -344,13 +342,11 @@
);
PRODUCT_BUNDLE_IDENTIFIER = com.alphacephei.VoskApiTest;
PRODUCT_NAME = "$(TARGET_NAME)";
PROVISIONING_PROFILE_SPECIFIER = "";
SWIFT_INSTALL_OBJC_HEADER = YES;
SWIFT_OBJC_BRIDGING_HEADER = VoskApiTest/bridging.h;
SWIFT_OPTIMIZATION_LEVEL = "-Onone";
SWIFT_SWIFT3_OBJC_INFERENCE = Default;
SWIFT_VERSION = 4.0;
TARGETED_DEVICE_FAMILY = "1,2";
};
name = Debug;
};
@@ -359,10 +355,7 @@
buildSettings = {
ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon;
CLANG_ENABLE_MODULES = YES;
"CODE_SIGN_IDENTITY[sdk=iphoneos*]" = "iPhone Developer";
CODE_SIGN_STYLE = Manual;
DEVELOPMENT_TEAM = "";
ENABLE_BITCODE = NO;
ENABLE_BITCODE = YES;
INFOPLIST_FILE = VoskApiTest/Info.plist;
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks";
LIBRARY_SEARCH_PATHS = (
@@ -371,12 +364,10 @@
);
PRODUCT_BUNDLE_IDENTIFIER = com.alphacephei.VoskApiTest;
PRODUCT_NAME = "$(TARGET_NAME)";
PROVISIONING_PROFILE_SPECIFIER = "";
SWIFT_INSTALL_OBJC_HEADER = YES;
SWIFT_OBJC_BRIDGING_HEADER = VoskApiTest/bridging.h;
SWIFT_SWIFT3_OBJC_INFERENCE = Default;
SWIFT_VERSION = 4.0;
TARGETED_DEVICE_FAMILY = "1,2";
};
name = Release;
};
@@ -1,6 +1,9 @@
<?xml version="1.0" encoding="UTF-8"?>
<Workspace
version = "1.0">
<FileRef
location = "group:/Users/shmyrev/Documents/IOS/VoskApiTest/VoskApiTest/Vosk/vosk-model-small-en-us-0.15">
</FileRef>
<FileRef
location = "self:">
</FileRef>
+1 -1
View File
@@ -14,7 +14,7 @@ public final class Vosk {
var recognizer : OpaquePointer!
init(model: VoskModel, sampleRate: Float) {
recognizer = vosk_recognizer_new_spk(model.model, sampleRate, model.spkModel)
recognizer = vosk_recognizer_new_spk(model.model, model.spkModel, sampleRate)
}
deinit {
+36 -117
View File
@@ -1,4 +1,4 @@
// Copyright 2020-2021 Alpha Cephei Inc.
// Copyright 2020 Alpha Cephei Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
@@ -43,7 +43,7 @@ typedef struct VoskRecognizer VoskRecognizer;
/** Loads model data from the file and returns the model object
*
* @param model_path: the path of the model on the filesystem
* @returns model object or NULL if problem occured */
@ @returns model object */
VoskModel *vosk_model_new(const char *model_path);
@@ -55,18 +55,10 @@ VoskModel *vosk_model_new(const char *model_path);
void vosk_model_free(VoskModel *model);
/** Check if a word can be recognized by the model
* @param word: the word
* @returns the word symbol if @param word exists inside the model
* or -1 otherwise.
* Reminding that word symbol 0 is for <epsilon> */
int vosk_model_find_word(VoskModel *model, const char *word);
/** Loads speaker model data from the file and returns the model object
*
* @param model_path: the path of the model on the filesystem
* @returns model object or NULL if problem occured */
* @returns model object */
VoskSpkModel *vosk_spk_model_new(const char *model_path);
@@ -79,13 +71,9 @@ void vosk_spk_model_free(VoskSpkModel *model);
/** Creates the recognizer object
*
* The recognizers process the speech and return text using shared model data
* @param model VoskModel containing static data for recognizer. Model can be
* shared across recognizers, even running in different threads.
* @param sample_rate The sample rate of the audio you going to feed into the recognizer.
* Make sure this rate matches the audio content, it is a common
* issue causing accuracy problems.
* @returns recognizer object or NULL if problem occured */
* The recognizers process the speech and return text using shared model data
* @param sample_rate The sample rate of the audio you going to feed into the recognizer
* @returns recognizer object */
VoskRecognizer *vosk_recognizer_new(VoskModel *model, float sample_rate);
@@ -94,14 +82,10 @@ VoskRecognizer *vosk_recognizer_new(VoskModel *model, float sample_rate);
* With the speaker recognition mode the recognizer not just recognize
* text but also return speaker vectors one can use for speaker identification
*
* @param model VoskModel containing static data for recognizer. Model can be
* shared across recognizers, even running in different threads.
* @param sample_rate The sample rate of the audio you going to feed into the recognizer.
* Make sure this rate matches the audio content, it is a common
* issue causing accuracy problems.
* @param spk_model speaker model for speaker identification
* @returns recognizer object or NULL if problem occured */
VoskRecognizer *vosk_recognizer_new_spk(VoskModel *model, float sample_rate, VoskSpkModel *spk_model);
* @param sample_rate The sample rate of the audio you going to feed into the recognizer
* @returns recognizer object */
VoskRecognizer *vosk_recognizer_new_spk(VoskModel *model, VoskSpkModel *spk_model, float sample_rate);
/** Creates the recognizer object with the phrase list
@@ -114,46 +98,42 @@ VoskRecognizer *vosk_recognizer_new_spk(VoskModel *model, float sample_rate, Vos
* Only recognizers with lookahead models support this type of quick configuration.
* Precompiled HCLG graph models are not supported.
*
* @param model VoskModel containing static data for recognizer. Model can be
* shared across recognizers, even running in different threads.
* @param sample_rate The sample rate of the audio you going to feed into the recognizer.
* Make sure this rate matches the audio content, it is a common
* issue causing accuracy problems.
* @param sample_rate The sample rate of the audio you going to feed into the recognizer
* @param grammar The string with the list of phrases to recognize as JSON array of strings,
* for example "["one two three four five", "[unk]"]".
*
* @returns recognizer object or NULL if problem occured */
* @returns recognizer object */
VoskRecognizer *vosk_recognizer_new_grm(VoskModel *model, float sample_rate, const char *grammar);
/** Adds speaker model to already initialized recognizer
/** Accept voice data
*
* Can add speaker recognition model to already created recognizer. Helps to initialize
* speaker recognition for grammar-based recognizer.
* accept and process new chunk of voice data
*
* @param spk_model Speaker recognition model */
void vosk_recognizer_set_spk_model(VoskRecognizer *recognizer, VoskSpkModel *spk_model);
* @param data - audio data in PCM 16-bit mono format
* @param length - length of the audio data
* @returns true if silence is occured and you can retrieve a new utterance with result method */
int vosk_recognizer_accept_waveform(VoskRecognizer *recognizer, const char *data, int length);
/** Configures recognizer to output n-best results
*
* <pre>
* {
* "alternatives": [
* { "text": "one two three four five", "confidence": 0.97 },
* { "text": "one two three for five", "confidence": 0.03 },
* ]
* }
* </pre>
*
* @param max_alternatives - maximum alternatives to return from recognition results
*/
void vosk_recognizer_set_max_alternatives(VoskRecognizer *recognizer, int max_alternatives);
/** Enables words with times in the output
/** Same as above but the version with the short data for language bindings where you have
* audio as array of shorts */
int vosk_recognizer_accept_waveform_s(VoskRecognizer *recognizer, const short *data, int length);
/** Same as above but the version with the float data for language bindings where you have
* audio as array of floats */
int vosk_recognizer_accept_waveform_f(VoskRecognizer *recognizer, const float *data, int length);
/** Returns speech recognition result
*
* @returns the result in JSON format which contains decoded line, decoded
* words, times in seconds and confidences. You can parse this result
* with any json parser
*
* <pre>
* {
* "result" : [{
* "conf" : 1.000000,
* "end" : 1.110000,
@@ -176,54 +156,13 @@ void vosk_recognizer_set_max_alternatives(VoskRecognizer *recognizer, int max_al
* "word" : "zero"
* }, {
* "conf" : 1.000000,
* "end" : 2.610000,
* "end" : 2.610000,
* "start" : 2.340000,
* "word" : "one"
* }],
* </pre>
*
* @param words - boolean value
*/
void vosk_recognizer_set_words(VoskRecognizer *recognizer, int words);
/** Accept voice data
*
* accept and process new chunk of voice data
*
* @param data - audio data in PCM 16-bit mono format
* @param length - length of the audio data
* @returns 1 if silence is occured and you can retrieve a new utterance with result method
* 0 if decoding continues
* -1 if exception occured */
int vosk_recognizer_accept_waveform(VoskRecognizer *recognizer, const char *data, int length);
/** Same as above but the version with the short data for language bindings where you have
* audio as array of shorts */
int vosk_recognizer_accept_waveform_s(VoskRecognizer *recognizer, const short *data, int length);
/** Same as above but the version with the float data for language bindings where you have
* audio as array of floats */
int vosk_recognizer_accept_waveform_f(VoskRecognizer *recognizer, const float *data, int length);
/** Returns speech recognition result
*
* @returns the result in JSON format which contains decoded line, decoded
* words, times in seconds and confidences. You can parse this result
* with any json parser
*
* <pre>
* {
* "text" : "what zero zero zero one"
* "text" : "what zero zero zero one"
* }
* </pre>
*
* If alternatives enabled it returns result with alternatives, see also vosk_recognizer_set_alternatives().
*
* If word times enabled returns word time, see also vosk_recognizer_set_word_times().
*/
const char *vosk_recognizer_result(VoskRecognizer *recognizer);
@@ -235,7 +174,7 @@ const char *vosk_recognizer_result(VoskRecognizer *recognizer);
*
* <pre>
* {
* "partial" : "cyril one eight zero"
* "partial" : "cyril one eight zero"
* }
* </pre>
*/
@@ -251,12 +190,6 @@ const char *vosk_recognizer_partial_result(VoskRecognizer *recognizer);
const char *vosk_recognizer_final_result(VoskRecognizer *recognizer);
/** Resets the recognizer
*
* Resets current results so the recognition can continue from scratch */
void vosk_recognizer_reset(VoskRecognizer *recognizer);
/** Releases recognizer object
*
* Underlying model is also unreferenced and if needed released */
@@ -271,20 +204,6 @@ void vosk_recognizer_free(VoskRecognizer *recognizer);
*/
void vosk_set_log_level(int log_level);
/**
* Init, automatically select a CUDA device and allow multithreading.
* Must be called once from the main thread.
* Has no effect if HAVE_CUDA flag is not set.
*/
void vosk_gpu_init();
/**
* Init CUDA device in a multi-threaded environment.
* Must be called for each thread.
* Has no effect if HAVE_CUDA flag is not set.
*/
void vosk_gpu_thread_init();
#ifdef __cplusplus
}
#endif
+5 -1
View File
@@ -8,8 +8,12 @@ application {
repositories {
mavenCentral()
maven {
url 'https://alphacephei.com/maven/'
}
}
dependencies {
implementation group: 'com.alphacephei', name: 'vosk', version: '0.3.75'
implementation group: 'net.java.dev.jna', name: 'jna', version: '5.7.0'
implementation group: 'com.alphacephei', name: 'vosk', version: '0.3.30+'
}
+10 -23
View File
@@ -1,31 +1,18 @@
buildscript {
repositories {
mavenCentral()
}
}
plugins {
id 'java-library'
id 'maven-publish'
id 'com.vanniktech.maven.publish' version '0.18.0'
}
archivesBaseName = 'vosk'
group = 'com.alphacephei'
version = '0.3.30'
repositories {
mavenCentral()
}
archivesBaseName = 'vosk'
group = 'com.alphacephei'
version = '0.3.75'
mavenPublish {
group = 'com.alphacephei'
version = version
sonatypeHost = 's01'
}
dependencies {
api group: 'net.java.dev.jna', name: 'jna', version: '5.18.1'
implementation group: 'net.java.dev.jna', name: 'jna', version: '5.7.0'
testImplementation 'junit:junit:4.13'
}
@@ -58,14 +45,14 @@ publishing {
}
}
}
repositories {
maven {
url = "repo"
}
}
}
test {
dependsOn cleanTest
testLogging.showStandardStreams = true
}
java {
withSourcesJar()
withJavadocJar()
}
+4 -26
View File
@@ -1,6 +1,7 @@
package org.vosk;
import com.sun.jna.Native;
import com.sun.jna.Library;
import com.sun.jna.Platform;
import com.sun.jna.Pointer;
import java.io.File;
@@ -12,9 +13,8 @@ import java.nio.file.StandardCopyOption;
public class LibVosk {
private static void unpackDll(File targetDir, String lib) throws IOException {
try (InputStream source = LibVosk.class.getResourceAsStream("/win32-x86-64/" + lib + ".dll")) {
Files.copy(source, new File(targetDir, lib + ".dll").toPath(), StandardCopyOption.REPLACE_EXISTING);
}
InputStream source = LibVosk.class.getResourceAsStream("/win32-x86-64/" + lib + ".dll");
Files.copy(source, new File(targetDir, lib + ".dll").toPath(), StandardCopyOption.REPLACE_EXISTING);
}
static {
@@ -23,7 +23,7 @@ public class LibVosk {
// We have to unpack dependencies
try {
// To get a tmp folder we unpack small library and mark it for deletion
File tmpFile = Native.extractFromResourcePath("/win32-x86-64/empty", LibVosk.class.getClassLoader());
File tmpFile = Native.extractFromResourcePath("/win32-x86-64/empty");
File tmpDir = tmpFile.getParentFile();
new File(tmpDir, tmpFile.getName() + ".x").createNewFile();
@@ -62,8 +62,6 @@ public class LibVosk {
public static native void vosk_recognizer_set_words(Pointer recognizer, boolean words);
public static native void vosk_recognizer_set_partial_words(Pointer recognizer, boolean partial_words);
public static native void vosk_recognizer_set_spk_model(Pointer recognizer, Pointer spk_model);
public static native boolean vosk_recognizer_accept_waveform(Pointer recognizer, byte[] data, int len);
@@ -78,30 +76,10 @@ public class LibVosk {
public static native String vosk_recognizer_partial_result(Pointer recognizer);
public static native void vosk_recognizer_set_grm(Pointer recognizer, String grammar);
public static native void vosk_recognizer_reset(Pointer recognizer);
public static native void vosk_recognizer_set_endpointer_mode(Pointer recognizer, int mode);
public static native void vosk_recognizer_set_endpointer_delays(Pointer recognizer, float t_start_max, float t_end, float t_max);
public static native void vosk_recognizer_free(Pointer recognizer);
public static native Pointer vosk_text_processor_new(String verbalizer, String tagger);
public static native void vosk_text_processor_free(Pointer processor);
public static native String vosk_text_processor_itn(Pointer processor, String input);
/**
* Set log level for Kaldi messages.
*
* @param loglevel the level
* 0 - default value to print info and error messages but no debug
* less than 0 - don't print info messages
* greater than 0 - more verbose mode
*/
public static void setLogLevel(LogLevel loglevel) {
vosk_set_log_level(loglevel.getValue());
}
+1 -6
View File
@@ -1,18 +1,13 @@
package org.vosk;
import java.io.IOException;
import com.sun.jna.PointerType;
public class Model extends PointerType implements AutoCloseable {
public Model() {
}
public Model(String path) throws IOException {
public Model(String path) {
super(LibVosk.vosk_model_new(path));
if (getPointer() == null) {
throw new IOException("Failed to create a model");
}
}
@Override
+3 -216
View File
@@ -1,163 +1,32 @@
package org.vosk;
import com.sun.jna.PointerType;
import java.io.IOException;
public class Recognizer extends PointerType implements AutoCloseable {
/**
* Creates the recognizer object.
*
* The recognizers process the speech and return text using shared model data
* @param model VoskModel containing static data for recognizer. Model can be
* shared across recognizers, even running in different threads.
* @param sampleRate The sample rate of the audio you are going to feed into the recognizer.
* Make sure this rate matches the audio content, it is a common
* issue causing accuracy problems.
* @throws IOException if the recognizer could not be created
*/
public Recognizer(Model model, float sampleRate) throws IOException {
public Recognizer(Model model, float sampleRate) {
super(LibVosk.vosk_recognizer_new(model, sampleRate));
if (getPointer() == null) {
throw new IOException("Failed to create a recognizer");
}
}
/**
* Creates the recognizer object with speaker recognition.
*
* With the speaker recognition mode the recognizer not just recognize
* text but also return speaker vectors one can use for speaker identification
*
* @param model VoskModel containing static data for recognizer. Model can be
* shared across recognizers, even running in different threads.
* @param sampleRate The sample rate of the audio you are going to feed into the recognizer.
* Make sure this rate matches the audio content, it is a common
* issue causing accuracy problems.
* @param spkModel speaker model for speaker identification
* @throws IOException if the recognizer could not be created
*/
public Recognizer(Model model, float sampleRate, SpeakerModel spkModel) throws IOException {
public Recognizer(Model model, float sampleRate, SpeakerModel spkModel) {
super(LibVosk.vosk_recognizer_new_spk(model.getPointer(), sampleRate, spkModel.getPointer()));
if (getPointer() == null) {
throw new IOException("Failed to create a recognizer");
}
}
/**
* Creates the recognizer object with the phrase list.
*
* Sometimes when you want to improve recognition accuracy and when you don't need
* to recognize large vocabulary you can specify a list of phrases to recognize. This
* will improve recognizer speed and accuracy but might return [unk] if user said
* something different.
*
* Only recognizers with lookahead models support this type of quick configuration.
* Precompiled HCLG graph models are not supported.
*
* @param model VoskModel containing static data for recognizer. Model can be
* shared across recognizers, even running in different threads.
* @param sampleRate The sample rate of the audio you are going to feed into the recognizer.
* Make sure this rate matches the audio content, it is a common
* issue causing accuracy problems.
* @param grammar The string with the list of phrases to recognize as JSON array of strings,
* for example "["one two three four five", "[unk]"]".
* @throws IOException if the recognizer could not be created
*/
public Recognizer(Model model, float sampleRate, String grammar) throws IOException {
public Recognizer(Model model, float sampleRate, String grammar) {
super(LibVosk.vosk_recognizer_new_grm(model.getPointer(), sampleRate, grammar));
if (getPointer() == null) {
throw new IOException("Failed to create a recognizer");
}
}
/**
* Configures recognizer to output n-best results.
*
* <pre>
* {
* "alternatives": [
* { "text": "one two three four five", "confidence": 0.97 },
* { "text": "one two three for five", "confidence": 0.03 },
* ]
* }
* </pre>
*
* @param maxAlternatives - maximum alternatives to return from recognition results
*/
public void setMaxAlternatives(int maxAlternatives) {
LibVosk.vosk_recognizer_set_max_alternatives(this.getPointer(), maxAlternatives);
}
/** Enables words with times in the output
*
* <pre>
* "result" : [{
* "conf" : 1.000000,
* "end" : 1.110000,
* "start" : 0.870000,
* "word" : "what"
* }, {
* "conf" : 1.000000,
* "end" : 1.530000,
* "start" : 1.110000,
* "word" : "zero"
* }, {
* "conf" : 1.000000,
* "end" : 1.950000,
* "start" : 1.530000,
* "word" : "zero"
* }, {
* "conf" : 1.000000,
* "end" : 2.340000,
* "start" : 1.950000,
* "word" : "zero"
* }, {
* "conf" : 1.000000,
* "end" : 2.610000,
* "start" : 2.340000,
* "word" : "one"
* }],
* </pre>
*
* @param words - boolean value
*/
public void setWords(boolean words) {
LibVosk.vosk_recognizer_set_words(this.getPointer(), words);
}
/**
* Like above return words and confidences in partial results.
*
* @param partial_words - boolean value
*/
public void setPartialWords(boolean partial_words) {
LibVosk.vosk_recognizer_set_partial_words(this.getPointer(), partial_words);
}
/**
* Adds speaker model to already initialized recognizer.
*
* Can add speaker recognition model to already created recognizer.
* Helps to initialize speaker recognition for grammar-based recognizer.
*
* @param spkModel Speaker recognition model
*/
public void setSpeakerModel(SpeakerModel spkModel) {
LibVosk.vosk_recognizer_set_spk_model(this.getPointer(), spkModel.getPointer());
}
/**
* Accept and process new chunk of voice data.
*
* @param data - audio data in PCM 16-bit mono format
* @param len - length of the audio data
* @return 1 if silence is occurred and you can retrieve a new utterance with result method
* 0 if decoding continues
* -1 if exception occurred
*/
public boolean acceptWaveForm(byte[] data, int len) {
return LibVosk.vosk_recognizer_accept_waveform(this.getPointer(), data, len);
}
@@ -170,104 +39,22 @@ public class Recognizer extends PointerType implements AutoCloseable {
return LibVosk.vosk_recognizer_accept_waveform_f(this.getPointer(), data, len);
}
/**
* Returns speech recognition result
*
* @return the result in JSON format which contains decoded line, decoded
* words, times in seconds and confidences. You can parse this result
* with any json parser
*
* <pre>
* {
* "text" : "what zero zero zero one"
* }
* </pre>
*
* If alternatives enabled it returns result with alternatives, see also #setMaxAlternatives().
*
* If word times enabled returns word time, see also #setWordTimes().
*/
public String getResult() {
return LibVosk.vosk_recognizer_result(this.getPointer());
}
/**
* Returns partial speech recognition.
*
* @return partial speech recognition text which is not yet finalized.
* result may change as recognizer process more data.
*
* <pre>
* {
* "partial" : "cyril one eight zero"
* }
* </pre>
*/
public String getPartialResult() {
return LibVosk.vosk_recognizer_partial_result(this.getPointer());
}
/**
* Returns speech recognition result. Same as result, but doesn't wait for silence.
* You usually call it in the end of the stream to get final bits of audio. It
* flushes the feature pipeline, so all remaining audio chunks got processed.
*
* @return speech result in JSON format.
*/
public String getFinalResult() {
return LibVosk.vosk_recognizer_final_result(this.getPointer());
}
/**
* Reconfigures recognizer to use grammar.
*
* @param grammar Set of phrases in JSON array of strings or "[]" to use default model graph.
* @see #Recognizer(Model, float, String)
*/
public void setGrammar(String grammar) {
LibVosk.vosk_recognizer_set_grm(this.getPointer(), grammar);
}
/**
* Resets the recognizer.
* Resets current results so the recognition can continue from scratch.
*/
public void reset() {
LibVosk.vosk_recognizer_reset(this.getPointer());
}
/**
* Endpointer delay mode
*/
public class EndpointerMode {
public static final int DEFAULT = 0;
public static final int SHORT = 1;
public static final int LONG = 2;
public static final int VERY_LONG = 3;
}
/**
* Configures endpointer mode for recognizer
*/
public void setEndpointerMode(int mode) {
LibVosk.vosk_recognizer_set_endpointer_mode(this.getPointer(), mode);
}
/**
* Set endpointer delays
*
* @param t_start_max timeout for stopping recognition in case of initial silence (usually around 5.0)
* @param t_end timeout for stopping recognition in milliseconds after we recognized something (usually around 0.5 - 1.0)
* @param t_max timeout for forcing utterance end in milliseconds (usually around 20-30)
**/
public void setEndpointerDelays(float t_start_max, float t_end, float t_max) {
LibVosk.vosk_recognizer_set_endpointer_delays(this.getPointer(), t_start_max, t_end, t_max);
}
/**
* Releases recognizer object.
* Underlying model is also unreferenced and if needed, released.
*/
@Override
public void close() {
LibVosk.vosk_recognizer_free(this.getPointer());
@@ -1,36 +1,13 @@
package org.vosk;
import com.sun.jna.PointerType;
import java.io.IOException;
/**
* Helps to initialize speaker recognition for grammar-based recognizer.
*/
public class SpeakerModel extends PointerType implements AutoCloseable {
public SpeakerModel() {
}
/**
* Loads speaker model data from the file.
*
* The path must contain:
* - a config file: mfcc.conf
* - kaldi nnet: final.ext.raw
* - mean.vec
* - transform.mat
*
* @param path the path of the model on the filesystem
* @throws IOException if the model could not be created
*
* @see <a href="http://kaldi-asr.org/doc/structkaldi_1_1MfccOptions.html">Kaldi MfccOptions</a>
* @see <a href="http://kaldi-asr.org/doc/classkaldi_1_1nnet3_1_1Nnet.html">Kaldi Nnet</a>
*/
public SpeakerModel(String path) throws IOException {
public SpeakerModel(String path) {
super(LibVosk.vosk_spk_model_new(path));
if (getPointer() == null) {
throw new IOException("Failed to create a speaker model");
}
}
@Override
@@ -15,10 +15,8 @@ import javax.sound.sampled.UnsupportedAudioFileException;
import org.vosk.LogLevel;
import org.vosk.Recognizer;
import org.vosk.Recognizer.EndpointerMode;
import org.vosk.LibVosk;
import org.vosk.Model;
import org.vosk.TextProcessor;
public class DecoderTest {
@@ -32,7 +30,6 @@ public class DecoderTest {
recognizer.setMaxAlternatives(10);
recognizer.setWords(true);
recognizer.setPartialWords(true);
int nbytes;
byte[] b = new byte[4096];
@@ -96,25 +93,4 @@ public class DecoderTest {
}
Assert.assertTrue(true);
}
@Test
public void decoderEndpointerDelays() throws IOException, UnsupportedAudioFileException {
try (Model model = new Model("model");
Recognizer recognizer = new Recognizer(model, 16000)) {
recognizer.setEndpointerMode(EndpointerMode.VERY_LONG);
recognizer.setEndpointerDelays(5.0f, 3.0f, 50.0f);
}
Assert.assertTrue(true);
}
@Test(expected = IOException.class)
public void decoderTestException() throws IOException {
Model model = new Model("model_missing");
}
@Test
public void testItn() throws IOException {
TextProcessor p = new TextProcessor("model/itn/en_itn_tagger.fst", "model/itn/en_itn_verbalizer.fst");
System.out.println(p.itn("as easy as one two three"));
}
}
-3
View File
@@ -1,3 +0,0 @@
.gradle/
.idea/
build/
-1
View File
@@ -1 +0,0 @@
Doomsdayrs doomsdayrs@gmail.com
-8
View File
@@ -1,8 +0,0 @@
# Maintenance
To maintain this module, please ensure the following.
1. Kotlin version is kept up to date.
This will be found in the plugins block of the [build.gradle.kts](./build.gradle.kts).
Ensure both multiplatform and serialization have the same value.
2. Ensure dependencies are up to date
3. Ensure that the android min & target sdks are up to date.
-63
View File
@@ -1,63 +0,0 @@
# vosk-api-kotlin
The vosk-api wrapped using Kotlin Multiplatform.
## Usage
The following are ways to use this wrapper.
### JVM
For Java & Android targets.
```kotlin
dependencoes {
val voskVersion = "0.4.0-alpha0"
// Generic
implementation("com.alphacephei:vosk-api-kotlin:$voskVersion")
// Android
implementation("com.alphacephei:vosk-api-kotlin-android:$voskVersion")
}
```
## Building
To build this project, follow the following steps.
1. Install `libvosk`.
This can be done from [source][source install] (parent monorepo)
or [downloaded][download].
2. Download a [Vosk model](https://alphacephei.com/vosk/models) to use.
- It is suggested to use a small model to speed up tests.
3. Once both are downloaded and placed into a proper location (hopefully following UNIX specification).
Set the following environment variables:
- `VOSK_MODEL` to the path of the model.
- `VOSK_PATH` to the path of `libvosk`.
These are used by the various tests to operate.
4. Now that the required steps are complete, one can run `./gradlew build`.
## Kotlin/Native
Currently, the native target is disabled due to a lack of vosk-api packaging on platforms.
Further worsened by the fact that installing the vosk-api on Linux systems is a chore.
First, either install from [source][source install]
or [download][download] and install into the proper unix directories as expected in
[libvosk.def](./src/nativeInterop/cinterop/libvosk.def).
To enable development for Kotlin/Native, do either for the following.
- Add `NATIVE_EXPERIMENT=true` to your environment.
- Go into [build.gradle.kts](./build.gradle.kts) & find `enableNative` & set the right side to true.
Afterwards, when syncing the project, the native source sets will become available to work on.
It is suggested to run `cinteropLibvoskNative` to generate the Kotlin C bindings to work with.
## Future
- Possibly target Kotlin/JS
- Possibly target Kotlin/Objective-C (?)
[source install]: https://alphacephei.com/vosk/install
[download]: https://github.com/alphacep/vosk-api/releases/latest
-204
View File
@@ -1,204 +0,0 @@
import org.jetbrains.dokka.gradle.DokkaTask
import org.jetbrains.kotlin.gradle.ExperimentalKotlinGradlePluginApi
import org.jetbrains.kotlin.gradle.dsl.JvmTarget
/*
* Copyright 2020 Alpha Cephei Inc. & Doomsdayrs
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
plugins {
kotlin("multiplatform") version "2.0.0"
id("com.android.library")
`maven-publish`
id("org.jetbrains.dokka") version "1.9.20"
kotlin("plugin.serialization") version "2.0.0"
}
group = "com.alphacephei"
version = "0.3.75"
repositories {
google()
mavenCentral()
}
val dokkaOutputDir = "$buildDir/dokka"
tasks.getByName<DokkaTask>("dokkaHtml") {
outputDirectory.set(file(dokkaOutputDir))
}
val deleteDokkaOutputDir by tasks.register<Delete>("deleteDokkaOutputDirectory") {
delete(dokkaOutputDir)
}
val javadocJar = tasks.register<Jar>("javadocJar") {
dependsOn(deleteDokkaOutputDir, tasks.dokkaHtml)
archiveClassifier.set("javadoc")
from(dokkaOutputDir)
}
fun org.jetbrains.kotlin.gradle.dsl.KotlinMultiplatformExtension.native(
configure: org.jetbrains.kotlin.gradle.plugin.mpp.KotlinNativeTargetWithHostTests.() -> Unit = {}
) {
when {
org.jetbrains.kotlin.konan.target.HostManager.hostIsMingw -> mingwX64("native")
org.jetbrains.kotlin.konan.target.HostManager.hostIsLinux -> linuxX64("native")
org.jetbrains.kotlin.konan.target.HostManager.hostIsMac -> if (org.jetbrains.kotlin.konan.target.HostManager.hostArch() == "arm64") {
macosArm64("native")
} else {
macosX64("native")
}
else -> error("Unsupported Host OS: ${org.jetbrains.kotlin.konan.target.HostManager.hostOs()}")
}.apply(configure)
}
kotlin {
jvm {
@OptIn(ExperimentalKotlinGradlePluginApi::class)
compilerOptions {
jvmTarget.set(JvmTarget.JVM_17)
}
testRuns["test"].executionTask.configure {
useJUnitPlatform()
environment("MODEL", "VOSK_MODEL")
//environment("MODEL", "/home/doomsdayrs/Downloads/vosk-model-small-en-us-0.15/")
environment("LIBRARY", "VOSK_PATH")
//environment("LIBRARY", "/usr/local/lib64/libvosk/libvosk.so")
environment("AUDIO", "$projectDir/../python/example/test.wav")
}
}
androidTarget {
publishAllLibraryVariants()
}
/**
* If native target should be enabled or not.
*
* Currently disabled as there is no proper packaging distribution currently.
*/
@Suppress("SimplifyBooleanWithConstants") // Ignore, the false is for overrides
val enableNative = System.getenv("NATIVE_EXPERIMENT") == "true" || false
if (enableNative)
native {
val main by compilations.getting
val libvosk by main.cinterops.creating
binaries {
sharedLib()
}
}
@OptIn(ExperimentalKotlinGradlePluginApi::class)
applyDefaultHierarchyTemplate {
withJvm()
withAndroidTarget()
if (enableNative)
withNative()
}
publishing {
publications {
withType<MavenPublication> {
artifact(javadocJar)
pom {
url.set("http://www.alphacephei.com.com/vosk/")
licenses {
license {
name.set("The Apache License, Version 2.0")
url.set("http://www.apache.org/licenses/LICENSE-2.0.txt")
}
}
developers {
developer {
id.set("com.alphacephei")
name.set("Alpha Cephei Inc")
email.set("contact@alphacephei.com")
}
}
scm {
connection.set("scm:git:git://github.com/alphacep/vosk-api.git")
url.set("https://github.com/alphacep/vosk-api/")
}
}
}
}
}
val jna_version = "5.14.0"
val coroutines_version = "1.7.3"
sourceSets {
val commonMain by getting {
dependencies {
api("org.jetbrains.kotlinx:kotlinx-serialization-json:1.7.0")
api("org.jetbrains.kotlinx:kotlinx-coroutines-core:$coroutines_version")
}
}
val commonTest by getting {
dependencies {
implementation(kotlin("test"))
implementation("org.jetbrains.kotlinx:kotlinx-coroutines-test:$coroutines_version")
}
}
val jvmMain by getting {
dependencies {
api("net.java.dev.jna:jna:$jna_version")
}
}
val jvmTest by getting
if (enableNative) {
val nativeMain by getting
}
val androidMain by getting {
dependsOn(jvmMain)
dependencies {
api("net.java.dev.jna:jna:$jna_version@aar")
}
}
val androidUnitTest by getting {
dependencies {
implementation("junit:junit:4.13.2")
}
}
}
}
android {
namespace = "com.alphacephei.library"
compileSdk = 34
sourceSets["main"].manifest.srcFile("src/androidMain/AndroidManifest.xml")
defaultConfig {
minSdk = 24
targetSdk = 34
}
compileOptions {
sourceCompatibility = JavaVersion.VERSION_17
targetCompatibility = JavaVersion.VERSION_17
}
publishing {
multipleVariants {
withSourcesJar()
withJavadocJar()
allVariants()
}
}
}
-31
View File
@@ -1,31 +0,0 @@
/*
* Copyright 2020 Alpha Cephei Inc. & Doomsdayrs
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
pluginManagement {
repositories {
google()
gradlePluginPortal()
mavenCentral()
}
resolutionStrategy {
eachPlugin {
if (requested.id.namespace == "com.android") {
useModule("com.android.tools.build:gradle:8.3.0")
}
}
}
}
rootProject.name = "vosk-api-kotlin"
@@ -1,6 +0,0 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.alphacephei.library">
<uses-permission android:name="android.permission.RECORD_AUDIO" />
</manifest>
@@ -1,46 +0,0 @@
/*
* Copyright 2023 Alpha Cephei Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.vosk.android
/**
* Interface to receive recognition results
*/
interface RecognitionListener {
/**
* Called when partial recognition result is available.
*/
fun onPartialResult(hypothesis: String)
/**
* Called after silence occured.
*/
fun onResult(hypothesis: String)
/**
* Called after stream end.
*/
fun onFinalResult(hypothesis: String)
/**
* Called when an error occurs.
*/
fun onError(exception: Exception)
/**
* Called after timeout expired
*/
fun onTimeout()
}
@@ -1,241 +0,0 @@
/*
* Copyright 2023 Alpha Cephei Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.vosk.android
import android.annotation.SuppressLint
import android.media.AudioFormat
import android.media.AudioRecord
import android.media.MediaRecorder.AudioSource
import android.os.Handler
import android.os.Looper
import org.vosk.Recognizer
import java.io.IOException
import kotlin.math.roundToInt
/**
* Service that records audio in a thread, passes it to a recognizer and emits
* recognition results. Recognition events are passed to a client using
* [RecognitionListener]
*/
class SpeechService @Throws(IOException::class) constructor(
private val recognizer: Recognizer,
sampleRate: Float
) {
private val sampleRate: Int
private val bufferSize: Int
private val recorder: AudioRecord
private var recognizerThread: RecognizerThread? = null
private val mainHandler = Handler(Looper.getMainLooper())
/**
* Creates speech service. Service holds the AudioRecord object, so you
* need to call [.shutdown] in order to properly finalize it.
*
* @throws IOException thrown if audio recorder can not be created for some reason.
*/
init {
this.sampleRate = sampleRate.toInt()
bufferSize = (this.sampleRate * BUFFER_SIZE_SECONDS).roundToInt()
@SuppressLint("MissingPermission")
recorder = AudioRecord(
AudioSource.VOICE_RECOGNITION, this.sampleRate,
AudioFormat.CHANNEL_IN_MONO,
AudioFormat.ENCODING_PCM_16BIT, bufferSize * 2
)
if (recorder.state == AudioRecord.STATE_UNINITIALIZED) {
recorder.release()
throw IOException(
"Failed to initialize recorder. Microphone might be already in use."
)
}
}
/**
* Starts recognition. Does nothing if recognition is active.
*
* @return true if recognition was actually started
*/
fun startListening(listener: RecognitionListener): Boolean {
if (null != recognizerThread) return false
recognizerThread = RecognizerThread(listener)
recognizerThread!!.start()
return true
}
/**
* Starts recognition. After specified timeout listening stops and the
* endOfSpeech signals about that. Does nothing if recognition is active.
*
*
* timeout - timeout in milliseconds to listen.
*
* @return true if recognition was actually started
*/
fun startListening(listener: RecognitionListener, timeout: Int): Boolean {
if (null != recognizerThread) return false
recognizerThread = RecognizerThread(listener, timeout)
recognizerThread!!.start()
return true
}
private fun stopRecognizerThread(): Boolean {
if (null == recognizerThread) return false
try {
recognizerThread!!.interrupt()
recognizerThread!!.join()
} catch (e: InterruptedException) {
// Restore the interrupted status.
Thread.currentThread().interrupt()
}
recognizerThread = null
return true
}
/**
* Stops recognition. Listener should receive final result if there is
* any. Does nothing if recognition is not active.
*
* @return true if recognition was actually stopped
*/
fun stop(): Boolean {
return stopRecognizerThread()
}
/**
* Cancel recognition. Do not post any new events, simply cancel processing.
* Does nothing if recognition is not active.
*
* @return true if recognition was actually stopped
*/
fun cancel(): Boolean {
if (recognizerThread != null) {
recognizerThread!!.setPause(true)
}
return stopRecognizerThread()
}
/**
* Shutdown the recognizer and release the recorder
*/
fun shutdown() {
recorder.release()
}
fun setPause(paused: Boolean) {
if (recognizerThread != null) {
recognizerThread!!.setPause(paused)
}
}
/**
* Resets recognizer in a thread, starts recognition over again
*/
fun reset() {
if (recognizerThread != null) {
recognizerThread!!.reset()
}
}
private inner class RecognizerThread @JvmOverloads constructor(
var listener: RecognitionListener,
timeout: Int = Companion.NO_TIMEOUT
) : Thread() {
private var remainingSamples: Int
private val timeoutSamples: Int
@Volatile
private var paused = false
@Volatile
private var reset = false
init {
timeoutSamples = if (timeout != Companion.NO_TIMEOUT) {
timeout * sampleRate / 1000
} else {
Companion.NO_TIMEOUT
}
remainingSamples = timeoutSamples
}
/**
* When we are paused, don't process audio by the recognizer and don't emit
* any listener results
*
* @param paused the status of pause
*/
fun setPause(paused: Boolean) {
this.paused = paused
}
/**
* Set reset state to signal reset of the recognizer and start over
*/
fun reset() {
reset = true
}
override fun run() {
recorder.startRecording()
if (recorder.recordingState == AudioRecord.RECORDSTATE_STOPPED) {
recorder.stop()
val ioe = IOException(
"Failed to start recording. Microphone might be already in use."
)
mainHandler.post { listener.onError(ioe) }
}
val buffer = ShortArray(bufferSize)
while (!interrupted()
&& (timeoutSamples == Companion.NO_TIMEOUT || remainingSamples > 0)
) {
val nread = recorder.read(buffer, 0, buffer.size)
if (paused) {
continue
}
if (reset) {
recognizer.reset()
reset = false
}
if (nread < 0) throw RuntimeException("error reading audio buffer")
if (recognizer.acceptWaveform(buffer)) {
val result = recognizer.result
mainHandler.post { listener.onResult(result) }
} else {
val partialResult = recognizer.partialResult
mainHandler.post { listener.onPartialResult(partialResult) }
}
if (timeoutSamples != NO_TIMEOUT) {
remainingSamples -= nread
}
}
recorder.stop()
if (!paused) {
// If we met timeout signal that speech ended
if (timeoutSamples != NO_TIMEOUT && remainingSamples <= 0) {
mainHandler.post { listener.onTimeout() }
} else {
val finalResult = recognizer.finalResult
mainHandler.post { listener.onFinalResult(finalResult) }
}
}
}
}
companion object {
private const val NO_TIMEOUT = -1
private const val BUFFER_SIZE_SECONDS = 0.2f
}
}
@@ -1,151 +0,0 @@
/*
* Copyright 2023 Alpha Cephei Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.vosk.android
import android.os.Handler
import android.os.Looper
import org.vosk.Recognizer
import java.io.IOException
import java.io.InputStream
import kotlin.math.roundToInt
/**
* Service that recognizes stream audio in a thread, passes it to a recognizer and emits
* recognition results. Recognition events are passed to a client using
* [RecognitionListener]
*/
class SpeechStreamService(
private val recognizer: Recognizer,
inputStream: InputStream,
sampleRate: Float
) {
private val inputStream: InputStream
private val sampleRate: Int
private val bufferSize: Int
private var recognizerThread: Thread? = null
private val mainHandler = Handler(Looper.getMainLooper())
/**
* Creates speech service.
*/
init {
this.sampleRate = sampleRate.toInt()
this.inputStream = inputStream
bufferSize = (this.sampleRate * BUFFER_SIZE_SECONDS * 2).roundToInt()
}
/**
* Starts recognition. Does nothing if recognition is active.
*
* @return true if recognition was actually started
*/
fun start(listener: RecognitionListener): Boolean {
if (null != recognizerThread) return false
recognizerThread = RecognizerThread(listener)
recognizerThread!!.start()
return true
}
/**
* Starts recognition. After specified timeout listening stops and the
* endOfSpeech signals about that. Does nothing if recognition is active.
*
*
* timeout - timeout in milliseconds to listen.
*
* @return true if recognition was actually started
*/
fun start(listener: RecognitionListener, timeout: Int): Boolean {
if (null != recognizerThread) return false
recognizerThread = RecognizerThread(listener, timeout)
recognizerThread!!.start()
return true
}
/**
* Stops recognition. All listeners should receive final result if there is
* any. Does nothing if recognition is not active.
*
* @return true if recognition was actually stopped
*/
fun stop(): Boolean {
if (null == recognizerThread) return false
try {
recognizerThread!!.interrupt()
recognizerThread!!.join()
} catch (e: InterruptedException) {
// Restore the interrupted status.
Thread.currentThread().interrupt()
}
recognizerThread = null
return true
}
private inner class RecognizerThread @JvmOverloads constructor(
var listener: RecognitionListener,
timeout: Int = Companion.NO_TIMEOUT
) : Thread() {
private var remainingSamples: Int
private val timeoutSamples: Int
init {
if (timeout != Companion.NO_TIMEOUT) timeoutSamples =
timeout * sampleRate / 1000 else timeoutSamples = Companion.NO_TIMEOUT
remainingSamples = timeoutSamples
}
override fun run() {
val buffer = ByteArray(bufferSize)
while (!interrupted()
&& (timeoutSamples == Companion.NO_TIMEOUT || remainingSamples > 0)
) {
try {
val nread = inputStream.read(buffer, 0, buffer.size)
if (nread < 0) {
break
} else {
val isSilence: Boolean = recognizer.acceptWaveform(buffer)
if (isSilence) {
val result = recognizer.result
mainHandler.post { listener.onResult(result) }
} else {
val partialResult = recognizer.partialResult
mainHandler.post { listener.onPartialResult(partialResult) }
}
}
if (timeoutSamples != NO_TIMEOUT) {
remainingSamples -= nread
}
} catch (e: IOException) {
mainHandler.post { listener.onError(e) }
}
}
// If we met timeout signal that speech ended
if (timeoutSamples != NO_TIMEOUT && remainingSamples <= 0) {
mainHandler.post { listener.onTimeout() }
} else {
val finalResult = recognizer.finalResult
mainHandler.post { listener.onFinalResult(finalResult) }
}
}
}
companion object {
private const val NO_TIMEOUT = -1
private const val BUFFER_SIZE_SECONDS = 0.2f
}
}
@@ -1,138 +0,0 @@
/*
* Copyright 2023 Alpha Cephei Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.vosk.android
import android.content.Context
import android.content.res.AssetManager
import android.os.Environment
import android.os.Handler
import android.os.Looper
import android.util.Log
import org.vosk.Model
import java.io.*
import java.util.concurrent.Executor
import java.util.concurrent.Executors
import java.util.function.Consumer
/**
* Provides utility methods to sync model files to external storage to allow
* C++ code access them. Relies on file named "uuid" to track updates.
*/
object StorageService {
private val TAG = StorageService::class.simpleName
@JvmStatic
fun unpack(
context: Context,
sourcePath: String,
targetPath: String,
completeCallback: Consumer<Model>,
errorCallback: Consumer<IOException>
) {
val executor: Executor =
Executors.newSingleThreadExecutor() // change according to your requirements
val handler = Handler(Looper.getMainLooper())
executor.execute {
try {
val outputPath = sync(context, sourcePath, targetPath)
val model = Model(outputPath)
handler.post { completeCallback.accept(model) }
} catch (e: IOException) {
handler.post { errorCallback.accept(e) }
}
}
}
@JvmStatic
@Throws(IOException::class)
fun sync(context: Context, sourcePath: String, targetPath: String): String {
val assetManager = context.assets
val externalFilesDir = context.getExternalFilesDir(null)
?: throw IOException(
"cannot get external files dir, "
+ "external storage state is " + Environment.getExternalStorageState()
)
val targetDir = File(externalFilesDir, targetPath)
val resultPath = File(targetDir, sourcePath).absolutePath
val sourceUUID = readLine(assetManager.open("$sourcePath/uuid"))
try {
val targetUUID = readLine(FileInputStream(File(targetDir, "$sourcePath/uuid")))
if (targetUUID == sourceUUID) return resultPath
} catch (e: FileNotFoundException) {
// ignore
}
deleteContents(targetDir)
copyAssets(assetManager, sourcePath, targetDir)
// Copy uuid
copyFile(assetManager, "$sourcePath/uuid", targetDir)
return resultPath
}
@Throws(IOException::class)
private fun readLine(inputStream: InputStream): String {
return BufferedReader(InputStreamReader(inputStream)).use { it.readLine() }
}
private fun deleteContents(dir: File): Boolean {
val files = dir.listFiles()
var success = true
if (files != null) {
for (file in files) {
if (file.isDirectory) {
success = success and deleteContents(file)
}
if (!file.delete()) {
success = false
}
}
}
return success
}
@Throws(IOException::class)
private fun copyAssets(assetManager: AssetManager, path: String, outPath: File) {
val assets = assetManager.list(path) ?: return
if (assets.isEmpty()) {
if (!path.endsWith("uuid")) copyFile(assetManager, path, outPath)
} else {
val dir = File(outPath, path)
if (!dir.exists()) {
Log.v(TAG, "Making directory " + dir.absolutePath)
if (!dir.mkdirs()) {
Log.v(TAG, "Failed to create directory " + dir.absolutePath)
}
}
for (asset in assets) {
copyAssets(assetManager, "$path/$asset", outPath)
}
}
}
@Throws(IOException::class)
private fun copyFile(assetManager: AssetManager, fileName: String, outPath: File) {
Log.v(TAG, "Copy $fileName to $outPath")
assetManager.open(fileName).use { inputStream ->
FileOutputStream("$outPath/$fileName").use { out ->
val buffer = ByteArray(4000)
var read: Int
while (inputStream.read(buffer).also { read = it } != -1) {
out.write(buffer, 0, read)
}
}
}
}
}
@@ -1,22 +0,0 @@
/*
* Copyright 2023 Alpha Cephei Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.vosk
/**
* Thrown when a [Recognizer] cannot accept a given waveform
*/
class AcceptWaveformException(data: Any) : Exception("Could not accept waveform: $data")
@@ -1,38 +0,0 @@
/*
* Copyright 2023 Alpha Cephei Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.vosk
import org.vosk.exception.ModelException
/**
* Batch model object
*
* @since 26 / 12 / 2022
* @constructor Creates the batch recognizer object
*/
expect class BatchModel @Throws(ModelException::class) constructor(path: String) : Freeable {
/**
* Releases batch model object
*/
override fun free()
/**
* Wait for the processing
*/
fun await()
}
@@ -1,62 +0,0 @@
/*
* Copyright 2023 Alpha Cephei Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.vosk
/**
* Batch recognizer object
*
* @since 26 / 12 / 2022
* @constructor Creates batch recognizer object
*/
expect class BatchRecognizer constructor(model: BatchModel, sampleRate: Float) : Freeable {
/**
* Releases batch recognizer object
*/
override fun free()
/**
* Accept batch voice data
*/
fun acceptWaveform(data: ByteArray)
/**
* Set NLSML output
* @param nlsml - boolean value
*/
fun setNLSML(nlsml: Boolean)
/**
* Closes the stream
*/
fun finishStream()
/**
* Return results
*/
val frontResult: String
/**
* Release and free first retrieved result
*/
fun pop()
/**
* Get amount of pending chunks for more intelligent waiting
*/
val pendingChunks: Int
}
@@ -1,27 +0,0 @@
/*
* Copyright 2024 Alpha Cephei Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.vosk
/**
* VoskEpMode
*/
enum class EndPointerMode {
ANSWER_DEFAULT,
ANSWER_SHORT,
ANSWER_LONG,
ANSWER_VERY_LONG
}
@@ -1,31 +0,0 @@
/*
* Copyright 2023 Alpha Cephei Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.vosk
/**
* Denotes an object that must be freed afterwards.
*
* On JVM, This is done via AutoClosable.
*/
@Suppress("SpellCheckingInspection")
interface Freeable {
/**
* Dereference the related object
*/
fun free()
}
@@ -1,40 +0,0 @@
/*
* Copyright 2023 Alpha Cephei Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.vosk
/**
* Log level for Kaldi messages.
*
* @since 26 / 12 / 2022
*/
enum class LogLevel(val value: Int) {
/**
* Don't print info messages
*/
WARNINGS(-1),
/**
* Default value to print info and error messages but no debug
*/
INFO(0),
/**
* More verbose mode
*/
DEBUG(1);
}
@@ -1,52 +0,0 @@
/*
* Copyright 2023 Alpha Cephei Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.vosk
import org.vosk.exception.IOException
/**
* Model stores all the data required for recognition
*
* It contains static data and can be shared across processing
* threads.
*
* @since 26 / 12 / 2022
* @constructor Loads model data from the file and returns the model object
* @param path the path of the model on the filesystem
* @throws IOException if the path provided is invalid
*/
expect class Model @Throws(IOException::class) constructor(path: String) : Freeable {
/**
* Check if a word can be recognized by the model
* @param word: the word
* @returns the word symbol if @param word exists inside the model
* or -1 otherwise.
* Reminding that word symbol 0 is for <epsilon>
*/
fun findWord(word: String): Int
/**
* Releases the model memory
*
* The model object is reference-counted so if some recognizer
* depends on this model, model might still stay alive. When
* last recognizer is released, model will be released too.
*/
override fun free()
}
@@ -1,273 +0,0 @@
/*
* Copyright 2023 Alpha Cephei Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.vosk
import org.vosk.exception.RecognizerException
/**
* Recognizer object is the main object which processes data.
*
* Each recognizer usually runs in own thread and takes audio as input.
* Once audio is processed recognizer returns JSON object as a string
* which represent decoded information - words, confidences, times, n-best lists,
* speaker information and so on
*
* @since 26 / 12 / 2022
*/
expect class Recognizer : Freeable {
/**
* Creates the recognizer object
*
* The recognizers process the speech and return text using shared model data
* @param model VoskModel containing static data for recognizer. Model can be
* shared across recognizers, even running in different threads.
* @param sampleRate The sample rate of the audio you going to feed into the recognizer.
* Make sure this rate matches the audio content, it is a common
* issue causing accuracy problems.
* @throws RecognizerException if a problem occurred
*/
@Throws(RecognizerException::class)
constructor(model: Model, sampleRate: Float)
/**
* Creates the recognizer object with speaker recognition
*
* With the speaker recognition mode the recognizer not just recognize
* text but also return speaker vectors one can use for speaker identification
*
* @param model VoskModel containing static data for recognizer. Model can be
* shared across recognizers, even running in different threads.
* @param sampleRate The sample rate of the audio you going to feed into the recognizer.
* Make sure this rate matches the audio content, it is a common
* issue causing accuracy problems.
* @param speakerModel speaker model for speaker identification
* @throws RecognizerException if a problem occurred
*/
@Throws(RecognizerException::class)
constructor(model: Model, sampleRate: Float, speakerModel: SpeakerModel)
/**
* Creates the recognizer object with the phrase list
*
* Sometimes when you want to improve recognition accuracy and when you don't need
* to recognize large vocabulary you can specify a list of phrases to recognize. This
* will improve recognizer speed and accuracy but might return [unk] if user said
* something different.
*
* Only recognizers with lookahead models support this type of quick configuration.
* Precompiled HCLG graph models are not supported.
*
* @param model VoskModel containing static data for recognizer. Model can be
* shared across recognizers, even running in different threads.
* @param sampleRate The valuesample rate of the audio you going to feed into the recognizer.
* Make sure this rate matches the audio content, it is a common
* issue causing accuracy problems.
* @param grammar The string with the list of phrases to recognize as JSON array of strings,
* for example "["one two three four five", "[unk]"]".
*
* @throws RecognizerException if a problem occurred
*/
@Throws(RecognizerException::class)
constructor(model: Model, sampleRate: Float, grammar: String)
/**
* Adds speaker model to already initialized recognizer
*
* Can add speaker recognition model to already created recognizer. Helps to initialize
* speaker recognition for grammar-based recognizer.
*
* @param speakerModel Speaker recognition model
*/
fun setSpeakerModel(speakerModel: SpeakerModel)
/**
* Reconfigures recognizer to use grammar
*
* @param grammar Set of phrases in JSON array of strings or "[]" to use default model graph.
* See also vosk_recognizer_new_grm
*/
fun setGrammar(grammar: String)
/**
* Configures recognizer to output n-best results
*
* <pre>
* {
* "alternatives": [
* { "text": "one two three four five", "confidence": 0.97 },
* { "text": "one two three for five", "confidence": 0.03 },
* ]
* }
* </pre>
*
* @param maxAlternatives - maximum alternatives to return from recognition results
*/
fun setMaxAlternatives(maxAlternatives: Int)
/**
* Enables words with times in the output
*
* <pre>
* "result" : [{
* "conf" : 1.000000,
* "end" : 1.110000,
* "start" : 0.870000,
* "word" : "what"
* }, {
* "conf" : 1.000000,
* "end" : 1.530000,
* "start" : 1.110000,
* "word" : "zero"
* }, {
* "conf" : 1.000000,
* "end" : 1.950000,
* "start" : 1.530000,
* "word" : "zero"
* }, {
* "conf" : 1.000000,
* "end" : 2.340000,
* "start" : 1.950000,
* "word" : "zero"
* }, {
* "conf" : 1.000000,
* "end" : 2.610000,
* "start" : 2.340000,
* "word" : "one"
* }],
* </pre>
*
* C equivalent = vosk_recognizer_set_words
* @param words - boolean value
*/
fun setOutputWordTimes(words: Boolean)
/**
* Like [setOutputWordTimes] return words and confidences in partial results
*
* @param partialWords - boolean value
*/
fun setPartialWords(partialWords: Boolean)
/**
* Set NLSML output
* @param nlsml - boolean value
*/
fun setNLSML(nlsml: Boolean)
/**
* Set endpointer scaling factor
*
* @param mode Endpointer mode
**/
fun setEndPointerMode(mode: EndPointerMode)
/**
* Set endpointer delays
*
* @param tStartMax timeout for stopping recognition in case of initial silence (usually around 5.0)
* @param tEnd timeout for stopping recognition in milliseconds after we recognized something (usually around 0.5 - 1.0)
* @param tMax timeout for forcing utterance end in milliseconds (usually around 20-30)
**/
fun setEndPointerDelays(tStartMax: Float, tEnd: Float, tMax: Float)
/**
* Accept voice data
*
* accept and process new chunk of voice data
*
* @param data Audio data in PCM 16-bit mono format.
* @param length Length of the audio data.
* @returns
* 1 - If silence is occurred and you can retrieve a new utterance with result method
* 0 - If decoding continues
* -1 - If exception occurred
*/
@Throws(AcceptWaveformException::class)
fun acceptWaveform(data: ByteArray): Boolean
/**
* Same as [acceptWaveform] but the version with the short data for language bindings where you have
* audio as array of shorts
*/
@Throws(AcceptWaveformException::class)
fun acceptWaveform(data: ShortArray): Boolean
/**
* Same as [acceptWaveform] but the version with the float data for language bindings where you have
* audio as array of floats
*/
@Throws(AcceptWaveformException::class)
fun acceptWaveform(data: FloatArray): Boolean
/**
* Returns speech recognition result
*
* @returns the result in JSON format which contains decoded line, decoded
* words, times in seconds and confidences. You can parse this result
* with any json parser
*
* <pre>
* {
* "text" : "what zero zero zero one"
* }
* </pre>
*
* If alternatives enabled it returns result with alternatives, see also [setMaxAlternatives].
*
* If word times enabled returns word time, see also [setOutputWordTimes].
*/
val result: String
/**
* Returns partial speech recognition
*
* @returns partial speech recognition text which is not yet finalized.
* result may change as recognizer process more data.
*
* <pre>
* {
* "partial" : "cyril one eight zero"
* }
* </pre>
*/
val finalResult: String
/**
* Returns speech recognition result. Same as result, but doesn't wait for silence
* You usually call it in the end of the stream to get final bits of audio. It
* flushes the feature pipeline, so all remaining audio chunks got processed.
*
* @returns speech result in JSON format.
*/
val partialResult: String
/**
* Resets the recognizer
*
* Resets current results so the recognition can continue from scratch
*/
fun reset()
/**
* Releases recognizer object
*
* Underlying model is also unreferenced and if needed released
*/
override fun free()
}
@@ -1,40 +0,0 @@
/*
* Copyright 2023 Alpha Cephei Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.vosk
import org.vosk.exception.ModelException
/**
* Speaker model is the same as model but contains the data
* for speaker identification.
*
* @since 26 / 12 / 2022
* @constructor Loads speaker model data from the file and returns the model object
* @param path the path of the model on the filesystem
* @throws ModelException if the path provided is invalid
*/
expect class SpeakerModel @Throws(ModelException::class) constructor(path: String) : Freeable {
/**
* Releases the model memory
*
* The model object is reference-counted so if some recognizer
* depends on this model, model might still stay alive. When
* last recognizer is released, model will be released too.
*/
override fun free()
}
@@ -1,32 +0,0 @@
/*
* Copyright 2024 Alpha Cephei Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.vosk
/**
* Inverse text normalization
*
* @since 2024/06/19
* @constructor Create text processor
*/
expect class TextProcessor constructor(tagger: Char, verbalizer: Char) : Freeable {
/** Release text processor */
override fun free()
/** Convert string */
fun itn(input: Char): Char
}
@@ -1,46 +0,0 @@
/*
* Copyright 2023 Alpha Cephei Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.vosk
/**
* 26 / 12 / 2022
*
* Control overarching features of libvosk.
*/
expect object Vosk {
/**
* Set log level for Kaldi messages
*
* @param logLevel the level
*/
fun setLogLevel(logLevel: LogLevel)
/**
* Init, automatically select a CUDA device and allow multithreading.
* Must be called once from the main thread.
* Has no effect if HAVE_CUDA flag is not set.
*/
fun gpuInit()
/**
* Init CUDA device in a multi-threaded environment.
* Must be called for each thread.
* Has no effect if HAVE_CUDA flag is not set.
*/
fun gpuThreadInit()
}
@@ -1,22 +0,0 @@
/*
* Copyright 2023 Alpha Cephei Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.vosk.exception
/**
* Internal common IO exception. On JVM this is just a type alias.
*/
expect open class IOException(message: String?) : Exception
@@ -1,22 +0,0 @@
/*
* Copyright 2023 Alpha Cephei Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.vosk.exception
/**
* Thrown when there is an exception creating a model.
*/
class ModelException(path: String): IOException("Failed to find model: $path")
@@ -1,22 +0,0 @@
/*
* Copyright 2023 Alpha Cephei Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.vosk.exception
/**
* Thrown when the recognizer fails to be created
*/
class RecognizerException: IOException("Failed to create recognizer.")
@@ -1,31 +0,0 @@
/*
* Copyright 2023 Alpha Cephei Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.vosk.json
import kotlinx.serialization.Serializable
/**
* Represents an alternative result.
*
* @since 26 / 12 / 2022
*/
@Serializable
data class Alternative(
val confidence: Double,
val result: List<Result> = emptyList(),
val text: String
)
@@ -1,61 +0,0 @@
/*
* Copyright 2023 Alpha Cephei Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.vosk.json
import kotlinx.serialization.decodeFromString
import kotlinx.serialization.json.Json
import kotlinx.serialization.json.encodeToJsonElement
import org.vosk.Recognizer
import org.vosk.Model
import org.vosk.exception.RecognizerException
/*
* 26 / 12 / 2022
*/
/**
* Vosk JSON encoder
*/
val voskJson = Json { encodeDefaults = true }
/**
* Get the result as a JSON object
*/
fun Recognizer.resultAsJson(): ResultOutput =
voskJson.decodeFromString(result)
/**
* Get the final result as a JSON object
*/
fun Recognizer.finalResultAsJson(): ResultOutput =
voskJson.decodeFromString(finalResult)
/**
* Get the partial result as a JSON object
*/
fun Recognizer.partialResultAsJson(): PartialResultOutput =
voskJson.decodeFromString(partialResult)
/**
* Create a [Recognizer], but using a list for grammar instead.
*
* The grammar list is converted into a JSON array
*/
@Throws(RecognizerException::class)
fun Recognizer(model: Model, sampleRate: Float, grammar: List<String>) =
Recognizer(model, sampleRate, voskJson.encodeToJsonElement(grammar).toString())
@@ -1,32 +0,0 @@
/*
* Copyright 2023 Alpha Cephei Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.vosk.json
import kotlinx.serialization.SerialName
import kotlinx.serialization.Serializable
/**
* Represents a partial result
*
* @since 26 / 12 / 2022
*/
@Serializable
data class PartialResultOutput(
val partial: String,
@SerialName("partial_result")
val partialResult: List<Result> = emptyList(),
)
@@ -1,32 +0,0 @@
/*
* Copyright 2023 Alpha Cephei Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.vosk.json
import kotlinx.serialization.Serializable
/**
* Represents a result for any given word.
*
* @since 26 / 12 / 2022
*/
@Serializable
data class Result(
val conf: Double? = null,
val end: Double,
val start: Double,
val word: String,
)
@@ -1,31 +0,0 @@
/*
* Copyright 2023 Alpha Cephei Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.vosk.json
import kotlinx.serialization.Serializable
/**
* Result output
*
* @since 26 / 12 / 2022
*/
@Serializable
data class ResultOutput(
val alternatives: List<Alternative> = emptyList(),
val result: List<Result> = emptyList(),
val text: String? = null
)
@@ -1,38 +0,0 @@
/*
* Copyright 2023 Alpha Cephei Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.vosk.json
/**
* For extension functions transforming (input streams->recognizers) into flows.
*/
sealed interface WaveformResult {
/**
* A result made after a period of silence.
*/
data class Result(val result: ResultOutput) : WaveformResult
/**
* A partial result that is being made in progress.
*/
data class PartialResult(val result: PartialResultOutput) : WaveformResult
/**
* A final result made after there is no more content to feed.
*/
data class FinalResult(val result: ResultOutput) : WaveformResult
}
@@ -1,83 +0,0 @@
/*
* Copyright 2023 Alpha Cephei Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.vosk
import com.sun.jna.PointerType
import org.vosk.exception.ModelException
import java.io.File
import java.nio.file.Path
import kotlin.io.path.absolutePathString
/**
* Batch model object
*
* @since 26 / 12 / 2022
*/
actual class BatchModel : Freeable, PointerType, AutoCloseable {
/**
* Empty constructor for JNA
*/
constructor()
/**
* Creates the batch recognizer object
*/
@Throws(ModelException::class)
actual constructor(path: String) : super(
LibVosk.vosk_batch_model_new(path) ?: throw ModelException(path)
)
/**
* Constructor using a Path, will retrieve absolutePath
*
* @param path to batch model
*/
@Throws(ModelException::class)
constructor(path: Path) : this(path.absolutePathString())
/**
* Constructor using a File, will retrieve absolutePath
*
* @param file to batch model
*/
@Throws(ModelException::class)
constructor(file: File) : this(file.absolutePath)
/**
* Releases batch model object
*/
actual override fun free() {
LibVosk.vosk_batch_model_free(this)
}
/**
* Wait for the processing
*/
actual fun await() {
LibVosk.vosk_batch_model_wait(this)
}
/**
* @see free
*/
override fun close() {
free()
}
}
@@ -1,95 +0,0 @@
/*
* Copyright 2023 Alpha Cephei Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.vosk
import com.sun.jna.PointerType
/**
* Batch recognizer object
*
* @since 26 / 12 / 2022
* @constructor Creates batch recognizer object
*/
actual class BatchRecognizer : Freeable, PointerType, AutoCloseable {
/**
* Empty constructor for JNA
*/
constructor()
/**
* Creates batch recognizer object
*/
actual constructor(model: BatchModel, sampleRate: Float) :
super(LibVosk.vosk_batch_recognizer_new(model, sampleRate))
/**
* Releases batch recognizer object
*/
actual override fun free() {
LibVosk.vosk_batch_recognizer_free(this);
}
/**
* Accept batch voice data
*/
actual fun acceptWaveform(data: ByteArray) {
LibVosk.vosk_batch_recognizer_accept_waveform(this, data, data.size)
}
/**
* Set NLSML output
* @param nlsml - boolean value
*/
actual fun setNLSML(nlsml: Boolean) {
LibVosk.vosk_batch_recognizer_set_nlsml(this, nlsml)
}
/**
* Closes the stream
*/
actual fun finishStream() {
LibVosk.vosk_batch_recognizer_finish_stream(this)
}
/**
* Return results
*/
actual val frontResult: String
get() = LibVosk.vosk_batch_recognizer_front_result(this)
/**
* Release and free first retrieved result
*/
actual fun pop() {
LibVosk.vosk_batch_recognizer_pop(this)
}
/**
* Get amount of pending chunks for more intelligent waiting
*/
actual val pendingChunks: Int
get() = LibVosk.vosk_batch_recognizer_get_pending_chunks(this)
/**
* @see free
*/
override fun close() {
free()
}
}
@@ -1,213 +0,0 @@
/*
* Copyright 2023 Alpha Cephei Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.vosk
import com.sun.jna.Native
import com.sun.jna.Platform
import com.sun.jna.Pointer
import java.io.File
import java.io.IOException
import java.io.InputStream
import java.nio.file.Files
import java.nio.file.StandardCopyOption
/**
* 26 / 12 / 2022
*/
@Suppress("FunctionName")
internal object LibVosk {
@JvmStatic
@Deprecated(
"LibVosk is now for internal JNA, use Vosk instead",
ReplaceWith("Vosk.setLogLevel(logLevel)", "org.vosk.Vosk")
)
fun setLogLevel(logLevel: LogLevel) {
Vosk.setLogLevel(logLevel)
}
@Throws(IOException::class)
private fun unpackDll(targetDir: File, lib: String) {
Vosk::class.java.getResourceAsStream("/win32-x86-64/$lib.dll")!!.use {
Files.copy(
it,
File(targetDir, "$lib.dll").toPath(),
StandardCopyOption.REPLACE_EXISTING
)
}
}
init {
when {
Platform.isAndroid() -> {
Native.register(LibVosk::class.java, "vosk")
}
Platform.isWindows() -> {
// We have to unpack dependencies
try {
// To get a tmp folder we unpack small library and mark it for deletion
val tmpFile: File = Native.extractFromResourcePath(
"/win32-x86-64/empty",
LibVosk::class.java.classLoader
)
val tmpDir = tmpFile.parentFile!!
File(tmpDir, tmpFile.name + ".x").createNewFile()
// Now unpack dependencies
unpackDll(tmpDir, "libwinpthread-1");
unpackDll(tmpDir, "libgcc_s_seh-1");
unpackDll(tmpDir, "libstdc++-6");
} catch (e: IOException) {
// Nothing for now, it will fail on next step
} finally {
Native.register(LibVosk::class.java, "libvosk");
}
}
else -> {
Native.register(LibVosk::class.java, "vosk");
}
}
}
external fun vosk_model_new(path: String): Pointer?
external fun vosk_model_free(model: Model)
external fun vosk_model_find_word(model: Model, word: String): Int
external fun vosk_spk_model_new(path: String): Pointer?
external fun vosk_spk_model_free(model: SpeakerModel)
external fun vosk_recognizer_new(model: Model, sampleRate: Float): Pointer?
external fun vosk_recognizer_new_spk(
model: Model,
sampleRate: Float,
spkModel: SpeakerModel
): Pointer?
external fun vosk_recognizer_new_grm(
model: Model,
sampleRate: Float,
grammar: String?
): Pointer?
external fun vosk_recognizer_set_spk_model(recognizer: Recognizer, spk_model: SpeakerModel)
external fun vosk_recognizer_set_grm(recognizer: Recognizer, grammar: String)
external fun vosk_recognizer_set_max_alternatives(recognizer: Recognizer, maxAlternatives: Int)
external fun vosk_recognizer_set_words(recognizer: Recognizer, words: Boolean)
external fun vosk_recognizer_set_partial_words(recognizer: Recognizer, partial_words: Boolean)
external fun vosk_recognizer_set_nlsml(recognizer: Recognizer, nlsml: Boolean)
external fun vosk_recognizer_accept_waveform(
recognizer: Recognizer,
data: ByteArray?,
len: Int
): Int
external fun vosk_recognizer_accept_waveform_s(
recognizer: Recognizer,
data: ShortArray?,
len: Int
): Int
external fun vosk_recognizer_accept_waveform_f(
recognizer: Recognizer,
data: FloatArray?,
len: Int
): Int
external fun vosk_recognizer_result(recognizer: Recognizer): String
external fun vosk_recognizer_final_result(recognizer: Recognizer): String
external fun vosk_recognizer_partial_result(recognizer: Recognizer): String
external fun vosk_recognizer_reset(recognizer: Recognizer)
external fun vosk_recognizer_free(recognizer: Recognizer)
external fun vosk_set_log_level(level: Int)
external fun vosk_gpu_init()
external fun vosk_gpu_thread_init()
external fun vosk_batch_model_new(path: String): Pointer?
external fun vosk_batch_model_free(model: BatchModel)
external fun vosk_batch_model_wait(model: BatchModel)
external fun vosk_batch_recognizer_new(batchModel: BatchModel, sampleRate: Float): Pointer
external fun vosk_batch_recognizer_free(recognizer: BatchRecognizer)
external fun vosk_batch_recognizer_accept_waveform(
recognizer: BatchRecognizer,
data: ByteArray?,
length: Int
)
external fun vosk_batch_recognizer_set_nlsml(
recognizer: BatchRecognizer,
nlsml: Boolean
)
external fun vosk_batch_recognizer_finish_stream(
recognizer: BatchRecognizer
)
external fun vosk_batch_recognizer_front_result(
recognizer: BatchRecognizer
): String
external fun vosk_batch_recognizer_pop(recognizer: BatchRecognizer)
external fun vosk_batch_recognizer_get_pending_chunks(recognizer: BatchRecognizer): Int
external fun vosk_text_processor_new(tagger: Char, verbalizer: Char): Pointer
external fun vosk_text_processor_free(processor: TextProcessor)
external fun vosk_text_processor_itn(processor: TextProcessor, input: Char): Char
external fun vosk_recognizer_set_endpointer_mode(recognizer: Recognizer, ordinal: Int)
external fun vosk_recognizer_set_endpointer_delays(
recognizer: Recognizer,
tStartMax: Float,
tEnd: Float,
tMax: Float
)
}
@@ -1,95 +0,0 @@
/*
* Copyright 2023 Alpha Cephei Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.vosk
import com.sun.jna.PointerType
import org.vosk.exception.IOException
import org.vosk.exception.ModelException
import java.io.File
import java.nio.file.Path
import kotlin.io.path.absolutePathString
/**
* Model stores all the data required for recognition
*
* It contains static data and can be shared across processing
* threads.
*
* @since 26 / 12 / 2022
* @constructor Loads model data from the file and returns the model object
* @throws IOException if the path provided is invalid
*/
actual class Model : Freeable, PointerType, AutoCloseable {
/**
* Empty constructor for JNA
*/
constructor()
/**
* Loads model data from the file and returns the model object
*/
@Throws(IOException::class)
actual constructor(path: String) : super(
LibVosk.vosk_model_new(path) ?: throw ModelException(path)
)
/**
* Constructor using a Path, will retrieve absolutePath
*
* @param path to batch model
*/
@Throws(IOException::class)
constructor(path: Path) : this(path.absolutePathString())
/**
* Constructor using a File, will retrieve absolutePath
*
* @param file to batch model
*/
@Throws(IOException::class)
constructor(file: File) : this(file.absolutePath)
/**
* Check if a word can be recognized by the model
* @param word: the word
* @returns the word symbol if @param word exists inside the model
* or -1 otherwise.
* Reminding that word symbol 0 is for <epsilon>
*/
actual fun findWord(word: String): Int =
LibVosk.vosk_model_find_word(this, word)
/**
* Releases the model memory
*
* The model object is reference-counted so if some recognizer
* depends on this model, model might still stay alive. When
* last recognizer is released, model will be released too.
*/
actual override fun free() {
LibVosk.vosk_model_free(this)
}
/**
* @see free
*/
override fun close() {
free()
}
}
@@ -1,353 +0,0 @@
/*
* Copyright 2023 Alpha Cephei Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.vosk
import com.sun.jna.PointerType
import org.vosk.exception.RecognizerException
/**
* Recognizer object is the main object which processes data.
*
* Each recognizer usually runs in own thread and takes audio as input.
* Once audio is processed recognizer returns JSON object as a string
* which represent decoded information - words, confidences, times, n-best lists,
* speaker information and so on
*
* @since 26 / 12 / 2022
*/
actual class Recognizer : Freeable, PointerType, AutoCloseable {
/**
* Empty constructor for JNA
*/
constructor()
/**
* Creates the recognizer object
*
* The recognizers process the speech and return text using shared model data
* @param model VoskModel containing static data for recognizer. Model can be
* shared across recognizers, even running in different threads.
* @param sampleRate The sample rate of the audio you going to feed into the recognizer.
* Make sure this rate matches the audio content, it is a common
* issue causing accuracy problems.
* @throws RecognizerException if a problem occurred
*/
@Throws(RecognizerException::class)
actual constructor(model: Model, sampleRate: Float) :
super(LibVosk.vosk_recognizer_new(model, sampleRate) ?: throw RecognizerException())
/**
* Creates the recognizer object with speaker recognition
*
* With the speaker recognition mode the recognizer not just recognize
* text but also return speaker vectors one can use for speaker identification
*
* @param model VoskModel containing static data for recognizer. Model can be
* shared across recognizers, even running in different threads.
* @param sampleRate The sample rate of the audio you going to feed into the recognizer.
* Make sure this rate matches the audio content, it is a common
* issue causing accuracy problems.
* @param speakerModel speaker model for speaker identification
* @throws RecognizerException if a problem occurred
*/
@Throws(RecognizerException::class)
actual constructor(
model: Model,
sampleRate: Float,
speakerModel: SpeakerModel
) : super(
LibVosk.vosk_recognizer_new_spk(model, sampleRate, speakerModel)
?: throw RecognizerException()
)
/**
* Creates the recognizer object with the phrase list
*
* Sometimes when you want to improve recognition accuracy and when you don't need
* to recognize large vocabulary you can specify a list of phrases to recognize. This
* will improve recognizer speed and accuracy but might return [unk] if user said
* something different.
*
* Only recognizers with lookahead models support this type of quick configuration.
* Precompiled HCLG graph models are not supported.
*
* @param model VoskModel containing static data for recognizer. Model can be
* shared across recognizers, even running in different threads.
* @param sampleRate The valuesample rate of the audio you going to feed into the recognizer.
* Make sure this rate matches the audio content, it is a common
* issue causing accuracy problems.
* @param grammar The string with the list of phrases to recognize as JSON array of strings,
* for example "["one two three four five", "[unk]"]".
*
* @throws RecognizerException if a problem occurred
*/
@Throws(RecognizerException::class)
actual constructor(
model: Model,
sampleRate: Float,
grammar: String
) : super(
LibVosk.vosk_recognizer_new_grm(model, sampleRate, grammar) ?: throw RecognizerException()
)
/**
* JVM analog of Kotlin extension
* @see [org.vosk.json.Recognizer]
*/
@Throws(RecognizerException::class)
constructor(
model: Model,
sampleRate: Float,
grammar: List<String>
) : super(
// We need the full qualifer to avoid any build issues
@Suppress("RemoveRedundantQualifierName")
org.vosk.json.Recognizer(model, sampleRate, grammar).pointer
)
/**
* Adds speaker model to already initialized recognizer
*
* Can add speaker recognition model to already created recognizer. Helps to initialize
* speaker recognition for grammar-based recognizer.
*
* @param speakerModel Speaker recognition model
*/
actual fun setSpeakerModel(speakerModel: SpeakerModel) {
LibVosk.vosk_recognizer_set_spk_model(this, speakerModel)
}
/**
* Reconfigures recognizer to use grammar
*
* @param grammar Set of phrases in JSON array of strings or "[]" to use default model graph.
* See also vosk_recognizer_new_grm
*/
actual fun setGrammar(grammar: String) {
LibVosk.vosk_recognizer_set_grm(this, grammar)
}
/**
* Configures recognizer to output n-best results
*
* <pre>
* {
* "alternatives": [
* { "text": "one two three four five", "confidence": 0.97 },
* { "text": "one two three for five", "confidence": 0.03 },
* ]
* }
* </pre>
*
* @param maxAlternatives - maximum alternatives to return from recognition results
*/
actual fun setMaxAlternatives(maxAlternatives: Int) {
LibVosk.vosk_recognizer_set_max_alternatives(this, maxAlternatives)
}
/**
* Enables words with times in the output
*
* <pre>
* "result" : [{
* "conf" : 1.000000,
* "end" : 1.110000,
* "start" : 0.870000,
* "word" : "what"
* }, {
* "conf" : 1.000000,
* "end" : 1.530000,
* "start" : 1.110000,
* "word" : "zero"
* }, {
* "conf" : 1.000000,
* "end" : 1.950000,
* "start" : 1.530000,
* "word" : "zero"
* }, {
* "conf" : 1.000000,
* "end" : 2.340000,
* "start" : 1.950000,
* "word" : "zero"
* }, {
* "conf" : 1.000000,
* "end" : 2.610000,
* "start" : 2.340000,
* "word" : "one"
* }],
* </pre>
*
* @param words - boolean value
*/
actual fun setOutputWordTimes(words: Boolean) {
LibVosk.vosk_recognizer_set_words(this, words)
}
/**
* Like [setOutputWordTimes] return words and confidences in partial results
*
* @param partialWords - boolean value
*/
actual fun setPartialWords(partialWords: Boolean) {
LibVosk.vosk_recognizer_set_partial_words(this, partialWords)
}
/**
* Set NLSML output
* @param nlsml - boolean value
*/
actual fun setNLSML(nlsml: Boolean) {
LibVosk.vosk_recognizer_set_nlsml(this, nlsml)
}
/**
* Accept voice data
*
* accept and process new chunk of voice data
*
* @param data Audio data in PCM 16-bit mono format.
* @param length Length of the audio data.
* @returns
* 1 - If silence is occurred and you can retrieve a new utterance with result method
* 0 - If decoding continues
* -1 - If exception occurred
*/
@Throws(AcceptWaveformException::class)
actual fun acceptWaveform(data: ByteArray): Boolean {
val result = LibVosk.vosk_recognizer_accept_waveform(this, data, data.size)
if (result == -1) throw AcceptWaveformException(data)
return result == 1
}
/**
* Same as [acceptWaveform] but the version with the short data for language bindings where you have
* audio as array of shorts
*/
@Throws(AcceptWaveformException::class)
actual fun acceptWaveform(data: ShortArray): Boolean {
val result = LibVosk.vosk_recognizer_accept_waveform_s(this, data, data.size)
if (result == -1) throw AcceptWaveformException(data)
return result == 1
}
/**
* Same as [acceptWaveform] but the version with the float data for language bindings where you have
* audio as array of floats
*/
@Throws(AcceptWaveformException::class)
actual fun acceptWaveform(data: FloatArray): Boolean {
val result = LibVosk.vosk_recognizer_accept_waveform_f(this, data, data.size)
if (result == -1) throw AcceptWaveformException(data)
return result == 1
}
/**
* Returns speech recognition result
*
* @returns the result in JSON format which contains decoded line, decoded
* words, times in seconds and confidences. You can parse this result
* with any json parser
*
* <pre>
* {
* "text" : "what zero zero zero one"
* }
* </pre>
*
* If alternatives enabled it returns result with alternatives, see also [setMaxAlternatives].
*
* If word times enabled returns word time, see also [setOutputWordTimes].
*/
actual val result: String
get() = LibVosk.vosk_recognizer_result(this)
/**
* Returns partial speech recognition
*
* @returns partial speech recognition text which is not yet finalized.
* result may change as recognizer process more data.
*
* <pre>
* {
* "partial" : "cyril one eight zero"
* }
* </pre>
*/
actual val finalResult: String
get() = LibVosk.vosk_recognizer_final_result(this)
/**
* Returns speech recognition result. Same as result, but doesn't wait for silence
* You usually call it in the end of the stream to get final bits of audio. It
* flushes the feature pipeline, so all remaining audio chunks got processed.
*
* @returns speech result in JSON format.
*/
actual val partialResult: String
get() = LibVosk.vosk_recognizer_partial_result(this)
/**
* Resets the recognizer
*
* Resets current results so the recognition can continue from scratch
*/
actual fun reset() {
LibVosk.vosk_recognizer_reset(this)
}
/**
* Releases recognizer object
*
* Underlying model is also unreferenced and if needed released
*/
actual override fun free() {
LibVosk.vosk_recognizer_free(this)
}
/**
* @see free
*/
override fun close() {
free()
}
/**
* Set endpointer scaling factor
*
* @param mode Endpointer mode
**/
actual fun setEndPointerMode(mode: EndPointerMode) {
LibVosk.vosk_recognizer_set_endpointer_mode(this, mode.ordinal)
}
/**
* Set endpointer delays
*
* @param tStartMax timeout for stopping recognition in case of initial silence (usually around 5.0)
* @param tEnd timeout for stopping recognition in milliseconds after we recognized something (usually around 0.5 - 1.0)
* @param tMax timeout for forcing utterance end in milliseconds (usually around 20-30)
**/
actual fun setEndPointerDelays(
tStartMax: Float,
tEnd: Float,
tMax: Float
) {
LibVosk.vosk_recognizer_set_endpointer_delays(this, tStartMax, tEnd, tMax)
}
}
@@ -1,84 +0,0 @@
/*
* Copyright 2023 Alpha Cephei Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.vosk
import com.sun.jna.PointerType
import org.vosk.exception.ModelException
import java.io.File
import java.nio.file.Path
import kotlin.io.path.absolutePathString
/**
* Speaker model is the same as model but contains the data
* for speaker identification.
*
* @since 26 / 12 / 2022
* @constructor Loads speaker model data from the file and returns the model object
* @throws ModelException if the path provided is invalid
*/
actual class SpeakerModel : Freeable, PointerType, AutoCloseable {
/**
* Empty constructor for JNA
*/
constructor()
/**
* Loads speaker model data from the file and returns the model object
*
* @param path the path of the model on the filesystem
*/
@Throws(ModelException::class)
actual constructor(path: String) : super(
LibVosk.vosk_spk_model_new(path) ?: throw ModelException(path)
)
/**
* Constructor using a Path, will retrieve absolutePath
*
* @param path to batch model
*/
@Throws(ModelException::class)
constructor(path: Path) : this(path.absolutePathString())
/**
* Constructor using a File, will retrieve absolutePath
*
* @param file to batch model
*/
@Throws(ModelException::class)
constructor(file: File) : this(file.absolutePath)
/**
* Releases the model memory
*
* The model object is reference-counted so if some recognizer
* depends on this model, model might still stay alive. When
* last recognizer is released, model will be released too.
*/
actual override fun free() {
LibVosk.vosk_spk_model_free(this)
}
/**
* @see free
*/
override fun close() {
free()
}
}
@@ -1,51 +0,0 @@
/*
* Copyright 2024 Alpha Cephei Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.vosk
import com.sun.jna.PointerType
/**
* Inverse text normalization
*
* @since 2024/06/19
*/
actual class TextProcessor :
Freeable, PointerType, AutoCloseable {
/**
* Create text processor
*/
actual constructor(tagger: Char, verbalizer: Char) :
super(LibVosk.vosk_text_processor_new(tagger, verbalizer))
/** Release text processor */
actual override fun free() {
LibVosk.vosk_text_processor_free(this)
}
/** Convert string */
actual fun itn(input: Char): Char =
LibVosk.vosk_text_processor_itn(this, input)
/**
* @see free
*/
override fun close() {
free()
}
}
@@ -1,55 +0,0 @@
/*
* Copyright 2023 Alpha Cephei Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.vosk
/**
* 26 / 12 / 2022
*
* Control overarching features of libvosk.
*/
actual object Vosk {
/**
* Set log level for Kaldi messages
*
* @param logLevel the level
*/
@JvmStatic
actual fun setLogLevel(logLevel: LogLevel) {
LibVosk.vosk_set_log_level(logLevel.value)
}
/**
* Init, automatically select a CUDA device and allow multithreading.
* Must be called once from the main thread.
* Has no effect if HAVE_CUDA flag is not set.
*/
@JvmStatic
actual fun gpuInit() {
LibVosk.vosk_gpu_init()
}
/**
* Init CUDA device in a multi-threaded environment.
* Must be called for each thread.
* Has no effect if HAVE_CUDA flag is not set.
*/
@JvmStatic
actual fun gpuThreadInit() {
LibVosk.vosk_gpu_thread_init()
}
}
@@ -1,55 +0,0 @@
/*
* Copyright 2023 Alpha Cephei Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.vosk
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.flowOn
import kotlinx.coroutines.flow.map
import org.vosk.json.WaveformResult
import org.vosk.json.finalResultAsJson
import org.vosk.json.partialResultAsJson
import org.vosk.json.resultAsJson
/**
* Feed an [Flow] of [ByteArray] into a [Recognizer].
*
* The returned flow will emit an [WaveformResult] for each result parsed.
*
* This expects a null terminator to signify the end of a stream.
*
* This will not close the [recognizer] at the end of reading.
*
* Any exceptions will be fed into the flow,
* and should be collected via [kotlinx.coroutines.flow.catch].
*
* Flows on [Dispatchers.IO] to prevent blocking the main thread.
*
* @see [Recognizer.acceptWaveform]
*/
fun Flow<ByteArray?>.feed(recognizer: Recognizer): Flow<WaveformResult> =
map {
if (it != null) {
if (recognizer.acceptWaveform(it)) {
WaveformResult.Result(recognizer.resultAsJson())
} else {
WaveformResult.PartialResult(recognizer.partialResultAsJson())
}
} else {
WaveformResult.FinalResult(recognizer.finalResultAsJson())
}
}.flowOn(Dispatchers.IO)
@@ -1,22 +0,0 @@
/*
* Copyright 2023 Alpha Cephei Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.vosk.exception
/**
* Analog of [java.io.IOException]
*/
actual typealias IOException = java.io.IOException
-212
View File
@@ -1,212 +0,0 @@
/*
* Copyright 2023 Alpha Cephei Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import kotlinx.coroutines.*
import kotlinx.coroutines.flow.*
import kotlinx.coroutines.test.runTest
import org.vosk.*
import java.io.BufferedInputStream
import java.io.FileInputStream
import java.io.IOException
import java.nio.ByteBuffer
import java.nio.ByteOrder
import javax.sound.sampled.AudioSystem
import javax.sound.sampled.UnsupportedAudioFileException
import kotlin.test.Test
class DecoderTest {
val modelPath = System.getenv("MODEL")
val testFile = System.getenv("AUDIO")
init {
System.load(System.getenv("LIBRARY"))
Vosk.setLogLevel(LogLevel.DEBUG)
}
@Test
fun grammarList() {
Model(modelPath).use { model ->
Recognizer(model, 16000f, listOf("one")).apply {
setMaxAlternatives(10)
setOutputWordTimes(true)
setPartialWords(true)
}
}
}
@Test
@Throws(IOException::class, UnsupportedAudioFileException::class)
fun decoderTest() {
Model(modelPath).use { model ->
AudioSystem.getAudioInputStream(BufferedInputStream(FileInputStream(testFile)))
.use { ais ->
Recognizer(model, 16000f).apply {
setMaxAlternatives(10)
setOutputWordTimes(true)
setPartialWords(true)
}.use { recognizer ->
val b = ByteArray(4096)
while (ais.read(b) >= 0) {
if (recognizer.acceptWaveform(b)) {
println(recognizer.result)
} else {
println(recognizer.partialResult)
}
}
println(recognizer.finalResult)
}
}
}
}
@OptIn(ExperimentalCoroutinesApi::class)
@Test
@Throws(IOException::class, UnsupportedAudioFileException::class)
fun decoderTestFlow() = runTest {
Model(modelPath).use { model ->
Recognizer(model, 16000f).apply {
setMaxAlternatives(10)
setOutputWordTimes(true)
setPartialWords(true)
}.use { recognizer ->
AudioSystem.getAudioInputStream(BufferedInputStream(FileInputStream(testFile)))
.use { ais ->
flow {
val b = ByteArray(4096)
while (ais.read(b) >= 0) {
emit(b)
}
emit(null)
}.flowOn(Dispatchers.IO)
.feed(recognizer)
.collect {
println(it)
}
}
}
}
}
/**
* This test aims to simulate the situation for Dicio,
* In which we can receive the audio input stream before the recognizer is setup.
*
* It is recommended to use a large model on desktop to properly see how long it takes to load up.
*/
@Test
@Throws(IOException::class, UnsupportedAudioFileException::class)
fun decoderTestFlowBuffered() {
// Scope to buffer into
val scope = CoroutineScope(Dispatchers.IO)
AudioSystem.getAudioInputStream(BufferedInputStream(FileInputStream(testFile))).use { ais ->
val byteFlow = flow {
val b = ByteArray(4096)
while (ais.read(b) >= 0) {
emit(b)
}
emit(null)
}.flowOn(Dispatchers.IO)
.shareIn(scope, SharingStarted.Eagerly, 100)
// Tell us the current buffer size
println("Buffered size:" + byteFlow.replayCache.size)
var startTime = System.currentTimeMillis()
Model(modelPath).use { model ->
var resultTime = System.currentTimeMillis() - startTime
println("Model initialized in: $resultTime")
startTime = System.currentTimeMillis()
Recognizer(model, 16000f).apply {
setMaxAlternatives(10)
setOutputWordTimes(true)
setPartialWords(true)
}.use { recognizer ->
resultTime = System.currentTimeMillis() - startTime
println("Recognizer initialized in: $resultTime")
println("Buffered size:" + byteFlow.replayCache.size)
runBlocking {
byteFlow
.feed(recognizer)
.take(byteFlow.replayCache.size)
.collect {
println(it)
}
}
}
}
}
}
@Test
@Throws(IOException::class, UnsupportedAudioFileException::class)
fun decoderTestShort() {
Model(modelPath).use { model ->
AudioSystem.getAudioInputStream(BufferedInputStream(FileInputStream(testFile)))
.use { ais ->
Recognizer(model, 16000f).use { recognizer ->
val b = ByteArray(4096)
val s = ShortArray(2048)
while (ais.read(b) >= 0) {
ByteBuffer.wrap(b).order(ByteOrder.LITTLE_ENDIAN).asShortBuffer().get(s)
if (recognizer.acceptWaveform(s)) {
println(recognizer.result)
} else {
println(recognizer.partialResult)
}
}
println(recognizer.finalResult)
}
}
}
}
@Test
@Throws(IOException::class, UnsupportedAudioFileException::class)
fun decoderTestGrammar() {
Model(modelPath).use { model ->
AudioSystem.getAudioInputStream(BufferedInputStream(FileInputStream(testFile)))
.use { ais ->
Recognizer(
model, 16000f, "[\"one two three four five six seven eight nine zero oh\"]"
).use { recognizer ->
val b = ByteArray(4096)
while (ais.read(b) >= 0) {
if (recognizer.acceptWaveform(b)) {
println(recognizer.result)
} else {
println(recognizer.partialResult)
}
}
println(recognizer.finalResult)
}
}
}
}
@Test
fun decoderTestException() {
try {
val model = Model("model_missing")
assert(false)
} catch (e: IOException) {
assert(true)
}
}
}
@@ -1,25 +0,0 @@
headers = libvosk/vosk_api.h
package = libvosk
noStringConversion = \
vosk_batch_recognizer_accept_waveform \
vosk_recognizer_accept_waveform
compilerOpts.linux = \
-I/usr/include/libvosk/ \
-I/usr/local/include/libvosk/ \
-I/usr/include/ \
-I/usr/local/include/
compilerOpts.linux_x64 = \
-I/usr/lib64/libvosk/ \
-I/usr/local/lib64/libvosk/
linkerOpts.linux = \
-L/usr/lib/ \
-L/usr/local/lib/ \
-llibvosk
linkerOpts.linux_x64 = \
-L/usr/lib64/ \
-L/usr/local/lib64/
@@ -1,50 +0,0 @@
/*
* Copyright 2020 Alpha Cephei Inc. & Doomsdayrs
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.vosk
import cnames.structs.VoskBatchModel
import kotlinx.cinterop.CPointer
import libvosk.vosk_batch_model_free
import libvosk.vosk_batch_model_new
import libvosk.vosk_batch_model_wait
/**
* Batch model object
*
* 26 / 12 / 2022
*/
actual class BatchModel(val pointer: CPointer<VoskBatchModel>) : Freeable {
/**
* Creates the batch recognizer object
*/
@Throws(IOException::class)
actual constructor(path: String) : this(vosk_batch_model_new(path) ?: throw ioException(path))
/**
* Releases batch model object
*/
actual override fun free() {
vosk_batch_model_free(pointer)
}
/**
* Wait for the processing
*/
actual fun await() {
vosk_batch_model_wait(pointer)
}
}
@@ -1,86 +0,0 @@
/*
* Copyright 2020 Alpha Cephei Inc. & Doomsdayrs
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.vosk
import cnames.structs.VoskBatchRecognizer
import kotlinx.cinterop.CPointer
import kotlinx.cinterop.toCValues
import kotlinx.cinterop.toKString
import libvosk.*
/**
* Batch recognizer object
*
* 26 / 12 / 2022
*/
actual class BatchRecognizer(val pointer: CPointer<VoskBatchRecognizer>) : Freeable {
/**
* Creates batch recognizer object
*/
actual constructor(
model: BatchModel,
sampleRate: Float
) : this(vosk_batch_recognizer_new(model.pointer, sampleRate)!!)
/**
* Releases batch recognizer object
*/
actual override fun free() {
vosk_batch_recognizer_free(pointer)
}
/**
* Accept batch voice data
*/
actual fun acceptWaveform(data: ByteArray) {
vosk_batch_recognizer_accept_waveform(pointer, data.toCValues(), data.size)
}
/**
* Set NLSML output
* @param nlsml - boolean value
*/
actual fun setNLSML(nlsml: Boolean) {
vosk_batch_recognizer_set_nlsml(pointer, nlsml.toInt())
}
/**
* Closes the stream
*/
actual fun finishStream() {
vosk_batch_recognizer_finish_stream(pointer)
}
/**
* Return results
*/
actual val frontResult: String
get() = vosk_batch_recognizer_front_result(pointer)!!.toKString()
/**
* Release and free first retrieved result
*/
actual fun pop() {
vosk_batch_recognizer_pop(pointer)
}
/**
* Get amount of pending chunks for more intelligent waiting
*/
actual val pendingChunks: Int
get() = vosk_batch_recognizer_get_pending_chunks(pointer)
}
@@ -1,23 +0,0 @@
/*
* Copyright 2020 Alpha Cephei Inc. & Doomsdayrs
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.vosk
/**
* Use a [Freeable], then immediately free it and return any values.
*/
fun <T : Freeable, R> T.use(block: (T) -> R): R =
block(this).also { free() }
@@ -1,3 +0,0 @@
package org.vosk
actual class IOException actual constructor(message: String?) : Exception()
@@ -1,63 +0,0 @@
/*
* Copyright 2020 Alpha Cephei Inc. & Doomsdayrs
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.vosk
import cnames.structs.VoskModel
import kotlinx.cinterop.CPointer
import libvosk.vosk_model_find_word
import libvosk.vosk_model_free
import libvosk.vosk_model_new
/**
* Model stores all the data required for recognition
*
* It contains static data and can be shared across processing
* threads.
*
* 26 / 12 / 2022
*/
actual class Model(val pointer: CPointer<VoskModel>) : Freeable {
/**
* Loads model data from the file and returns the model object
*
* @param path: the path of the model on the filesystem
* @returns model object or NULL if problem occured
*/
@Throws(IOException::class)
actual constructor(path: String) : this(vosk_model_new(path) ?: throw ioException(path))
/**
* Check if a word can be recognized by the model
* @param word: the word
* @returns the word symbol if @param word exists inside the model
* or -1 otherwise.
* Reminding that word symbol 0 is for <epsilon>
*/
actual fun findWord(word: String): Int =
vosk_model_find_word(pointer, word)
/**
* Releases the model memory
*
* The model object is reference-counted so if some recognizer
* depends on this model, model might still stay alive. When
* last recognizer is released, model will be released too.
*/
actual override fun free() {
vosk_model_free(pointer)
}
}

Some files were not shown because too many files have changed in this diff Show More