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 2 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
95 changes: 79 additions & 16 deletions app/components/tts-config.tsx
Original file line number Diff line number Diff line change
@@ -1,19 +1,59 @@
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";

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);

async function openaiSpeech(text: string) {
if (speechStatus) {
ttsPlayer.stop();
setSpeechStatus(false);
} else {
const api = new ClientApi(ModelProvider.GPT);
const config = useAppConfig.getState();
setSpeechLoading(true);
ttsPlayer.init();
let audioBuffer: ArrayBuffer;
audioBuffer = await api.llm.speech({
model: config.ttsConfig.model,
input: text,
voice: config.ttsConfig.voice,
speed: config.ttsConfig.speed,
});
setSpeechStatus(true);
ttsPlayer
.play(audioBuffer, () => {
setSpeechStatus(false);
})
.catch((e) => {
console.error("[OpenAI Speech]", e);
setSpeechStatus(false);
})
.finally(() => setSpeechLoading(false));
}
}
Dakai marked this conversation as resolved.
Show resolved Hide resolved
return (
<>
<ListItem
Expand Down Expand Up @@ -88,23 +128,46 @@ 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={() => {
if (speechStatus) {
ttsPlayer.stop();
setSpeechStatus(false);
Dakai marked this conversation as resolved.
Show resolved Hide resolved
} else {
openaiSpeech(
"NextChat,Unleash your imagination, experience the future of AI conversation.",
);
}
}}
/>
Dogtiti marked this conversation as resolved.
Show resolved Hide resolved

<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>
</ListItem>
<ListItem
title={Locale.Settings.TTS.Speed.Title}
Expand Down
Loading