/**
* analyze_loudness Tool
*
* Analyzes audio loudness metrics following EBU R128 standard.
*/
import { Tool } from '@modelcontextprotocol/sdk/types.js';
import { LoudnessAnalysisResult } from '../types/mcp-tools.js';
import {
IRCAM_API_CONFIG,
IrcamLoudnessResponse,
transformLoudnessResponse,
} from '../types/ircam-api.js';
import { httpPost, buildApiUrl } from '../utils/http.js';
import { validateAudioUrl } from '../utils/validation.js';
import { formatError } from '../utils/errors.js';
/**
* Tool definition for MCP registration
*/
export const analyzeLoudnessTool: Tool = {
name: 'analyze_loudness',
description:
'Analyze the loudness of an audio file following EBU R128 standard. ' +
'Accepts a public URL to an audio file (MP3, WAV, FLAC, OGG, M4A). ' +
'Returns integrated loudness (LUFS), true peak (dB), and loudness range (LU).',
inputSchema: {
type: 'object',
properties: {
audio_url: {
type: 'string',
description: 'Public URL to the audio file to analyze',
},
},
required: ['audio_url'],
},
};
/**
* Handle analyze_loudness tool call
*/
export async function handleAnalyzeLoudness(
args: Record<string, unknown>
): Promise<LoudnessAnalysisResult> {
const audioUrl = args.audio_url as string;
// Validate input
const validation = validateAudioUrl(audioUrl);
if (!validation.valid) {
throw validation.error;
}
// Call IRCAM Loudness Analyzer API
const url = buildApiUrl(IRCAM_API_CONFIG.ENDPOINTS.LOUDNESS_ANALYZER);
const response = await httpPost<IrcamLoudnessResponse>(url, {
url: audioUrl,
});
if (!response.ok || !response.data) {
throw response.error || formatError('UNKNOWN_ERROR', 'Failed to analyze loudness');
}
// Transform and return result
return transformLoudnessResponse(response.data);
}