Skip to content

Commit 2e9bb43

Browse files
authored
Merge pull request #6092 from eric-urban/eur/ai-speech-translation-qs
Get started with speech translation using TypeScript
2 parents eeccf5b + 6c2cfef commit 2e9bb43

File tree

15 files changed

+334
-139
lines changed

15 files changed

+334
-139
lines changed

articles/ai-services/speech-service/get-started-speech-translation.md

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@ manager: nitinme
77
ms.service: azure-ai-speech
88
ms.custom: devx-track-extended-java, devx-track-go, devx-track-js, devx-track-python
99
ms.topic: quickstart
10-
ms.date: 3/10/2025
10+
ms.date: 7/17/2025
1111
ms.author: eur
12-
zone_pivot_groups: programming-languages-speech-services
12+
zone_pivot_groups: programming-languages-speech-translation
1313
keywords: speech translation
1414
---
1515

@@ -47,12 +47,12 @@ keywords: speech translation
4747
[!INCLUDE [Python include](./includes/quickstarts/speech-translation-basics/python.md)]
4848
::: zone-end
4949

50-
::: zone pivot="programming-language-rest"
51-
[!INCLUDE [REST include](includes/quickstarts/speech-translation-basics/rest.md)]
50+
::: zone pivot="programming-language-typescript"
51+
[!INCLUDE [TypeScript include](includes/quickstarts/speech-translation-basics/typescript.md)]
5252
::: zone-end
5353

54-
::: zone pivot="programming-language-cli"
55-
[!INCLUDE [CLI include](includes/quickstarts/speech-translation-basics/cli.md)]
54+
::: zone pivot="programming-language-rest"
55+
[!INCLUDE [REST include](includes/quickstarts/speech-translation-basics/rest.md)]
5656
::: zone-end
5757

5858

articles/ai-services/speech-service/includes/quickstarts/speech-translation-basics/cli.md

Lines changed: 0 additions & 84 deletions
This file was deleted.

articles/ai-services/speech-service/includes/quickstarts/speech-translation-basics/cpp.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
author: eric-urban
33
ms.service: azure-ai-speech
44
ms.topic: include
5-
ms.date: 06/08/2022
5+
ms.date: 7/17/2025
66
ms.author: eur
77
---
88

articles/ai-services/speech-service/includes/quickstarts/speech-translation-basics/csharp.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
author: eric-urban
33
ms.service: azure-ai-speech
44
ms.topic: include
5-
ms.date: 06/08/2022
5+
ms.date: 7/17/2025
66
ms.author: eur
77
---
88

articles/ai-services/speech-service/includes/quickstarts/speech-translation-basics/go.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
author: eric-urban
33
ms.service: azure-ai-speech
44
ms.topic: include
5-
ms.date: 06/08/2022
5+
ms.date: 7/17/2025
66
ms.author: eur
77
---
88

articles/ai-services/speech-service/includes/quickstarts/speech-translation-basics/intro.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
author: eric-urban
33
ms.service: azure-ai-speech
44
ms.topic: include
5-
ms.date: 06/08/2022
5+
ms.date: 7/17/2025
66
ms.author: eur
77
---
88

articles/ai-services/speech-service/includes/quickstarts/speech-translation-basics/java.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
author: eric-urban
33
ms.service: azure-ai-speech
44
ms.topic: include
5-
ms.date: 06/08/2022
5+
ms.date: 7/17/2025
66
ms.author: eur
77
---
88

articles/ai-services/speech-service/includes/quickstarts/speech-translation-basics/javascript.md

Lines changed: 46 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
author: eric-urban
33
ms.service: azure-ai-speech
44
ms.topic: include
5-
ms.date: 06/08/2022
5+
ms.date: 7/17/2025
66
ms.author: eur
77
---
88

@@ -14,55 +14,60 @@ ms.author: eur
1414

1515
[!INCLUDE [Prerequisites](../../common/azure-prerequisites.md)]
1616

17-
## Set up the environment
17+
## Set up
1818

19-
Before you can do anything, you need to install the Speech SDK for JavaScript. If you just want the package name to install, run `npm install microsoft-cognitiveservices-speech-sdk`. For guided installation instructions, see the [SDK installation guide](../../../quickstarts/setup-platform.md?pivots=programming-language-javascript).
19+
1. Create a new folder `translation-quickstart` and go to the quickstart folder with the following command:
2020

21-
### Set environment variables
22-
23-
[!INCLUDE [Environment variables](../../common/environment-variables.md)]
21+
```shell
22+
mkdir translation-quickstart && cd translation-quickstart
23+
```
24+
25+
1. Create the `package.json` with the following command:
2426

25-
## Translate speech from a file
27+
```shell
28+
npm init -y
29+
```
2630

27-
Follow these steps to create a Node.js console application for speech recognition.
31+
1. Install the Speech SDK for JavaScript with:
2832

29-
1. Open a command prompt where you want the new project, and create a new file named `SpeechTranslation.js`.
30-
1. Install the Speech SDK for JavaScript:
3133
```console
3234
npm install microsoft-cognitiveservices-speech-sdk
3335
```
34-
1. Copy the following code into `SpeechTranslation.js`:
3536

36-
```javascript
37-
const fs = require("fs");
38-
const sdk = require("microsoft-cognitiveservices-speech-sdk");
37+
### Retrieve resource information
3938

39+
[!INCLUDE [Environment variables](../../common/environment-variables.md)]
40+
41+
## Translate speech from a file
42+
43+
To translate speech from a file:
44+
45+
1. Create a new file named *translation.js* with the following content:
46+
47+
```javascript
48+
import { readFileSync } from "fs";
49+
import { SpeechTranslationConfig, AudioConfig, TranslationRecognizer, ResultReason, CancellationDetails, CancellationReason } from "microsoft-cognitiveservices-speech-sdk";
4050
// This example requires environment variables named "SPEECH_KEY" and "SPEECH_REGION"
41-
const speechTranslationConfig = sdk.SpeechTranslationConfig.fromSubscription(process.env.SPEECH_KEY, process.env.SPEECH_REGION);
51+
const speechTranslationConfig = SpeechTranslationConfig.fromSubscription(process.env.SPEECH_KEY, process.env.SPEECH_REGION);
4252
speechTranslationConfig.speechRecognitionLanguage = "en-US";
43-
44-
var language = "it";
53+
const language = "it";
4554
speechTranslationConfig.addTargetLanguage(language);
46-
4755
function fromFile() {
48-
let audioConfig = sdk.AudioConfig.fromWavFileInput(fs.readFileSync("YourAudioFile.wav"));
49-
let translationRecognizer = new sdk.TranslationRecognizer(speechTranslationConfig, audioConfig);
50-
51-
translationRecognizer.recognizeOnceAsync(result => {
56+
const audioConfig = AudioConfig.fromWavFileInput(readFileSync("YourAudioFile.wav"));
57+
const translationRecognizer = new TranslationRecognizer(speechTranslationConfig, audioConfig);
58+
translationRecognizer.recognizeOnceAsync((result) => {
5259
switch (result.reason) {
53-
case sdk.ResultReason.TranslatedSpeech:
60+
case ResultReason.TranslatedSpeech:
5461
console.log(`RECOGNIZED: Text=${result.text}`);
5562
console.log("Translated into [" + language + "]: " + result.translations.get(language));
56-
5763
break;
58-
case sdk.ResultReason.NoMatch:
64+
case ResultReason.NoMatch:
5965
console.log("NOMATCH: Speech could not be recognized.");
6066
break;
61-
case sdk.ResultReason.Canceled:
62-
const cancellation = sdk.CancellationDetails.fromResult(result);
67+
case ResultReason.Canceled:
68+
const cancellation = CancellationDetails.fromResult(result);
6369
console.log(`CANCELED: Reason=${cancellation.reason}`);
64-
65-
if (cancellation.reason == sdk.CancellationReason.Error) {
70+
if (cancellation.reason === CancellationReason.Error) {
6671
console.log(`CANCELED: ErrorCode=${cancellation.ErrorCode}`);
6772
console.log(`CANCELED: ErrorDetails=${cancellation.errorDetails}`);
6873
console.log("CANCELED: Did you set the speech resource key and region values?");
@@ -75,15 +80,19 @@ Follow these steps to create a Node.js console application for speech recognitio
7580
fromFile();
7681
```
7782
78-
1. In `SpeechTranslation.js`, replace `YourAudioFile.wav` with your own WAV file. This example only recognizes speech from a WAV file. For information about other audio formats, see [How to use compressed input audio](~/articles/ai-services/speech-service/how-to-use-codec-compressed-audio-input-streams.md). This example supports up to 30 seconds audio.
79-
1. To change the speech recognition language, replace `en-US` with another [supported language](~/articles/ai-services/speech-service/language-support.md?tabs=stt#supported-languages). Specify the full locale with a dash (`-`) separator. For example, `es-ES` for Spanish (Spain). The default language is `en-US` if you don't specify a language. For details about how to identify one of multiple languages that might be spoken, see [language identification](~/articles/ai-services/speech-service/language-identification.md).
80-
1. To change the translation target language, replace `it` with another [supported language](~/articles/ai-services/speech-service/language-support.md?tabs=speech-translation#supported-languages). With few exceptions you only specify the language code that precedes the locale dash (`-`) separator. For example, use `es` for Spanish (Spain) instead of `es-ES`. The default language is `en` if you don't specify a language.
83+
- In `translation.js`, replace `YourAudioFile.wav` with your own WAV file. This example only recognizes speech from a WAV file. For information about other audio formats, see [How to use compressed input audio](~/articles/ai-services/speech-service/how-to-use-codec-compressed-audio-input-streams.md). This example supports up to 30 seconds audio.
8184
82-
Run your new console application to start speech recognition from a file:
85+
- To change the speech recognition language, replace `en-US` with another [supported language](~/articles/ai-services/speech-service/language-support.md?tabs=stt#supported-languages). Specify the full locale with a dash (`-`) separator. For example, `es-ES` for Spanish (Spain). The default language is `en-US` if you don't specify a language. For details about how to identify one of multiple languages that might be spoken, see [language identification](~/articles/ai-services/speech-service/language-identification.md).
8386
84-
```console
85-
node.exe SpeechTranslation.js
86-
```
87+
- To change the translation target language, replace `it` with another [supported language](~/articles/ai-services/speech-service/language-support.md?tabs=speech-translation#supported-languages). With few exceptions you only specify the language code that precedes the locale dash (`-`) separator. For example, use `es` for Spanish (Spain) instead of `es-ES`. The default language is `en` if you don't specify a language.
88+
89+
1. Run your new console application to start speech recognition from a file:
90+
91+
```console
92+
node translation.js
93+
```
94+
95+
## Output
8796
8897
The speech from the audio file should be output as translated text in the target language:
8998
@@ -93,6 +102,7 @@ Translated into [it]: Sono entusiasta di provare la traduzione vocale.
93102
```
94103
95104
## Remarks
105+
96106
Now that you've completed the quickstart, here are some additional considerations:
97107
98108
This example uses the `recognizeOnceAsync` operation to transcribe utterances of up to 30 seconds, or until silence is detected. For information about continuous recognition for longer audio, including multi-lingual conversations, see [How to translate speech](~/articles/ai-services/speech-service/how-to-translate-speech.md).

articles/ai-services/speech-service/includes/quickstarts/speech-translation-basics/objectivec.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
author: eric-urban
33
ms.service: azure-ai-speech
44
ms.topic: include
5-
ms.date: 06/08/2022
5+
ms.date: 7/17/2025
66
ms.author: eur
77
---
88

articles/ai-services/speech-service/includes/quickstarts/speech-translation-basics/python.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
author: eric-urban
33
ms.service: azure-ai-speech
44
ms.topic: include
5-
ms.date: 06/08/2022
5+
ms.date: 7/17/2025
66
ms.author: eur
77
---
88

0 commit comments

Comments
 (0)