73 lines
2.2 KiB
TypeScript

// Follow this setup guide to integrate the Deno language server with your editor:
// https://deno.land/manual/getting_started/setup_your_environment
// This enables autocomplete, go to definition, etc.
// Import necessary modules
import "jsr:@supabase/functions-js/edge-runtime.d.ts";
import { KokoroTTS } from "https://cdn.jsdelivr.net/npm/kokoro-js@1.2.1/dist/kokoro.js/+esm";
console.log("generate-tts function initialized");
Deno.serve(async (req) => {
if (req.method === "OPTIONS") {
// Handle preflight requests
return new Response(null, {
headers: {
"Access-Control-Allow-Origin": "*",
"Access-Control-Allow-Headers":
"authorization, x-client-info, apikey, content-type",
},
});
}
try {
console.log("Received request for TTS generation");
// Parse the incoming request body
const { text, voice, index } = await req.json();
console.log("Parsed request body:", { text, voice, index });
// Validate the input
if (!text || !voice) {
return new Response(
JSON.stringify({ error: "Missing required parameters: text or voice" }),
{ status: 400, headers: { "Content-Type": "application/json" } }
);
}
console.log(`Generating TTS for text: "${text}" with voice: "${voice}"`);
// Initialize KokoroTTS
const model_id = "onnx-community/Kokoro-82M-v1.0-ONNX";
const tts = await KokoroTTS.from_pretrained(model_id, {
dtype: "fp32",
});
// Generate the speech audio
const audio = await tts.generate(text, {
voice,
});
const arrayBuffer = await audio.toWav();
// audioUrl should be the base64 encoded audio blob
const audioUrl = `data:audio/wav;base64,${btoa(
String.fromCharCode(...new Uint8Array(arrayBuffer))
)}`;
console.log(`TTS generated successfully for index: ${index}`);
// Return the audio URL
return new Response(JSON.stringify({ audioUrl }), {
headers: { "Content-Type": "application/json" },
});
} catch (error) {
console.error("Error generating TTS:", error);
// Return an error response
return new Response(JSON.stringify({ error: "Failed to generate TTS" }), {
status: 500,
headers: { "Content-Type": "application/json" },
});
}
});