forked from deepgram-starters/text-to-speech-starter-node
-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
98 lines (80 loc) · 2.47 KB
/
server.js
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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
const { createClient } = require("@deepgram/sdk");
const express = require("express");
const http = require("http");
const fs = require("fs");
const dotenv = require("dotenv");
const path = require("path");
dotenv.config();
const app = express();
const server = http.createServer(app);
app.use(express.json());
const deepgram = createClient(process.env.DEEPGRAM_API_KEY);
app.use(express.static("public/"));
app.use("/audio", express.static("audio"));
app.post("/api", async (req, res) => {
const { body } = req;
const { text, model } = body;
try {
const filePath = await getAudio(text, model);
res.json({ audioUrl: filePath });
} catch (err) {
console.error(err);
res.status(500).send("Internal Server Error");
}
});
const getAudio = async (text, model) => {
const response = await deepgram.speak.request({ text }, { model });
const stream = await response.getStream();
if (stream) {
const buffer = await getAudioBuffer(stream);
try {
// Ensure 'audio' directory exists
const audioDirectory = path.join(__dirname, "audio");
if (!fs.existsSync(audioDirectory)) {
fs.mkdirSync(audioDirectory);
}
// Write audio file to 'audio' directory
await new Promise((resolve, reject) => {
fs.writeFile(path.join(audioDirectory, "audio.wav"), buffer, (err) => {
if (err) {
console.error("Error writing audio to file:", err);
reject(err);
} else {
console.log("Audio file written to audio.wav");
resolve();
}
});
});
} catch (err) {
throw err;
}
return "/audio/audio.wav";
} else {
console.error("Error generating audio:", stream);
throw new Error("Error generating audio: Stream is empty");
}
};
// Helper function to convert stream to audio buffer
const getAudioBuffer = async (response) => {
const reader = response.getReader();
const chunks = [];
while (true) {
const { done, value } = await reader.read();
if (done) break;
chunks.push(value);
}
const dataArray = chunks.reduce(
(acc, chunk) => Uint8Array.from([...acc, ...chunk]),
new Uint8Array(0)
);
return Buffer.from(dataArray.buffer);
};
// Serve the index.html file on root path
app.get("/", (req, res) => {
res.sendFile(path.join(__dirname, "/public/index.html"));
});
// Start the server
const PORT = process.env.PORT || 3000;
server.listen(PORT, () => {
console.log(`Server is listening on port ${PORT}`);
});