JavaScript용 Cognitive Services Speech SDK

개요

음성 지원 애플리케이션 개발을 간소화하기 위해 Microsoft는 Speech Service에서 사용할 Speech SDK를 제공합니다. Speech SDK는 일관된 네이티브 Speech to Text 및 Speech Translation API를 제공합니다.

npm 모듈 설치

Cognitive Services Speech SDK npm 모듈 설치

npm install microsoft-cognitiveservices-speech-sdk

다음 코드 조각에서는 파일에서 간단한 음성 인식을 수행하는 방법을 보여줍니다.

// Pull in the required packages.
var sdk = require("microsoft-cognitiveservices-speech-sdk");
var fs = require("fs");

// Replace with your own subscription key, service region (e.g., "westus"), and
// the name of the file you want to run through the speech recognizer.
var subscriptionKey = "YourSubscriptionKey";
var serviceRegion = "YourServiceRegion"; // e.g., "westus"
var filename = "YourAudioFile.wav"; // 16000 Hz, Mono

// Create the push stream we need for the speech sdk.
var pushStream = sdk.AudioInputStream.createPushStream();

// Open the file and push it to the push stream.
fs.createReadStream(filename).on('data', function(arrayBuffer) {
  pushStream.write(arrayBuffer.buffer);
}).on('end', function() {
  pushStream.close();
});

// We are done with the setup
console.log("Now recognizing from: " + filename);

// Create the audio-config pointing to our stream and
// the speech config specifying the language.
var audioConfig = sdk.AudioConfig.fromStreamInput(pushStream);
var speechConfig = sdk.SpeechConfig.fromSubscription(subscriptionKey, serviceRegion);

// Setting the recognition language to English.
speechConfig.speechRecognitionLanguage = "en-US";

// Create the speech recognizer.
var recognizer = new sdk.SpeechRecognizer(speechConfig, audioConfig);

// Start the recognizer and wait for a result.
recognizer.recognizeOnceAsync(
  function (result) {
    console.log(result);

    recognizer.close();
    recognizer = undefined;
  },
  function (err) {
    console.trace("err - " + err);

    recognizer.close();
    recognizer = undefined;
  });

이전 예제에서는 단일 발화를 인식하는 단일 샷 인식을 사용합니다. 연속 인식을 사용하여 인식을 중지할 시기를 제어할 수도 있습니다. 더 많은 옵션을 보려면 단계별 빠른 시작을 확인하세요.

샘플