Files
social-router/src/test/tts.ts
2025-07-04 00:01:22 +08:00

58 lines
1.8 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

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();