Spaces:
Running
Running
File size: 8,162 Bytes
7731906 078f847 7731906 7c4c4b4 7731906 7c4c4b4 7731906 078f847 7731906 798f9fe 7731906 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 | <?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;
}
}
|