music-generation/generate_plan.php
DJP ddd35655da Convert ElevenLabs Sound Effects to Music Generation API
- Updated API endpoints from sound-generation to music endpoints
- Added simple prompt music generation mode
- Added advanced mode with composition plan generation
- Disabled SSO login for local testing
- Updated UI to reflect music generation instead of sound effects
- Created separate endpoint for composition plan generation
- Updated webhook tracking for music generation

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <noreply@anthropic.com>
2025-08-19 10:11:07 -04:00

86 lines
No EOL
2.6 KiB
PHP

<?php
// Composition Plan Generation Endpoint for ElevenLabs Music API
header('Content-Type: application/json');
// Load configuration
$config = require_once 'config.php';
if ($_SERVER['REQUEST_METHOD'] !== 'POST') {
http_response_code(405);
echo json_encode(['success' => false, 'error' => 'Method not allowed']);
exit;
}
$prompt = $_POST['prompt'] ?? '';
$musicLengthMs = (int)($_POST['music_length_ms'] ?? 0);
$apiKey = $config['elevenlabs_api_key'];
if (empty($prompt)) {
echo json_encode(['success' => false, 'error' => 'Prompt is required']);
exit;
}
if (empty($apiKey) || $apiKey === 'your-api-key-here') {
echo json_encode(['success' => false, 'error' => 'ElevenLabs API key not configured']);
exit;
}
// Generate composition plan
$planResult = generateCompositionPlan($prompt, $musicLengthMs, $apiKey);
if ($planResult['success']) {
echo json_encode(['success' => true, 'plan' => $planResult['plan']]);
} else {
echo json_encode(['success' => false, 'error' => $planResult['error']]);
}
function generateCompositionPlan($prompt, $musicLengthMs, $apiKey) {
$url = 'https://api.elevenlabs.io/v1/music/plan';
$data = [
'prompt' => $prompt,
'model_id' => 'music_v1'
];
// Add music length if specified (minimum 10 seconds)
if ($musicLengthMs >= 10000) {
$data['music_length_ms'] = $musicLengthMs;
}
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));
curl_setopt($ch, CURLOPT_HTTPHEADER, [
'xi-api-key: ' . $apiKey,
'Content-Type: application/json'
]);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_TIMEOUT, 60);
$response = curl_exec($ch);
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
if (curl_error($ch)) {
curl_close($ch);
return ['success' => false, 'error' => 'Connection error: ' . curl_error($ch)];
}
curl_close($ch);
if ($httpCode === 200) {
$planData = json_decode($response, true);
if ($planData) {
return ['success' => true, 'plan' => $planData];
} else {
return ['success' => false, 'error' => 'Invalid response format'];
}
} else {
$errorResponse = json_decode($response, true);
$errorMessage = $errorResponse['detail'] ?? 'Unknown error occurred';
return ['success' => false, 'error' => 'API Error (' . $httpCode . '): ' . $errorMessage];
}
}
?>