json(['error' => true, 'message' => 'Text is required']);
}
$service = new TtsService();
$result = $service->synthesize($text, $lang);
if (!isset($result['error'])) {
$result['hash'] = TtsService::cacheKey($text, $lang);
}
$this->json($result);
} catch (\Throwable $e) {
$this->json(['error' => true, 'message' => $e->getMessage()]);
}
}
public function check() {
$checks = [];
$checks[] = '
PHP Info
';
$checks[] = 'PHP Version: ' . PHP_VERSION . '
';
$checks[] = 'Server: ' . ($_SERVER['SERVER_SOFTWARE'] ?? 'Unknown') . '
';
$checks[] = 'Host: ' . ($_SERVER['HTTP_HOST'] ?? 'Unknown') . '
';
$checks[] = 'Required Extensions
';
$checks[] = (extension_loaded('json') ? '✅ json' : '❌ json') . '
';
$checks[] = (extension_loaded('mbstring') ? '✅ mbstring' : '❌ mbstring') . '
';
$checks[] = (extension_loaded('sockets') ? '✅ sockets' : '⚠️ sockets (blocked, not needed — using stream_socket_client)') . '
';
$checks[] = (function_exists('stream_socket_client') ? '✅ stream_socket_client' : '❌ stream_socket_client') . '
';
$checks[] = 'Edge TTS Transport
';
$checks[] = 'Native EdgeTTS uses stream_socket_client() via ReactPHP (no sockets ext required)
';
$checks[] = (class_exists(\Afaya\EdgeTTS\Service\EdgeTTS::class) ? '✅ EdgeTTS class found' : '❌ EdgeTTS class missing') . '
';
$checks[] = (extension_loaded('curl') ? '✅ curl' : '❌ curl') . '
';
$checks[] = 'Google TTS Lao (tl=lo)
';
$loText = urlencode('ພຸດທະ');
$clients = ['t', 'gtx', 'tw-ob'];
foreach ($clients as $client) {
$url = "https://translate.google.com/translate_tts?ie=UTF-8&q={$loText}&tl=lo&client={$client}";
$ch = curl_init($url);
curl_setopt_array($ch, [
CURLOPT_RETURNTRANSFER => true, CURLOPT_TIMEOUT => 8, CURLOPT_SSL_VERIFYPEER => false,
CURLOPT_USERAGENT => 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36',
CURLOPT_REFERER => 'https://translate.google.com/',
]);
$res = curl_exec($ch);
$code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
$checks[] = "Client '{$client}': HTTP {$code} (" . strlen($res) . " bytes)
";
}
$checks[] = 'FreeTTS Test
';
$ftUrl = 'https://freetts.org/api/tts';
$payload = json_encode(['text' => 'ພຸດທະ', 'voice' => 'lo-LA-KeomanyNeural', 'rate' => '+0%', 'pitch' => '+0Hz']);
$ch = curl_init($ftUrl);
curl_setopt_array($ch, [
CURLOPT_POST => true, CURLOPT_POSTFIELDS => $payload, CURLOPT_RETURNTRANSFER => true, CURLOPT_TIMEOUT => 10,
CURLOPT_SSL_VERIFYPEER => false, CURLOPT_HTTPHEADER => ['Content-Type: application/json', 'User-Agent: Mozilla/5.0']
]);
$res = curl_exec($ch);
$code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
$checks[] = "Status: {$code}
";
if ($code === 200) {
$data = json_decode($res, true);
$checks[] = "File ID: " . ($data['file_id'] ?? 'none') . "
";
} else {
$checks[] = "Error: " . htmlspecialchars(substr($res, 0, 100)) . "
";
}
$checks[] = 'TTS Service Test (Lao)
';
try {
$service = new TtsService();
$result = $service->synthesize('ພຸດທະ', 'lo-LA');
if (isset($result['error'])) {
$checks[] = '❌ Error: ' . htmlspecialchars($result['message']) . '
';
if (isset($result['debug_info'])) $checks[] = 'Debug: ' . json_encode($result['debug_info']) . '
';
} else {
$audioLen = strlen(base64_decode($result['audioContent']));
$checks[] = "✅ SUCCESS! Generated {$audioLen} bytes of Lao audio.
";
}
} catch (\Throwable $e) {
$checks[] = '❌ Exception: ' . htmlspecialchars($e->getMessage()) . '
';
}
echo '';
echo 'TTS Diagnostics
';
echo implode("\n", $checks);
echo '';
}
public function play($hash) {
$text = $_GET['text'] ?? '';
$language = $_GET['language'] ?? 'lo-LA';
if (empty($text)) {
http_response_code(400);
header('Content-Type: application/json');
echo json_encode(['error' => 'text parameter required']);
exit;
}
$service = new TtsService();
$result = $service->synthesize($text, $language);
if (isset($result['error'])) {
http_response_code(500);
header('Content-Type: application/json');
echo json_encode($result);
exit;
}
$audio = base64_decode($result['audioContent'], true);
if ($audio === false || strlen($audio) < 100) {
http_response_code(502);
header('Content-Type: application/json');
echo json_encode(['error' => 'Failed to decode audio']);
exit;
}
while (ob_get_level()) ob_end_clean();
header('Content-Type: audio/mpeg');
header('Content-Length: ' . strlen($audio));
header('Accept-Ranges: bytes');
header('Cache-Control: public, max-age=31536000');
echo $audio;
exit;
}
public function cache() {
try {
$input = json_decode(file_get_contents('php://input'), true);
$text = $input['text'] ?? '';
$lang = $input['language'] ?? 'lo-LA';
$force = !empty($input['force']);
if (empty($text)) {
$this->json(['error' => true, 'message' => 'Text is required']);
}
$dir = __DIR__ . '/../../storage/tts';
if (!is_dir($dir)) @mkdir($dir, 0755, true);
$hash = TtsService::cacheKey($text, $lang);
$mp3File = $dir . '/' . $hash . '.mp3';
$metaFile = $dir . '/' . $hash . '.json';
if (file_exists($mp3File) && !$force) {
$meta = file_exists($metaFile) ? json_decode(file_get_contents($metaFile), true) : [];
$this->json([
'cached' => true,
'hash' => $hash,
'timepoints' => $meta['timepoints'] ?? [],
]);
}
if ($force) { @unlink($mp3File); @unlink($metaFile); }
// Same engine chain as /api/tts/synthesize (pre-generated Charon library for Lao, then Edge)
$result = (new TtsService())->synthesize($text, $lang);
if (isset($result['error'])) {
$this->json($result);
}
$this->json([
'cached' => false,
'hash' => $hash,
'audioContent' => $result['audioContent'],
'timepoints' => $result['timepoints'] ?? [],
]);
} catch (\Throwable $e) {
$this->json(['error' => true, 'message' => $e->getMessage()]);
}
}
private function json($data) {
while (ob_get_level()) ob_end_clean();
header('Content-Type: application/json; charset=utf-8');
echo json_encode($data, JSON_UNESCAPED_UNICODE);
exit;
}
}