-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvsx.php
More file actions
345 lines (294 loc) · 12.6 KB
/
Copy pathvsx.php
File metadata and controls
345 lines (294 loc) · 12.6 KB
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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
<?php
/* Voice Sample Extractor */
$path = dirname((__FILE__)) . DIRECTORY_SEPARATOR;
$GLOBALS["ENGINE_PATH"]=$path;
require_once $path . "lib/runtime_bootstrap.php";
dialecticRuntimeBootstrap($path, [
'load_general_settings' => true,
'load_stt_connector' => false,
'load_tts_connector' => 'pockettts',
'load_player_name' => true,
]);
require_once $path . "lib/utils.php";
require_once $path . "lib/fuz_convert.php"; // API KEY must be there
require_once $path . "lib/auditing.php";
require_once $path . "lib/logger.php";
require_once $path . "lib/voice_clone_sync.php";
$db = $GLOBALS["db"] ?? new sql();
$GLOBALS["db"] = $db;
require_once $path . "lib/core/npc_master.class.php";
require_once $path . "lib/core/api_badge.class.php";
require_once $path . "lib/core/core_profiles.class.php";
require_once $path . "lib/core/llm_connector.class.php";
require_once $path . "lib/core/tts_connector.class.php";
require_once $path . "lib/semaphore_manager.class.php";
function dialecticVsxRespond(int $statusCode, bool $ok, string $message = '', array $extra = []): void
{
if (!headers_sent()) {
http_response_code($statusCode);
header('Content-Type: application/json; charset=utf-8');
}
$payload = array_merge([
'schema' => 'dialectic.voice_sample.response.v1',
'request_id' => class_exists('Logger') ? Logger::getRequestId() : '',
'ok' => $ok,
], $extra);
if ($message !== '') {
$payload[$ok ? 'message' : 'error'] = $message;
}
echo json_encode($payload, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE) . PHP_EOL;
exit;
}
function dialecticVsxDecodeMetadata(): array
{
$method = strtoupper(strval($_SERVER['REQUEST_METHOD'] ?? 'GET'));
if ($method !== 'POST') {
dialecticVsxRespond(405, false, 'Method Not Allowed', [
'method' => $method,
]);
}
$rawMetadata = trim(strval($_POST['metadata'] ?? ''));
if ($rawMetadata === '') {
dialecticVsxRespond(400, false, 'Missing voice sample metadata');
}
$metadata = json_decode($rawMetadata, true);
if (!is_array($metadata)) {
dialecticVsxRespond(400, false, 'Invalid voice sample metadata JSON');
}
$schema = trim(strval($metadata['schema'] ?? ''));
if ($schema !== 'dialectic.voice_sample.v1') {
dialecticVsxRespond(400, false, 'Unsupported voice sample metadata schema', [
'schema' => $schema,
]);
}
$actorName = trim(strval($metadata['actor_name'] ?? ''));
$sourcePath = trim(strval($metadata['original_name'] ?? ''));
if ($actorName === '' || $sourcePath === '') {
dialecticVsxRespond(400, false, 'Voice sample metadata requires actor_name and original_name');
}
return [
'actor_name' => $actorName,
'original_name' => $sourcePath,
'reference_text' => trim(strval($metadata['reference_text'] ?? '')),
'game' => trim(strval($metadata['game'] ?? 'fnv')),
];
}
function dialecticVsxReplaceFile(string $source, string $target): bool
{
$targetDir = dirname($target);
if (!is_dir($targetDir) && !@mkdir($targetDir, 0775, true) && !is_dir($targetDir)) {
return false;
}
try {
$suffix = bin2hex(random_bytes(6));
} catch (Throwable $_e) {
$suffix = str_replace('.', '', uniqid('', true));
}
$temporary = $target . '.tmp.' . $suffix;
if (!@copy($source, $temporary) || !is_file($temporary) || filesize($temporary) <= 44) {
@unlink($temporary);
return false;
}
if (@rename($temporary, $target)) {
return true;
}
if (is_file($target) && !@unlink($target)) {
@unlink($temporary);
return false;
}
$replaced = @rename($temporary, $target);
if (!$replaced) {
@unlink($temporary);
}
return $replaced;
}
function dialecticVsxWriteSampleMetadata(string $wavPath, string $codename, array $metadata): void
{
$payload = [
'schema' => 'dialectic.voice_sample.metadata.v1',
'voice_id' => $codename,
'source' => strval($metadata['original_name'] ?? ''),
'reference_text' => strval($metadata['reference_text'] ?? ''),
'game' => strval($metadata['game'] ?? 'fnv'),
'sha256' => hash_file('sha256', $wavPath) ?: '',
'bytes' => filesize($wavPath),
'updated_at' => gmdate('c'),
];
@file_put_contents(
preg_replace('/\.wav$/i', '.json', $wavPath),
json_encode($payload, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE)
);
}
function normalize_endpoint_url($url)
{
// Remove trailing slashes
$url = rtrim($url, '/');
return $url;
}
function dialecticVsxResolveCloneTtsRuntime(string $actorName): array
{
$ttsConnector = new TTSConnector();
$supportedCloneDrivers = ['xtts-fastapi', 'chatterbox', 'pockettts', 'inworld'];
$fallbackDriver = $ttsConnector->normalizeDriverValue($GLOBALS["TTSFUNCTION"] ?? 'pockettts');
if ($fallbackDriver === '') {
$fallbackDriver = 'pockettts';
}
$selectedDriver = $fallbackDriver;
$profileData = null;
if ($actorName !== '') {
$profile = new CoreProfile();
if (strcasecmp($actorName, 'The Narrator') === 0) {
require_once $GLOBALS["ENGINE_PATH"] . "lib/core/narrator.class.php";
$narrator = new Narrator();
$profileId = intval($narrator->getProfileId() ?? 0);
if ($profileId > 0) {
$profileData = $profile->getById($profileId);
}
} else {
$npcMaster = new NpcMaster();
$currentNpcData = $npcMaster->getByName($actorName);
if ($currentNpcData) {
$profileId = intval($currentNpcData['profile_id'] ?? 0);
if ($profileId > 0) {
$profileData = $profile->getById($profileId);
} else {
$profileData = $profile->getDefaultNpc();
}
}
}
if ($profileData) {
$profileConnectorRow = $ttsConnector->ensureConnectorForProfile($profileData);
$profileDriver = $ttsConnector->normalizeDriverValue($profileConnectorRow['driver'] ?? '');
if ($profileConnectorRow && in_array($profileDriver, $supportedCloneDrivers, true)) {
$GLOBALS["DIALECTIC_CORE_CURRENT_PROFILE_DATA"] = $profileData;
$profile->setOldGlobals($profileData);
$selectedDriver = $profileDriver;
} elseif ($profileConnectorRow) {
Logger::info("[vsx] Actor '{$actorName}' uses non-clone TTS driver '{$profileDriver}', falling back to {$fallbackDriver}");
}
}
}
$providerKey = $ttsConnector->getProviderKeyFromDriver($selectedDriver);
$providerConfig = ($providerKey !== '' && isset($GLOBALS["TTS"][$providerKey]) && is_array($GLOBALS["TTS"][$providerKey]))
? $GLOBALS["TTS"][$providerKey]
: [];
$endpoint = trim(strval($providerConfig['endpoint'] ?? $providerConfig['url'] ?? $providerConfig['URL'] ?? ''));
if ($endpoint === '' && $selectedDriver !== $fallbackDriver) {
$fallbackProviderKey = $ttsConnector->getProviderKeyFromDriver($fallbackDriver);
$fallbackConfig = ($fallbackProviderKey !== '' && isset($GLOBALS["TTS"][$fallbackProviderKey]) && is_array($GLOBALS["TTS"][$fallbackProviderKey]))
? $GLOBALS["TTS"][$fallbackProviderKey]
: [];
$endpoint = trim(strval($fallbackConfig['endpoint'] ?? $fallbackConfig['url'] ?? $fallbackConfig['URL'] ?? ''));
$providerKey = $fallbackProviderKey;
$providerConfig = $fallbackConfig;
$selectedDriver = $fallbackDriver;
}
$voicelogic = trim(strval($providerConfig['voicelogic'] ?? ''));
if ($voicelogic === '') {
$voicelogic = 'voicetype';
}
return [
'driver' => $selectedDriver,
'provider_key' => $providerKey,
'endpoint' => ($endpoint !== '') ? normalize_endpoint_url($endpoint) : '',
'voicelogic' => $voicelogic,
];
}
$GLOBALS["AUDIT_RUNID_REQUEST"] = "vsx";
// Put info into DB asap
$voiceSampleMetadata = dialecticVsxDecodeMetadata();
$actorName = $voiceSampleMetadata['actor_name'];
$sourcePath = $voiceSampleMetadata['original_name'];
$vsxTtsRuntime = dialecticVsxResolveCloneTtsRuntime($actorName);
$voicelogic = $vsxTtsRuntime['voicelogic'];
$ttsEndpoint = $vsxTtsRuntime['endpoint'];
Logger::info("[vsx] Using clone driver '{$vsxTtsRuntime['driver']}' for actor '{$actorName}' with endpoint '{$ttsEndpoint}'");
// Lock
$semaphore_timeout = $GLOBALS["SEMAPHORES_TIMEOUT"] ?? 300;
if (!SemaphoreWait("VSX", $semaphore_timeout, 47, null)) {
Logger::warn("[vsx] semaphore wait failed in " . __FILE__ . " " . __LINE__);
terminate();
}
$sourceParts = preg_split('/[\\\\\\/]+/', $sourcePath);
$sourceVoiceId = "";
if (is_array($sourceParts) && count($sourceParts) >= 4) {
$sourceVoiceId = strtolower(trim((string)$sourceParts[3]));
}
$codename = $sourceVoiceId !== "" ? $sourceVoiceId : npcNameToCodename($actorName);
$npcMaster = new NpcMaster();
$currentNpcData = $npcMaster->getByName($actorName);
if ($currentNpcData) {
if (empty($currentNpcData["voiceid"]) && $codename !== "") {
$currentNpcData["voiceid"] = $codename;
}
$extended = $npcMaster->getExtendedData($currentNpcData);
unset($extended["voice_refresh_requested_at"]);
$extended["voice_refresh_last_result"] = "sample_uploaded";
$extended["voice_refresh_last_resolved_at"] = time();
$extended["voice_sample_source"] = $sourcePath;
$extended["voice_sample_reference_text"] = $voiceSampleMetadata['reference_text'];
$currentNpcData = $npcMaster->setExtendedData($currentNpcData, $extended);
$currentNpcData = $npcMaster->updateByArray($currentNpcData);
}
// Release lock, this is the time consuming part, we have the needed data into the database
audit_log("vsx.php data available for $codename");
SemaphoreManager::release("VSX");
$ext = strtolower(pathinfo(str_replace('\\', '/', $sourcePath), PATHINFO_EXTENSION));
if (!in_array($ext, ['fuz', 'xwm', 'wav', 'ogg'], true)) {
dialecticVsxRespond(400, false, 'Unsupported voice sample extension', [
'extension' => $ext,
]);
}
if (empty($_FILES["file"]["tmp_name"]) || !is_file($_FILES["file"]["tmp_name"])) {
dialecticVsxRespond(400, false, 'No voice sample uploaded');
}
$finalName = __DIR__ . DIRECTORY_SEPARATOR . "soundcache/_vsx_" . md5($_FILES["file"]["tmp_name"]) . ".$ext";
@copy($_FILES["file"]["tmp_name"], $finalName);
$voiceCacheFile = $path . "data/voices/$codename.wav";
$cacheAlreadyAvailable = is_file($voiceCacheFile) && filesize($voiceCacheFile) > 44;
if (filesize($_FILES["file"]["tmp_name"]) == 0) {
Logger::error("Empty file {$_FILES["file"]["tmp_name"]}");
dialecticVsxRespond(400, false, 'Uploaded voice sample was empty');
}
Logger::info("Received sample: {$sourcePath}");
if ($ext === "fuz") {
$finalFile = fuzToWav($finalName);
} else if ($ext === "xwm") {
$finalFile = xwmToWav($finalName);
} else if ($ext === "wav") {
$finalFile = wavToWav($finalName);
} else {
$finalFile = oggToWav($finalName);
}
if (empty($finalFile) || !file_exists($finalFile) || filesize($finalFile) <= 0) {
Logger::error("[vsx] Failed to create converted voice sample for {$codename} from {$sourcePath}");
dialecticVsxRespond(500, false, 'Voice sample conversion failed', [
'codename' => $codename,
]);
}
// Always refresh the cache from the newly uploaded source. A bad first import must not become permanent.
$cacheCopyOk = dialecticVsxReplaceFile($finalFile, $voiceCacheFile);
if (!$cacheCopyOk || !file_exists($voiceCacheFile) || filesize($voiceCacheFile) <= 0) {
Logger::error("[vsx] Failed to copy converted voice sample to {$voiceCacheFile}");
dialecticVsxRespond(500, false, 'Voice sample cache copy failed', [
'codename' => $codename,
]);
}
dialecticVsxWriteSampleMetadata($voiceCacheFile, $codename, $voiceSampleMetadata);
Logger::info("[vsx] Cached normalized voice sample {$codename}.wav sha256=" . hash_file('sha256', $voiceCacheFile));
if ($ttsEndpoint === '') {
Logger::info("[vsx] Cached {$codename}.wav locally for {$vsxTtsRuntime['driver']}; no local clone endpoint required");
}
dialectic_sync_voice_clone_sample($codename, $voiceCacheFile, [
'root' => $path,
'actor_name' => $actorName,
'driver' => $vsxTtsRuntime['driver'] ?? '',
'force_upload' => true,
]);
audit_log("vsx.php voice available for {$actorName}");
dialecticVsxRespond(200, true, 'Voice sample uploaded', [
'codename' => $codename,
'driver' => $vsxTtsRuntime['driver'] ?? '',
'already_available' => $cacheAlreadyAvailable,
'cached_path' => "data/voices/$codename.wav",
]);