Partilhar via


SDK de Voz dos Serviços Cognitivos para JavaScript

Descrição Geral

Para simplificar o desenvolvimento de aplicações com capacidade de voz, a Microsoft fornece o SDK de Voz para utilização com o serviço de Voz. O SDK de Voz fornece APIs nativas de Conversão de Voz em Texto e Tradução de Voz consistentes.

Instalar o módulo npm

Instalar o módulo npm do SDK de Voz dos Serviços Cognitivos

npm install microsoft-cognitiveservices-speech-sdk

Exemplo

Os fragmentos de código seguintes ilustram como fazer o reconhecimento de voz simples a partir de um ficheiro:

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

O exemplo anterior utiliza o reconhecimento de tiro único, que reconhece uma única expressão. Também pode utilizar o reconhecimento contínuo para controlar quando parar de reconhecer. Veja o nosso início rápido passo a passo para obter muitas mais opções.

Amostras