次の方法で共有


JavaScript 用 Cognitive Services Speech SDK

概要

音声対応アプリケーションの開発を簡略化するために、Microsoft は Speech サービスで使用する Speech SDK を提供しています。 Speech SDK には、一貫性のあるネイティブな Speech-to-Text API と 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;
  });

前の例では、1 つの発話を認識するシングルショット認識を使用しています。 継続的認識を使用して 、認識 を停止するタイミングを制御することもできます。 その他のオプションについては、 ステップ バイ ステップのクイック スタート を参照してください。

サンプル