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

Conversation

Federkun
Copy link
Contributor

@Federkun Federkun commented Sep 23, 2021

Why

As getVoices is populated asynchronous, on web, it may return an empty array. See https://stackoverflow.com/questions/49506716/speechsynthesis-getvoices-returns-empty-array-on-windows

On native it's possible to provide a voice's identifier as part of the options, to select a different voice. This PR allow web to do the same thing.

How

Test Plan

I applied this as a patch and tested on chrome (only chrome).

Checklist

  • Documentation is up to date to reflect these changes (eg: https://docs.expo.io and README.md).
  • This diff will work correctly for expo build (eg: updated @expo/xdl).
  • This diff will work correctly for expo prebuild & EAS Build (eg: updated a module plugin).

As `getVoices` is populated asynchronous, it will return an empty array, see https://stackoverflow.com/questions/49506716/speechsynthesis-getvoices-returns-empty-array-on-windows
On native it's possible to provide a `voice`'s identifier as part of the options, to select a different voice. Allow web to do the same thing.
@expo-bot expo-bot added the bot: suggestions ExpoBot has some suggestions label Sep 23, 2021
Comment on lines +56 to +64
const voices = await getVoices();
message.voice =
voices[
Math.max(
0,
voices.findIndex((voice) => voice.voiceURI === options.voice)
)
];
}
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

Comment on lines +94 to +95
async getVoices(): Promise<WebVoice[]> {
const voices = await getVoices();
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

@expo-bot expo-bot added bot: passed checks ExpoBot has nothing to complain about and removed bot: suggestions ExpoBot has some suggestions labels Sep 23, 2021
Copy link
Contributor

@bbarthec bbarthec left a comment

Choose a reason for hiding this comment

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

@Federkun, great job on this one! 👏 🥳
It looks rock solid 🪨
Let's add this single SO reference to the comment as well and we're ready to merge 😉

packages/expo-speech/src/Speech/ExponentSpeech.web.ts Outdated Show resolved Hide resolved
Co-authored-by: Bartłomiej Bukowski <bartlomiejbukowski.b@gmail.com>
@Federkun
Copy link
Contributor Author

Federkun commented Sep 23, 2021

Brilliant, added, thank you so much!

@bbarthec bbarthec merged commit ca09792 into expo:master Sep 23, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bot: passed checks ExpoBot has nothing to complain about
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants