Pen Settings



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


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


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.


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.


		<title>Text to speech converter in JS</title>
			@import url('');
				background: papayawhip;
				font-family: Roboto;
				font-size: 18px;
				background: white;
				position: absolute;
				transform: translateX(-50%) translateY(-50%);
				width: 800px;
				box-shadow: 0 4px 15px 0px rgba(71, 0, 41, .5);
				border-radius: 5px;

				display: flex;
				flex-wrap: wrap;
				justify-content: center;
				color: salmon;
				font-size: 32px;
				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;

				margin: 15px 0;
				flex-basis: 100%;
				display: flex;
				justify-content: space-around;
				height: 30px;
				padding: 0 10px;
				flex-basis: 100%;

				background: #88e8e4;
				padding: 10px 20px;
				color: #000;
				border: 1px solid #3abcb7;
				cursor: pointer;
				text-align: center;
				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;
				outline: none;
			.btn-box:hover .speak_btn{
				box-shadow: 0px 4px 15px #666;
			.btn-box:active .speak_btn{
				box-shadow: 0px 2px 7px #666;

		<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.

			<div class="box voice-box">
				<span class="label">Choose a Voice</span>
				<select class="voices-list" id="voice_options"></select>
			<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 class="box">
				<span class="label">Rate</span>
				<input type="range" id="rate_slider" min="0" max="2" value="1" step="0.2" />

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

		<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 =;
					option.innerHTML =;
					voices_map[] = voice;

			function speak (e) {

				//clear and reset

				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;
			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(){



				speechSynthesis.onvoiceschanged = load_voices;