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

[web][speech] Improve expo speech for web. #14516

Merged
merged 6 commits into from
Sep 23, 2021
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions packages/expo-speech/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
### 🐛 Bug fixes

- Fix setting speaking listener for projects with `react-native@>0.64.0`. ([#13654](https://github.com/expo/expo/pull/13654) by [@dsokal](https://github.com/dsokal))
- Fix empty voices list on web and allow the change voice when using `speak`. ([#4516](https://github.com/expo/expo/pull/14516) by [@Federkun](https://github.com/Federkun))

### 💡 Others

Expand Down
2 changes: 1 addition & 1 deletion packages/expo-speech/build/ExponentSpeech.web.d.ts

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

25 changes: 22 additions & 3 deletions packages/expo-speech/build/ExponentSpeech.web.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion packages/expo-speech/build/ExponentSpeech.web.js.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

33 changes: 30 additions & 3 deletions packages/expo-speech/src/Speech/ExponentSpeech.web.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,23 @@ import { SpeechOptions, WebVoice, VoiceQuality } from './Speech.types';
//https://developer.mozilla.org/en-US/docs/Web/API/SpeechSynthesisUtterance/text
const MAX_SPEECH_INPUT_LENGTH = 32767;

async function getVoices(): Promise<SpeechSynthesisVoice[]> {
return new Promise<SpeechSynthesisVoice[]>((resolve) => {
const voices = window.speechSynthesis.getVoices();

if (voices.length > 0) {
resolve(voices);
return;
}

// when a page loads it takes some amount of time to populate the voices list
window.speechSynthesis.onvoiceschanged = function () {
Federkun marked this conversation as resolved.
Show resolved Hide resolved
const voices = window.speechSynthesis.getVoices();
resolve(voices);
};
});
}

export default {
get name(): string {
return 'ExponentSpeech';
Expand Down Expand Up @@ -32,9 +49,19 @@ export default {
message.volume = options.volume;
}
if ('_voiceIndex' in options && options._voiceIndex != null) {
const voices = window.speechSynthesis.getVoices();
const voices = await getVoices();
message.voice = voices[Math.min(voices.length - 1, Math.max(0, options._voiceIndex))];
}
if (typeof options.voice === 'string') {
const voices = await getVoices();
message.voice =
voices[
Math.max(
0,
voices.findIndex((voice) => voice.voiceURI === options.voice)
)
];
}
Comment on lines +57 to +65
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If the voice provided in the options don't match any available voice we fallback to the first one in the list.
Not doing any clever stuff with voice.default here, as _voiceIndex does the same

if (typeof options.onResume === 'function') {
message.onresume = options.onResume;
}
Expand Down Expand Up @@ -64,8 +91,8 @@ export default {

return message;
},
getVoices(): WebVoice[] {
const voices = window.speechSynthesis.getVoices();
async getVoices(): Promise<WebVoice[]> {
const voices = await getVoices();
Comment on lines +95 to +96
Copy link
Contributor Author

@Federkun Federkun Sep 23, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is fine to be async. It's used in an async function already:

export async function getAvailableVoicesAsync(): Promise<Voice[]> {
  if (!ExponentSpeech.getVoices) {
    throw new UnavailabilityError('Speech', 'getVoices');
  }
  return ExponentSpeech.getVoices(); // <--
}

It's not a BC

return voices.map((voice) => ({
identifier: voice.voiceURI,
quality: VoiceQuality.Default,
Expand Down