nativescript-vosk
Version:
Speech recognition plugin using Vosk library.
161 lines • 7.04 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.syncModel = exports.SpeechService = void 0;
var core_1 = require("@nativescript/core");
var AppPackageName = global.androidx.core.app;
var ContentPackageName = global.androidx.core.content;
var SpeechService = (function () {
function SpeechService() {
var _this = this;
this.speechService = null;
var self = this;
core_1.Application.android.on(core_1.AndroidApplication.activityRequestPermissionsEvent, function (args) {
for (var i = 0; i < args.permissions.length; i++) {
if (args.grantResults[i] === android.content.pm.PackageManager.PERMISSION_DENIED) {
if (self.onPermissionRejected) {
self.onPermissionRejected("Please allow access to the Microphone and try again.");
}
else {
console.log("Please allow access to the Microphone and try again. (tip: pass in a reject to receive this message in your app)");
}
return;
}
}
if (self.onPermissionGranted) {
self.onPermissionGranted();
}
});
core_1.Application.on(core_1.Application.suspendEvent, function (args) {
if (_this.speechService !== null) {
_this.cancel();
_this.shutdown();
}
});
}
SpeechService.prototype.available = function () {
return new Promise(function (resolve, reject) {
resolve(android.speech.SpeechRecognizer.isRecognitionAvailable(core_1.Utils.android.getApplicationContext()));
});
};
SpeechService.prototype.requestPermission = function () {
var _this = this;
console.log(">> requestPermission");
return new Promise(function (resolve, reject) {
_this._requestPermission(function () { return resolve(true); }, function () { return resolve(false); });
});
};
SpeechService.prototype.startListening = function (model, listener) {
var _this = this;
return new Promise(function (resolve, reject) {
var onPermissionGranted = function () {
var loopHandler = new android.os.Handler(android.os.Looper.getMainLooper());
loopHandler.post(new java.lang.Runnable({
run: function () {
var recognizer = new org.vosk.Recognizer(model, 16000.0);
_this.speechService = new org.vosk.android.SpeechService(recognizer, 16000.0);
_this.speechService.startListening(new org.vosk.android.RecognitionListener({
onPartialResult: function (hyp) {
listener.onPartialResult(hyp);
},
onResult: function (hyp) {
listener.onResult(hyp);
},
onFinalResult: function (hyp) {
listener.onFinalResult(hyp);
},
onTimeout: function () {
listener.onTimeout();
},
onError: function (e) {
listener.onError();
}
}));
resolve(true);
}
}));
};
if (!_this.wasPermissionGranted()) {
_this._requestPermission(onPermissionGranted, reject);
return;
}
onPermissionGranted();
});
};
SpeechService.prototype.stop = function () {
var _this = this;
return new Promise(function (resolve, reject) {
if (_this.speechService === null) {
reject("Not running");
return;
}
var loopHandler = new android.os.Handler(android.os.Looper.getMainLooper());
loopHandler.post(new java.lang.Runnable({
run: function () {
_this.speechService.stop();
resolve();
}
}));
});
};
SpeechService.prototype.cancel = function () {
var _this = this;
return new Promise(function (resolve, reject) {
if (_this.speechService === null) {
reject("Not running");
return;
}
var loopHandler = new android.os.Handler(android.os.Looper.getMainLooper());
loopHandler.post(new java.lang.Runnable({
run: function () {
_this.speechService.cancel();
resolve();
}
}));
});
};
SpeechService.prototype.shutdown = function () {
var _this = this;
return new Promise(function (resolve, reject) {
if (_this.speechService === null) {
reject("Not running");
return;
}
var loopHandler = new android.os.Handler(android.os.Looper.getMainLooper());
loopHandler.post(new java.lang.Runnable({
run: function () {
_this.speechService.shutdown();
resolve();
}
}));
});
};
SpeechService.prototype.wasPermissionGranted = function () {
var hasPermission = android.os.Build.VERSION.SDK_INT < 23;
if (!hasPermission) {
hasPermission = android.content.pm.PackageManager.PERMISSION_GRANTED ===
ContentPackageName.ContextCompat.checkSelfPermission(core_1.Utils.android.getApplicationContext(), android.Manifest.permission.RECORD_AUDIO);
}
return hasPermission;
};
SpeechService.prototype._requestPermission = function (onPermissionGranted, reject) {
this.onPermissionGranted = onPermissionGranted;
this.onPermissionRejected = reject;
AppPackageName.ActivityCompat.requestPermissions(core_1.Application.android.foregroundActivity || core_1.Application.android.startActivity, [android.Manifest.permission.RECORD_AUDIO], 444);
};
return SpeechService;
}());
exports.SpeechService = SpeechService;
function syncModel() {
return new Promise(function (resolve, reject) {
var loopHandler = new android.os.Handler(android.os.Looper.getMainLooper());
loopHandler.post(new java.lang.Runnable({
run: function () {
var outputPath = org.vosk.android.StorageService.sync(core_1.Utils.android.getApplicationContext(), "model-en-us", "model");
var model = new org.vosk.Model(outputPath);
resolve(model);
}
}));
});
}
exports.syncModel = syncModel;
//# sourceMappingURL=speech-recognition-vosk.android.js.map