Great! To create a full text-to-speech (TTS) web app using IVONA Brian voice (which is available through Amazon Polly), you'll need a backend to communicate with Amazon Polly's API (because the API requires authentication, secret keys, and secure server-to-server communication), plus a front-end to provide the UI.
Here's what the system will look like:
- Backend: Node.js/Express server that exposes an API endpoint
/synthesize accepting text and returning an audio stream (or audio URL).
- Frontend: HTML + JS app that sends user text to backend, gets the audio, and plays it.
Prerequisites
- AWS account with Amazon Polly enabled
- AWS Access Key ID and Secret Access Key with permissions for Polly
- Node.js installed locally or on your server
Step 1 — Backend: Node.js Express server with AWS SDK for Polly
First, install dependencies:
npm init -y
npm install express aws-sdk cors
Create server.js file:
const express = require('express');
const AWS = require('aws-sdk');
const cors = require('cors');
const app = express();
const port = process.env.PORT || 3000;
app.use(cors());
app.use(express.json());
// Configure AWS SDK
AWS.config.update({
region: 'us-east-1', // Polly's region
accessKeyId: process.env.AWS_ACCESS_KEY_ID, // Set in your environment variables!
secretAccessKey: process.env.AWS_SECRET_ACCESS_KEY,
});
const polly = new AWS.Polly();
app.post('/synthesize', async (req, res) => {
const { text } = req.body;
if (!text) {
return res.status(400).json({ error: 'No text provided' });
}
const params = {
Text: text,
OutputFormat: 'mp3',
VoiceId: 'Brian', // IVONA Brian voice
TextType: 'text' // plain text
};
try {
const data = await polly.synthesizeSpeech(params).promise();
if (data.AudioStream instanceof Buffer) {
res.set({
'Content-Type': 'audio/mpeg',
'Content-Length': data.AudioStream.length
});
res.send(data.AudioStream);
} else {
res.status(500).json({ error: 'Audio stream not returned as buffer' });
}
} catch (err) {
console.error('Error calling Polly:', err);
res.status(500).json({ error: 'Error synthesizing speech' });
}
});
app.listen(port, () => {
console.log(`TTS server listening on port ${port}`);
});
Environment Variables
Create a .env file or export your AWS credentials in your shell environment before launching the server:
export AWS_ACCESS_KEY_ID=YOUR_ACCESS_KEY_ID
export AWS_SECRET_ACCESS_KEY=YOUR_SECRET_ACCESS_KEY
Or use a .env file and load with dotenv if you want.
Step 2 — Frontend: HTML + JS that calls the backend and plays audio
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>Text-to-Speech with Amazon Polly (Brian)</title>
<style>
body {
font-family: Arial, sans-serif;
padding: 2rem;
max-width: 600px;
margin: auto;
background-color: #f9f9f9;
}
textarea {
width: 100%;
height: 150px;
font-size: 1.1rem;
padding: 0.5rem;
margin-bottom: 1rem;
resize: vertical;
}
button {
padding: 0.7rem 1.5rem;
font-size: 1rem;
background-color: #007bff;
border: none;
color: white;
border-radius: 4px;
cursor: pointer;
}
button:disabled {
background-color: #999;
cursor: not-allowed;
}
audio {
margin-top: 1rem;
width: 100%;
outline: none;
}
</style>
</head>
<body>
<h1>Text-to-Speech with Amazon Polly (Brian)</h1>
<textarea id="text-input" placeholder="Type something to be read aloud..."></textarea>
<br />
<button id="speak-btn">Speak</button>
<audio id="audio" controls></audio>
<script>
const speakBtn = document.getElementById('speak-btn');
const textInput = document.getElementById('text-input');
const audio = document.getElementById('audio');
speakBtn.addEventListener('click', async () => {
const text = textInput.value.trim();
if (!text) return;
speakBtn.disabled = true;
speakBtn.textContent = 'Synthesizing...';
audio.src = '';
try {
const response = await fetch('http://localhost:3000/synthesize', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ text }),
});
if (!response.ok) {
throw new Error('Server error: ' + response.statusText);
}
const arrayBuffer = await response.arrayBuffer();
const blob = new Blob([arrayBuffer], { type: 'audio/mpeg' });
const url = URL.createObjectURL(blob);
audio.src = url;
audio.play();
} catch (err) {
alert('Error: ' + err.message);
} finally {
speakBtn.disabled = false;
speakBtn.textContent = 'Speak';
}
});
</script>
</body>
</html>
How to run everything:
- Set your AWS credentials as environment variables.
- Run the server:
node server.js
- Open the HTML file in your browser (make sure CORS is allowed;
cors is enabled in the backend).
- Type text, click "Speak", and hear the IVONA Brian voice!
Notes and possible improvements
- Security: Never expose AWS keys in frontend.
- Deployment: Host backend securely (e.g., Heroku, AWS Lambda).
- Authentication: Add authentication to limit abuse of the TTS API.
- Caching: Cache audio for repeated texts for performance.
- Other features: Voice selection, adjustable rate/pitch, text sanitization.
Let me know if you want me to help with any of those or with deploying the backend!