k2speech-speech-sdk
v1.0.6
Published
k2speech speech sdk java script version
Downloads
8
Maintainers
Readme
Introduction
This is the SDK for k2speech speech-to-text web API, to help user integrate the API into their javascript application, either in browser or in nodejs environment.
Features
- [ ] Microphone audio source
- [ ] File audio source
- [ ] Websocket support
- [ ] work in both browser and nodejs environment
Installation
# install from npm
npm i k2speech-speech-sdk
usage example
for nodejs:
# install third party libraries for this demo:
npm i fs
//import our sdk library: k2speech-speech-sdk
import pkg from 'k2speech-speech-sdk';
//import fs library for local file operation
import * as fs from "fs";
const {RecognizerConfig, Recognizer, FileAudioSource, Callback} = pkg;
//RecognizerConfig contains the configuration for Recognizer
//three argument are required:
//token(string): access token got from speech-to-text web portal,
//ip(string): the deployed service ip or domain name
//port(string): the deployed service port
let config = new RecognizerConfig('testtoken', "3.82.117.197", "7000");
//use fs library to read test wave file
const file = fs.readFileSync('test.wav');
//create a file audio source object, as input of Recognizer
let source = new FileAudioSource(file, '');
//create the Recognizer object, using previous created
//RecognizerConfig object and FileAudioSource object
let reco = new Recognizer(config, source)
//set open call back function which will be invoked
//when the Recognizer connect to the service succesfully
reco.setOpenCallback(() =>{console.log("open callback");});
//set close call back function which will be invoked
//when the Recognizer connection is closed
reco.setCloseCallback(() =>{
console.log("recognition result: " + reco.getRecognitionResult());
});
//set message call back function which will be invoked
//when the decode message is received,
//it could happen in the period of decode process
reco.setMessageCallback((message) => {console.log("message:"+message)});
//use the Recognizer to start recognize
reco.recognize().then(() => {console.log("file sent!")});
for browser: file mode recognition:
<script src="dist/k2speech-sdk.browser.min.js"></script>
var files = document.getElementById('file').files;
const file = files[0];
//get token from speech-to-text web portal, set associated API ip and port
var config = new K2SpeechSDK.RecognizerConfig("testtoken", "3.82.117.197", "7000");
var source = new K2SpeechSDK.FileAudioSource(file, '');
var reco = new K2SpeechSDK.Recognizer(config, source)
reco.setOpenCallback(() =>{console.log("open callback");});
reco.setCloseCallback(() =>{console.log("recognized result: " + reco.getRecognitionResult())});
reco.setMessageCallback((message) => {console.log("message:"+message)});
reco.recognize().then(() => {console.log("file sent!")});
Microphone streaming recognition:
<script src="dist/k2speech-sdk.browser.min.js"></script>
//get token from speech-to-text web portal, set associated API ip and port
var config = new K2SpeechSDK.RecognizerConfig("testtoken", "3.82.117.197", "7000");
var source = new K2SpeechSDK.MicAudioSource();
var reco = new K2SpeechSDK.Recognizer(config, source)
reco.setOpenCallback(() =>{console.log("open callback");});
reco.setCloseCallback(() =>{console.log("recognized result: " + reco.getRecognitionResult())});
reco.setMessageCallback((message) => {console.log("message:"+message)});
reco.recognize().then(() => {console.log("Mic audio recognition started")});