![]() 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/private_html/ |
# Callback.php Flow Explanation
## Overview
The callback.php file receives webhooks from the API.Box music generation service when tracks are processed. It handles multiple callback formats and updates the database accordingly.
## Main Flow
### 1. **Initial Setup** (Lines 1-34)
- Sets up error reporting and headers
- Logs all incoming callbacks to `callback_log.txt`
- Reads raw input from API
### 2. **Data Parsing** (Lines 651-660)
- Parses JSON from API
- Logs the parsed structure
### 3. **Callback Type Detection** (Lines 662-1312)
The callback handles **4 different formats**:
#### **Format 1: Standard Callback with `task_id`** (Lines 663-843)
```json
{
"task_id": "abc123",
"status": "complete",
"audio_url": "...",
"data": {...}
}
```
**Process:**
1. Gets `task_id` and `status`
2. Fetches original track data from database (prompt, title)
3. Extracts metadata using `extractComprehensiveMetadata()`
4. Extracts title, duration, tags, model_name, lyrics
5. Downloads audio/video files locally
6. Updates database with `updateMusicTrack()`
7. Saves full response to `task_results/{task_id}.json`
#### **Format 2: Alternative Format with `id`** (Lines 845-939)
```json
{
"id": "abc123",
"status": "complete",
...
}
```
- Same process as Format 1, but uses `id` instead of `task_id`
#### **Format 3: Error Callback (Code 531)** (Lines 941-963)
```json
{
"code": 531,
"msg": "Generation failed",
"data": {
"task_id": "abc123"
}
}
```
- Marks track as `failed` in database
- Logs error message
#### **Format 4: Complex Callback with `callbackType`** (Lines 965-1301)
```json
{
"callbackType": "complete",
"task_id": "abc123",
"data": {...}
}
```
**Handles different callback types:**
- `complete` - Track is finished
- `text` - Text/lyrics update
- `first` - First chunk of data
- `processing` - Still processing
**For `complete` callbacks:**
1. Extracts audio URLs from nested structure
2. Handles variations (multiple audio files)
3. Downloads all audio files
4. Updates database with all variations
5. Stores variations in `audio_variations` table
## Key Functions
### `extractComprehensiveMetadata($data, $originalPrompt)` (Line 445)
- **Purpose**: Extracts BPM, Genre, Key, Mood from API response
- **Priority**: API response → Prompt parsing → Defaults
- **Returns**: Array with all metadata
### `extractTitleFromCallback($data)` (Line 82)
- Checks multiple locations: `data['title']`, `data['data']['title']`, `data['data']['data'][0]['title']`
- Returns first found title
### `extractDurationFromCallback($data)` (Line 114)
- Checks: `data['duration']`, `data['data']['duration']`, etc.
- Returns duration in seconds
### `extractTagsFromCallback($data)` (Line 146)
- Extracts tags from various locations
- Returns array of tags
### `getTrackOriginalData($taskId)` (Line 307)
- Fetches original prompt and title from database
- Used to preserve user-provided titles
### `updateMusicTrack()` (from config/database.php)
- Updates track in database with:
- Status (processing/complete/failed)
- Audio/video URLs
- Lyrics
- Metadata (JSON)
- Title, duration, tags, model_name
### `downloadAndStoreAudio($url, $taskId, $type)` (Line ~200)
- Downloads audio from API URL
- Saves locally to `audio_files/` directory
- Returns local URL
## Title Handling Logic
1. **If user provided title** (not empty, not "Untitled Track"):
- ✅ **KEEP user title** - never overwrite
2. **If no user title** (empty or default):
- ✅ **Use API title** if available
- ⚠️ **Leave empty** if API doesn't provide
## Metadata Extraction Priority
1. **API Response** (highest priority)
- Uses `data['bpm']`, `data['genre']`, `data['key']`, etc.
2. **Prompt Parsing** (fallback)
- Parses original prompt for "BPM: 150", "Style: Electronic", etc.
3. **Defaults** (last resort)
- Genre: "Electronic"
- BPM: Random 80-160
- Key: "C major"
- Mood: "neutral"
## Status Flow
```
processing → complete ✅
processing → failed ❌
```
## File Storage
- **Logs**: `callback_log.txt` - All callback activity
- **Results**: `task_results/{task_id}.json` - Full API response backup
- **Audio**: `audio_files/{task_id}_main.mp3` - Downloaded audio files
- **Variations**: `audio_files/{task_id}_variation_{index}.mp3`
## Common Issues
### Issue 1: Empty Title
- **Cause**: User didn't provide title AND API didn't return one
- **Fix**: Check `callback_log.txt` for "No title available"
### Issue 2: Wrong Genre/Style
- **Cause**: API returned different genre than prompt
- **Fix**: Check metadata extraction logs in `callback_log.txt`
### Issue 3: Missing Audio
- **Cause**: Download failed or URL invalid
- **Fix**: Check "Downloaded main audio to: failed" in logs
### Issue 4: Variations Not Saved
- **Cause**: Callback format doesn't match expected structure
- **Fix**: Check `callback_log.txt` for "Stored X variations"
## Debugging
1. **Check `callback_log.txt`** - See what API sent
2. **Check `task_results/{task_id}.json`** - Full API response
3. **Check database** - See what was actually saved
4. **Check error logs** - PHP errors in server logs
## Current Problems to Fix
1. **Style defaulting to "Pop"** - Fixed in create_music.php (now only sends if genre specified)
2. **Empty title** - Fixed in create_music.php (now only sends if title provided)
3. **Duration wrong** - Check `create_music_debug.log` to see what's being sent