www.xbdev.net
xbdev - software development
Thursday May 23, 2024
home | contact | Support | Text to Speech Reader (LaTeX Text) Sit back and listen ....

     
 

Text to Speech Reader (LaTeX Text)

Sit back and listen ....

 



Reads Text Aloud (Ignores LaTeX Tags/Comments) - Give it a Try!


About: Simple web-based helper tool for proof reading LaTeX text, read the text out using the browser built in 'text-to-speech' synthesizer while ignoring LaTeX specific tags and comments.

How to use: Type the text you want read aloud (or copy and paste from your LaTeX document) into the textarea above, then click 'read'. Tags and comments, such as \cite{..} will automatically be ignored and not read out.


Very useful if you work with LaTeX documents reguarly - which can contain a mixture of tags - reads the text (skips tags).


Source Code (Vanila Javascript/HTML)


The implementation source code is very simple, and is include below for educational purposes. The reason the code is so simple is it builds on the browser's builtin speech API. (e.g.,
speechSynthesis
).

<div style='height:100px;width:100%;'>

Reads Text Aloud (Ignores LaTeX Tags/Comments) <br><br>

<
button onclick='readx();'>Read</button>
<
button onclick='stopx();'>Stop</button>

</
div>

<
textarea id='text' style='width:100%;height:80%;min-height:400px;'>
Type text to read here ...

\section{Example}
In the year 1902 John Smith [2,4invented time travel \cite{citationon a theoretical level
yet the [2concepts ignore this text
%ignore this line
are still used today
.

</
textarea>

<
div>
<
br>
AboutSimple web-based helper tool for proof reading LaTeX textread the text out using the browser built in 'text-to-speech' synthesizer while ignoring LaTeX specific tags and comments.<br><br>
How to use: Type the text you want read aloud (or copy and paste from your LaTeX documentinto the textarea abovethen click 'read'.  Tags and commentssuch as \cite{..} will automatically be ignored and not read out.
</
div>
<
br>

<
script>
function 
readx()
{
    
let tb   document.getElementById('text');
    
let txt tb.value;

    
txt txt.replace(/\\cite{.+}/gm'');
    
txt txt.replace(/%.+$/gm'');
    
txt txt.replace(/\\section{(.+)}/gm'$1');
    
txt txt.replace(/\[.+\]/gm'');

    
console.logtxt );
    
    
ReadText(txt);
}

function 
stopx()
{
    
tts.Synth.cancel();
}


var 
tts = {};
tts.Synth window.speechSynthesis;
tts.Voices = [];
tts.Voices tts.Synth.getVoices(); // get a list of available voices.
tts.DvIndex 0//Used to help identify the default tts voice for Chrome or FF on the users platform.
tts.DvRate 0.85// used to set speech rate between 0 and 2, 1 = 'normal'- there are other seemingly optional parameters like pitch, language, volume.
tts.On false//Set to false to prevent tts production.

function ReadText(txt){
    
    
tts.Synth.cancel();
    
// Use tts to read text. A new speech synthesis utterance instance is required for each tts output for FF.
    // Chrome lets you redefine the SpeechSynthesizerUtterance.txt property-
    // as needed without having to create a new object every time you want speech.
     
let ttsSpeechChunk = new SpeechSynthesisUtterance(txt);
     
ttsSpeechChunk.voice tts.Voices[tts.DvIndex]; //use default voice -- some voice must be assigned for FF to work.
     
ttsSpeechChunk.rate tts.DvRate
     
tts.Synth.speak(ttsSpeechChunk);     
     
    
// if ( last )
     //ttsSpeechChunk.onend = function(e){
         //console.log('finished');
    //     Reveal.next();
         //console.log( e );
    // }
     
};

const 
queryString window.location.search;
const 
urlParams = new URLSearchParams(queryString);
var 
selectvoice "";
if ( 
urlParams.has('voice') )
{
    
selectvoice urlParams.get('voice')
}

// fix to get the default voice
setTimeout(function(){
    
tts.Voices tts.Synth.getVoices();
    
console.log('Available voices:')
    for (var 
ix 0ix tts.Voices.lengthix++)
    {
        
let voice tts.Voices[ix];
        
console.log'  voice:[' voice.name ']' );
        
        if ( 
voice.default )
        {
            
tts.DvIndex ix;
            
console.log'>>> default voice: ' voice.name );
        }
        
// *** hack - force voice ***
        
if ( voice.name == 'Google UK English Female' )
        {
            
tts.DvIndex ix;
        }
        
        if ( 
selectvoice != "" )
        {
            if ( 
voice.name == selectvoice )
            {
                
console.log'forcing voice: ' selectvoice );
                
tts.DvIndex ix;
                break;
            }
        }
        
    }
    
}, 
500 );
</
script>



















 
Advert (Support Website)

 
 Visitor:
Copyright (c) 2002-2024 xbdev.net - All rights reserved.
Designated articles, tutorials and software are the property of their respective owners.