@alphaspeech/asr_kit
v2.0.8
Published
A npm package to simplify connecting to the alphaspeech live ASR in typescript and react.
Downloads
42
Readme
asr_kit npm package
This npm package provides a client for to the alphaspeech pro live ASR via webRTC.
Install requirements
Run npm install at root-project level. Dependencies for the example project will automatically be installed with this command.
Usage
- Create the client
const asr = new AlphaspeechClient({
asr_url: domain.url,
jwt_token: domain.auth,
configuration: domain.rtcConf,
domainConf: domain.domainConf,
settings: {
useTestAudioFiles: false,
testAudioFileName: ""
}
} as AlphaspeechClientProps);- Define your onHypothesis function
function onHypothesis(response: any) {
let message: any = JSON.parse(response.data);
// handle your message here
// to get just the message text
let messageText: str = message.hypothesis.map(c => c.content).join(' ');
}- Define state handling
const [currentState, setCurrentState] = useState(0);
useEffect(() => {
switch (currentState) {
case state.connecting:
asr.connectToASR(onHypothesis, currentState, setCurrentState, isFirstTimeShowAllowAccessDialog, setIsFirstTimeShowAllowAccessDialog);
break;
case state.disconnecting:
asr.stopSpeechRecognition(setCurrentState);
break;
case state.unmute:
asr.unmute(setCurrentState);
break;
case state.mute:
asr.mute(currentState, setCurrentState);
break;
}
}, [currentState]);- Define first time connection parameters to ensure microphone access will be prompted
const [isFirstTimeShowAllowAccessDialog, setIsFirstTimeShowAllowAccessDialog] = useState(true);- Connect to ASR
setCurrentState(state.connecting);If you mute the ASR, remember that the connection will time out after ~60s without activity. In order to keep a session alive, regularly call asr.hold() to reset the timeout counter.
How to develop this package
Refer to https://gitlab.com/alphaspeech/alphaspeech-npm-asr-kit
Structure
./index.tsx contains every export the package should have.
The src/service directory contains connectivity methods to other services.
