Hërzouz
BBuildShip
•Created by Hërzouz on 7/22/2024 in #❓・buildship-help
Amazon Polly API
Hello Buildshipers!
I am trying to create a workflow that connects to Amazon Polly API. Is this possible? I am a total noob when it comes to buildship. If you have any tips or ideas i am totaly focused on your returns.
Now, i am here:
import { PollyClient, SynthesizeSpeechCommand } from "@aws-sdk/client-polly";
const AWS_ACCESS_KEY_ID = process.env.AWS_ACCESS_KEY_ID;
const AWS_SECRET_ACCESS_KEY = process.env.AWS_SECRET_ACCESS_KEY;
const AWS_REGION = process.env.AWS_REGION;
if (!AWS_ACCESS_KEY_ID !AWS_SECRET_ACCESS_KEY !AWS_REGION) {
throw new Error('AWS credentials or region are not set in environment variables');
}
export default async function synthesizeSpeech({
text,
voiceId,
format = 'mp3',
sampleRate = '8000',
textType = 'text',
region,
engine = 'neural',
languageCode
}) {
const awsRegion = region || AWS_REGION;
if (!awsRegion) {
throw new Error('Region is missing');
}
const client = new PollyClient({
region: awsRegion,
credentials: {
accessKeyId: AWS_ACCESS_KEY_ID,
secretAccessKey: AWS_SECRET_ACCESS_KEY
}
});
const input = {
Text: text,
OutputFormat: format,
VoiceId: voiceId,
SampleRate: sampleRate,
TextType: textType,
Engine: engine,
LanguageCode: languageCode
};
const command = new SynthesizeSpeechCommand(input);
try {
const { AudioStream, ContentType } = await client.send(command);
if (AudioStream && ContentType === 'audio/mpeg') {
let audioBuffer = [];
for await (let chunk of AudioStream) {
audioBuffer.push(chunk);
}
return Buffer.concat(audioBuffer).toString('base64');
}
throw new Error('AudioStream is empty or ContentType is not MP3');
} catch (error) {
throw new Error(
Error synthesizing speech: ${error.message}
);
}
}
/
Cheers!6 replies