🎵 Music Generator

Note: ElevenLabs Music Generation requires special access and additional terms acceptance. Contact your account team to enable music generation access. Learn more about music terms
$prompt ]; // Only add music_length_ms if it's specified and valid (minimum 10 seconds) if (!empty($musicLengthMs) && is_numeric($musicLengthMs) && $musicLengthMs >= 10000) { $data['music_length_ms'] = (int)$musicLengthMs; } // Don't include model_id as it defaults to music_v1 return generateMusicRequest($url, $data, $apiKey, $error, 'ElevenLabs Music Generation', $prompt); } function generateMusicWithPlan($compositionPlan, $apiKey, &$error) { $url = 'https://api.elevenlabs.io/v1/music'; $data = [ 'composition_plan' => json_decode($compositionPlan, true), 'model_id' => 'music_v1' ]; return generateMusicRequest($url, $data, $apiKey, $error, 'ElevenLabs Music Generation (Advanced)', 'Advanced composition'); } function generateMusicRequest($url, $data, $apiKey, &$error, $generationType, $prompt) { $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, 120); // Increased timeout for music generation $response = curl_exec($ch); $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE); // Log the request for debugging error_log("Music API Request - URL: $url, Data: " . json_encode($data) . ", HTTP Code: $httpCode, Response: " . substr($response, 0, 500)); if (curl_error($ch)) { $error = 'Connection error: ' . curl_error($ch); curl_close($ch); return false; } curl_close($ch); if ($httpCode === 200) { $filename = 'music_' . date('Y-m-d_H-i-s') . '.mp3'; $filepath = __DIR__ . '/generated/' . $filename; if (!is_dir(__DIR__ . '/generated')) { mkdir(__DIR__ . '/generated', 0755, true); } if (file_put_contents($filepath, $response)) { // Send webhook for provenance tracking try { $webhookProcessor = new AudioWebhookProcessor(); // Get user info from SSO session if available $userEmail = 'music-generation@oliver.agency'; // Default $userName = 'Anonymous User'; // Default // Check for SSO session data (when SSO is enabled) if (isset($_SESSION['sso_user_email'])) { $userEmail = $_SESSION['sso_user_email']; } if (isset($_SESSION['sso_user_name'])) { $userName = $_SESSION['sso_user_name']; } $webhookData = [ 'prompt' => $prompt, 'generation_type' => $generationType, 'settings' => $data, 'audio_data' => $response, 'client' => 'Oliver Agency - Music Generation', 'user_email' => $userEmail, 'user_name' => $userName, 'deliverable_number' => '1000000', 'additional_data' => [ 'filename' => $filename, 'file_size' => strlen($response), 'generation_timestamp' => date('Y-m-d H:i:s'), 'api_endpoint' => 'elevenlabs-music', 'content_type' => 'Music Generation', 'generation_mode' => isset($data['composition_plan']) ? 'Advanced (with Composition Plan)' : 'Simple Prompt', 'user_agent' => $_SERVER['HTTP_USER_AGENT'] ?? 'Unknown', 'ip_address' => $_SERVER['REMOTE_ADDR'] ?? 'Unknown' ] ]; $webhookSuccess = $webhookProcessor->sendGenerationData($webhookData); if (!$webhookSuccess) { error_log("Webhook failed for music generation: " . $filename); } } catch (Exception $e) { error_log("Webhook error: " . $e->getMessage()); } return 'generated/' . $filename; } else { $error = 'Failed to save the generated music file.'; return false; } } else { // Better error handling $errorResponse = json_decode($response, true); if (json_last_error() === JSON_ERROR_NONE && is_array($errorResponse)) { // Successfully parsed JSON response if (isset($errorResponse['detail'])) { $errorDetail = is_array($errorResponse['detail']) ? json_encode($errorResponse['detail']) : $errorResponse['detail']; } elseif (isset($errorResponse['message'])) { $errorDetail = is_array($errorResponse['message']) ? json_encode($errorResponse['message']) : $errorResponse['message']; } elseif (isset($errorResponse['error'])) { $errorDetail = is_array($errorResponse['error']) ? json_encode($errorResponse['error']) : $errorResponse['error']; } else { $errorDetail = json_encode($errorResponse); } } else { // Raw response if JSON parsing failed $errorDetail = substr($response, 0, 500); } $error = 'API Error (' . $httpCode . '): ' . $errorDetail; return false; } } ?>
Simple mode uses just a prompt, Advanced mode creates a detailed composition plan first
Describe the music you want to generate (max 2000 characters)
Auto
0 = Auto duration, 10-300 seconds for manual control
Error:


Download Music Track