Skip to content

Commit e15e768

Browse files
authored
Merge pull request #254 from jankapunkt/fix/ms-natural-voices
fix: prevent resumeInfinity on Edge/MS (Natural) voices
2 parents 7e5b828 + 2abf344 commit e15e768

File tree

6 files changed

+73
-27
lines changed

6 files changed

+73
-27
lines changed

dist/EasySpeech.cjs.js

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -769,7 +769,10 @@ EasySpeech.speak = function (_ref4) {
769769
utterance.volume = getValue({
770770
volume: volume
771771
});
772-
debugUtterance(utterance);
772+
var isMsNatural = utterance.voice && utterance.voice.name && utterance.voice.name.toLocaleLowerCase().includes('(natural)');
773+
debugUtterance(utterance, {
774+
isMsNatural: isMsNatural
775+
});
773776
utteranceEvents.forEach(function (name) {
774777
var _internal$handlers;
775778
var fn = handlers[name];
@@ -801,7 +804,8 @@ EasySpeech.speak = function (_ref4) {
801804
utterance.addEventListener('start', function () {
802805
patches.paused = false;
803806
patches.speaking = true;
804-
var useResumeInfinity = typeof infiniteResume === 'boolean' ? infiniteResume : !patches.isFirefox && !patches.isSafari && patches.isAndroid !== true;
807+
var defaultResumeInfinity = !isMsNatural && !patches.isFirefox && !patches.isSafari && patches.isAndroid !== true;
808+
var useResumeInfinity = typeof infiniteResume === 'boolean' ? infiniteResume : defaultResumeInfinity;
805809
if (useResumeInfinity) {
806810
resumeInfinity(utterance);
807811
}
@@ -826,7 +830,7 @@ EasySpeech.speak = function (_ref4) {
826830
clearTimeout(timeoutResumeInfinity);
827831
internal.speechSynthesis.cancel();
828832
setTimeout(function () {
829-
internal.speechSynthesis.speak(utterance);
833+
return internal.speechSynthesis.speak(utterance);
830834
}, 10);
831835
});
832836
};
@@ -837,7 +841,10 @@ var debugUtterance = function debugUtterance(_ref5) {
837841
pitch = _ref5.pitch,
838842
rate = _ref5.rate,
839843
volume = _ref5.volume;
840-
debug("utterance: voice=".concat(voice === null || voice === void 0 ? void 0 : voice.name, " volume=").concat(volume, " rate=").concat(rate, " pitch=").concat(pitch));
844+
var _ref6 = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {},
845+
_ref6$isMsNatural = _ref6.isMsNatural,
846+
isMsNatural = _ref6$isMsNatural === void 0 ? false : _ref6$isMsNatural;
847+
debug("utterance: voice=".concat(voice === null || voice === void 0 ? void 0 : voice.name, " volume=").concat(volume, " rate=").concat(rate, " pitch=").concat(pitch, " isMsNatural=").concat(isMsNatural));
841848
};
842849

843850
/**

dist/EasySpeech.es5.js

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -767,7 +767,10 @@ EasySpeech.speak = function (_ref4) {
767767
utterance.volume = getValue({
768768
volume: volume
769769
});
770-
debugUtterance(utterance);
770+
var isMsNatural = utterance.voice && utterance.voice.name && utterance.voice.name.toLocaleLowerCase().includes('(natural)');
771+
debugUtterance(utterance, {
772+
isMsNatural: isMsNatural
773+
});
771774
utteranceEvents.forEach(function (name) {
772775
var _internal$handlers;
773776
var fn = handlers[name];
@@ -799,7 +802,8 @@ EasySpeech.speak = function (_ref4) {
799802
utterance.addEventListener('start', function () {
800803
patches.paused = false;
801804
patches.speaking = true;
802-
var useResumeInfinity = typeof infiniteResume === 'boolean' ? infiniteResume : !patches.isFirefox && !patches.isSafari && patches.isAndroid !== true;
805+
var defaultResumeInfinity = !isMsNatural && !patches.isFirefox && !patches.isSafari && patches.isAndroid !== true;
806+
var useResumeInfinity = typeof infiniteResume === 'boolean' ? infiniteResume : defaultResumeInfinity;
803807
if (useResumeInfinity) {
804808
resumeInfinity(utterance);
805809
}
@@ -824,7 +828,7 @@ EasySpeech.speak = function (_ref4) {
824828
clearTimeout(timeoutResumeInfinity);
825829
internal.speechSynthesis.cancel();
826830
setTimeout(function () {
827-
internal.speechSynthesis.speak(utterance);
831+
return internal.speechSynthesis.speak(utterance);
828832
}, 10);
829833
});
830834
};
@@ -835,7 +839,10 @@ var debugUtterance = function debugUtterance(_ref5) {
835839
pitch = _ref5.pitch,
836840
rate = _ref5.rate,
837841
volume = _ref5.volume;
838-
debug("utterance: voice=".concat(voice === null || voice === void 0 ? void 0 : voice.name, " volume=").concat(volume, " rate=").concat(rate, " pitch=").concat(pitch));
842+
var _ref6 = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {},
843+
_ref6$isMsNatural = _ref6.isMsNatural,
844+
isMsNatural = _ref6$isMsNatural === void 0 ? false : _ref6$isMsNatural;
845+
debug("utterance: voice=".concat(voice === null || voice === void 0 ? void 0 : voice.name, " volume=").concat(volume, " rate=").concat(rate, " pitch=").concat(pitch, " isMsNatural=").concat(isMsNatural));
839846
};
840847

841848
/**

dist/EasySpeech.iife.js

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -882,7 +882,10 @@ var EasySpeech = (function () {
882882
utterance.volume = getValue({
883883
volume: volume
884884
});
885-
debugUtterance(utterance);
885+
var isMsNatural = utterance.voice && utterance.voice.name && utterance.voice.name.toLocaleLowerCase().includes('(natural)');
886+
debugUtterance(utterance, {
887+
isMsNatural: isMsNatural
888+
});
886889
utteranceEvents.forEach(function (name) {
887890
var _internal$handlers;
888891
var fn = handlers[name];
@@ -914,7 +917,8 @@ var EasySpeech = (function () {
914917
utterance.addEventListener('start', function () {
915918
patches.paused = false;
916919
patches.speaking = true;
917-
var useResumeInfinity = typeof infiniteResume === 'boolean' ? infiniteResume : !patches.isFirefox && !patches.isSafari && patches.isAndroid !== true;
920+
var defaultResumeInfinity = !isMsNatural && !patches.isFirefox && !patches.isSafari && patches.isAndroid !== true;
921+
var useResumeInfinity = typeof infiniteResume === 'boolean' ? infiniteResume : defaultResumeInfinity;
918922
if (useResumeInfinity) {
919923
resumeInfinity(utterance);
920924
}
@@ -939,7 +943,7 @@ var EasySpeech = (function () {
939943
clearTimeout(timeoutResumeInfinity);
940944
internal.speechSynthesis.cancel();
941945
setTimeout(function () {
942-
internal.speechSynthesis.speak(utterance);
946+
return internal.speechSynthesis.speak(utterance);
943947
}, 10);
944948
});
945949
};
@@ -950,7 +954,10 @@ var EasySpeech = (function () {
950954
pitch = _ref5.pitch,
951955
rate = _ref5.rate,
952956
volume = _ref5.volume;
953-
debug("utterance: voice=".concat(voice === null || voice === void 0 ? void 0 : voice.name, " volume=").concat(volume, " rate=").concat(rate, " pitch=").concat(pitch));
957+
var _ref6 = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {},
958+
_ref6$isMsNatural = _ref6.isMsNatural,
959+
isMsNatural = _ref6$isMsNatural === void 0 ? false : _ref6$isMsNatural;
960+
debug("utterance: voice=".concat(voice === null || voice === void 0 ? void 0 : voice.name, " volume=").concat(volume, " rate=").concat(rate, " pitch=").concat(pitch, " isMsNatural=").concat(isMsNatural));
954961
};
955962

956963
/**

dist/EasySpeech.js

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -734,7 +734,14 @@ EasySpeech.speak = ({ text, voice, pitch, rate, volume, force, infiniteResume, .
734734
utterance.pitch = getValue({ pitch });
735735
utterance.rate = getValue({ rate });
736736
utterance.volume = getValue({ volume });
737-
debugUtterance(utterance);
737+
738+
const isMsNatural =
739+
utterance.voice &&
740+
utterance.voice.name &&
741+
utterance.voice.name
742+
.toLocaleLowerCase()
743+
.includes('(natural)');
744+
debugUtterance(utterance, { isMsNatural });
738745

739746
utteranceEvents.forEach(name => {
740747
const fn = handlers[name];
@@ -769,9 +776,15 @@ EasySpeech.speak = ({ text, voice, pitch, rate, volume, force, infiniteResume, .
769776
patches.paused = false;
770777
patches.speaking = true;
771778

779+
const defaultResumeInfinity = (
780+
!isMsNatural &&
781+
!patches.isFirefox &&
782+
!patches.isSafari &&
783+
patches.isAndroid !== true
784+
);
772785
const useResumeInfinity = typeof infiniteResume === 'boolean'
773786
? infiniteResume
774-
: !patches.isFirefox && !patches.isSafari && patches.isAndroid !== true;
787+
: defaultResumeInfinity;
775788

776789
if (useResumeInfinity) {
777790
resumeInfinity(utterance);
@@ -798,15 +811,13 @@ EasySpeech.speak = ({ text, voice, pitch, rate, volume, force, infiniteResume, .
798811
clearTimeout(timeoutResumeInfinity);
799812
internal.speechSynthesis.cancel();
800813

801-
setTimeout(() => {
802-
internal.speechSynthesis.speak(utterance);
803-
}, 10);
814+
setTimeout(() => internal.speechSynthesis.speak(utterance), 10);
804815
})
805816
};
806817

807818
/** @private **/
808-
const debugUtterance = ({ voice, pitch, rate, volume }) => {
809-
debug(`utterance: voice=${voice?.name} volume=${volume} rate=${rate} pitch=${pitch}`);
819+
const debugUtterance = ({ voice, pitch, rate, volume }, { isMsNatural = false } = {}) => {
820+
debug(`utterance: voice=${voice?.name} volume=${volume} rate=${rate} pitch=${pitch} isMsNatural=${isMsNatural}`);
810821
};
811822

812823
/**

src/EasySpeech.js

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -734,7 +734,14 @@ EasySpeech.speak = ({ text, voice, pitch, rate, volume, force, infiniteResume, .
734734
utterance.pitch = getValue({ pitch })
735735
utterance.rate = getValue({ rate })
736736
utterance.volume = getValue({ volume })
737-
debugUtterance(utterance)
737+
738+
const isMsNatural =
739+
utterance.voice &&
740+
utterance.voice.name &&
741+
utterance.voice.name
742+
.toLocaleLowerCase()
743+
.includes('(natural)')
744+
debugUtterance(utterance, { isMsNatural })
738745

739746
utteranceEvents.forEach(name => {
740747
const fn = handlers[name]
@@ -769,9 +776,15 @@ EasySpeech.speak = ({ text, voice, pitch, rate, volume, force, infiniteResume, .
769776
patches.paused = false
770777
patches.speaking = true
771778

779+
const defaultResumeInfinity = (
780+
!isMsNatural &&
781+
!patches.isFirefox &&
782+
!patches.isSafari &&
783+
patches.isAndroid !== true
784+
)
772785
const useResumeInfinity = typeof infiniteResume === 'boolean'
773786
? infiniteResume
774-
: !patches.isFirefox && !patches.isSafari && patches.isAndroid !== true
787+
: defaultResumeInfinity
775788

776789
if (useResumeInfinity) {
777790
resumeInfinity(utterance)
@@ -798,15 +811,13 @@ EasySpeech.speak = ({ text, voice, pitch, rate, volume, force, infiniteResume, .
798811
clearTimeout(timeoutResumeInfinity)
799812
internal.speechSynthesis.cancel()
800813

801-
setTimeout(() => {
802-
internal.speechSynthesis.speak(utterance)
803-
}, 10)
814+
setTimeout(() => internal.speechSynthesis.speak(utterance), 10)
804815
})
805816
}
806817

807818
/** @private **/
808-
const debugUtterance = ({ voice, pitch, rate, volume }) => {
809-
debug(`utterance: voice=${voice?.name} volume=${volume} rate=${rate} pitch=${pitch}`)
819+
const debugUtterance = ({ voice, pitch, rate, volume }, { isMsNatural = false } = {}) => {
820+
debug(`utterance: voice=${voice?.name} volume=${volume} rate=${rate} pitch=${pitch} isMsNatural=${isMsNatural}`)
810821
}
811822

812823
/**

tests/unit.tests.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,10 @@ describe('unit tests', function () {
126126
expect(defaultVoice).to.equal(voices[1])
127127
})
128128
it('sets a language-specific voice as default voice, if no .default is available', async () => {
129-
global.navigator = { language: 'de-DE' }
129+
const window = {}
130+
window.window = window // this is intended!
131+
global.window = global.window || window
132+
window.navigator = { language: 'de-DE' }
130133
const voices = [{}, {}, { lang: 'de_DE' }]
131134
await initScope({
132135
speechSynthesis: { getVoices: () => voices }

0 commit comments

Comments
 (0)