Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URL's added here will be added as <link>s in order, and before the CSS in the editor. If you link to another Pen, it will include the CSS from that Pen. If the preprocessor matches, it will attempt to combine them before processing.

+ add another resource

JavaScript

Babel includes JSX processing.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Packages

Add Packages

Search for and use JavaScript packages from npm here. By selecting a package, an import statement will be added to the top of the JavaScript editor for this package.

Behavior

Save Automatically?

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                <html>
	<head>
		<title>Text to speech converter in JS</title>
		<style>
			@import url('https://fonts.googleapis.com/css?family=Roboto');
			
			body{
				background: papayawhip;
				font-family: Roboto;
				font-size: 18px;
			}
			.main{
				background: white;
				position: absolute;
				top:50%;
				left:50%;
				transform: translateX(-50%) translateY(-50%);
				width: 800px;
				padding:20px;
				box-shadow: 0 4px 15px 0px rgba(71, 0, 41, .5);
				border-radius: 5px;

				display: flex;
				flex-wrap: wrap;
				justify-content: center;
			}
			.main__title{
				color: salmon;
				font-size: 32px;
			}
			.main__input{
				flex:1 1 100%;
				height: 250px;
				resize: none;
				outline: none;
				border: 1px solid #ccc;
				border-radius: 5px;
				font-size: inherit;
				font-family: inherit;
				padding: 15px;
				margin-bottom: 15px;
			}

			.box{
				margin: 15px 0;
				flex-basis: 100%;
				display: flex;
				justify-content: space-around;
			}
			.voices-list{
				height: 30px;
				flex:2;
				padding: 0 10px;
			}
			.label{
				flex:1;
			}
			input[type="range"]{
				flex:2;
			}
			.btn-box{
				flex-basis: 100%;
				display:flex;
				
			}
			.speak_btn{

				background: #88e8e4;
				padding: 10px 20px;
				color: #000;
				border: 1px solid #3abcb7;
				cursor: pointer;
				flex:1;
				text-align: center;
				transform:translateY(-4px);
				box-shadow: 0px 2px 7px #666;
				transition: transform .2s,box-shadow .2s;
				/*pointer-events: none;*/
				border-radius: 5px;
				font-size: inherit;
				font-family: inherit;
				text-transform: uppercase;
				color:white;
				outline: none;
			}
			.btn-box:hover .speak_btn{
				transform:translateY(-6px);
				box-shadow: 0px 4px 15px #666;
			}
			.btn-box:active .speak_btn{
				transform:translateY(-4px);
				box-shadow: 0px 2px 7px #666;
			}




		</style>
	</head>
	<body>
		<div class="main">
			<h3 class="main__title">Native JS Text To Speech</h3>

			<textarea class="main__input" id="source_text">When I was about 6 years old, I spent a lot of time playing computer games with my best friend. His family had a room full of computers. To me, they were irresistible. Magic. I spent many hours exploring all the games. One day I asked my friend, “how do we make a game?” He didn’t know, so we asked his dad, who reached up on a high shelf and pulled down a book of games written in Basic. So began my journey with programming. By the time public school got around to teaching algebra, I already knew the topic well, because programming is basically algebra. It can be, anyway.
            </textarea>

			<div class="box voice-box">
				<span class="label">Choose a Voice</span>
				<select class="voices-list" id="voice_options"></select>
			</div>
			<div class="box">
				<span class="label">Volume</span>
				<input type="range" id="volume_slider" min="0" max="1" value="0.5" step="0.1" />
			</div>
			<div class="box">
				<span class="label">Rate</span>
				<input type="range" id="rate_slider" min="0" max="2" value="1" step="0.2" />
			</div>

			<div class="box">
				<span class="label">Pitch</span>
				<input type="range" id="pitch_slider" min="0" max="2" value="1" step="0.2" />
			</div>
			<div class="box btn-box">
				<button id="speak_btn" class="speak_btn">Speak</button>
			</div>
			
		</div>

		<script >
		
			const $  = (s)=> document.querySelector(s);
			const $$ = (s)=> document.querySelectorAll(s);

			const voice_options 	= $('#voice_options');
			const volume_slider 	= $('#volume_slider');
			const rate_slider 		= $('#rate_slider');
			const pitch_slider 		= $('#pitch_slider');
			const source_text 		= $('#source_text');
			const speak_btn 		= $('#speak_btn');

			const voices_map = {};

			function check_compatibilty () {
				if(!('speechSynthesis' in window)){
					alert('This application features are not supported on this browser. try using google chrome or firefox please');
				}
			};

			function load_voices () {

				const voices = speechSynthesis.getVoices();
				
				//populate the select drop down menu with the available voices
				for( let voice of voices){
					const option = document.createElement('option');
					option.value = voice.name;
					option.innerHTML = voice.name;
					voice_options.appendChild(option);
					voices_map[voice.name] = voice;
				};
			};

			function speak (e) {
				console.log("speak()")

				//clear and reset
				speechSynthesis.cancel();

				const text = source_text.value;

				//splitting up long texts to sentences. 
				const sentences = text.split(".");

				for(let sentence of sentences){
					let chunk = create_speech_chunk();
					chunk.text = sentence;
					speechSynthesis.speak(chunk);
				}
			};
			function create_speech_chunk(){
				//this creates an empty chunk of speech stream.
				let msg = new SpeechSynthesisUtterance();

				msg.voice = voices_map[voice_options.value]; 
				//msg.voiceURI = 'native';
				msg.volume = volume_slider.value; // 0 to 1
				msg.rate = rate_slider.value; // 0.1 to 10
				msg.pitch = pitch_slider.value; //0 to 2
				msg.lang = 'en-GB';
				msg.onstart 	= function(event) { console.log("started") };
				msg.onend 		= function(event) { console.log('Finished in ' + event.elapsedTime + ' seconds.') };
				msg.onerror 	= function(event) { console.log('Errored ' + event) }
				msg.onpause 	= function(event) { console.log('paused ' + event) }
				msg.onboundary 	= function(event) { console.log('onboundary ' + event) }

				return msg;
			}

			function init(){

				console.log("init")

				check_compatibilty();

				speechSynthesis.onvoiceschanged = load_voices;

				speak_btn.addEventListener('click',speak);

			}

			init();

		</script>
		
	</body>
</html>
              
            
!

CSS

              
                
              
            
!

JS

              
                
              
            
!
999px

Console