1
0

localXTTS.ts 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. import { TTSBackend } from "@/types/backend";
  2. export async function localXTTSTTS(config: TTSBackend["localXTTS"],message: string) {
  3. const baseUrl = config?.localXTTS_url
  4. .replace(/\/+$/, '')
  5. .replace('/api/tts-generate', '');
  6. // Log the config values for debugging
  7. console.log('[AllTalk] Config values:', {
  8. url: config?.localXTTS_url,
  9. version: config?.alltalk_version,
  10. voice: config?.alltalk_voice,
  11. rvcVoice: config?.alltalk_rvc_voice,
  12. rvcPitch: config?.alltalk_rvc_pitch,
  13. language: config?.alltalk_language
  14. });
  15. const formData = new URLSearchParams({
  16. text_input: message,
  17. text_filtering: 'standard',
  18. character_voice_gen: config?.alltalk_voice || 'female_01.wav',
  19. narrator_enabled: 'false',
  20. narrator_voice_gen: config?.alltalk_voice || 'female_01.wav',
  21. text_not_inside: 'character',
  22. language: config?.alltalk_language || 'en',
  23. output_file_name: 'amica_output',
  24. output_file_timestamp: 'true',
  25. autoplay: 'false',
  26. autoplay_volume: '0.8',
  27. });
  28. // Add RVC parameters only for V2
  29. if (config?.alltalk_version === "v2") {
  30. const rvcVoice = config.alltalk_rvc_voice;
  31. if (rvcVoice && rvcVoice !== 'Disabled') {
  32. formData.append('rvccharacter_voice_gen', rvcVoice);
  33. formData.append('rvccharacter_pitch', config.alltalk_rvc_pitch || '0');
  34. }
  35. }
  36. try {
  37. console.log('[AllTalk] Processing text:', message);
  38. console.log('[AllTalk] Form data:', Object.fromEntries(formData));
  39. const res = await fetch(`${baseUrl}/api/tts-generate`, {
  40. method: "POST",
  41. body: formData,
  42. });
  43. if (!res.ok) {
  44. console.error('[AllTalk] Initial request failed:', res.status, res.statusText);
  45. throw new Error("AllTalk TTS API Error");
  46. }
  47. const data = await res.json();
  48. console.log('[AllTalk] TTS Response:', data);
  49. // Handle V1/V2 URL differences
  50. const audioUrl = config?.alltalk_version === "v1"
  51. ? data.output_file_url // V1 returns full URL
  52. : `${baseUrl}${data.output_file_url}`; // V2 returns relative path
  53. console.log('[AllTalk] Generated audio URL:', audioUrl);
  54. // Fetch the actual audio data
  55. const audioResponse = await fetch(audioUrl);
  56. if (!audioResponse.ok) {
  57. throw new Error("Failed to fetch audio data");
  58. }
  59. const audioData = await audioResponse.arrayBuffer();
  60. console.log('[AllTalk] Received audio data size:', audioData.byteLength);
  61. return { audio: audioData };
  62. } catch (e) {
  63. console.error('[AllTalk] Error:', e);
  64. throw new Error(`AllTalk TTS Error: ${(e as Error).message}`);
  65. }
  66. }