web / app /Controllers /TtsController.php
souliya_pps
Claude Opus 5.5
feat(tts): serve pre-generated Gemini Charon audio for Lao sutras
798f9fe
Raw History Blame Contribute Delete
8.16 kB
<?php
namespace App\Controllers;
use App\Services\TtsService;
class TtsController {
private $supportedLangs = ['lo-LA', 'th-TH', 'en-US'];
public function synthesize() {
try {
$input = json_decode(file_get_contents('php://input'), true);
$text = $input['text'] ?? '';
$lang = $input['language'] ?? 'lo-LA';
if (empty($text)) {
$this->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[] = '<h3>PHP Info</h3>';
$checks[] = 'PHP Version: ' . PHP_VERSION . '<br>';
$checks[] = 'Server: ' . ($_SERVER['SERVER_SOFTWARE'] ?? 'Unknown') . '<br>';
$checks[] = 'Host: ' . ($_SERVER['HTTP_HOST'] ?? 'Unknown') . '<br>';
$checks[] = '<h3>Required Extensions</h3>';
$checks[] = (extension_loaded('json') ? '✅ json' : '❌ json') . '<br>';
$checks[] = (extension_loaded('mbstring') ? '✅ mbstring' : '❌ mbstring') . '<br>';
$checks[] = (extension_loaded('sockets') ? '✅ sockets' : '⚠️ sockets (blocked, not needed — using stream_socket_client)') . '<br>';
$checks[] = (function_exists('stream_socket_client') ? '✅ stream_socket_client' : '❌ stream_socket_client') . '<br>';
$checks[] = '<h3>Edge TTS Transport</h3>';
$checks[] = 'Native EdgeTTS uses <strong>stream_socket_client()</strong> via ReactPHP (no sockets ext required)<br>';
$checks[] = (class_exists(\Afaya\EdgeTTS\Service\EdgeTTS::class) ? '✅ EdgeTTS class found' : '❌ EdgeTTS class missing') . '<br>';
$checks[] = (extension_loaded('curl') ? '✅ curl' : '❌ curl') . '<br>';
$checks[] = '<h3>Google TTS Lao (tl=lo)</h3>';
$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)<br>";
}
$checks[] = '<h3>FreeTTS Test</h3>';
$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}<br>";
if ($code === 200) {
$data = json_decode($res, true);
$checks[] = "File ID: " . ($data['file_id'] ?? 'none') . "<br>";
} else {
$checks[] = "Error: " . htmlspecialchars(substr($res, 0, 100)) . "<br>";
}
$checks[] = '<h3>TTS Service Test (Lao)</h3>';
try {
$service = new TtsService();
$result = $service->synthesize('ພຸດທະ', 'lo-LA');
if (isset($result['error'])) {
$checks[] = '❌ Error: ' . htmlspecialchars($result['message']) . '<br>';
if (isset($result['debug_info'])) $checks[] = 'Debug: ' . json_encode($result['debug_info']) . '<br>';
} else {
$audioLen = strlen(base64_decode($result['audioContent']));
$checks[] = "✅ SUCCESS! Generated {$audioLen} bytes of Lao audio.<br>";
}
} catch (\Throwable $e) {
$checks[] = '❌ Exception: ' . htmlspecialchars($e->getMessage()) . '<br>';
}
echo '<html><body style="font-family:sans-serif;padding:20px;font-size:14px">';
echo '<h2>TTS Diagnostics</h2>';
echo implode("\n", $checks);
echo '</body></html>';
}
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;
}
}