-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Expand file tree
/
Copy pathaudio.ts
More file actions
executable file
·66 lines (54 loc) · 2.1 KB
/
Copy pathaudio.ts
File metadata and controls
executable file
·66 lines (54 loc) · 2.1 KB
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#!/usr/bin/env -S npm run tsn -- -T
import OpenAI, { toFile } from 'openai';
import fs from 'node:fs';
import path from 'node:path';
import { Readable } from 'node:stream';
import { pipeline } from 'node:stream/promises';
// gets API Key from environment variable OPENAI_API_KEY
const openai = new OpenAI();
const speechFile = path.resolve(__dirname, './speech.mp3');
/** Runs streaming and buffered speech-generation examples sequentially. */
async function main(): Promise<void> {
await streamingDemoNode();
await blockingDemo();
}
main();
/** Streams synthesized speech directly to a file without buffering the complete response. */
async function streamingDemoNode(): Promise<void> {
const response = await openai.audio.speech.create({
model: 'tts-1',
voice: 'alloy',
input: 'the quick brown chicken jumped over the lazy dogs',
});
const stream = response.body;
if (!stream) {
throw new Error('Speech response did not include an audio stream.');
}
console.log(`Streaming response to ${speechFile}`);
await streamToFile(Readable.fromWeb(stream), speechFile);
console.log('Finished streaming');
}
/** Buffers synthesized speech before submitting it for transcription and translation. */
async function blockingDemo(): Promise<void> {
const mp3 = await openai.audio.speech.create({
model: 'tts-1',
voice: 'alloy',
input: 'the quick brown fox jumped over the lazy dogs',
});
const buffer = Buffer.from(await mp3.arrayBuffer());
await fs.promises.writeFile(speechFile, buffer);
const transcription = await openai.audio.transcriptions.create({
file: await toFile(buffer, 'speech.mp3'),
model: 'whisper-1',
});
console.log(transcription.text);
const translation = await openai.audio.translations.create({
file: await toFile(buffer, 'speech.mp3'),
model: 'whisper-1',
});
console.log(translation.text);
}
/** Writes a Node.js readable stream to disk and propagates source or destination errors. */
async function streamToFile(stream: NodeJS.ReadableStream, destination: fs.PathLike): Promise<void> {
await pipeline(stream, fs.createWriteStream(destination));
}