Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Experiment with Open AI TTS voices #1269

Draft
wants to merge 4 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions BookReaderDemo/demo-internetarchive.html
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,26 @@
<i>Le Comte de Monte-Cristo</i> by Alexandre Dumas
</a>
</li>
<li>
German: <a href="/BookReaderDemo/demo-internetarchive.html?ocaid=buddenbrooksroma00mann">
<i>Buddenbrooks</i> by Thomas Mann
</a>
</li>
<li>
Japanese: <a href="/BookReaderDemo/demo-internetarchive.html?ocaid=sekainorekishi00chiy">
<i>Sekai no rekishi</i> by Ken Chiyoda
</a>
</li>
<li>
Albanian: <a href="/BookReaderDemo/demo-internetarchive.html?ocaid=lincol00kall">
<i>Lincoln</i> by Xhevat Kallajxhi
</a>
</li>
<li>
Tagalog: <a href="/BookReaderDemo/demo-internetarchive.html?ocaid=apz7279.0001.001.umich.edu">
<i>Salita</i> by R. G. L.
</a>
</li>
</ul>
</li>
<li>
Expand Down
13 changes: 13 additions & 0 deletions src/css/_controls.scss
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,19 @@
}
}
}

.engine.selected {
font-weight: bold;
text-decoration: none;
pointer-events: none;
}

.engine:not(:last-child)::after {
content: "|";
text-decoration: none;
display: inline-block;
padding: 0 0.5ch;
}
}

.scrubber {
Expand Down
77 changes: 60 additions & 17 deletions src/plugins/tts/AbstractTTSEngine.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,16 +51,17 @@ export default class AbstractTTSEngine {
this.events = $({});
/** @type {SpeechSynthesisVoice} */
this.voice = null;
this.preloadNext = true;
// Listen for voice changes (fired by subclasses)
this.events.on('voiceschanged', this.updateBestVoice);
this.events.trigger('voiceschanged');
setTimeout(() => this.events.trigger('voiceschanged'), 0);
}

/**
* @abstract
* @return {boolean}
*/
static isSupported() { throw new Error("Unimplemented abstract class"); }
isSupported() { throw new Error("Unimplemented abstract class"); }

/**
* @abstract
Expand All @@ -78,13 +79,14 @@ export default class AbstractTTSEngine {
/**
* @param {number} leafIndex
* @param {number} numLeafs total number of leafs in the current book
* @param {PageChunkIterator} chunkIterator
*/
start(leafIndex, numLeafs) {
start(leafIndex, numLeafs, chunkIterator = null) {
this.playing = true;
this.paused = false;
this.opts.onLoadingStart();

this._chunkIterator = new PageChunkIterator(numLeafs, leafIndex, {
this._chunkIterator = chunkIterator ?? new PageChunkIterator(numLeafs, leafIndex, {
server: this.opts.server,
bookPath: this.opts.bookPath,
pageBufferSize: 5,
Expand Down Expand Up @@ -140,7 +142,7 @@ export default class AbstractTTSEngine {
}

/** @param {string} voiceURI */
setVoice(voiceURI) {
async setVoice(voiceURI) {
// if the user actively selects a voice, don't re-choose best voice anymore
// MS Edge fires voices changed randomly very often
this.events.off('voiceschanged', this.updateBestVoice);
Expand All @@ -149,7 +151,15 @@ export default class AbstractTTSEngine {
if (this.opts.bookLanguage && hasLocalStorage()) {
localStorage.setItem(`BRtts-voice-${this.opts.bookLanguage}`, this.voice.voiceURI);
}
if (this.activeSound) this.activeSound.setVoice(this.voice);
if (this.activeSound) {
if (this.nextStepPromise) {
this._chunkIterator.decrement();
this.nextStepPromise = this.loadNextStep();
}
this.opts.onLoadingStart();
await this.activeSound.setVoice(this.voice);
this.opts.onLoadingComplete();
}
}

/** @param {number} newRate */
Expand All @@ -158,29 +168,53 @@ export default class AbstractTTSEngine {
if (this.activeSound) this.activeSound.setPlaybackRate(newRate);
}

/** @private */
async step() {
async loadNextStep() {
const chunk = await this._chunkIterator.next();
if (chunk == PageChunkIterator.AT_END) {
this.stop();
this.opts.onDone();
return;
return { chunk };
}
this.opts.onLoadingStart();

const sound = this.createSound(chunk);
sound.chunk = chunk;
sound.rate = this.playbackRate;
sound.voice = this.voice;
sound.load(() => this.opts.onLoadingComplete());

this.opts.onLoadingComplete();
await sound.load(() => this.opts.onLoadingComplete());
return {
chunk,
sound,
};
}

/** @private */
async step() {
const loadPromise = this.nextStepPromise ? this.nextStepPromise : this.loadNextStep();
if (!this.nextStepPromise) this.opts.onLoadingStart();
else {
const raceResolve = await Promise.race([
loadPromise,
new Promise(resolve => setTimeout(() => resolve('timeout'), 100)),
]);

if (raceResolve === 'timeout') {
this.opts.onLoadingStart();
}
}
const {chunk, sound} = await loadPromise;
if (chunk == PageChunkIterator.AT_END) {
this.stop();
this.opts.onDone();
return;
}
// this.opts.onLoadingComplete();
await this.opts.beforeChunkPlay(chunk);

if (!this.playing) return;

const playPromise = await this.playSound(sound)
const playPromise = this.playSound(sound)
.then(()=> this.opts.afterChunkPlay(sound.chunk));
if (this.preloadNext) {
this.nextStepPromise = this.loadNextStep();
}

if (this.paused) this.pause();
await playPromise;
Expand Down Expand Up @@ -266,7 +300,16 @@ export default class AbstractTTSEngine {
// Chrome Android was returning voice languages like `en_US` instead of `en-US`
const matchingVoices = voices.filter(v => v.lang.replace('_', '-').startsWith(lang));
if (matchingVoices.length) {
return matchingVoices.find(v => v.default) || matchingVoices[0];
return (
// Prefer Microsoft 'Natural' voices
matchingVoices.find(v => v.voiceURI.match(/Microsoft.*\(Natural\)/g))
// Prefer Google voices
|| matchingVoices.find(v => v.voiceURI.match(/Google/g))
// Prefer default if one is specified. This seems to be kind of random though,
// and is usually one of the lower quality voices.
|| matchingVoices.find(v => v.default)
|| matchingVoices[0]
);
}
}
}
Expand Down
Loading
Loading