$chunkText) { $idx = $i; $bin = fetch_tts_chunk($chunkText, $idx, $totalChunks); if ($bin === false) { http_response_code(502); header('Content-Type: text/plain; charset=utf-8'); echo "Failed to fetch audio from TTS provider. Check tts_error.log on server."; exit; } // append binary data $totalAudio .= $bin; } // output combined MP3 // Clean any output buffer (to avoid corrupting audio) while (ob_get_level()) ob_end_clean(); header('Content-Type: audio/mpeg'); header('Content-Disposition: attachment; filename="hindi-tts.mp3"'); echo $totalAudio; exit;