Skip to content

Commit

Permalink
fix: delay the setVoice until before starting play tts, close #2042
Browse files Browse the repository at this point in the history
  • Loading branch information
hyoban committed Jan 23, 2025
1 parent a03dcd9 commit e64da25
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 30 deletions.
43 changes: 21 additions & 22 deletions apps/main/src/tipc/reader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,32 @@ export const readerRoute = {
.input<{
id: string
text: string
voice: string
}>()
.action(async ({ input }) => {
const { id, text } = input

.action(async ({ input, context: { sender } }) => {
const { id, text, voice } = input
if (!text) {
return null
}

const window = BrowserWindow.fromWebContents(sender)
if (!window) return

// It's ok to set voice every time, because it will be cached by msedge-tts
await tts
.setMetadata(voice, OUTPUT_FORMAT.WEBM_24KHZ_16BIT_MONO_OPUS)
.catch((error: unknown) => {
console.error("Failed to set voice", error)
if (error instanceof Error) {
return callWindowExpose(window).toast.error(error.message, {
duration: 1000,
})
}
return callWindowExpose(window).toast.error("Failed to set voice", {
duration: 1000,
})
})

const filePath = path.join(app.getPath("userData"), `${id}.webm`)
if (fs.existsSync(filePath)) {
return filePath
Expand All @@ -62,25 +80,6 @@ export const readerRoute = {
}
}),

setVoice: t.procedure.input<string>().action(async ({ input, context: { sender } }) => {
const window = BrowserWindow.fromWebContents(sender)
if (!window) return

await tts
.setMetadata(input, OUTPUT_FORMAT.WEBM_24KHZ_16BIT_MONO_OPUS)
.catch((error: unknown) => {
console.error("Failed to set voice", error)
if (error instanceof Error) {
return callWindowExpose(window).toast.error(error.message, {
duration: 1000,
})
}
return callWindowExpose(window).toast.error("Failed to set voice", {
duration: 1000,
})
})
}),

detectCodeStringLanguage: t.procedure
.input<{ codeString: string }>()
.action(async ({ input }) => {
Expand Down
2 changes: 1 addition & 1 deletion apps/mobile/src/hooks/useIsRouteOnlyOne.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ export const useIsRouteOnlyOne = () => {
const navigation = useNavigation()
const state = navigation.getState()

const routeOnlyOne = state.routes.length === 1
const routeOnlyOne = state?.routes.length === 1

return routeOnlyOne
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import {
ReadabilityStatus,
useEntryInReadabilityStatus,
} from "~/atoms/readability"
import { useGeneralSettingKey } from "~/atoms/settings/general"
import { shortcuts } from "~/constants/shortcuts"
import { useEntryReadabilityToggle } from "~/hooks/biz/useEntryActions"
import { tipcClient } from "~/lib/client"
Expand Down Expand Up @@ -50,6 +51,7 @@ export const ElectronAdditionActions = IN_ELECTRON
})

const [ttsLoading, setTtsLoading] = useState(false)
const voice = useGeneralSettingKey("voice")

if (!populatedEntry) return null

Expand All @@ -70,7 +72,8 @@ export const ElectronAdditionActions = IN_ELECTRON
} else {
const filePath = await tipcClient?.tts({
id: populatedEntry.entries.id,
text: (await parseHtml(populatedEntry.entries.content)).toText(),
text: parseHtml(populatedEntry.entries.content).toText(),
voice,
})
if (filePath) {
AudioPlayer.mount({
Expand Down
7 changes: 1 addition & 6 deletions apps/renderer/src/providers/setting-sync.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -75,12 +75,7 @@ const useLanguageSync = () => {
}, [language])
}
const useGeneralSettingSync = () => {
const voice = useGeneralSettingKey("voice")
useEffect(() => {
if (voice) {
tipcClient?.setVoice(voice)
}
}, [voice])
useGeneralSettingKey("voice")
}

export const SettingSync = () => {
Expand Down

0 comments on commit e64da25

Please sign in to comment.