Compartir a través de


Speech SDK de Cognitive Services para JavaScript

Información general

Para simplificar el desarrollo de aplicaciones habilitadas para voz, Microsoft proporciona el SDK de Voz para su uso con el servicio voz. Speech SDK proporciona API coherentes nativas de conversión de voz en texto y de traducción de voz.

Instalación del módulo npm

Instalación del módulo npm del SDK de Voz de Cognitive Services

npm install microsoft-cognitiveservices-speech-sdk

Ejemplo

Los fragmentos de código siguientes muestran cómo realizar un reconocimiento de voz simple desde un archivo:

// 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;
  });

En el ejemplo anterior se usa el reconocimiento de una sola captura, que reconoce una sola expresión. También puede usar el reconocimiento continuo para controlar cuándo dejar de reconocer. Consulte nuestra guía de inicio rápido paso a paso para obtener muchas más opciones.

Ejemplos