SHARE
TWEET

Untitled

a guest Jan 21st, 2020 65 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. "use strict";
  2. var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
  3.     var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
  4.     if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
  5.     else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
  6.     return c > 3 && r && Object.defineProperty(target, key, r), r;
  7. };
  8. var __metadata = (this && this.__metadata) || function (k, v) {
  9.     if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
  10. };
  11. Object.defineProperty(exports, "__esModule", { value: true });
  12. const common_1 = require("@nestjs/common");
  13. const speech_config_service_1 = require("./speech-config.service");
  14. let SpeechService = class SpeechService {
  15.     constructor(config) {
  16.         this.config = config;
  17.         this.speechSDK = require('microsoft-cognitiveservices-speech-sdk');
  18.         this.clients = {};
  19.     }
  20.     recognizeOnAsync(buffer) {
  21.         const audioStream = this.speechSDK.AudioInputStream.createPushStream();
  22.         const highWaterMark = 8192 * 8;
  23.         for (let i = 0; i < buffer.length; i += highWaterMark) {
  24.             const chunkSize = i + highWaterMark > buffer.length ? buffer.length : i + highWaterMark;
  25.             const chunk = buffer.slice(i, chunkSize);
  26.             audioStream.write(chunk.slice());
  27.         }
  28.         audioStream.close();
  29.         const recognizer = this.createRecognizer(audioStream);
  30.         const promise = new Promise((resolve, reject) => {
  31.             recognizer.recognizeOnceAsync((result) => {
  32.                 resolve(result.json);
  33.                 recognizer.close();
  34.             }, (error) => {
  35.                 reject(error);
  36.                 recognizer.close();
  37.             });
  38.         });
  39.         return promise;
  40.     }
  41.     startContinuousRecognition(clientId, recognizing, recognized) {
  42.         const stream = this.speechSDK.AudioInputStream.createPushStream();
  43.         const recognizer = this.createRecognizer(stream);
  44.         recognizer.canceled = (s, e) => console.log('canceled', s, e);
  45.         recognizer.recognizing = (s, e) => recognizing(e.result.json);
  46.         recognizer.recognized = (s, e) => recognized(e.result.json);
  47.         const promise = new Promise((resolve, reject) => {
  48.             recognizer.recognizeOnceAsync((result) => {
  49.                 resolve(result.json);
  50.                 recognizer.close();
  51.                 delete this.clients[clientId];
  52.             }, (error) => {
  53.                 reject(error);
  54.             });
  55.         });
  56.         this.clients[clientId] = { recognizer, stream };
  57.         return promise;
  58.     }
  59.     stopContinousRecognition(clientId) {
  60.         const client = this.clients[clientId];
  61.         if (client) {
  62.             client.stream.close();
  63.         }
  64.     }
  65.     recognizeOnContinous(clientId, buffer) {
  66.         const client = this.clients[clientId];
  67.         if (!client)
  68.             return false;
  69.         client.stream.write(buffer.slice());
  70.     }
  71.     createRecognizer(audioStream, language) {
  72.         const audioConfig = this.speechSDK.AudioConfig.fromStreamInput(audioStream);
  73.         const speechConfig = this.speechSDK.SpeechConfig.fromSubscription(this.config.getSubscriptionKey(), this.config.getRegion());
  74.         speechConfig.endpointId = this.config.getEndpointId();
  75.         speechConfig.speechRecognitionLanguage = language ? language : this.config.getDefaultLanguage();
  76.         const proxy = this.config.getProxy();
  77.         if (proxy) {
  78.             speechConfig.setProxy(proxy.url, proxy.port);
  79.         }
  80.         return new this.speechSDK.SpeechRecognizer(speechConfig, audioConfig);
  81.     }
  82. };
  83. SpeechService = __decorate([
  84.     common_1.Injectable(),
  85.     __metadata("design:paramtypes", [speech_config_service_1.SpeechConfigService])
  86. ], SpeechService);
  87. exports.SpeechService = SpeechService;
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Top