Skip to content

Commit

Permalink
add openai tts
Browse files Browse the repository at this point in the history
  • Loading branch information
bastipnt committed Sep 1, 2024
1 parent 06332f0 commit ce12a58
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 11 deletions.
Binary file modified bun.lockb
Binary file not shown.
20 changes: 10 additions & 10 deletions index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { serve } from "bun";
import TTS from "./tts";
import { file, serve } from "bun";
import ChatManager from "./chatManager";
import UserManager, { User } from "./userManager";
import TTS from "./tts-openai";

const tts = new TTS();
const userManager = new UserManager();
Expand Down Expand Up @@ -32,17 +32,17 @@ const server = serve({
if (url.pathname === "/") return new Response("Home page!", CORS_HEADERS);

if (url.pathname === "/tts" && req.method === "POST") {
const res = new Response();
return res;
// const res = new Response();
// return res;

// const data = await req.json();
// const text = data?.text;
const data = await req.json();
const text = data?.text;

// const audioPath = await tts.create(text);
// if (!audioPath) return new Response("An error occured!", CORS_HEADERS);
const audioPath = await tts.create(text);
if (!audioPath) return new Response("An error occured!", CORS_HEADERS);

// const res = new Response(file(audioPath), CORS_HEADERS);
// return res;
const res = new Response(file(audioPath), CORS_HEADERS);
return res;
}

if (url.pathname === "/chat" && req.method === "POST") {
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,6 @@
"@picovoice/web-voice-processor": "^4.0.8",
"edgedb": "^1.4.1",
"node-llama-cpp": "^2.8.2",
"openai": "^4.27.0"
"openai": "^4.57.0"
}
}
28 changes: 28 additions & 0 deletions tts-openai.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import fs from "fs";
import OpenAI from "openai";

class TTS {
openai: OpenAI;

constructor() {
this.openai = new OpenAI();
}

create = async (text?: string): Promise<string> => {
const audioPath = `tts_audios/audio.mp3`;
if (!text) return "";

const mp3 = await this.openai.audio.speech.create({
model: "tts-1",
voice: "nova",
input: text,
speed: 1.2,
});

const buffer = Buffer.from(await mp3.arrayBuffer());
await fs.promises.writeFile(audioPath, buffer);
return audioPath;
};
}

export default TTS;

0 comments on commit ce12a58

Please sign in to comment.