Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
web worker support
  • Loading branch information
kripken committed Jan 13, 2012
1 parent 904faf2 commit 4ea90aa
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 9 deletions.
2 changes: 1 addition & 1 deletion demo.html
Expand Up @@ -10,7 +10,7 @@
<body>
<h1>speak.js</h1>
<h2>Text-To-Speech on the Web</h2>
<form onsubmit="speak(text.value, { amplitude: amplitude.value, wordgap: workdgap.value, pitch: pitch.value, speed: speed.value, debug: true }); return false">
<form onsubmit="speak(text.value, { amplitude: amplitude.value, wordgap: workdgap.value, pitch: pitch.value, speed: speed.value, worker: true }); return false">
Text: <input type="text" name="text" size=50 value="Never gonna give, you, up.">
Amplitude: <input type="text" name="amplitude" size=5 value="100">
Pitch: <input type="text" name="pitch" size=5 value="50">
Expand Down
24 changes: 16 additions & 8 deletions speakClient.js
Expand Up @@ -69,15 +69,23 @@ function speak(text, args) {
}
}

var startTime = Date.now();
function handleWav(wav) {
var data = parseWav(wav); // validate the data and parse it

var wav = generateSpeech(text, args);
var data = parseWav(wav); // validate the data and parse it

// TODO: try playAudioDataAPI(data), and fallback if failed
playHTMLAudioElement(wav);
// TODO: try playAudioDataAPI(data), and fallback if failed
playHTMLAudioElement(wav);
}

var endTime = Date.now();
if (args && args.debug) console.log('speak.js: took ' + (endTime - startTime) + ' ms.');
if (!(args && args.worker)) {
// Do everything right now. speakGenerator.js must have been loaded.
handleWav(generateSpeech(text, args));
} else {
// Call a worker, which will return a wav that we then play
var worker = new Worker('speakWorker.js');
worker.onmessage = function(event) {
handleWav(event.data);
};
worker.postMessage({ text: text, args: args });
}
}

6 changes: 6 additions & 0 deletions speakWorker.js
@@ -0,0 +1,6 @@
importScripts('speakGenerator.js');

onmessage = function(event) {
postMessage(generateSpeech(event.data.text, event.data.args));
};

0 comments on commit 4ea90aa

Please sign in to comment.