gpt-sovits-sdk
Version:
Node.js SDK for GPT-SoVITS API
229 lines (228 loc) • 8.55 kB
JavaScript
;
/**
* GPT-SoVITS SDK 基本使用示例
*/
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || (function () {
var ownKeys = function(o) {
ownKeys = Object.getOwnPropertyNames || function (o) {
var ar = [];
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
return ar;
};
return ownKeys(o);
};
return function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
__setModuleDefault(result, mod);
return result;
};
})();
Object.defineProperty(exports, "__esModule", { value: true });
const index_1 = require("../index");
const path = __importStar(require("path"));
const fs = __importStar(require("fs"));
// 创建输出目录
const outputDir = path.join(__dirname, '../../output');
if (!fs.existsSync(outputDir)) {
fs.mkdirSync(outputDir, { recursive: true });
}
// 创建客户端实例
const client = (0, index_1.createClient)({
baseUrl: 'http://127.0.0.1:9880', // 默认API地址
timeout: 60000, // 设置超时时间为60秒
debug: true // 启用调试输出
});
/**
* 示例1: 获取API信息
*/
async function getApiInfo() {
console.log('获取API信息...');
try {
const rootInfo = await client.getRoot();
console.log('API根信息:', rootInfo);
const healthInfo = await client.getHealth();
console.log('API健康状态:', healthInfo);
}
catch (error) {
console.error('获取API信息失败:', error);
}
}
/**
* 示例2: 获取模型和参考音频列表
*/
async function getResourceInfo() {
console.log('\n获取资源信息...');
try {
const modelsResponse = await client.getModels();
console.log('可用模型列表:');
if (modelsResponse.data) {
console.log('- GPT模型:', modelsResponse.data.gpt_models.map((m) => m.name).join(', '));
console.log('- SoVITS模型:', modelsResponse.data.sovits_models.map((m) => m.name).join(', '));
}
const referenceAudiosResponse = await client.getReferenceAudios();
if (referenceAudiosResponse.data) {
console.log('参考音频列表:', referenceAudiosResponse.data.audios.length, '个音频');
}
// 获取特定角色的情感音频
const emotionAudiosResponse = await client.getEmotionAudios({
character: '卡齐娜',
language: '中文'
});
if (emotionAudiosResponse.data) {
console.log('情感音频列表:', emotionAudiosResponse.data.referenceAudios.length, '个音频');
}
}
catch (error) {
console.error('获取资源信息失败:', error);
}
}
/**
* 示例3: 文本转语音(返回音频文件路径)
*/
async function textToSpeech() {
console.log('\n执行文本转语音...');
try {
const result = await client.textToSpeech({
text: '你好,这是一个测试文本。',
textLang: index_1.TextLanguage.CHINESE,
refAudioPath: 'emotions/卡齐娜/中文/【中立_neutral】那个人是希巴拉克大人?可是,他看起来好年轻啊。.wav',
promptLang: index_1.TextLanguage.CHINESE,
mediaType: index_1.MediaType.WAV
});
console.log('TTS结果:', result);
// 如果需要,可以下载生成的音频文件
if (result && result.audio_path) {
const audioData = await client.getAudio(result.audio_path);
const outputPath = path.join(outputDir, `tts_example_${Date.now()}.wav`);
fs.writeFileSync(outputPath, Buffer.from(audioData));
console.log('音频文件已保存到:', outputPath);
}
}
catch (error) {
console.error('文本转语音失败:', error);
}
}
/**
* 示例4: 文本转语音(直接返回音频数据)
*/
async function textToSpeechDirect() {
console.log('\n执行直接文本转语音...');
try {
const outputPath = path.join(outputDir, `tts_direct_example_${Date.now()}.wav`);
// 直接保存到文件
await client.textToSpeechDirect({
text: '这是直接返回音频数据的测试。',
textLang: index_1.TextLanguage.CHINESE,
refAudioPath: 'emotions/卡齐娜/中文/【中立_neutral】那个人是希巴拉克大人?可是,他看起来好年轻啊。.wav',
promptLang: index_1.TextLanguage.CHINESE,
mediaType: index_1.MediaType.WAV
}, outputPath);
console.log('直接TTS音频已保存到:', outputPath);
}
catch (error) {
console.error('直接文本转语音失败:', error);
}
}
/**
* 示例5: 设置模型
*/
async function setModels() {
console.log('\n设置模型...');
try {
// 设置GPT模型
const gptResult = await client.setGptModel('GPT_weights_v2/纳塔-e10.ckpt');
console.log('设置GPT模型结果:', gptResult);
// 设置SoVITS模型
const sovitsResult = await client.setSovitsModel('SoVITS_weights_v2/纳塔-e10.pth');
console.log('设置SoVITS模型结果:', sovitsResult);
}
catch (error) {
console.error('设置模型失败:', error);
}
}
/**
* 获取可用模型
*/
async function getModels() {
try {
const response = await client.getModels();
console.log('API响应状态:', response.code, response.message);
if (response.data) {
console.log('GPT模型数量:', response.data.gpt_models.length);
console.log('SoVITS模型数量:', response.data.sovits_models.length);
// 显示第一个GPT模型(如果有)
if (response.data.gpt_models.length > 0) {
console.log('第一个GPT模型:', response.data.gpt_models[0].name);
}
// 显示第一个SoVITS模型(如果有)
if (response.data.sovits_models.length > 0) {
console.log('第一个SoVITS模型:', response.data.sovits_models[0].name);
}
}
}
catch (error) {
console.error('获取模型失败:', error);
}
}
/**
* 获取参考音频
*/
async function getReferenceAudios() {
try {
const response = await client.getReferenceAudios();
console.log('API响应状态:', response.code, response.message);
if (response.data) {
console.log('参考音频数量:', response.data.audios.length);
console.log('子目录数量:', response.data.directories?.length || 0);
// 显示子目录(如果有)
if (response.data.directories && response.data.directories.length > 0) {
console.log('子目录列表:', response.data.directories);
// 获取第一个子目录中的音频
const subdirName = response.data.directories[0];
const subdirResponse = await client.getReferenceAudios(subdirName);
if (subdirResponse.data) {
console.log(`子目录 "${subdirName}" 中的音频数量:`, subdirResponse.data.audios.length);
}
}
}
}
catch (error) {
console.error('获取参考音频失败:', error);
}
}
/**
* 运行所有示例
*/
async function runAllExamples() {
try {
await getApiInfo();
await getResourceInfo();
await setModels();
await textToSpeech();
await textToSpeechDirect();
console.log('\n所有示例执行完成!');
}
catch (error) {
console.error('执行示例时出错:', error);
}
}
// 执行示例
runAllExamples();