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

feat: add voice audio preview button in tts-config option 为 TTS 设置选项添加了语音预览按钮 #5651

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
107 changes: 91 additions & 16 deletions app/components/tts-config.tsx
Original file line number Diff line number Diff line change
@@ -1,19 +1,76 @@
import { TTSConfig, TTSConfigValidator } from "../store";
import React, { useState } from "react";

import Locale from "../locales";
import { ListItem, Select } from "./ui-lib";
import {
ModelProvider,
DEFAULT_TTS_ENGINE,
DEFAULT_TTS_ENGINES,
DEFAULT_TTS_MODELS,
DEFAULT_TTS_VOICES,
} from "../constant";
import { InputRange } from "./input-range";
import { IconButton } from "./button";
import SpeakIcon from "../icons/speak.svg";
import SpeakStopIcon from "../icons/speak-stop.svg";
import { createTTSPlayer } from "../utils/audio";
import { useAppConfig } from "../store";
import { ClientApi } from "../client/api";
import { showToast } from "../components/ui-lib";

const ttsPlayer = createTTSPlayer();
export function TTSConfigList(props: {
ttsConfig: TTSConfig;
updateConfig: (updater: (config: TTSConfig) => void) => void;
}) {
const [speechLoading, setSpeechLoading] = useState(false);
const [speechStatus, setSpeechStatus] = useState(false);

const config = useAppConfig.getState();

function stopSpeech() {
ttsPlayer.stop();
setSpeechStatus(false);
}

async function playSpeech(text: string, ttsConfig: TTSConfig) {
try {
const api = new ClientApi(ModelProvider.GPT);
setSpeechLoading(true);
ttsPlayer.init();

const audioBuffer = await api.llm.speech({
model: ttsConfig.model,
input: text,
voice: ttsConfig.voice,
speed: ttsConfig.speed,
});

setSpeechStatus(true);
await ttsPlayer.play(audioBuffer, () => {
setSpeechStatus(false);
});
} catch (error) {
console.error("[OpenAI Speech]", error);
setSpeechStatus(false);
// Implement user-facing error notification here
if (typeof (error as Error).message === "string") {
showToast((error as Error).message);
}
Comment on lines +58 to +60
Copy link
Contributor

Choose a reason for hiding this comment

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

🛠️ Refactor suggestion

Improve error type checking.

The error type assertion could be improved using type guards for better type safety.

-if (typeof (error as Error).message === "string") {
+if (error instanceof Error) {
   showToast(error.message);
+} else {
+  showToast("An unexpected error occurred");
 }
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
if (typeof (error as Error).message === "string") {
showToast((error as Error).message);
}
if (error instanceof Error) {
showToast(error.message);
} else {
showToast("An unexpected error occurred");
}

} finally {
setSpeechLoading(false);
}
}

async function openaiSpeech(text: string) {
if (speechStatus) {
stopSpeech();
} else {
await playSpeech(text, config.ttsConfig);
}
}

return (
<>
<ListItem
Expand Down Expand Up @@ -88,23 +145,41 @@ export function TTSConfigList(props: {
title={Locale.Settings.TTS.Voice.Title}
subTitle={Locale.Settings.TTS.Voice.SubTitle}
>
<Select
value={props.ttsConfig.voice}
onChange={(e) => {
props.updateConfig(
(config) =>
(config.voice = TTSConfigValidator.voice(
<div style={{ display: "flex", gap: "10px" }}>
<IconButton
aria={Locale.Chat.Actions.Speech}
icon={speechStatus ? <SpeakStopIcon /> : <SpeakIcon />}
text={
speechLoading
? "Loading..."
: speechStatus
? Locale.Chat.Actions.Stop
: Locale.Chat.Actions.Speech
}
onClick={() => {
openaiSpeech(
"NextChat,Unleash your imagination, experience the future of AI conversation.",
);
}}
/>

<Select
value={props.ttsConfig.voice}
onChange={(e) => {
props.updateConfig((config) => {
config.voice = TTSConfigValidator.voice(
e.currentTarget.value,
)),
);
}}
>
{DEFAULT_TTS_VOICES.map((v, i) => (
<option value={v} key={i}>
{v}
</option>
))}
</Select>
);
});
}}
>
{DEFAULT_TTS_VOICES.map((v, i) => (
<option value={v} key={i}>
{v}
</option>
))}
</Select>
</div>
Comment on lines +148 to +182
Copy link
Contributor

Choose a reason for hiding this comment

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

🛠️ Refactor suggestion

Several UI improvements recommended.

  1. The preview text should be localized
  2. Consider moving inline styles to CSS classes
  3. The loading state could benefit from a loading spinner component
-<div style={{ display: "flex", gap: "10px" }}>
+<div className="tts-voice-controls">

-text={
-  speechLoading
-    ? "Loading..."
-    : speechStatus
-    ? Locale.Chat.Actions.Stop
-    : Locale.Chat.Actions.Speech
-}
+text={
+  speechLoading
+    ? Locale.Chat.Actions.Loading
+    : speechStatus
+    ? Locale.Chat.Actions.Stop
+    : Locale.Chat.Actions.Speech
+}

-"NextChat,Unleash your imagination, experience the future of AI conversation."
+Locale.Settings.TTS.PreviewText

Add this CSS to your stylesheet:

.tts-voice-controls {
  display: flex;
  gap: 10px;
  align-items: center;
}

</ListItem>
<ListItem
title={Locale.Settings.TTS.Speed.Title}
Expand Down