🎵 Sound Effect Generator
$text,
'prompt_influence' => (float)$promptInfluence
];
if (!empty($duration) && is_numeric($duration)) {
$data['duration_seconds'] = (float)$duration;
}
$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)) {
$error = 'Connection error: ' . curl_error($ch);
curl_close($ch);
return false;
}
curl_close($ch);
if ($httpCode === 200) {
$filename = 'sound_effect_' . 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)) {
return 'generated/' . $filename;
} else {
$error = 'Failed to save the generated sound file.';
return false;
}
} else {
$errorResponse = json_decode($response, true);
$error = 'API Error (' . $httpCode . '): ' . ($errorResponse['detail'] ?? 'Unknown error occurred');
return false;
}
}
?>
Error: