summaryrefslogtreecommitdiff
path: root/src/pages/api/tts.ts
blob: bd9a6974118f5363d122474084f3f7931dda612e (plain)
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
// 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;
}