Skip to content

Commit

Permalink
Voices should be language based
Browse files Browse the repository at this point in the history
- if no language is set, show all voices
- only show local voices
- only show languages of local voices
  • Loading branch information
mrdjohnson committed Jul 29, 2024
1 parent e1ba454 commit 9455361
Showing 1 changed file with 41 additions and 41 deletions.
82 changes: 41 additions & 41 deletions src/features/settings/panels/general/AppGeneralPanel.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { observer } from 'mobx-react-lite'
import { getSnapshot } from 'mobx-state-tree'
import { useEffect, useState } from 'react'
import { useEffect, useMemo, useState } from 'react'
import { useSpeech, useVoices } from 'react-text-to-speech'
import { Select, SelectItem, Switch } from '@nextui-org/react'
import { useForm } from 'react-hook-form'
Expand All @@ -19,6 +19,7 @@ import { settingStore } from '~/models/SettingStore'
import { connectionModelStore } from '~/features/connections/ConnectionModelStore'

import { ChatStoreSnapshotHandler } from '~/utils/transfer/ChatStoreSnapshotHandler'
import _ from 'lodash'

const DownlodSelector = () => {
const [includeImages, setIncludeImages] = useState(true)
Expand Down Expand Up @@ -83,7 +84,7 @@ type VoiceFormDataType = {
}

const SpeechSelector = observer(() => {
const { languages, voices } = useVoices()
const { voices } = useVoices()

const {
handleSubmit,
Expand All @@ -109,6 +110,20 @@ const SpeechSelector = observer(() => {
const selectedLanguage = watch('language')
const selectedVoiceUri = watch('voiceUri')

const localVoices = useMemo(() => {
return _.filter(voices, { localService: true })
}, [voices])

const filteredVoices = useMemo(() => {
if (_.isEmpty(selectedLanguage)) return localVoices

return localVoices.filter(({ lang }) => lang === selectedLanguage)
}, [selectedLanguage, localVoices])

const languages = useMemo(() => {
return _.chain(localVoices).map('lang').uniq().value()
}, [localVoices])

useEffect(() => {
reset({
language: voice?.language || '',
Expand Down Expand Up @@ -179,31 +194,18 @@ const SpeechSelector = observer(() => {
label="Language"
{...register('language')}
>
{languages
.map(language => (
<SelectItem
key={language}
value={language}
className="w-full !min-w-[13ch] text-base-content"
classNames={{
description: ' text',
}}
>
{language}
</SelectItem>
))
.concat(
<SelectItem
key={''}
value={''}
className="w-full !min-w-[13ch] text-base-content"
classNames={{
description: ' text',
}}
>
{''}
</SelectItem>,
)}
{[''].concat(languages).map(language => (
<SelectItem
key={language}
value={language}
className="w-full !min-w-[13ch] text-base-content"
classNames={{
description: ' text',
}}
>
{language}
</SelectItem>
))}
</Select>

<Select
Expand All @@ -218,20 +220,18 @@ const SpeechSelector = observer(() => {
label="Voice"
{...register('voiceUri')}
>
{voices
.filter(({ lang }) => lang === selectedLanguage)
.map(({ voiceURI, name }) => (
<SelectItem
key={voiceURI}
value={voiceURI}
className="w-full !min-w-[13ch] text-base-content"
classNames={{
description: ' text',
}}
>
{name}
</SelectItem>
))}
{filteredVoices.map(({ voiceURI, name }) => (
<SelectItem
key={voiceURI}
value={voiceURI}
className="w-full !min-w-[13ch] text-base-content"
classNames={{
description: ' text',
}}
>
{name}
</SelectItem>
))}
</Select>

<div className="flex flex-row">
Expand Down

0 comments on commit 9455361

Please sign in to comment.