Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

better javascript

  • Loading branch information...
commit 24f13ccf0439b06037327bccf7be77943b00a34c 1 parent 5a5f026
@hbouvier authored
Showing with 77 additions and 41 deletions.
  1. +14 −41 www/index.html
  2. +63 −0 www/tts.js
View
55 www/index.html
@@ -1,54 +1,27 @@
<html>
<head>
+ <script src='tts.js'></script>
<script>
- var index = 0;
- var playing = 0;
- var audio = [];
- function queue(text) {
- if (text === '') return;
- console.log('queuing '+index + ' text:'+text);
- audio[index] = document.createElement('audio');
- audio[index].src='http://localhost:8082/ws/tts?text='+text;
- audio[index].load();
- audio[index].addEventListener('ended', function () {
- console.log('ending '+playing);
- ++playing;
- if (playing < index) {
- console.log('playing '+playing);
- audio[playing].play();
- } else {
- index = playing = 0;
- }
- });
- //document.body.appendChild(audio[index]);
- ++index;
- }
-
-
- function play() {
- if (index > 0 && playing === 0) {
- console.log('playing '+playing);
- audio[playing].play();
- }
- }
- function playAll() {
+ function queueAll() {
var one = document.getElementById('one');
var two = document.getElementById('two');
var three = document.getElementById('three');
var foour = document.getElementById('four');
- queue(one.value);
- queue(two.value);
- queue(three.value);
- queue(four.value);
- play();
+ tts.queue(one.value);
+ tts.queue(two.value);
+ tts.queue(three.value);
+ tts.queue(four.value);
}
</script>
</head>
<body>
- <span> Phrase one <input id='one' type='edit' size=80 value=''></input> </span><br>
- <span> Phrase tow <input id='two' type='edit' size=80 value=''></input> </span><br>
- <span> Phrase three <input id='three' type='edit' size=80 value=''></input> </span><br>
- <span> Phrase four <input id='four' type='edit' size=80 value=''></input> </span><br>
- <input type='button' value='Play' onclick='playAll()'></input>
+ <span> Phrase one <input id='one' type='edit' size=80 value='' onblur='tts.queue(this.value)'></input> </span><br>
+ <span> Phrase tow <input id='two' type='edit' size=80 value='' onblur='tts.queue(this.value)'></input> </span><br>
+ <span> Phrase three <input id='three' type='edit' size=80 value='' onblur='tts.queue(this.value)'></input> </span><br>
+ <span> Phrase four <input id='four' type='edit' size=80 value='' onblur='tts.queue(this.value)'></input> </span><br>
+ <input type='button' value='Play' onclick='tts.play()'></input>
+ <input type='button' value='Pause' onclick='tts.pause()'></input>
+ <input type='button' value='Stop' onclick='tts.stop()'></input>
+ <input type='button' value='ReQueueAll' onclick='queueAll()'></input>
</body>
</html>
View
63 www/tts.js
@@ -0,0 +1,63 @@
+var tts = (function() {
+
+ var audioElements = [];
+ var indexOfAudioPlaying = -1;
+ var paused = false;
+ var ttsServerHost = 'http://localhost:8082';
+
+ function queue(text) {
+ if (text === '') return;
+ console.log('tts::queuing '+audioElements.length + ' text:'+text);
+ var audio = document.createElement('audio');
+ audio.src= ttsServerHost + '/ws/tts?text='+text;
+ audio.load();
+ audio.addEventListener('ended', function () {
+ console.log('ending '+indexOfAudioPlaying);
+ ++indexOfAudioPlaying;
+ if (indexOfAudioPlaying < audioElements.length) {
+ console.log('playing '+indexOfAudioPlaying);
+ audioElements[indexOfAudioPlaying].play();
+ } else {
+ indexOfAudioPlaying = -1;
+ audioElements = [];
+ }
+ });
+ audioElements.push(audio);
+ }
+ function play() {
+ if (audioElements.length > 0) {
+ if (indexOfAudioPlaying === -1) {
+ indexOfAudioPlaying = 0;
+ console.log('playing '+indexOfAudioPlaying);
+ audioElements[indexOfAudioPlaying].play();
+ } else if (paused) {
+ console.log('unpausing '+indexOfAudioPlaying);
+ paused = false;
+ audioElements[indexOfAudioPlaying].play();
+ }
+ }
+ }
+ function stop() {
+ if (indexOfAudioPlaying !== -1) {
+ console.log('stopping '+indexOfAudioPlaying);
+ var audio = audioElements[indexOfAudioPlaying];
+ indexOfAudioPlaying = -1;
+ audioElements = [];
+ audio.load('');
+ }
+ }
+ function pause() {
+ if (!paused && indexOfAudioPlaying !== -1) {
+ console.log('pausing '+indexOfAudioPlaying);
+ paused = true;
+ audioElements[indexOfAudioPlaying].pause();
+ }
+ }
+ return {
+ setHost : function(host) {ttsServerHost = host;},
+ queue: queue,
+ play : play,
+ stop : stop,
+ pause: pause
+ };
+})();
Please sign in to comment.
Something went wrong with that request. Please try again.