forked from coze-dev/coze-js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvoice.ts
69 lines (58 loc) · 2.2 KB
/
voice.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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
/* eslint-disable @typescript-eslint/naming-convention */
/* eslint-disable @typescript-eslint/no-explicit-any */
import { fileURLToPath } from 'node:url';
import { dirname, join } from 'node:path';
import fs from 'fs';
import { type Voice } from '@coze/api';
import { client } from './client.js';
const __filename = fileURLToPath(import.meta.url);
const __dirname = dirname(__filename);
const filePath = join(__dirname, '../tmp/voice.mp3');
const fileBuffer = await fs.createReadStream(filePath);
async function voiceClone() {
const voiceObj = await client.audio.voices.clone({
audio_format: 'mp3',
file: fileBuffer as any,
voice_name: '湾湾小何2',
preview_text:
'今天天气真是太好了,阳光灿烂,心情超级棒,但是朋友最近的感情问题也让我心痛不已,好像世界末日一样,真的好为他难过。',
voice_id: '742894224871836***',
});
console.log('client.audio.voices.clone', voiceObj);
}
async function createSpeech() {
const speechBuffer = await client.audio.speech.create({
input:
'今天天气真是太好了,阳光灿烂,心情超级棒,但是朋友最近的感情问题也让我心痛不已,好像世界末日一样,真的好为他难过。',
voice_id: '742894224871836***',
response_format: 'mp3',
});
const audioPath = join(__dirname, '../tmp/speech.mp3');
// Check if file exists to prevent accidental overwrites
if (fs.existsSync(audioPath)) {
console.warn(`Warning: Overwriting existing file at ${audioPath}`);
}
await fs.writeFileSync(audioPath, speechBuffer as any, 'binary');
console.log('Speech saved successfully:', audioPath);
}
async function listAllVoices() {
const PAGE_SIZE = 20;
let page = 1;
let allVoices: Voice[] = [];
while (true) {
const response = await client.audio.voices.list({
page_size: PAGE_SIZE,
page_num: page,
});
allVoices = allVoices.concat(response.voice_list);
if (response.has_more === false) {
break;
}
page++;
}
console.log(`Total voices found: ${allVoices.length}`);
return allVoices;
}
await voiceClone().catch(console.error);
await createSpeech().catch(console.error);
await listAllVoices().catch(console.error);