minimax-mcp-tools
Version:
Async MCP server with Minimax API integration for image generation and text-to-speech
130 lines • 5.29 kB
JavaScript
import { MinimaxBaseClient } from '../core/base-client.js';
import { API_CONFIG, DEFAULTS, MODELS, VOICES } from '../config/constants.js';
import { FileHandler } from '../utils/file-handler.js';
import { ErrorHandler } from '../utils/error-handler.js';
export class TextToSpeechService extends MinimaxBaseClient {
constructor(options = {}) {
super(options);
}
async generateSpeech(params) {
try {
const payload = this.buildPayload(params);
const response = await this.post(API_CONFIG.ENDPOINTS.TEXT_TO_SPEECH, payload);
return await this.processTTSResponse(response, params);
}
catch (error) {
const processedError = ErrorHandler.handleAPIError(error);
ErrorHandler.logError(processedError, { service: 'tts', params });
throw processedError;
}
}
buildPayload(params) {
const ttsDefaults = DEFAULTS.TTS;
const model = params.highQuality ? 'speech-2.6-hd' : 'speech-2.6-turbo';
const payload = {
model: model,
text: params.text,
voice_setting: {
voice_id: params.voiceId || ttsDefaults.voiceId,
speed: params.speed || ttsDefaults.speed,
vol: params.volume || ttsDefaults.volume,
pitch: params.pitch || ttsDefaults.pitch,
emotion: params.emotion || ttsDefaults.emotion
},
audio_setting: {
sample_rate: parseInt(params.sampleRate || ttsDefaults.sampleRate),
bitrate: parseInt(params.bitrate || ttsDefaults.bitrate),
format: params.format || ttsDefaults.format,
channel: ttsDefaults.channel
}
};
if (params.languageBoost) {
payload.language_boost = params.languageBoost;
}
if (params.intensity !== undefined || params.timbre !== undefined || params.sound_effects !== undefined) {
payload.voice_modify = {};
if (params.intensity !== undefined) {
payload.voice_modify.intensity = params.intensity;
}
if (params.timbre !== undefined) {
payload.voice_modify.timbre = params.timbre;
}
if (params.sound_effects !== undefined) {
payload.voice_modify.sound_effects = params.sound_effects;
}
}
return this.cleanPayload(payload);
}
cleanPayload(obj) {
if (typeof obj !== 'object' || obj === null) {
return obj;
}
if (Array.isArray(obj)) {
return obj.map(item => this.cleanPayload(item)).filter(item => item !== undefined);
}
const result = {};
for (const [key, value] of Object.entries(obj)) {
if (value === undefined)
continue;
if (typeof value === 'object' && value !== null) {
const cleanedValue = this.cleanPayload(value);
if (typeof cleanedValue === 'object' && !Array.isArray(cleanedValue) && Object.keys(cleanedValue).length === 0) {
continue;
}
result[key] = cleanedValue;
}
else {
result[key] = value;
}
}
return result;
}
async processTTSResponse(response, params) {
const audioHex = response.data?.audio;
if (!audioHex) {
throw new Error('No audio data received from API');
}
const audioBytes = Buffer.from(audioHex, 'hex');
await FileHandler.writeFile(params.outputFile, audioBytes);
const ttsDefaults = DEFAULTS.TTS;
const result = {
audioFile: params.outputFile,
voiceUsed: params.voiceId || ttsDefaults.voiceId,
model: params.highQuality ? 'speech-2.6-hd' : 'speech-2.6-turbo',
duration: response.data?.duration || null,
format: params.format || ttsDefaults.format,
sampleRate: parseInt(params.sampleRate || ttsDefaults.sampleRate),
bitrate: parseInt(params.bitrate || ttsDefaults.bitrate)
};
return result;
}
getSupportedModels() {
return Object.keys(MODELS.TTS);
}
getSupportedVoices() {
return Object.keys(VOICES);
}
getVoiceInfo(voiceId) {
return VOICES[voiceId] || null;
}
getModelInfo(modelName) {
return MODELS.TTS[modelName] || null;
}
validateVoiceParameters(params) {
const ttsDefaults = DEFAULTS.TTS;
const voice = this.getVoiceInfo(params.voiceId || ttsDefaults.voiceId);
const model = params.highQuality ? 'speech-2.6-hd' : 'speech-2.6-turbo';
const issues = [];
if (!voice && params.voiceId) {
issues.push(`Unknown voice ID: ${params.voiceId}`);
}
if (params.emotion && params.emotion !== 'neutral') {
const emotionSupportedModels = ['speech-2.6-hd', 'speech-2.6-turbo'];
if (!emotionSupportedModels.includes(model)) {
issues.push(`Emotion parameter not supported by model ${model}`);
}
}
return issues;
}
}
//# sourceMappingURL=tts-service.js.map