generated from tailored/router-db-template
58 lines
1.8 KiB
TypeScript
58 lines
1.8 KiB
TypeScript
import { config } from './common.ts';
|
||
|
||
const API_KEY = config.BAILIAN_API_KEY;
|
||
// 使用DashScope API进行TTS (文本转语音) 请求
|
||
const dashscopeTTS = async ({ text, voice = 'Chelsie', token }) => {
|
||
try {
|
||
const response = await fetch('https://dashscope.aliyuncs.com/api/v1/services/aigc/multimodal-generation/generation', {
|
||
method: 'POST',
|
||
headers: {
|
||
Authorization: `Bearer ${token}`,
|
||
'Content-Type': 'application/json',
|
||
},
|
||
body: JSON.stringify({
|
||
// model: 'qwen-tts',
|
||
model: 'qwen-tts-latest',
|
||
input: {
|
||
text,
|
||
voice,
|
||
},
|
||
}),
|
||
});
|
||
|
||
if (!response.ok) {
|
||
throw new Error(`HTTP error! Status: ${response.status}`);
|
||
}
|
||
|
||
const data = await response.json();
|
||
return data;
|
||
} catch (error) {
|
||
console.error('TTS 请求失败:', error);
|
||
throw error;
|
||
}
|
||
};
|
||
|
||
// 使用示例
|
||
const sampleText =
|
||
'那我来给大家推荐一款T恤,这款呢真的是超级好看,这个颜色呢很显气质,而且呢也是搭配的绝佳单品,大家可以闭眼入,真的是非常好看,对身材的包容性也很好,不管啥身材的宝宝呢,穿上去都是很好看的。推荐宝宝们下单哦。';
|
||
|
||
async function main() {
|
||
const voiceList = ['Cherry', 'Serena', 'Ethan', 'Chelsie'];
|
||
const latestVoiceList = ['Dylan', 'Jada', 'Sunny'];
|
||
try {
|
||
const result = await dashscopeTTS({ text: sampleText, voice: latestVoiceList[2], token: API_KEY });
|
||
console.log('TTS 生成成功:', result);
|
||
|
||
// 如果API返回音频数据(通常是base64格式),可以这样处理
|
||
// 例如: 保存到文件或播放音频
|
||
if (result.output?.audio) {
|
||
// 处理音频数据
|
||
console.log('获取到音频数据,长度:', result.output.audio.length);
|
||
}
|
||
} catch (error) {
|
||
console.error('处理失败:', error);
|
||
}
|
||
}
|
||
|
||
main();
|