-
Notifications
You must be signed in to change notification settings - Fork 1.1k
feat: add Text-to-Speech support via Web Speech API #502
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
Open
x64zx
wants to merge
7
commits into
siteboon:main
Choose a base branch
from
x64zx:feat/tts-web-speech-api
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 3 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
09f8fed
feat: add Text-to-Speech support via Web Speech API
KAWANO-CO-LTD 81d3265
fix: address CodeRabbit review — i18n, a11y, unmount cleanup
KAWANO-CO-LTD 1babd63
fix: seed lastSpokenIndex to prevent replaying historical messages
KAWANO-CO-LTD 839ca30
fix: guard parseFloat against NaN from corrupted localStorage
KAWANO-CO-LTD cf5f795
fix: use ref for chatMessages.length in toggle closure
KAWANO-CO-LTD 81b538b
perf: skip speechSynthesis polling when TTS is inactive
KAWANO-CO-LTD 2bcce9c
fix: exclude thinking messages from TTS auto-speech
KAWANO-CO-LTD File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
202 changes: 202 additions & 0 deletions
202
src/components/quick-settings-panel/view/QuickSettingsTtsSection.tsx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,202 @@ | ||
| import { useTranslation } from 'react-i18next'; | ||
| import { Volume2, VolumeX, Play } from 'lucide-react'; | ||
| import type { VoiceInfo } from '../../../hooks/useSpeechOutput'; | ||
| import { SETTING_ROW_CLASS } from '../constants'; | ||
| import QuickSettingsSection from './QuickSettingsSection'; | ||
|
|
||
| type QuickSettingsTtsSectionProps = { | ||
| enabled: boolean; | ||
| onToggle: () => void; | ||
| rate: number; | ||
| onRateChange: (rate: number) => void; | ||
| pitch: number; | ||
| onPitchChange: (pitch: number) => void; | ||
| voiceURI: string; | ||
| onVoiceChange: (voiceURI: string) => void; | ||
| lang: string; | ||
| onLangChange: (lang: string) => void; | ||
| filteredVoices: VoiceInfo[]; | ||
| availableLanguages: string[]; | ||
| onTestVoice: () => void; | ||
| isSpeaking: boolean; | ||
| onStop: () => void; | ||
| }; | ||
|
|
||
| export default function QuickSettingsTtsSection({ | ||
| enabled, | ||
| onToggle, | ||
| rate, | ||
| onRateChange, | ||
| pitch, | ||
| onPitchChange, | ||
| voiceURI, | ||
| onVoiceChange, | ||
| lang, | ||
| onLangChange, | ||
| filteredVoices, | ||
| availableLanguages, | ||
| onTestVoice, | ||
| isSpeaking, | ||
| onStop, | ||
| }: QuickSettingsTtsSectionProps) { | ||
| const { t } = useTranslation('settings'); | ||
|
|
||
| return ( | ||
| <QuickSettingsSection title={t('quickSettings.tts.sectionTitle')}> | ||
| {/* Enable/Disable toggle */} | ||
| <div className={SETTING_ROW_CLASS}> | ||
| <span id="tts-enabled-label" className="flex items-center gap-2 text-sm text-gray-900 dark:text-white"> | ||
| {enabled ? ( | ||
| <Volume2 className="h-4 w-4 text-gray-600 dark:text-gray-400" /> | ||
| ) : ( | ||
| <VolumeX className="h-4 w-4 text-gray-600 dark:text-gray-400" /> | ||
| )} | ||
| {t('quickSettings.tts.enabled')} | ||
| </span> | ||
| <button | ||
| type="button" | ||
| role="switch" | ||
| aria-checked={enabled} | ||
| aria-labelledby="tts-enabled-label" | ||
| onClick={onToggle} | ||
| className={`relative inline-flex h-5 w-9 shrink-0 cursor-pointer rounded-full border-2 border-transparent transition-colors focus:outline-none focus:ring-2 focus:ring-blue-500 focus:ring-offset-2 ${ | ||
| enabled ? 'bg-blue-600' : 'bg-gray-200 dark:bg-gray-700' | ||
| }`} | ||
| > | ||
| <span | ||
| className={`pointer-events-none inline-block h-4 w-4 transform rounded-full bg-white shadow ring-0 transition-transform ${ | ||
| enabled ? 'translate-x-4' : 'translate-x-0' | ||
| }`} | ||
| /> | ||
| </button> | ||
| </div> | ||
|
|
||
| {enabled && ( | ||
| <> | ||
| {/* Language filter */} | ||
| <div className="space-y-1 px-1"> | ||
| <label htmlFor="tts-lang-select" className="text-xs text-gray-500 dark:text-gray-400"> | ||
| {t('quickSettings.tts.language')} | ||
| </label> | ||
| <select | ||
| id="tts-lang-select" | ||
| value={lang} | ||
| onChange={(e) => { | ||
| onLangChange(e.target.value); | ||
| onVoiceChange(''); | ||
| }} | ||
| className="w-full rounded-md border border-gray-300 bg-white px-2 py-1.5 text-sm text-gray-900 focus:border-blue-500 focus:outline-none focus:ring-1 focus:ring-blue-500 dark:border-gray-600 dark:bg-gray-800 dark:text-white" | ||
| > | ||
| <option value="">{t('quickSettings.tts.allLanguages')}</option> | ||
| {availableLanguages.map((l) => ( | ||
| <option key={l} value={l}> | ||
| {l} | ||
| </option> | ||
| ))} | ||
| </select> | ||
| </div> | ||
|
|
||
| {/* Voice selection */} | ||
| <div className="space-y-1 px-1"> | ||
| <label htmlFor="tts-voice-select" className="text-xs text-gray-500 dark:text-gray-400"> | ||
| {t('quickSettings.tts.voice', { count: filteredVoices.length })} | ||
| </label> | ||
| <select | ||
| id="tts-voice-select" | ||
| value={voiceURI} | ||
| onChange={(e) => onVoiceChange(e.target.value)} | ||
| className="w-full rounded-md border border-gray-300 bg-white px-2 py-1.5 text-sm text-gray-900 focus:border-blue-500 focus:outline-none focus:ring-1 focus:ring-blue-500 dark:border-gray-600 dark:bg-gray-800 dark:text-white" | ||
| > | ||
| <option value="">{t('quickSettings.tts.voiceAuto')}</option> | ||
| {filteredVoices.map((v) => ( | ||
| <option key={v.voiceURI} value={v.voiceURI}> | ||
| {v.name} ({v.lang}){v.localService ? '' : ` [${t('quickSettings.tts.network')}]`} | ||
| </option> | ||
| ))} | ||
| </select> | ||
| </div> | ||
|
|
||
| {/* Rate slider */} | ||
| <div className="space-y-1 px-1"> | ||
| <div className="flex items-center justify-between"> | ||
| <label htmlFor="tts-rate-input" className="text-xs text-gray-500 dark:text-gray-400"> | ||
| {t('quickSettings.tts.speed')} | ||
| </label> | ||
| <span className="text-xs font-mono text-gray-500 dark:text-gray-400"> | ||
| {rate.toFixed(1)}x | ||
| </span> | ||
| </div> | ||
| <input | ||
| id="tts-rate-input" | ||
| type="range" | ||
| min="0.5" | ||
| max="3.0" | ||
| step="0.1" | ||
| value={rate} | ||
| onChange={(e) => onRateChange(parseFloat(e.target.value))} | ||
| className="w-full accent-blue-600" | ||
| /> | ||
| <div className="flex justify-between text-[10px] text-gray-400"> | ||
| <span>0.5x</span> | ||
| <span>1.0x</span> | ||
| <span>2.0x</span> | ||
| <span>3.0x</span> | ||
| </div> | ||
| </div> | ||
|
|
||
| {/* Pitch slider */} | ||
| <div className="space-y-1 px-1"> | ||
| <div className="flex items-center justify-between"> | ||
| <label htmlFor="tts-pitch-input" className="text-xs text-gray-500 dark:text-gray-400"> | ||
| {t('quickSettings.tts.pitch')} | ||
| </label> | ||
| <span className="text-xs font-mono text-gray-500 dark:text-gray-400"> | ||
| {pitch.toFixed(1)} | ||
| </span> | ||
| </div> | ||
| <input | ||
| id="tts-pitch-input" | ||
| type="range" | ||
| min="0.5" | ||
| max="2.0" | ||
| step="0.1" | ||
| value={pitch} | ||
| onChange={(e) => onPitchChange(parseFloat(e.target.value))} | ||
| className="w-full accent-blue-600" | ||
| /> | ||
coderabbitai[bot] marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| <div className="flex justify-between text-[10px] text-gray-400"> | ||
| <span>{t('quickSettings.tts.pitchLow')}</span> | ||
| <span>{t('quickSettings.tts.pitchNormal')}</span> | ||
| <span>{t('quickSettings.tts.pitchHigh')}</span> | ||
| </div> | ||
| </div> | ||
|
|
||
| {/* Test / Stop button */} | ||
| <div className="px-1"> | ||
| <button | ||
| type="button" | ||
| onClick={isSpeaking ? onStop : onTestVoice} | ||
| className={`flex w-full items-center justify-center gap-2 rounded-md px-3 py-1.5 text-sm font-medium transition-colors ${ | ||
| isSpeaking | ||
| ? 'bg-red-100 text-red-700 hover:bg-red-200 dark:bg-red-900/30 dark:text-red-400 dark:hover:bg-red-900/50' | ||
| : 'bg-blue-100 text-blue-700 hover:bg-blue-200 dark:bg-blue-900/30 dark:text-blue-400 dark:hover:bg-blue-900/50' | ||
| }`} | ||
| > | ||
| {isSpeaking ? ( | ||
| <> | ||
| <VolumeX className="h-4 w-4" /> | ||
| {t('quickSettings.tts.stop')} | ||
| </> | ||
| ) : ( | ||
| <> | ||
| <Play className="h-4 w-4" /> | ||
| {t('quickSettings.tts.testVoice')} | ||
| </> | ||
| )} | ||
| </button> | ||
| </div> | ||
| </> | ||
| )} | ||
| </QuickSettingsSection> | ||
| ); | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,49 @@ | ||
| import { createContext, useContext, type ReactNode } from 'react'; | ||
| import { useSpeechOutput } from '../hooks/useSpeechOutput'; | ||
| import type { VoiceInfo } from '../hooks/useSpeechOutput'; | ||
|
|
||
| type TtsContextValue = { | ||
| enabled: boolean; | ||
| toggle: () => void; | ||
| rate: number; | ||
| setRate: (rate: number) => void; | ||
| pitch: number; | ||
| setPitch: (pitch: number) => void; | ||
| voiceURI: string; | ||
| setVoiceURI: (uri: string) => void; | ||
| lang: string; | ||
| setLang: (lang: string) => void; | ||
| isSpeaking: boolean; | ||
| speak: (text: string) => void; | ||
| stop: () => void; | ||
| testVoice: () => void; | ||
| availableVoices: VoiceInfo[]; | ||
| filteredVoices: VoiceInfo[]; | ||
| availableLanguages: string[]; | ||
| }; | ||
|
|
||
| const TtsContext = createContext<TtsContextValue | null>(null); | ||
|
|
||
| type ChatMessage = { | ||
| type: string; | ||
| content?: string; | ||
| isStreaming?: boolean; | ||
| isToolUse?: boolean; | ||
| isInteractivePrompt?: boolean; | ||
| [key: string]: unknown; | ||
| }; | ||
|
|
||
| export function TtsProvider({ | ||
| chatMessages, | ||
| children, | ||
| }: { | ||
| chatMessages: ChatMessage[]; | ||
| children: ReactNode; | ||
| }) { | ||
| const tts = useSpeechOutput(chatMessages); | ||
| return <TtsContext.Provider value={tts}>{children}</TtsContext.Provider>; | ||
| } | ||
|
|
||
| export function useTts(): TtsContextValue | null { | ||
| return useContext(TtsContext); | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.