aiTextToSpeech

This commit is contained in:
Kar
2024-11-05 20:46:04 +05:30
parent 101e1a2749
commit 0a78f89fa9
3 changed files with 60 additions and 2 deletions

View File

@@ -0,0 +1,50 @@
const aiTextToSpeech = async (req, res) => {
const AWS = require('aws-sdk');
AWS.config.update({
region: 'ap-south-1',
accessKeyId: process.env.AWS_polly2_ACCESS_KEY_ID,
secretAccessKey: process.env.AWS_polly2_SECRET_ACCESS_KEY,
});
const polly = new AWS.Polly();
const voiceIdMap = {
"Ava": "Ivy",
"Monstero": "Justin",
"Dax": "Kevin",
"Kai": "Daniele",
"Yara": "Sally"
};
const { text, voiceId: frontendVoiceId } = req.body;
if (!text || !frontendVoiceId) {
return res.status(400).json({ error: 'Text and voiceId are required' });
}
const pollyVoiceId = voiceIdMap[frontendVoiceId];
if (!pollyVoiceId) {
return res.status(400).json({ error: 'Invalid voiceId provided' });
}
const params = {
Text: text,
OutputFormat: 'mp3',
VoiceId: pollyVoiceId,
};
try {
const data = await polly.synthesizeSpeech(params).promise();
const audioBuffer = data.AudioStream;
res.json({
audioContent: audioBuffer.toString('base64'),
});
} catch (error) {
console.error('Error generating speech:', error);
res.status(500).json({ error: 'Error generating speech' });
}
}
module.exports = aiTextToSpeech;