Text-to-Speech For Nigerian Languages by ChinenyeN(m): 5:07am On Jan 23, 2016 |
This is my implementation of a simple Text-to-Speech (tts) web application. The example uses Ngwa (Igbo), but it can easily be amended for any other Nigerian language. It requires only HTML, JS and PHP. There is no need for SQL and CSS is up to the designer. site.html/site.php <!DOCTYPE html> <head> <title>Text-to-Speech for Ngwa</title> </head>
<body> <span id="term">mgbamgba ere</span><span><img src="img/snd_icon.png" id="sound_icon"/></span> </body> </html>
sitejs.js
var ajaxReq = null; // for AJAX request, obviously
// grab the speaker icon element var speaker = document.getElementById("sound_icon" ); speaker.onclick = function() { // grab the term var term = document.getElementById("term" ).innerHTML; var response = runAJAX ("tts.php", term); // run the AJAX request
// check to make sure that tts.php echoed the the response we need if (response.match("Done" )) { // use of new Date() query string is to ensure that the browser fetches a new copy of tts_audio.mp3 // otherwise the browser will play the same audio even for a different term var audio = new Audio("snd/tts/tts_audio.mp3?q=" + new Date()); audio.load(); audio.play(); } }
function runAJAX (script, param) { // new AJAX object ajaxReq = new XMLHttpRequest (); ajaxReq.onreadystatechange = function() { if(ajaxReq.readyState == 4 && ajaxReq.status == 200) { return ajaxReq.responseText; }
// set up GET request ajaxReq.open ("GET", "scripts/" + script + "?q=" + param, true);
// send it ajaxReq.send(); } }
tts.php <?php
// the actual tts engine
// require php script holding associative array of phonemes and filenames require_once("phonemes.php" );
// trim $_GET["q"] as needed to get just the term itself $term = trim($_GET ["q"], " \s" );
// regexp to separate the term into necessary phonemes // the regexp below is suitable for Ngwa // you would have to amend it to get the desired result for your language preg_match_all("/\s|[mn]([wy]|\W{0, })|[^-aeiou\s]+|[aeiou][^\w\s']{0,4}/", $term, $result);
// to hold tts_audio.mp3 data $tts_audio = '';
// compare regexp results with phonemes and stitch mp3 file as necessary foreach ($result[0] as $phon) { // check if phoneme exists in array if(array_key_exists($phon, $phonemes)) { if(($mp3 = file_get_contents(PHONEME . $phonemes[$phon])) !== FALSE) $tts_audio = $tts_audio . $mp3; } }
// write to final audio file tts_audio.mp3 if(($check = file_put_contents("../snd/tts/tts_audio.mp3", $tts_audio )) !== FALSE) echo "Done"; else echo "Failed";
?>
phonemes.php <?php // just to hold the array of phonemes cleanly // also define the file path to audio files
// define define("PHONEME", "../snd/phonemes/" );
// associative array of phonemes and filenames $phonemes = [ "a" => "a-high.mp3", ... "gb" => "gb.mp3", ... "z" => "z.mp3" ];
?>
That's it. The site will then speak Ngwa text out loud and do away with the need to have an audio file for each word or entry on the site. Keeping the server safe from being overrun by countless mp3 files. The only mp3 files you will need will be that of the individual phonemes themselves. 5 Likes 1 Share |
Re: Text-to-Speech For Nigerian Languages by jandukun: 6:42pm On Oct 29, 2016 |
ChinenyeN: This is my implementation of a simple Text-to-Speech (tts) web application. The example uses Ngwa (Igbo), but it can easily be amended for any other Nigerian language. It requires only HTML, JS and PHP. There is no need for SQL and CSS is up to the designer.
site.html/site.php <!DOCTYPE html> <head> <title>Text-to-Speech for Ngwa</title> </head>
<body> <span id="term">mgbamgba ere</span><span><img src="img/snd_icon.png" id="sound_icon"/></span> </body> </html>
sitejs.js
var ajaxReq = null; // for AJAX request, obviously
// grab the speaker icon element var speaker = document.getElementById("sound_icon" ); speaker.onclick = function() { // grab the term var term = document.getElementById("term" ).innerHTML; var response = runAJAX ("tts.php", term); // run the AJAX request
// check to make sure that tts.php echoed the the response we need if (response.match("Done" )) { // use of new Date() query string is to ensure that the browser fetches a new copy of tts_audio.mp3 // otherwise the browser will play the same audio even for a different term var audio = new Audio("snd/tts/tts_audio.mp3?q=" + new Date()); audio.load(); audio.play(); } }
function runAJAX (script, param) { // new AJAX object ajaxReq = new XMLHttpRequest (); ajaxReq.onreadystatechange = function() { if(ajaxReq.readyState == 4 && ajaxReq.status == 200) { return ajaxReq.responseText; }
// set up GET request ajaxReq.open ("GET", "scripts/" + script + "?q=" + param, true);
// send it ajaxReq.send(); } }
tts.php <?php
// the actual tts engine
// require php script holding associative array of phonemes and filenames require_once("phonemes.php" );
// trim $_GET["q"] as needed to get just the term itself $term = trim($_GET ["q"], " \s" );
// regexp to separate the term into necessary phonemes // the regexp below is suitable for Ngwa // you would have to amend it to get the desired result for your language preg_match_all("/\s|[mn]([wy]|\W{0, })|[^-aeiou\s]+|[aeiou][^\w\s']{0,4}/", $term, $result);
// to hold tts_audio.mp3 data $tts_audio = '';
// compare regexp results with phonemes and stitch mp3 file as necessary foreach ($result[0] as $phon) { // check if phoneme exists in array if(array_key_exists($phon, $phonemes)) { if(($mp3 = file_get_contents(PHONEME . $phonemes[$phon])) !== FALSE) $tts_audio = $tts_audio . $mp3; } }
// write to final audio file tts_audio.mp3 if(($check = file_put_contents("../snd/tts/tts_audio.mp3", $tts_audio )) !== FALSE) echo "Done"; else echo "Failed";
?>
phonemes.php <?php // just to hold the array of phonemes cleanly // also define the file path to audio files
// define define("PHONEME", "../snd/phonemes/" );
// associative array of phonemes and filenames $phonemes = [ "a" => "a-high.mp3", ... "gb" => "gb.mp3", ... "z" => "z.mp3" ];
?>
That's it. The site will then speak Ngwa text out loud and do away with the need to have an audio file for each word or entry on the site. Keeping the server safe from being overrun by countless mp3 files. The only mp3 files you will need will be that of the individual phonemes themselves. @ChinenyeN |
Re: Text-to-Speech For Nigerian Languages by jandukun: 6:47pm On Oct 29, 2016 |
Thanks for this great code, can I get a copy of the source code and doew it come in central igbo? |
Re: Text-to-Speech For Nigerian Languages by ChinenyeN(m): 2:29am On Nov 04, 2016 |
jandukun: Thanks for this great code, can I get a copy of the source code and doew it come in central igbo? Everything you see is the source code, specifically for the web platform. You will have to rewrite it in the appropriate language, if you intend to develop it on a different platform. Also, feel free to copy it and use it however way you please. You can customize it for central Igbo as well. Is that something you know how to do or are you asking for me to adjust the source code for central Igbo? |
Re: Text-to-Speech For Nigerian Languages by AdaIgbotique: 7:20pm On Aug 11, 2019 |
There is a TTS feature live on Igbotique ( www.Igbotique.com). It supports all the sounds in standard Igbo and the tones. It also features an Igbo-English dictionary with pronunciations 1 Like |