-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
Copy pathtextToSpeech.ts
50 lines (41 loc) · 1.67 KB
/
textToSpeech.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
/**
* Demonstrates how to convert text into speech.
*
* @summary text to speech.
* @azsdk-weight 100
*/
import "openai/shims/node";
import { AzureOpenAI } from "openai";
import { DefaultAzureCredential, getBearerTokenProvider } from "@azure/identity";
import { writeFile } from "node:fs/promises";
// Set AZURE_OPENAI_ENDPOINT to the endpoint of your
// OpenAI resource. You can find this in the Azure portal.
// Load the .env file if it exists
import "dotenv/config";
// You will need to set these environment variables or edit the following values
const speechFilePath = process.env["SPEECH_FILE_PATH"] || "<path to save the speech file>";
// Corresponds to your Model deployment within your OpenAI resource
// Navigate to the Azure OpenAI Studio to deploy a model.
const deployment = "tts";
const apiVersion = "2025-03-01-preview";
const credential = new DefaultAzureCredential();
const scope = "https://cognitiveservices.azure.com/.default";
const azureADTokenProvider = getBearerTokenProvider(credential, scope);
export async function main(): Promise<void> {
console.log("== Text to Speech Sample ==");
const openai = new AzureOpenAI({ azureADTokenProvider, deployment, apiVersion });
const response = await openai.audio.speech.create({
model: deployment,
voice: "alloy",
input: "the quick brown chicken jumped over the lazy dogs",
});
const stream = response.body;
console.log(`Streaming response to ${speechFilePath}`);
await writeFile(speechFilePath, stream);
console.log("Finished streaming");
}
main().catch((err) => {
console.error("The sample encountered an error:", err);
});