Skip to content
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: 19 additions & 1 deletion src/plugins/tts/WebTTSEngine.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,24 @@ export default class WebTTSEngine extends AbstractTTSEngine {
return typeof(window.speechSynthesis) !== 'undefined';
}

/**
* @param {SpeechSynthesisVoice} voice
* @returns {boolean}
*/
static isGoodVoice(voice) {
const badVoicePrefixes = [
// Exclude known novelty/undesired macOS voice families (e.g. joke/system variants)
'com.apple.speech.synthesis.voice.',
'com.apple.eloquence.',
'com.apple.voice.super-compact.',
];

return (
voice.voiceURI
&& !badVoicePrefixes.some(prefix => voice.voiceURI.startsWith(prefix))
Comment on lines +25 to +33
);
}

/** @param {TTSEngineOptions} options */
constructor(options) {
super(options);
Expand Down Expand Up @@ -80,7 +98,7 @@ export default class WebTTSEngine extends AbstractTTSEngine {

/** @override */
getVoices() {
const voices = speechSynthesis.getVoices();
const voices = speechSynthesis.getVoices().filter(WebTTSEngine.isGoodVoice);
if (voices.filter(v => v.default).length != 1) {
Comment on lines +101 to 102
// iOS bug where the default system voice is sometimes
// missing from the list
Expand Down
18 changes: 18 additions & 0 deletions tests/jest/plugins/tts/WebTTSEngine.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,24 @@ afterEach(() => {
});

describe('WebTTSEngine', () => {
test('isGoodVoice excludes known macOS joke voices', () => {
const voices = [
{ voiceURI: 'com.apple.speech.synthesis.voice.Bad News', name: 'Bad News' },
{ voiceURI: 'com.apple.eloquence.Eddy', name: 'Eddy' },
{ voiceURI: 'com.apple.voice.super-compact.en-US.Samantha', name: 'Samantha (SC)' },
{ voiceURI: 'com.apple.voice.compact.en-US.Samantha', name: 'Samantha' },
{ voiceURI: 'urn:moz-tts:sapi:David', name: 'David' },
{ name: 'No URI Voice' },
];

const filteredVoices = voices.filter(WebTTSEngine.isGoodVoice);

expect(filteredVoices).toEqual([
{ voiceURI: 'com.apple.voice.compact.en-US.Samantha', name: 'Samantha' },
{ voiceURI: 'urn:moz-tts:sapi:David', name: 'David' },
]);
});

test('getVoices should include default voice when no actual default', () => {
// iOS devices set all the voices to default -_-
speechSynthesis.getVoices = () => [
Expand Down
Loading