Azure SDK for JavaScript – Speech (Text‑to‑Speech)

← Back to SDK Docs

Overview

The Azure Speech service provides high‑quality text‑to‑speech (TTS) synthesis across many languages and voices. This page demonstrates how to use the @azure/ai-speech-sdk package in JavaScript to generate speech from text or SSML.

Installation

npm install @azure/ai-speech-sdk

Or with yarn:

yarn add @azure/ai-speech-sdk

Basic Usage

Below is a minimal example that creates a SpeechSynthesizer and plays the synthesized audio using the Web Speech API for demonstration purposes.

import {
  SpeechConfig,
  SpeechSynthesizer,
  AudioConfig
} from "@azure/ai-speech-sdk";

const speechConfig = SpeechConfig.fromSubscription(
  "YOUR_SUBSCRIPTION_KEY",
  "YOUR_REGION"
);
speechConfig.speechSynthesisVoiceName = "en-US-AriaNeural";

const audioConfig = AudioConfig.fromDefaultSpeakerOutput();

const synthesizer = new SpeechSynthesizer(speechConfig, audioConfig);

synthesizer.speakTextAsync(
  "Hello, Azure Text to Speech!",
  result => {
    if (result.reason === ResultReason.SynthesizingAudioCompleted) {
      console.log("Speech synthesized to speaker.");
    } else {
      console.error("Speech synthesis canceled:", result.errorDetails);
    }
    synthesizer.close();
  },
  err => {
    console.error(err);
    synthesizer.close();
  }
);

Interactive Sample

Enter text or SSML below and press Speak to hear the output. The sample uses the browser's native SpeechSynthesis API to simulate Azure TTS, so no credentials are required.

API Reference

Key classes and methods:

For full reference, see the official SDK docs.