Skip to content

Commit 3049188

Browse files
authored
Merge pull request #6088 from eric-urban/eur/ai-speech-diarization-ts
Get started with speech to text diarization using TypeScript
2 parents c519280 + ee34d6d commit 3049188

File tree

15 files changed

+281
-70
lines changed

15 files changed

+281
-70
lines changed

articles/ai-services/speech-service/get-started-stt-diarization.md

Lines changed: 4 additions & 4 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/16/2025
1111
ms.author: eur
12-
zone_pivot_groups: programming-languages-speech-services
12+
zone_pivot_groups: programming-languages-speech-diarization
1313
keywords: speech to text, speech to text software
1414
#customer intent: As a developer, I want to create speech to text applications that use diarization to identify speakers in multiple person conversations.
1515
---
@@ -52,8 +52,8 @@ keywords: speech to text, speech to text software
5252
[!INCLUDE [REST include](includes/quickstarts/stt-diarization/rest.md)]
5353
::: zone-end
5454

55-
::: zone pivot="programming-language-cli"
56-
[!INCLUDE [CLI include](includes/quickstarts/stt-diarization/cli.md)]
55+
::: zone pivot="programming-language-typescript"
56+
[!INCLUDE [TypeScript include](includes/quickstarts/stt-diarization/typescript.md)]
5757
::: zone-end
5858

5959
## Next step

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

Lines changed: 32 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -47,33 +47,41 @@ To transcribe speech from a file:
4747
1. Create a new file named *transcription.js* with the following content:
4848

4949
```javascript
50-
import { readFileSync } from "fs";
51-
import { SpeechConfig, AudioConfig, SpeechRecognizer, ResultReason, CancellationDetails, CancellationReason } from "microsoft-cognitiveservices-speech-sdk";
50+
import { readFileSync, createReadStream } from "fs";
51+
import { SpeechConfig, AudioConfig, ConversationTranscriber, AudioInputStream } from "microsoft-cognitiveservices-speech-sdk";
5252
// This example requires environment variables named "SPEECH_KEY" and "SPEECH_REGION"
5353
const speechConfig = SpeechConfig.fromSubscription(process.env.SPEECH_KEY, process.env.SPEECH_REGION);
54-
speechConfig.speechRecognitionLanguage = "en-US";
5554
function fromFile() {
56-
const audioConfig = AudioConfig.fromWavFileInput(readFileSync("YourAudioFile.wav"));
57-
const speechRecognizer = new SpeechRecognizer(speechConfig, audioConfig);
58-
speechRecognizer.recognizeOnceAsync((result) => {
59-
switch (result.reason) {
60-
case ResultReason.RecognizedSpeech:
61-
console.log(`RECOGNIZED: Text=${result.text}`);
62-
break;
63-
case ResultReason.NoMatch:
64-
console.log("NOMATCH: Speech could not be recognized.");
65-
break;
66-
case ResultReason.Canceled:
67-
const cancellation = CancellationDetails.fromResult(result);
68-
console.log(`CANCELED: Reason=${cancellation.reason}`);
69-
if (cancellation.reason === CancellationReason.Error) {
70-
console.log(`CANCELED: ErrorCode=${cancellation.ErrorCode}`);
71-
console.log(`CANCELED: ErrorDetails=${cancellation.errorDetails}`);
72-
console.log("CANCELED: Did you set the speech resource key and region values?");
73-
}
74-
break;
75-
}
76-
speechRecognizer.close();
55+
const filename = "katiesteve.wav";
56+
const audioConfig = AudioConfig.fromWavFileInput(readFileSync(filename));
57+
const conversationTranscriber = new ConversationTranscriber(speechConfig, audioConfig);
58+
const pushStream = AudioInputStream.createPushStream();
59+
createReadStream(filename).on('data', function (chunk) {
60+
pushStream.write(chunk.slice());
61+
}).on('end', function () {
62+
pushStream.close();
63+
});
64+
console.log("Transcribing from: " + filename);
65+
conversationTranscriber.sessionStarted = function (s, e) {
66+
console.log("SessionStarted event");
67+
console.log("SessionId:" + e.sessionId);
68+
};
69+
conversationTranscriber.sessionStopped = function (s, e) {
70+
console.log("SessionStopped event");
71+
console.log("SessionId:" + e.sessionId);
72+
conversationTranscriber.stopTranscribingAsync();
73+
};
74+
conversationTranscriber.canceled = function (s, e) {
75+
console.log("Canceled event");
76+
console.log(e.errorDetails);
77+
conversationTranscriber.stopTranscribingAsync();
78+
};
79+
conversationTranscriber.transcribed = function (s, e) {
80+
console.log("TRANSCRIBED: Text=" + e.result.text + " Speaker ID=" + e.result.speakerId);
81+
};
82+
// Start conversation transcription
83+
conversationTranscriber.startTranscribingAsync(function () { }, function (err) {
84+
console.trace("err - starting transcription: " + err);
7785
});
7886
}
7987
fromFile();

articles/ai-services/speech-service/includes/quickstarts/stt-diarization/cli.md

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

articles/ai-services/speech-service/includes/quickstarts/stt-diarization/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: 3/10/2025
5+
ms.date: 7/16/2025
66
ms.author: eur
77
---
88

articles/ai-services/speech-service/includes/quickstarts/stt-diarization/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: 3/10/2025
5+
ms.date: 7/16/2025
66
ms.author: eur
77
---
88

articles/ai-services/speech-service/includes/quickstarts/stt-diarization/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: 3/10/2025
5+
ms.date: 7/16/2025
66
ms.author: eur
77
---
88

articles/ai-services/speech-service/includes/quickstarts/stt-diarization/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: 3/10/2025
5+
ms.date: 7/16/2025
66
ms.author: eur
77
---
88

articles/ai-services/speech-service/includes/quickstarts/stt-diarization/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: 3/10/2025
5+
ms.date: 7/16/2025
66
ms.author: eur
77
---
88

articles/ai-services/speech-service/includes/quickstarts/stt-diarization/javascript.md

Lines changed: 33 additions & 24 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: 3/10/2025
5+
ms.date: 7/16/2025
66
ms.author: eur
77
---
88

@@ -14,49 +14,57 @@ ms.author: eur
1414

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

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

19-
To set up your environment, 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 `transcription-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)]
24-
25-
## Implement diarization from file with conversation transcription
26-
27-
Follow these steps to create a new console application for conversation transcription.
21+
```shell
22+
mkdir transcription-quickstart && cd transcription-quickstart
23+
```
24+
25+
1. Create the `package.json` with the following command:
2826

29-
1. Open a command prompt window where you want the new project, and create a new file named `ConversationTranscription.js`.
27+
```shell
28+
npm init -y
29+
```
3030

31-
1. Install the Speech SDK for JavaScript:
31+
1. Install the Speech SDK for JavaScript with:
3232

3333
```console
3434
npm install microsoft-cognitiveservices-speech-sdk
3535
```
3636

37-
1. Copy the following code into `ConversationTranscription.js`:
37+
### Retrieve resource information
38+
39+
[!INCLUDE [Environment variables](../../common/environment-variables.md)]
40+
41+
## Implement diarization from file with conversation transcription
42+
43+
Follow these steps to create a new console application for conversation transcription.
44+
45+
1. Create a new file named *transcription.js* with the following content:
3846

3947
```javascript
4048
const fs = require("fs");
4149
const sdk = require("microsoft-cognitiveservices-speech-sdk");
42-
50+
4351
// This example requires environment variables named "SPEECH_KEY" and "SPEECH_REGION"
4452
const speechConfig = sdk.SpeechConfig.fromSubscription(process.env.SPEECH_KEY, process.env.SPEECH_REGION);
45-
53+
4654
function fromFile() {
4755
const filename = "katiesteve.wav";
48-
56+
4957
let audioConfig = sdk.AudioConfig.fromWavFileInput(fs.readFileSync(filename));
5058
let conversationTranscriber = new sdk.ConversationTranscriber(speechConfig, audioConfig);
51-
59+
5260
var pushStream = sdk.AudioInputStream.createPushStream();
53-
61+
5462
fs.createReadStream(filename).on('data', function(arrayBuffer) {
5563
pushStream.write(arrayBuffer.slice());
5664
}).on('end', function() {
5765
pushStream.close();
5866
});
59-
67+
6068
console.log("Transcribing from: " + filename);
6169
6270
conversationTranscriber.sessionStarted = function(s, e) {
@@ -76,15 +84,15 @@ Follow these steps to create a new console application for conversation transcri
7684
conversationTranscriber.transcribed = function(s, e) {
7785
console.log("TRANSCRIBED: Text=" + e.result.text + " Speaker ID=" + e.result.speakerId);
7886
};
79-
87+
8088
// Start conversation transcription
8189
conversationTranscriber.startTranscribingAsync(
8290
function () {},
8391
function (err) {
8492
console.trace("err - starting transcription: " + err);
8593
}
8694
);
87-
95+
8896
}
8997
fromFile();
9098
```
@@ -98,11 +106,12 @@ Follow these steps to create a new console application for conversation transcri
98106
1. Run your new console application to start speech recognition from a file:
99107
100108
```console
101-
node.exe ConversationTranscription.js
109+
node transcription.js
102110
```
103111
104-
> [!IMPORTANT]
105-
> Make sure that you set the `SPEECH_KEY` and `SPEECH_REGION` [environment variables](#set-environment-variables). If you don't set these variables, the sample fails with an error message.
112+
Wait a few moments to get the response.
113+
114+
## Output
106115
107116
The transcribed conversation should be output as text:
108117

articles/ai-services/speech-service/includes/quickstarts/stt-diarization/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: 3/10/2025
5+
ms.date: 7/16/2025
66
ms.author: eur
77
---
88

0 commit comments

Comments
 (0)