To achieve the functionality you described, we'll need a front-end to select the voice/language and input text, which will communicate with a back-end service to generate speech using espeak and save the file locally. Here is a basic example that accomplishes this. Note that this example assumes you have espeak, gtts, and a local server environment (like Flask for Python) already set up on your Ubuntu machine.
This example also simplifies the front-end and uses Bootstrap 5 for styling. It doesn't cover every voice in the world but demonstrates how you could do it with a select dropdown. You might need to extend the voices options by yourself.
Step 1: Set Up Your Python Environment
First, make sure you have Python 3 installed. Then install Flask and gTTS by running:
pip install Flask gTTS
Step 2: Create the Flask App
Create a file named app.py:
from flask import Flask, render_template, request, jsonify
from gtts import gTTS
import os
app = Flask(__name__)
@app.route('/')
def index():
# assuming espeak is installed and can provide the list of voices/languages
languages = ['en', 'es', 'de', 'fr'] # Simplified list; extend as needed
return render_template('index.html', languages=languages)
@app.route('/synthesize', methods=['POST'])
def synthesize():
data = request.json
text = data['text']
lang = data['lang']
tts = gTTS(text=text, lang=lang)
filename = f"{lang}_speech.mp3"
filepath = os.path.join(os.environ['HOME'], filename)
tts.save(filepath)
return jsonify({"message": f"File saved as {filename} in your home directory."})
if __name__ == '__main__':
app.run(debug=True)
Step 3: Create the HTML Template with Bootstrap
Create a folder named templates in the same directory as your app.py, and inside that folder, create a file named index.html:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>gTTS with eSpeak</title>
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/css/bootstrap.min.css" rel="stylesheet">
<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
</head>
<body>
<div class="container mt-5">
<h1>Text to Speech Converter</h1>
<select id="voiceSelect" class="form-select mt-3">
{% for lang in languages %}
<option value="{{ lang }}">{{ lang }}</option>
{% endfor %}
</select>
<textarea id="textToSpeak" class="form-control mt-3" rows="5"></textarea>
<button id="synthesize" class="btn btn-primary mt-3">Convert & Save</button>
<button id="clearText" class="btn btn-secondary mt-3">Clear</button>
<div id="message" class="mt-3"></div>
</div>
<script>
$(document).ready(function() {
$('#synthesize').click(function() {
const text = $('#textToSpeak').val();
const lang = $('#voiceSelect').val();
$.ajax({
url: '/synthesize',
type: 'POST',
contentType: 'application/json',
data: JSON.stringify({text: text, lang: lang}),
success: function(response) {
$('#message').text(response.message);
},
error: function(error) {
console.log(error);
}
});
});
$('#clearText').click(function() {
$('#textToSpeak').val('');
$('#message').text('');
});
});
</script>
</body>
</html>
Running Your Flask App
- Start your Flask application by running
python app.py in your terminal.
- Open a browser and go to
http://127.0.0.1:5000/ to see your application.
Keep in mind, this implementation is simplified and focuses on demonstrating the architecture of a solution. Depending on your requirements, you may need to scale or secure this application. For instance, saving every file to the user's home directory without any cleanup might not be the best idea for a production environment.