Skip to content

Commit

Permalink
chore: remove logs
Browse files Browse the repository at this point in the history
  • Loading branch information
Wurielle committed Oct 6, 2024
1 parent 5438280 commit 9f00d57
Showing 1 changed file with 70 additions and 73 deletions.
143 changes: 70 additions & 73 deletions examples/custom-engine-api/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
const express = require('express')
const app = express()
const port = 3000
const pkg = require('./package.json')
const say = require('say')
const cors = require('cors')
Expand All @@ -14,85 +13,83 @@ const ENDPOINT_PORT = 3000

app.use(cors())
app.use(bodyParser.json())
app.use(bodyParser.urlencoded({ extended: true }))
app.use(bodyParser.urlencoded({extended: true}))

app.post('/list-voices', async (req, res) => {
try {
const {
body: {
credentials: {
apiKey, // API key provided by the user if required
},
},
} = req
console.log(req.body)
const voices = await new Promise((resolve, reject) => {
say.getInstalledVoices((err, voices) => {
if (err) return reject(err)
return resolve(voices)
})
})
res.status(200).json(
voices.map((name) => ({
id: name, // id must be unique
name, // name of the voice
category: 'Say', // category of the voice
languageCode: 'en-US', // language code of the voice
})),
)
} catch (err) {
res.status(500).json({ error: err.message })
}
try {
const {
body: {
credentials: {
apiKey, // API key provided by the user if required
},
},
} = req
const voices = await new Promise((resolve, reject) => {
say.getInstalledVoices((err, voices) => {
if (err) return reject(err)
return resolve(voices)
})
})
res.status(200).json(
voices.map((name) => ({
id: name, // id must be unique
name, // name of the voice
category: 'Say', // category of the voice
languageCode: 'en-US', // language code of the voice
})),
)
} catch (err) {
res.status(500).json({error: err.message})
}
})

app.post('/synthesize-speech', async (req, res) => {
try {
const {
body: {
credentials: {
apiKey, // API key provided by the user if required
},
payload: {
text, // message to be spoken
voice: {
id, // id must be unique
name, // name of the voice
category, // category of the voice
languageCode, // language code of the voice
},
},
},
} = req
console.log(req.body)
const outputFile = path.join(__dirname, 'example.mp3')
fs.mkdirSync(path.parse(outputFile).dir, { recursive: true })
fs.writeFileSync(outputFile, '')
try {
const {
body: {
credentials: {
apiKey, // API key provided by the user if required
},
payload: {
text, // message to be spoken
voice: {
id, // id must be unique
name, // name of the voice
category, // category of the voice
languageCode, // language code of the voice
},
},
},
} = req
const outputFile = path.join(__dirname, 'example.mp3')
fs.mkdirSync(path.parse(outputFile).dir, {recursive: true})
fs.writeFileSync(outputFile, '')

await new Promise((resolve, reject) => {
say.export(text, name, 1, outputFile, (err) => {
if (err) {
reject(err)
}
resolve(true)
})
})
await new Promise((resolve, reject) => {
say.export(text, name, 1, outputFile, (err) => {
if (err) {
reject(err)
}
resolve(true)
})
})

res.writeHead(200, {
'Content-Type': 'audio/mp3',
})
const stream = fs.createReadStream(outputFile).pipe(res)
stream.on('finish', () => {
fs.unlinkSync(outputFile)
})
} catch (err) {
res.status(500).json({ error: err.message })
}
res.writeHead(200, {
'Content-Type': 'audio/mp3',
})
const stream = fs.createReadStream(outputFile).pipe(res)
stream.on('finish', () => {
fs.unlinkSync(outputFile)
})
} catch (err) {
res.status(500).json({error: err.message})
}
})

app.listen(port, () => {
console.log(
`[${pkg.name}] API endpoint: ${ENDPOINT_BASE_URL}${
ENDPOINT_PORT ? `:${ENDPOINT_PORT}` : ''
}`,
)
app.listen(ENDPOINT_PORT, () => {
console.log(
`[${pkg.name}] API endpoint: ${ENDPOINT_BASE_URL}${
ENDPOINT_PORT ? `:${ENDPOINT_PORT}` : ''
}`,
)
})

0 comments on commit 9f00d57

Please sign in to comment.