Skip to content

Commit

Permalink
switch to openai tts
Browse files Browse the repository at this point in the history
  • Loading branch information
mhmadalaa committed May 28, 2024
1 parent d80bc40 commit 0b7cabf
Showing 1 changed file with 20 additions and 24 deletions.
44 changes: 20 additions & 24 deletions src/controllers/textToSpeechController.js
Original file line number Diff line number Diff line change
@@ -1,30 +1,26 @@
require('dotenv').config();
const axios = require('axios');
const stream = require('stream');

const openai = require('./../config/openaiConfig');
const catchAsync = require('./../utils/catchAsync');

const AI_API = process.env.AI_API;

exports.textToSpeech = catchAsync(async (req, res) => {
const dataToSend = {
text: req.body.text,
};
const response = await openai.audio.speech.create({
model: 'tts-1',
voice: 'alloy',
input: req.body.text,
});

// Set the appropriate headers for the response
res.setHeader('Content-Type', 'audio/mpeg');
res.setHeader('Content-Disposition', 'attachment; filename=speech.mp3');

// Convert the base64 audio content to a Buffer
const buffer = Buffer.from(await response.arrayBuffer());

axios
.get(`${AI_API}/tts`, {
data: dataToSend,
responseType: 'stream',
})
.then((response) => {
// Set the appropriate headers for the raw audio response
res.setHeader('Content-Type', 'audio/raw');
// Create a readable stream from the buffer
const audioStream = new stream.PassThrough();
audioStream.end(buffer);

// Pipe the audio stream to the response
response.data.pipe(res);
})
.catch((error) => {
res.status(404).json({
message: 'Error occured while applying text to speech',
});
});
});
// Pipe the audio stream to the response
audioStream.pipe(res);
});

0 comments on commit 0b7cabf

Please sign in to comment.