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

2918: Add tts on web #2985

Draft
wants to merge 22 commits into
base: main
Choose a base branch
from
Draft

2918: Add tts on web #2985

wants to merge 22 commits into from

Conversation

bahaaTuffaha
Copy link
Contributor

@bahaaTuffaha bahaaTuffaha commented Nov 6, 2024

Short description

Adding text-to-speech functionality for web.

Proposed changes

  • TtsPlayer.tsx will receive content and title from useTtsPlayer.ts hook then it will process content as following:

    • Adding period to any <p> or <li> tag if there is non at the end.
    • Splitting the content using a library sentencex to support most languages.
    • Adding the title to the array.
  • TtsPlayer.tsx using EasySpeech library for web speech api .. why I used it instead of directly using the native api read here: https://github.com/leaonline/easy-speech/blob/master/FAQ.md#why-does-this-library-exists-if-i-can-use-tts-natively-in-the-browser

  • Web Speech API has some limitation on devices like Android I can't pause so I implemented a way to increment currentSentencesIndex at each sentence so pausing will be depend on sentence index . Another issue with Firefox about triggering onEnd function at any .cancel() method unlike chromium based browsers (added useeffect to check whether it reached the last index or not ... Note : sometimes content gets finished reading before reaching end of the array)

  • This is different from tts-native: voices needs to be installed from OS so a modal will show up TtsHelpModal to guide users for each platform. Note: by default for windows for example you have what the keyboard layout have of languages also it may need a restart to see the newly installed language.

  • Modified Modal.tsx to accept new props: icon and styling just for (backgroundColor, borderRadius)

  • Notes:

    • boundaryGuard is created just to prevent handleBoundary from incrementing again incases like handleForward or handleBackward (I'm still not sure about this function as I did it out of frustration 😂)
    • Not Tested on iOS !

Side effects

  • CityContentToolbar.tsx, CategoriesPage.tsx, LocalNewsPage.tsx, TuNewsDetailPage.tsx, RemoteContent.tsx

Testing

  • At integreat : Go to any 3rd level content in categories or news.
  • Click on ToolbarItem on the side called readAloud
  • The player will show up press play to make it start reading content.

Observations

I tested this PR on linux and I got different result at each browser:
Chrome: works as expected + nice voices.
Firefox: I can't pause (linux) + using opensource voice library from mbrola mb-en1 sounds robotic .
Brave: (linux) not working at all because there is no build in speech to text like google chrome does.
Windows on the other hand.. most browsers can run good as long you download the voices from the OS it self.
I didn't test it on mac OS...

Resolved issues

Fixes: #2918


@steffenkleinle steffenkleinle changed the title 2918 add tts on web 2918: add tts on web Nov 11, 2024
@steffenkleinle steffenkleinle changed the title 2918: add tts on web 2918: Add tts on web Nov 11, 2024
Copy link
Member

@ztefanie ztefanie left a comment

Choose a reason for hiding this comment

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

Nice work 🚀

Tested this on chrome and firefox.

Firefox voice is really crappy, but chrome is really nice 🚀 Maybe we add hint, that some browsers better support this feature? Maybe @hauf-toni can share some input, how to handle this best from a UI perspective.

Found a few bug:

issue 1:

  1. Open this page: http://localhost:9000/augsburg/de/willkommen/willkommen-in-augsburg-2
  2. Click "read aloud"
  3. Click "play"
  4. Click on "Weiter"
    Result: The reader starts reading out the beginning of the current sentance, when clicking "weiter" for the first time. When clicking "weiter" a second time, it correctly jumps to the next sentance
    Expected result: When clicking "weiter" for the first time, it should directly jump to the next sentance.

issue 2:
When opening this page: http://localhost:9000/augsburg/de/willkommen
the "read aloud" button is displayed, but does nothing when clicked.
Expected: Should either not be displayed or does something when clicked

issue 3:
Open this page: http://localhost:9000/augsburg/de/behoerden-beratung/behoerden/auslaenderbehoerde
Click "next" to jump to the last sentance
in this line "Weitere Informationen finden Sie unter [hier]" the reader switches to the "start"-mode, but there is still this whole contact blog that is read

issue 4:
When clicking pause I see the initial view of the tts-player, with the first word of the page in the heading. I think this is confusing, as clicking play does not continue reading from the beginning. I would expect that the player just changes the pause button to showing a play button.
what i get when i click pause
pause-is
what i expected
pause2

issue 5:
on firefox when the tts reaches the end of the text, the player does not reset to start as it is for other browsers.

@@ -34,4 +36,6 @@ export const commonLightColors = {
invalidInput: '#B3261E',
warningColor: '#FFA726',
linkColor: '#0b57d0',
ttsPlayerWarningBackground: 'rgba(255, 253, 230, 1)',
warning_amber: 'rgba(249, 124, 0, 1)',
Copy link
Member

Choose a reason for hiding this comment

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

Please do not use _ in names. Use camelCase instead. Also please do not put the actual name of the color in the variable nameing, because then it would need renaming if the color changes.
Please stay consistent, either prefix both variables with "ttsPlayer" or none of them

@@ -39,6 +39,7 @@ const commonIntegreatBuildConfig: CommonBuildConfigType = {
fixedCity: null,
cityNotCooperatingTemplate,
chat: true,
tts: false,
Copy link
Member

Choose a reason for hiding this comment

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

Why is this "false". Do we only want this feature on beta for now?

platforms: # relevant platforms, possible values: web, android and ios
- web
en: Added an exciting new Text-to-Speech feature!
de: Eine spannende neue Text-zu-Sprache-Funktion wurde hinzugefügt!
Copy link
Member

Choose a reason for hiding this comment

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

Either remove this release_note or enable the feature on production, by also setting tts to true in the build-config for integreat.

@@ -6834,7 +6840,13 @@
"openInApp": "افتح في تطبيق {{appName}}",
"open": "افتح",
"view": "عرض",
"getOnPlayStore": "احصل عليه - على متجر Google Play"
"getOnPlayStore": "احصل عليه - على متجر Google Play",
Copy link
Member

Choose a reason for hiding this comment

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

Nice, one more language in the team 🚀

"accessibility": "Barrierefreiheit",
"readAloud": "Vorlesefunktion",
"previous": "Zurück",
"next": "Weiter",
Copy link
Member

Choose a reason for hiding this comment

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

💭 We already have translations for previous and next in the translations file, but in other sections/categories. I think it is fine to keep it, but we may think about this in the future, if it is worth the effort to restructure things to lower translation costs.

text: sentences.slice(index).join(' '),
voice: selectedVoice,
pitch: 1,
rate: 1,
Copy link
Member

Choose a reason for hiding this comment

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

i think pitch and rate could be deleted as these are the defaults

flex-direction: ${props => (props.$isPlaying ? 'column' : 'row')};
justify-content: center;
align-items: center;
align-self: center;
Copy link
Member

Choose a reason for hiding this comment

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

align self can be removed as it does nothing

import Icon from './base/Icon'

const StyledTtsPlayer = styled.dialog<{ $isPlaying: boolean }>`
background-color: #dedede;
Copy link
Member

Choose a reason for hiding this comment

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

Can you please move this color to the theme?

font-size: 18px;
`

const CloseButton = styled(Button)`
Copy link
Member

Choose a reason for hiding this comment

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

Please recude code duplication by moving shared styles between styled components by moving them to a shared base class like this:

const buttonBaseStyles = `
  display: flex;
  justify-content: center;
  align-items: center;
  transition: box-shadow 0.2s ease, transform 0.1s ease;

  &:active {
    box-shadow: none;
    transform: translateY(2px);
  }
`

const CloseButton = styled(Button)`
  ${buttonBaseStyles};
 // other stylings that are not shared

`

const StyledPlayIcon = styled(Button)`
background-color: #232323;
Copy link
Member

Choose a reason for hiding this comment

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

please move this color to the theme

@bahaaTuffaha
Copy link
Contributor Author

bahaaTuffaha commented Dec 13, 2024

issue 1:

  1. Open this page: http://localhost:9000/augsburg/de/willkommen/willkommen-in-augsburg-2
  2. Click "read aloud"
  3. Click "play"
  4. Click on "Weiter"
    Result: The reader starts reading out the beginning of the current sentance, when clicking "weiter" for the first time. When clicking "weiter" a second time, it correctly jumps to the next sentance
    Expected result: When clicking "weiter" for the first time, it should directly jump to the next sentance.

issue 2: When opening this page: http://localhost:9000/augsburg/de/willkommen the "read aloud" button is displayed, but does nothing when clicked. Expected: Should either not be displayed or does something when clicked

issue 3: Open this page: http://localhost:9000/augsburg/de/behoerden-beratung/behoerden/auslaenderbehoerde Click "next" to jump to the last sentance in this line "Weitere Informationen finden Sie unter [hier]" the reader switches to the "start"-mode, but there is still this whole contact blog that is read

Can you please mention which browser you used for each issue?

@ztefanie
Copy link
Member

Can you please mention which browser you used for each issue?

I mostly used Chrome, sometimes I double checked with Firefox

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Add TTS on web
2 participants