enable convert raw to mp3

This commit is contained in:
Quan HL
2024-01-11 15:59:24 +07:00
parent f2c635268f
commit 6ad136c36a
2 changed files with 27 additions and 8 deletions

View File

@@ -9,6 +9,7 @@ const {DbErrorBadRequest} = require('../../utils/errors');
const Account = require('../../models/account');
const sysError = require('../error');
const { getSpeechCredential, decryptCredential } = require('../../utils/speech-utils');
const PCMToMP3Encoder = require('../../record/encoder');
router.delete('/', async(req, res) => {
const {purgeTtsCache} = req.app.locals;
@@ -38,6 +39,7 @@ router.post('/Synthesize', async(req, res) => {
try {
const accountSid = parseAccountSid(req);
const body = req.body;
const encodingMp3 = req.body.encodingMp3 || false;
if (!body.speech_credential_sid || !body.text || !body.language || !body.voice) {
throw new DbErrorBadRequest('speech_credential_sid, text, language, voice are all required');
}
@@ -85,18 +87,31 @@ router.post('/Synthesize', async(req, res) => {
disableTtsCache: false
});
const stat = fs.statSync(filePath);
let contentType = 'audio/mpeg';
let readStream = fs.createReadStream(filePath);
if (['nuance', 'nvidia'].includes(cred.vendor) ||
(
process.env.JAMBONES_TTS_TRIM_SILENCE &&
['microsoft', 'azure'].includes(cred.vendor)
)
) {
if (encodingMp3) {
readStream = readStream
.pipe(new PCMToMP3Encoder({
channels: 1,
sampleRate: 8000,
bitRate: 128
}, logger));
} else {
contentType = 'application/octet-stream';
}
}
res.writeHead(200, {
'Content-Type': 'audio/mpeg',
'Content-Length': stat.size,
'Content-Type': contentType,
});
const readStream = fs.createReadStream(filePath);
// We replaced all the event handlers with a simple call to readStream.pipe()
readStream.pipe(res);
readStream.on('end', () => {
// Delete the file after it's been read
fs.unlink(filePath, (err) => {
if (err) throw err;
logger.info(`${filePath} was deleted`);

View File

@@ -4320,6 +4320,10 @@ paths:
type: string
description: voice ID
example: en-US-Standard-C
encodingMp3:
type: boolean
description: convert audio to mp3.
example: true
required:
- speech_credential_sid
- text