![]() Server : Apache/2 System : Linux server-15-235-50-60 5.15.0-164-generic #174-Ubuntu SMP Fri Nov 14 20:25:16 UTC 2025 x86_64 User : gositeme ( 1004) PHP Version : 8.2.29 Disable Function : exec,system,passthru,shell_exec,proc_close,proc_open,dl,popen,show_source,posix_kill,posix_mkfifo,posix_getpwuid,posix_setpgid,posix_setsid,posix_setuid,posix_setgid,posix_seteuid,posix_setegid,posix_uname Directory : /home/gositeme/domains/soundstudiopro.com/public_html/studio/ |
<?php
session_start();
require_once __DIR__ . '/../config/database.php';
require_once __DIR__ . '/../api_functions.php';
// Check if user is logged in
if (!isset($_SESSION['user_id'])) {
header('Location: /auth/login.php');
exit;
}
// Get user data
$pdo = getDBConnection();
$stmt = $pdo->prepare("SELECT * FROM users WHERE id = ?");
$stmt->execute([$_SESSION['user_id']]);
$user = $stmt->fetch();
if (!$user) {
$_SESSION['error'] = 'User not found';
header('Location: /studio.php');
exit;
}
// Check credits (3 credits for Add Vocals)
$creditCost = 3;
if ($user['credits'] < $creditCost) {
$_SESSION['error'] = "Insufficient credits. You need $creditCost credits to add vocals.";
header('Location: /studio.php');
exit;
}
// Get form data
$title = trim($_POST['title'] ?? '');
$trackId = $_POST['track_id'] ?? null;
$audioUrl = trim($_POST['audioUrl'] ?? '');
$audioFile = $_FILES['audioFile'] ?? null;
$lyrics = trim($_POST['lyrics'] ?? '');
$vocalStyle = $_POST['vocalStyle'] ?? '';
$language = $_POST['language'] ?? 'English';
$personaId = $_POST['personaId'] ?? null;
if (empty($title)) {
$_SESSION['error'] = 'Track title is required';
header('Location: /studio.php');
exit;
}
// Determine audio source
$finalAudioUrl = null;
if ($trackId) {
// Get audio URL from existing track
$stmt = $pdo->prepare("SELECT audio_url FROM music_tracks WHERE id = ? AND user_id = ?");
$stmt->execute([$trackId, $_SESSION['user_id']]);
$track = $stmt->fetch();
if ($track && $track['audio_url']) {
$finalAudioUrl = $track['audio_url'];
}
}
if (!$finalAudioUrl && $audioUrl) {
$finalAudioUrl = $audioUrl;
}
if (!$finalAudioUrl && $audioFile && $audioFile['error'] === UPLOAD_ERR_OK) {
// Handle file upload
$uploadDir = 'uploads/';
if (!is_dir($uploadDir)) {
mkdir($uploadDir, 0755, true);
}
$fileName = time() . '_' . $_SESSION['user_id'] . '_' . basename($audioFile['name']);
$uploadPath = $uploadDir . $fileName;
if (move_uploaded_file($audioFile['tmp_name'], $uploadPath)) {
$finalAudioUrl = 'https://soundstudiopro.com/' . $uploadPath;
} else {
$_SESSION['error'] = 'Failed to upload audio file.';
header('Location: /studio.php');
exit;
}
}
if (!$finalAudioUrl) {
$_SESSION['error'] = 'Please provide an audio track (select existing track, URL, or upload file)';
header('Location: /studio.php');
exit;
}
// Deduct credits
$newCredits = $user['credits'] - $creditCost;
$stmt = $pdo->prepare("UPDATE users SET credits = ? WHERE id = ?");
$stmt->execute([$newCredits, $_SESSION['user_id']]);
// Record credit transaction
$stmt = $pdo->prepare("
INSERT INTO credit_transactions (user_id, amount, type, description, created_at)
VALUES (?, ?, 'usage', 'Add Vocals: $title', NOW())
");
$stmt->execute([$_SESSION['user_id'], -$creditCost]);
$_SESSION['credits'] = $newCredits;
// Initialize API
$api_key = '63edba40620216c5aa2c04240ac41dbd'; // Your API key from api.box
$api = new APIBoxFunctions($api_key);
// Create track record
$temp_task_id = 'add_vocals_' . time() . '_' . $_SESSION['user_id'] . '_' . uniqid();
$metadata = [
'original_audio_url' => $finalAudioUrl,
'vocal_style' => $vocalStyle,
'language' => $language,
'persona_id' => $personaId,
'has_lyrics' => !empty($lyrics)
];
$track_id = $api->createTrackRecord($_SESSION['user_id'], $temp_task_id, $title, 'Add vocals to instrumental', 'add_vocals', $metadata);
// Prepare API request
$api_params = [
'audioUrl' => $finalAudioUrl,
'callBackUrl' => 'https://soundstudiopro.com/callback.php'
];
if (!empty($lyrics)) {
$api_params['lyrics'] = $lyrics;
}
if (!empty($vocalStyle)) {
$api_params['vocalStyle'] = $vocalStyle;
}
if (!empty($language)) {
$api_params['language'] = $language;
}
if ($personaId) {
$api_params['personaId'] = $personaId;
}
// Call Add Vocals API
$result = $api->addVocals($api_params);
if (isset($result['error'])) {
error_log("Add Vocals API error: " . json_encode($result));
$_SESSION['error'] = 'Failed to add vocals. Please try again.';
header('Location: /studio.php');
exit;
}
// Success
$_SESSION['success'] = "Vocals are being added to your track. You'll be notified when it's ready!";
header('Location: /studio.php');
exit;
?>