2023-10-09 17:22:48 +00:00
const { StatusCodes } = require ( "http-status-codes" )
const { sentiment , convertTextToSpeech , listVoice } = require ( "../utils" )
const language = require ( '@google-cloud/language' ) . v2
const CustomError = require ( '../errors' )
const voiceConfigList = require ( '../mockData/voice.json' )
const getSentiment = async ( req , res ) => {
const { text , } = req . body
const status = await sentiment ( text )
res . status ( StatusCodes . OK ) . json ( { status } )
}
const getAudioFromText = async ( req , res ) => {
const { text , voice _name , voice _gender , languageCode } = req . query
2023-10-10 13:58:54 +00:00
2023-10-09 17:22:48 +00:00
if ( ( voice _name || voice _gender || languageCode ) && languageCode == 'pt-BR' ) {
const config = { voice _name , voice _gender , languageCode }
for ( const key in config ) {
if ( config . hasOwnProperty ( key ) && config [ key ] === undefined ) {
throw new CustomError . BadRequestError ( ` The key ${ key } is required when setted one of the three configuration parameters: voice_name, voice_gender and languageCode ` )
}
}
const voice = voiceConfigList . find ( ( { name , ssmlGender , languageCode } ) => {
if ( name == config . voice _name && ssmlGender == config . voice _gender && languageCode == config . languageCode ) return { name , ssmlGender , languageCode }
} )
if ( ! voice )
2023-10-10 13:58:54 +00:00
throw new CustomError . BadRequestError ( ` Wrong config voice combination! Check the endpoint(http://localhost:6001/api/v1/nl/voice-config) to display the available configurations to a language ` )
2023-10-09 17:22:48 +00:00
}
const audioBuffer = await convertTextToSpeech ( text , voice _name , voice _gender , languageCode )
2023-10-10 13:58:54 +00:00
if ( voice _name && voice _gender && languageCode ) {
filename = ` ${ voice _name } _ ${ voice _gender } _ ${ languageCode } .mp3 `
}
else {
filename = ` pt-BR-Standard-B_MALE_pt-BR.mp3 `
}
// Set the Content-Disposition header
// res.set("Content-Disposition", `attachment; filename="${filename}"`);
res . set ( "Content-Disposition" , ` inline; filename=" ${ filename } " ` ) ;
2023-10-09 17:22:48 +00:00
res . contentType ( 'audio/mpeg' )
res . status ( StatusCodes . OK ) . send ( audioBuffer )
}
const getVoiceConfig = async ( req , res ) => {
const { languageCode } = req . query
console . log ( languageCode )
const configs = await listVoice ( languageCode )
res . status ( StatusCodes . OK ) . json ( { configs } )
}
module . exports = {
getSentiment ,
getAudioFromText ,
getVoiceConfig
}