import * as fs from "fs" ; import * as querystring from "querystring" ; import * as rp from "request-promise" ; import * as uuid from "uuid" ; /**
 * Created by betterclever on 6/30/17.
 */ export interface IBingRecognitionResult { RecognitionStatus : string ; DisplayText : string ; Offset : number ; Duration : number ; } export class BingRecognizer { private BING_SPEECH_TOKEN_ENDPOINT = "https://api.cognitive.microsoft.com/sts/v1.0/issueToken" ; private BING_SPEECH_ENDPOINT_STT = "https://speech.platform.bing.com/speech/recognition/interactive/cognitiveservices/v1" ; private subscriptionKey : string ; private token : string ; constructor ( subscriptionKey ) { this . subscriptionKey = subscriptionKey ; } public recognizeAudioStream ( input ) { return this . issueToken ( ) . then ( ( token ) => { this . token = token ; const params = { language : "en-us" , format : "json" , requestid : uuid . v4 ( ) } ; const options = { method : "POST" , uri : this . BING_SPEECH_ENDPOINT_STT + "?" + querystring . stringify ( params ) , body : input , headers : { "Authorization" : `Bearer ${ this . token }` , "Content-Type" : "audio/wav; codec=\"audio/pcm\"; samplerate=16000" } } ; return new Promise < string > ( ( resolve , reject ) => { rp ( options ) . then ( ( response ) => { const recognitionResponse = JSON . parse ( response ) ; if ( recognitionResponse . RecognitionStatus === "InitialSilenceTimeout" || recognitionResponse . RecognitionStatus === "NoMatch" ) { return reject ( "Recognition Error" ) ; } return resolve ( recognitionResponse . DisplayText ) ; } ) . catch ( ( error ) => { return reject ( error ) ; } ) ; } ) ; } ) ; } private issueToken ( ) { const options = { method : "POST" , uri : this . BING_SPEECH_TOKEN_ENDPOINT , headers : { "Ocp-Apim-Subscription-Key" : this . subscriptionKey } } ; return new Promise ( ( resolve , reject ) => { rp ( options ) . then ( ( token ) => { return resolve ( token ) ; } ) . catch ( ( error ) => { return reject ( error ) ; } ) ; } ) ; } }