-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapi-test.js
More file actions
66 lines (53 loc) · 1.69 KB
/
api-test.js
File metadata and controls
66 lines (53 loc) · 1.69 KB
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
// import textToSpeech from "@google-cloud/text-to-speech"
// import fs from "fs"
// import util from "util"
// import dotEnv from "dotenv"
// import logger from "./helpers/logger.js"
// import db from "./models"
// import chalk from "chalk"
const textToSpeech = require("@google-cloud/text-to-speech")
const fs = require("fs")
const util = require("util")
const dotEnv = require("dotenv")
const logger = require("./helpers/logger")
const db = require("./models")
const chalk = require("chalk")
dotEnv.config()
const client = new textToSpeech.TextToSpeechClient()
async function quickStart() {
const text = "Start!"
const request = {
input: { text },
voice: { languageCode: "en-US", ssmlGender: "NEUTRAL" },
audioConfig: { audioEncoding: "MP3" },
}
const [response] = await client.synthesizeSpeech(request)
// write binary audio content to a local file
const writeFile = util.promisify(fs.writeFile)
await writeFile("output.mp3", response.audioContent, "binary")
console.log("Audio content written to file: output.mp3")
}
async function saveToDb(input) {
try {
const request = {
input: { text: input },
voice: { languageCode: "en-US", ssmlGender: "NEUTRAL" },
audioConfig: { audioEncoding: "MP3" },
}
const [response] = await client.synthesizeSpeech(request)
const sound = await db.Sound.create({
text: input,
file: response.audioContent,
})
if (sound) {
logger.debug(chalk.green("Sound added to db"))
} else {
throw "Couldn't save sound"
}
} catch (error) {
logger.error(chalk.red("Error saving audio to db: "))
logger.error(chalk.red(error))
}
}
// quickStart()
saveToDb("Hello, there. This is a test.")