mirror of
https://github.com/jambonz/jambonz-api-server.git
synced 2026-07-04 19:21:53 +00:00
enable convert raw to mp3
This commit is contained in:
@@ -9,6 +9,7 @@ const {DbErrorBadRequest} = require('../../utils/errors');
|
|||||||
const Account = require('../../models/account');
|
const Account = require('../../models/account');
|
||||||
const sysError = require('../error');
|
const sysError = require('../error');
|
||||||
const { getSpeechCredential, decryptCredential } = require('../../utils/speech-utils');
|
const { getSpeechCredential, decryptCredential } = require('../../utils/speech-utils');
|
||||||
|
const PCMToMP3Encoder = require('../../record/encoder');
|
||||||
|
|
||||||
router.delete('/', async(req, res) => {
|
router.delete('/', async(req, res) => {
|
||||||
const {purgeTtsCache} = req.app.locals;
|
const {purgeTtsCache} = req.app.locals;
|
||||||
@@ -38,6 +39,7 @@ router.post('/Synthesize', async(req, res) => {
|
|||||||
try {
|
try {
|
||||||
const accountSid = parseAccountSid(req);
|
const accountSid = parseAccountSid(req);
|
||||||
const body = req.body;
|
const body = req.body;
|
||||||
|
const encodingMp3 = req.body.encodingMp3 || false;
|
||||||
if (!body.speech_credential_sid || !body.text || !body.language || !body.voice) {
|
if (!body.speech_credential_sid || !body.text || !body.language || !body.voice) {
|
||||||
throw new DbErrorBadRequest('speech_credential_sid, text, language, voice are all required');
|
throw new DbErrorBadRequest('speech_credential_sid, text, language, voice are all required');
|
||||||
}
|
}
|
||||||
@@ -85,18 +87,31 @@ router.post('/Synthesize', async(req, res) => {
|
|||||||
disableTtsCache: false
|
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, {
|
res.writeHead(200, {
|
||||||
'Content-Type': 'audio/mpeg',
|
'Content-Type': contentType,
|
||||||
'Content-Length': stat.size,
|
|
||||||
});
|
});
|
||||||
|
|
||||||
const readStream = fs.createReadStream(filePath);
|
|
||||||
// We replaced all the event handlers with a simple call to readStream.pipe()
|
|
||||||
readStream.pipe(res);
|
readStream.pipe(res);
|
||||||
|
|
||||||
readStream.on('end', () => {
|
readStream.on('end', () => {
|
||||||
// Delete the file after it's been read
|
|
||||||
fs.unlink(filePath, (err) => {
|
fs.unlink(filePath, (err) => {
|
||||||
if (err) throw err;
|
if (err) throw err;
|
||||||
logger.info(`${filePath} was deleted`);
|
logger.info(`${filePath} was deleted`);
|
||||||
|
|||||||
@@ -4320,6 +4320,10 @@ paths:
|
|||||||
type: string
|
type: string
|
||||||
description: voice ID
|
description: voice ID
|
||||||
example: en-US-Standard-C
|
example: en-US-Standard-C
|
||||||
|
encodingMp3:
|
||||||
|
type: boolean
|
||||||
|
description: convert audio to mp3.
|
||||||
|
example: true
|
||||||
required:
|
required:
|
||||||
- speech_credential_sid
|
- speech_credential_sid
|
||||||
- text
|
- text
|
||||||
|
|||||||
Reference in New Issue
Block a user