# azure-speech-utilities

> Provides a convenient abstraction layer over the Microsoft Cognitive Services Speech SDK, simplifying the integration of speech-to-text functionality into client applications. Using this npm package, developers can quickly integrate speech-to-text capabil

Latest version **1.0.0** (published 2024-03-04) · ISC license · 0 weekly downloads

## Install

```sh
npm install azure-speech-utilities
pnpm add azure-speech-utilities
yarn add azure-speech-utilities
bun add azure-speech-utilities
```

## Health

**Score 20/100 (F)** — status: abandoned.

Positive: esm support; no vulnerabilities.

Warnings: low downloads; no types.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.0.0 |
| Published | 2024-03-04 |
| First published | 2024-02-27 |
| Weekly downloads | 0 |
| License | ISC |
| TypeScript types | none |
| Module format | ESM + CommonJS |
| Dependencies | 1 |
| Unpacked size | 32.6 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 0 |
| Author | Utkarsh Pancholi |
| Maintainers | utkarsh212 |
| Keywords | azure, speech, speechtotext, cognitive, multilingual |

## Links

- npm: https://www.npmjs.com/package/azure-speech-utilities
- Repository: https://github.com/Utkarsh212/azure-speech-utilities
- Homepage: https://github.com/Utkarsh212/azure-speech-utilities#readme
- Issues: https://github.com/Utkarsh212/azure-speech-utilities/issues
- npm.io page: https://npm.io/package/azure-speech-utilities

## Dependencies (1)

- [microsoft-cognitiveservices-speech-sdk](https://npm.io/package/microsoft-cognitiveservices-speech-sdk.md) ^1.35.0

## Recent versions

- 1.0.0 (latest) — 2024-03-04
- 0.1.1 — 2024-02-27
- 0.1.0 — 2024-02-27
- 0.0.0 — 2024-02-27

## README

# azure-speech-utilities

Provides a convenient abstraction layer over the Microsoft Cognitive Services Speech SDK, simplifying the integration of speech-to-text and text-to-speech functionality into client/browser applications. Using this package, developers can quickly integrate basic STT and TTS capabilities into their applications without the need to write intricate code.

## Features:
- Perform a single speech recognition operation with ease.
- Enable continuous speech recognition for real-time applications.
- Multilingual speech recognition.
- Text to speech synthesis.
- SSML/Text input for TTS.

## Installing

Using npm:

```js
npm install azure-speech-utilities
```

## Function Description

### CreateRecognizer

Creates a new speech recognizer instance.

| Parameter | Type | Default Value | Description |
| :-------: | :--: | :-----------: | :----------: |
| cogSvcSubKey | string | `""` | The Cognitive Services subscription key for Speech Services. (Required, default is `empty string`) |
| cogSvcRegion | string | `""` | The region of Cognitive Services subscription. (Required, default is `empty string`) |
| recognitionLang | string[] | `["en-US"]` | An array of language recognition codes. (Optional, default is `["en-US"]`). |

### RecognizeOnceAsync

Used for single-shot recognition, which recognizes a single utterance. The end of a single utterance is determined by listening for silence at the end or until a maximum of 15 seconds of audio is processed.

| Parameter | Type | Default Value | Description |
| :-------: | :--: | :-----------: | :----------: |
| recognizer | sdk.SpeechRecognizer \| undefined | undefined | The speech `recognizer` instance to use. |


### ContinuousRecognitionAsync

 The previous function performs single-shot recognition, which recognizes a single utterance. In contrast, you can use continuous recognition to get a real-time recognized text stream. Make a call to [StopContinuousRecognitionAsync()](#stopcontinuousrecognitionasync) at some point to stop recognition

| Parameter | Type | Default Value | Description |
| :-------: | :--: | :-----------: | :----------: |
| recognizer | sdk.SpeechRecognizer \| undefined | undefined | The speech `recognizer` instance to use. |
| callbackRecognized | (value: string) => void | `val => console.log(value)` | A callback function called with recognized text. |
| callbackRecognizing | (value: string) => void | `val => console.log(value)` | A callback function called while speech is being recognized. |

### StopContinuousRecognitionAsync

Stops ongoing continuous speech recognition.

| Parameter | Type | Default Value | Description |
| :-------: | :--: | :-----------: | :----------: |
| recognizer | sdk.SpeechRecognizer \| undefined | undefined | The speech `recognizer` instance to use. |

**Note:** Use the same recognizer instance which you are using for [ContinuousRecognitionAsync()](#continuousrecognitionasync) as an argument to this function.

### CreateSynthesizer

Creates a new speech synthesizer instance.

| Parameter | Type | Default Value | Description |
| :-------: | :--: | :-----------: | :----------: |
| cogSvcSubKey | string | `""` | The Cognitive Services subscription key for Speech Services. (Required) |
| cogSvcRegion | string | `""` | The region of Cognitive Services subscription. (Required) |
| synthesisLang | string | `""` | The language code for the speech synthesizer. (Required) |
| synthesisVoiceName | string | `""` | The name of the voice to use for speech synthesis. (Optional, default is `""`) |
| createAudioConfig | boolean | `false` | Whether to create an audio config for speech output. (Optional, default is `false`) |

**Note:** The voice that speaks is determined in order of priority as follows:
- Passing false for `createAudioConfig`, doesn't play the audio by default on the current active output device.
- If you only set `synthesisLang`, the default voice for the specified locale speaks.
- If both `synthesisVoiceName` and `synthesisLang` are set, the `synthesisLang` setting is ignored. The voice that you specify by using `synthesisVoiceName` speaks.
- If the voice element is set by using Speech Synthesis Markup Language (SSML), the `synthesisVoiceName` and `synthesisLang` settings are ignored.

### SpeakAsync

Performs speech synthesis and returns the result (synthesized audio) in form of arrayBuffer.

| Parameter | Type | Default Value | Description |
| :-------: | :--: | :-----------: | :----------: |
| synthesizer | sdk.SpeechSynthesizer \| undefined | undefined | The speech `synthesizer` instance to use. |
| inputString |string | `"I'm excited to try text to speech"` | The text to be synthesized. |
| inputType | string | `"text"` | The format of the input text. (Optional, default is `"text"`) |
| callback | (result: sdk.SynthesisResult, error?: Error) => void | (result, error) => {} | A callback function called with the synthesis result or an error. |

## Example

**Recognize Once**
```js
import { CreateRecognizer, RecognizeOnceAsync  } from "azure-speech-utilities"

const CGV_KEY = "AZURE_SPEECH_SERVICE_KEY"
const CGV_REGION = "AZURE_SPEECH_SERVICE_REGION"

async function recognizeSpeech() {
    const recognizer = CreateRecognizer(CGV_KEY, CGV_REGION, ["hi-IN"])
    try {
            const recognizedText = await RecognizeOnceAsync(recognizer)
            if (recognizedText.type === "text") {
                console.log(recognizedText.message)
            } else {
                console.log(recognizedText.message)
            }
    } catch (error) {
      console.error(error)
    }
}
```

**Continuous Recognition**
```js
import { CreateRecognizer, ContinuousRecognitionAsync } from "azure-speech-utilities"

const CGV_KEY = "AZURE_SPEECH_SERVICE_KEY"
const CGV_REGION = "AZURE_SPEECH_SERVICE_REGION"

// As there are 2 or more recognition languages "hi-IN" and "en-US" so it will be multilingual recognition.
const recognizer = CreateRecognizer(CGV_KEY, CGV_REGION, ["hi-IN", "en-US"])

function callbackRecognized(text) {
    console.log("RECOGNIZED: ", text)
}

function callbackRecognizing(text) {
    console.log("RECOGNIZING: ", text)
}

async function recognizeSpeech() {
    try {
            const response = await ContinuousRecognitionAsync(recognizer, callbackRecognized, callbackRecognizing)
            if (response.type === "success") {
                console.log(response.message)
            } else {
                console.error(response.message)
            }
    } catch (error) {
      console.error(error)
    }
}

function stopContinuousRecognition() {
    StopContinuousRecognitionAsync(recognizer)
}
```

**Speak Async**
```js
import { CreateSynthesizer, SpeakAsync } from "azure-speech-utilities"

const CGV_KEY = "AZURE_SPEECH_SERVICE_KEY"
const CGV_REGION = "AZURE_SPEECH_SERVICE_REGION"
const SYNTHESIS_LANGUAGE = "en-US"
const SYNTHESIS_VOICE_NAME = "en-US-JennyNeural"

function handleSpeck() {
    // By default, the input type is 'text.' If you change the input type to 'ssml,' then the input string should be in the following SSML format.
    // const ssml = `
    // <speak version="1.0" xmlns="https://www.w3.org/2001/10/synthesis" xml:lang="${SYNTHESIS_LANGUAGE}">
    //     <voice name="${SYNTHESIS_VOICE_NAME}">
    //         When you're on the freeway, it's a good idea to use a GPS.
    //     </voice>
    // </speak>
    // `

    const text = "When you're on the freeway, it's a good idea to use a GPS."

    // Please note that the 'createAudioConfig' is set to false, meaning audio will not play by default on the currently active output device.
    const synthesizer = CreateSynthesizer(CGV_KEY, CGV_REGION, SYNTHESIS_LANGUAGE, SYNTHESIS_VOICE_NAME, false)

    SpeakAsync(synthesizer, text, "text", (result, error) => {
      if (error) {
        console.error(error)
      } else {
        console.log(result)
        const audioBlob = new Blob([result.audioData], { type: "audio/wav" })

        // You can use this URL as an audio source, which allows easy user control such as starting, stopping, resetting, etc.
        console.log(URL.createObjectURL(audioBlob))
      }
    })
  }

  const stopSpeaking = () => {
    audioRef.current.pause()
  }
```
**Note:** If you do not wish to play audio through an audio source, you can set `createAudioConfig` to `true`. This will cause the audio to play on the current active output device by default. However, using this method will not provide the user with the ability to reset, play, or pause the speaking audio.

## Contributing

This project welcomes contributions and suggestions.

---
_Source: https://npm.io/package/azure-speech-utilities · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
