-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
87 lines (70 loc) · 2.29 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
const path = require("path");
const express = require("express");
const cors = require("cors");
const bodyParser = require("body-parser");
const textToSpeech = require("@google-cloud/text-to-speech");
const fs = require("fs");
// our pseudo back-end server
// local host is 9999
// http://localhost:9999/
// link to succesful output
// http://localhost:9999/get-Audio
// link to mp3
// http://localhost:9999/audio/output_1694924441459.mp3
const app = express();
app.use(cors());
app.use(express.json());
app.options('*', cors());
const client = new textToSpeech.TextToSpeechClient({
keyFilename: "geniusgesture.json",
});
const audioDirectory = "./audio";
if (!fs.existsSync(audioDirectory)) {
fs.mkdirSync(audioDirectory);
}
const CACHED_AUDIO_FILES = new Set(fs.readdirSync(audioDirectory));
app.use(bodyParser.urlencoded({ extended: true }));
app.get("/", (req, res) => {
res.send(`
<form action="/get-Audio" method="post">
<textarea name="text" rows="4" cols="50" placeholder="Enter your text here..."></textarea><br>
<input type="submit" value="Convert to Audio">
</form>
`);
});
app.use("/audio", express.static(audioDirectory));
app.post("/get-Audio", async (req, res) => {
const text = req.body.text;
const request = {
input: { text: text },
voice: {
languageCode: "en-US",
ssmlGender: "NEUTRAL",
},
audioConfig: {
audioEncoding: "MP3",
},
};
if (CACHED_AUDIO_FILES.has(`${text}.mp3`)) {
console.log(`${text}.mp3 already exists in cache.`);
res.send(`${text}.mp3`);
return;
}
try {
const [response] = await client.synthesizeSpeech(request);
const audioFileName = `${text}.mp3`;
const audioFilePath = path.join(audioDirectory, audioFileName); // Save to /audio directory
// Save to cache
CACHED_AUDIO_FILES.add(audioFileName);
fs.writeFileSync(audioFilePath, response.audioContent, "binary");
console.log(`Audio content written to file: ${audioFilePath}`);
res.send(audioFileName); // Just send back the filename
} catch (error) {
console.error("Error while generating audio:", error);
res.status(500).send("Failed to generate audio.");
}
});
const PORT = 9999;
app.listen(PORT, () => {
console.log(`Server is running on http://localhost:${PORT}`);
});