diff options
Diffstat (limited to 'src/pages/api/tts.ts')
-rw-r--r-- | src/pages/api/tts.ts | 81 |
1 files changed, 81 insertions, 0 deletions
diff --git a/src/pages/api/tts.ts b/src/pages/api/tts.ts new file mode 100644 index 0000000..bd9a697 --- /dev/null +++ b/src/pages/api/tts.ts @@ -0,0 +1,81 @@ +// import db from "../../lib/db"; +import { randomFromArray } from "@/lib/utils"; +import { z } from "zod"; + +export const GET = async (request: Request): Promise<Response> => { + const url = URL.parse(request.url)!; + const params = url?.searchParams; + const word = params.get("word")!; + const lang = params.get("lang")!; + + try { + const res = await tts(word, lang); + return res; + } catch (error) { + return Response.json({ message: "Failure" }, { status: 500 }); + } +}; +const thaiVoices = [ + [ + "s3://voice-cloning-zero-shot/4353be7d-8cd3-4452-9e0b-bc4078c240d7/original/manifest.json", + "PlayDialog", + ], + [ + "s3://voice-cloning-zero-shot/4c495e1a-1352-4187-99eb-6e5dc7d55059/original/manifest.json", + "PlayDialog", + ], + [ + "s3://voice-cloning-zero-shot/59933136-5aca-4f42-827f-d354649c62a2/original/manifest.json", + "PlayDialog", + ], + [ + "s3://voice-cloning-zero-shot/ba9eb1c9-8897-4c41-9c79-f2cb428544a8/original/manifest.json", + "PlayDialog", + ], + [ + "s3://voice-cloning-zero-shot/bb585812-1c85-4a16-90f7-09c24b6c8186/original/manifest.json", + "PlayDialog", + ], + [ + "s3://voice-cloning-zero-shot/e1357526-c162-441b-afb9-285d3d21b9b4/original/manifest.json", + "PlayDialog", + ], + [ + "s3://voice-cloning-zero-shot/edd305a3-9cd2-4dd6-873f-9efc1f73aefc/original/manifest.json", + "PlayDialog", + ], + [ + "s3://voice-cloning-zero-shot/f80c355d-1075-4d2b-a53d-bb26aa4d1453/original/manifest.json", + "PlayDialog", + ], +]; + +async function tts(text: string, language: string) { + const USER_ID = Bun.env.PLAYHT_USER_ID!; + const API_KEY = Bun.env.PLAYHT_API_KEY!; + const [voice, voice_engine] = randomFromArray(thaiVoices); + console.log("tts", text); + const url = "https://api.play.ht/api/v2/tts/stream"; + const options = { + method: "POST", + headers: { + accept: "*/*", + "content-type": "application/json", + "X-USER-ID": USER_ID, + AUTHORIZATION: API_KEY, + }, + body: JSON.stringify({ + text, + voice, + // wav, mp3, ogg, flac, mulaw + output_format: "wav", + quality: "high", + voice_engine, + language, + temperature: 0.7, + }), + }; + + const res = await fetch(url, options); + return res; +} |