Skip to main content
Glama

synthesize_speech

Convert text to audio using customizable voice, speed, emotion, and pacing for expressive and professional speech synthesis in multiple formats.

Instructions

Convert text to speech with advanced voice controls and natural expression

Input Schema

NameRequiredDescriptionDefault
emotionNoVoice emotionneutral
filenameNoCustom filename for saved audio
outputFormatNoAudio output formatwav
pacingNoSpeech pacing stylenatural
saveFileNoSave audio to file
speedNoSpeech speed (0.25-3.0)
textYesText to convert to speech
voiceIdNoVoice to use for synthesisaf_heart
volumeNoAudio volume (0.1-2.0)

Input Schema (JSON Schema)

{ "properties": { "emotion": { "default": "neutral", "description": "Voice emotion", "enum": [ "neutral", "happy", "excited", "calm", "serious", "casual", "confident" ], "type": "string" }, "filename": { "description": "Custom filename for saved audio", "type": "string" }, "outputFormat": { "default": "wav", "description": "Audio output format", "enum": [ "wav", "mp3", "flac", "ogg" ], "type": "string" }, "pacing": { "default": "natural", "description": "Speech pacing style", "enum": [ "natural", "conversational", "presentation", "tutorial", "narrative", "fast", "slow" ], "type": "string" }, "saveFile": { "default": false, "description": "Save audio to file", "type": "boolean" }, "speed": { "default": 1, "description": "Speech speed (0.25-3.0)", "maximum": 3, "minimum": 0.25, "type": "number" }, "text": { "description": "Text to convert to speech", "maxLength": 10000, "type": "string" }, "voiceId": { "default": "af_heart", "description": "Voice to use for synthesis", "enum": [ "af_heart", "af_sky", "af_bella", "af_sarah", "af_nicole", "am_adam", "am_michael", "bf_emma", "bf_isabella", "bm_lewis" ], "type": "string" }, "volume": { "default": 1, "description": "Audio volume (0.1-2.0)", "maximum": 2, "minimum": 0.1, "type": "number" } }, "required": [ "text" ], "type": "object" }

Implementation Reference

  • Main handler function for synthesize_speech tool. Validates inputs, enhances text for natural speech, calculates adjusted speed, and orchestrates native TTS synthesis.
    async synthesizeSpeech(params: { text: string; voiceId?: string; speed?: number; emotion?: keyof typeof VoiceEmotions; pacing?: keyof typeof PacingStyles; volume?: number; outputFormat?: string; saveFile?: boolean; filename?: string; }) { const { text, voiceId = this.config.defaultVoice, speed = this.config.defaultSpeed, emotion = this.config.defaultEmotion as keyof typeof VoiceEmotions, pacing = this.config.defaultPacing as keyof typeof PacingStyles, volume = 1.0, outputFormat = 'wav', saveFile = this.config.enableFileOutput, filename } = params; // Ensure TTS engine is initialized await this.ensureInitialized(); // Input validation and sanitization if (!text || typeof text !== 'string') { throw new Error('Text must be a non-empty string'); } if (text.length > this.config.maxTextLength) { throw new Error(`Text length (${text.length}) exceeds maximum allowed (${this.config.maxTextLength})`); } // Validate voice ID if (voiceId && !Object.keys(AvailableVoices).includes(voiceId)) { throw new Error(`Invalid voice ID: ${voiceId}`); } // Validate speed range if (speed < 0.25 || speed > 3.0) { throw new Error(`Speed ${speed} out of range (0.25-3.0)`); } // Validate volume range if (volume < 0.1 || volume > 2.0) { throw new Error(`Volume ${volume} out of range (0.1-2.0)`); } // Validate output format if (!AudioFormats.includes(outputFormat as any)) { throw new Error(`Unsupported output format: ${outputFormat}`); } // Sanitize text input const sanitizedText = text .replace(/[<>]/g, '') // Remove HTML-like tags .replace(/[\x00-\x1F\x7F]/g, '') // Remove control characters .trim(); if (!sanitizedText) { throw new Error('Text is empty after sanitization'); } // Enhance text for natural speech const enhancedText = this.enhanceTextForSpeech(sanitizedText, emotion, pacing); // Calculate final speed const finalSpeed = this.calculateFinalSpeed(speed, emotion, pacing); // Synthesize using native TypeScript TTS engine const result = await this.synthesizeWithNativeTTS( enhancedText, voiceId, finalSpeed, emotion, pacing, volume, outputFormat, saveFile, filename ); if (result.success) { this.activeRequests.set(result.requestId, { status: 'completed', result }); return { success: true, requestId: result.requestId, duration: result.duration, voiceUsed: voiceId, speedUsed: finalSpeed, emotion, pacing, outputFormat, filePath: saveFile ? result.audioPath : undefined, message: `Successfully synthesized ${text.length} characters with ${voiceId} voice` }; } else { throw new Error(result.error || 'Synthesis failed'); } }
  • Tool registration in MCP ListTools handler defining name, description, and input schema.
    { name: 'synthesize_speech', description: 'Convert text to speech with advanced voice controls and natural expression', inputSchema: { type: 'object', properties: { text: { type: 'string', description: 'Text to convert to speech', maxLength: config.maxTextLength, }, voiceId: { type: 'string', description: 'Voice to use for synthesis', enum: Object.keys(AvailableVoices), default: config.defaultVoice, }, speed: { type: 'number', description: 'Speech speed (0.25-3.0)', minimum: 0.25, maximum: 3.0, default: config.defaultSpeed, }, emotion: { type: 'string', description: 'Voice emotion', enum: Object.keys(VoiceEmotions), default: config.defaultEmotion, }, pacing: { type: 'string', description: 'Speech pacing style', enum: Object.keys(PacingStyles), default: config.defaultPacing, }, volume: { type: 'number', description: 'Audio volume (0.1-2.0)', minimum: 0.1, maximum: 2.0, default: 1.0, }, outputFormat: { type: 'string', description: 'Audio output format', enum: [...AudioFormats], default: 'wav', }, saveFile: { type: 'boolean', description: 'Save audio to file', default: config.enableFileOutput, }, filename: { type: 'string', description: 'Custom filename for saved audio', }, }, required: ['text'], }, },
  • MCP CallToolRequestSchema handler case that receives tool call and invokes the synthesizeSpeech implementation.
    case 'synthesize_speech': const result = await ttsServer.synthesizeSpeech(args as any); return { content: [ { type: 'text', text: `🎙️ **TTS Synthesis Complete**\n\n` + `**Request ID:** ${result.requestId}\n` + `**Voice:** ${result.voiceUsed} (${AvailableVoices[result.voiceUsed as keyof typeof AvailableVoices]?.name})\n` + `**Duration:** ${result.duration?.toFixed(2)}s\n` + `**Speed:** ${result.speedUsed?.toFixed(2)}x\n` + `**Emotion:** ${result.emotion}\n` + `**Pacing:** ${result.pacing}\n` + `**Format:** ${result.outputFormat.toUpperCase()}\n` + `${result.filePath ? `**File:** ${result.filePath}\n` : ''}` + `\n${result.message}`, }, ], };
  • JSON schema for synthesize_speech tool inputs including all parameters and validation rules.
    type: 'object', properties: { text: { type: 'string', description: 'Text to convert to speech', maxLength: config.maxTextLength, }, voiceId: { type: 'string', description: 'Voice to use for synthesis', enum: Object.keys(AvailableVoices), default: config.defaultVoice, }, speed: { type: 'number', description: 'Speech speed (0.25-3.0)', minimum: 0.25, maximum: 3.0, default: config.defaultSpeed, }, emotion: { type: 'string', description: 'Voice emotion', enum: Object.keys(VoiceEmotions), default: config.defaultEmotion, }, pacing: { type: 'string', description: 'Speech pacing style', enum: Object.keys(PacingStyles), default: config.defaultPacing, }, volume: { type: 'number', description: 'Audio volume (0.1-2.0)', minimum: 0.1, maximum: 2.0, default: 1.0, }, outputFormat: { type: 'string', description: 'Audio output format', enum: [...AudioFormats], default: 'wav', }, saveFile: { type: 'boolean', description: 'Save audio to file', default: config.enableFileOutput, }, filename: { type: 'string', description: 'Custom filename for saved audio', }, }, required: ['text'],
  • NativeTTSEngine.synthesizeText - Low-level TTS synthesis called by main handler to produce raw audio data.
    async synthesizeText( text: string, voiceConfig: VoiceConfig, outputFormat: string = 'wav' ): Promise<SynthesisResult> { const requestId = randomUUID(); try { await this.initialize(); // Validate inputs if (!text || text.length === 0) { throw new Error('Text cannot be empty'); } if (text.length > 50000) { throw new Error(`Text too long: ${text.length} characters (max 50000)`); } // Simulate neural TTS synthesis const audioSegment = await this.generateAudioSegment(text, voiceConfig); return { success: true, audioSegment, requestId }; } catch (error) { return { success: false, error: error instanceof Error ? error.message : String(error), requestId }; }

Other Tools

Related Tools

Latest Blog Posts

MCP directory API

We provide all the information about MCP servers via our MCP API.

curl -X GET 'https://glama.ai/api/mcp/v1/servers/samihalawa/advanced-tts-mcp'

If you have feedback or need assistance with the MCP directory API, please join our Discord server