Skip to content

Commit 1a4c2bd

Browse files
committed
增加静默语音转换
1 parent e0a406f commit 1a4c2bd

4 files changed

Lines changed: 151 additions & 89 deletions

File tree

include/ChatBot.h

Lines changed: 38 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,8 @@ class Claude : public ChatBot {
156156
// 发送失败,打印错误信息
157157
LogError(r.error.message);
158158
}*/
159-
LogInfo("Claude : 不支持的操作");
159+
Submit("请忘记上面的会话内容");
160+
LogInfo("Claude : 重置成功");
160161
};
161162

162163
virtual void Load(std::string name = "default") override {
@@ -177,13 +178,13 @@ class Claude : public ChatBot {
177178
}
178179

179180
virtual Billing GetBilling() override {
180-
return {999, 999, 0, 999};
181+
return {999, 999, 0, Utils::getCurrentTimestamp()};
181182
};
182183

183184
virtual map<long long, string> GetHistory() override {
184185
try {
185186
History.clear();
186-
auto _ts=to_string(Logger::getCurrentTimestamp());
187+
auto _ts = to_string(Logger::getCurrentTimestamp());
187188
cpr::Payload payload = {
188189
{"channel", claudeData.channelID},
189190
{"latest", _ts},
@@ -215,5 +216,39 @@ class Claude : public ChatBot {
215216
ClaudeData claudeData;
216217
};
217218

219+
/*
220+
class Claude2 {
221+
public:
222+
Claude2(const std::string &sessionKey) : sessionKey_(sessionKey) {}
223+
224+
std::string startConversation(const std::string &message) {
225+
// 1. 开始新对话
226+
cpr::Response r = cpr::Post(cpr::Url{"https://api.claude.ai/conversation"},
227+
cpr::Body{{"message", message}},
228+
cpr::Header{{"Content-Type", "application/json"},
229+
{"Cookie", "sessionKey=" + sessionKey_}});
230+
231+
// 2. 获取对话ID,使用 nlohmann::json 解析
232+
json response = json::parse(r.text);
233+
std::string conversationId = response["conversationId"];
234+
235+
236+
// 3. 发送消息
237+
r = cpr::Post(cpr::Url{"https://api.claude.ai/conversation/" + conversationId},
238+
cpr::Body{{"message", message}},
239+
cpr::Header{{"Content-Type", "application/json"},
240+
{"Cookie", "sessionKey=" + sessionKey_}});
241+
242+
// 4. 获取回复
243+
std::string reply = r.text;
244+
245+
return reply;
246+
}
247+
248+
private:
249+
std::string sessionKey_;
250+
};
251+
*/
252+
218253

219254
#endif

sample/Application.cpp

Lines changed: 43 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ Application::Application(const Configure &configure, bool setting) {
1010
translator = CreateRef<Translate>(configure.baiDuTranslator);
1111
if (configure.claude.enable) {
1212
bot = CreateRef<Claude>(configure.claude);
13-
convid = "ClaudeOnly";
13+
convid = "Claude";
1414
ClaudeHitsory();
1515
} else {
1616
bot = CreateRef<ChatGPT>(configure.openAi);
@@ -29,8 +29,12 @@ Application::Application(const Configure &configure, bool setting) {
2929
Utils::SaveFile(live2D.model, "Lconfig.txt");
3030
Utils::OpenProgram(live2D.bin.c_str());
3131
}
32-
if (whisperData.enable && whisper && mwhisper)
32+
if (whisperData.enable && whisper && mwhisper) {
3333
listener->listen();
34+
}
35+
if (vits) {
36+
VitsListener();
37+
}
3438
}
3539

3640
GLuint Application::load_texture(const char *path) {
@@ -57,20 +61,33 @@ void Application::Vits(std::string text) {
5761
task.config = Utils::GetAbsolutePath(vitsData.config);
5862
task.text = text;
5963
task.sid = vitsData.speaker_id;
60-
static bool is_playing = false;
6164
Utils::SaveYaml("task.yaml", Utils::toYaml(task));
65+
}
66+
67+
void Application::VitsListener() {
68+
static bool is_playing = false;
6269

6370
std::thread([&]() {
64-
std::string result = Utils::exec(Utils::GetAbsolutePath(bin + VitsConvertor + "VitsConvertor" + exeSuffix));
71+
std::thread([&]() {
72+
Utils::exec(Utils::GetAbsolutePath(bin + VitsConvertor + "VitsConvertor" + exeSuffix));
73+
}).detach();
74+
VitsTask task;
75+
while (AppRunning) {
76+
//std::string result = Utils::exec(Utils::GetAbsolutePath(bin + VitsConvertor + "VitsConvertor" + exeSuffix));
77+
78+
//if (result.find("Synthesized and saved!")) {
79+
if (UFile::Exists(task.outpath)) {
80+
// 等待音频完成后再播放下一个
81+
while (is_playing) {
82+
std::this_thread::sleep_for(std::chrono::milliseconds(100));
83+
}
84+
is_playing = true;
85+
listener->playRecorded(whisperData.enable);
86+
is_playing = false;
6587

66-
if (result.find("Successfully saved!")) {
67-
// 等待音频完成后再播放下一个
68-
while (is_playing) {
69-
std::this_thread::sleep_for(std::chrono::milliseconds(100));
88+
std::remove(task.outpath.c_str());
7089
}
71-
is_playing = true;
72-
listener->playRecorded(whisperData.enable);
73-
is_playing = false;
90+
//}
7491
}
7592
}).detach();
7693
}
@@ -93,7 +110,8 @@ void Application::render_code_box() {
93110
style.Colors[ImGuiCol_ScrollbarGrabHovered] = ImVec4(0.8f, 0.8f, 0.8f, 1.0f);
94111
style.Colors[ImGuiCol_ScrollbarGrabActive] = ImVec4(0.9f, 0.9f, 0.9f, 1.0f);
95112

96-
ImGui::Begin("Code Box", nullptr, ImGuiWindowFlags_MenuBar | ImGuiWindowFlags_NoNav | ImGuiWindowFlags_NoTitleBar);
113+
ImGui::Begin("Code Box", nullptr,
114+
ImGuiWindowFlags_MenuBar | ImGuiWindowFlags_NoNav | ImGuiWindowFlags_NoTitleBar);
97115
ImGui::PushStyleVar(ImGuiStyleVar_FrameRounding, 5.0f);
98116

99117
static bool show_codes = false;
@@ -319,6 +337,9 @@ void Application::render_chat_box() {
319337
if (ImGui::Button(reinterpret_cast<const char *>(u8"重置会话"), ImVec2(200, 30))) {
320338
chat_history.clear();
321339
bot->Reset();
340+
if(configure.claude.enable){
341+
FirstTime = Utils::getCurrentTimestamp();
342+
}
322343
save(convid);
323344
}
324345
if (ImGui::Button(reinterpret_cast<const char *>(u8"删除当前会话"), ImVec2(200, 30)) &&
@@ -554,7 +575,8 @@ void Application::render_input_box() {
554575
ImGui::SetClipboardText(i.c_str());
555576
}
556577
if (vits && vitsData.enable) {
557-
std::string VitsText = translator->translate(Utils::ExtractNormalText(response), vitsData.lanType);
578+
std::string VitsText = translator->translate(Utils::ExtractNormalText(response),
579+
vitsData.lanType);
558580
Vits(VitsText);
559581
} else if (whisperData.enable) {
560582
listener->listen();
@@ -633,7 +655,8 @@ void Application::render_setting_box() {
633655
TEXT_BUFFER);
634656
ImGui::Checkbox(reinterpret_cast<const char *>(u8"使用远程代理"), &configure.openAi.useWebProxy);
635657
if (!configure.openAi.useWebProxy)
636-
ImGui::InputText(reinterpret_cast<const char *>(u8"对OpenAI使用的代理"), configure.openAi.proxy.data(),
658+
ImGui::InputText(reinterpret_cast<const char *>(u8"对OpenAI使用的代理"),
659+
configure.openAi.proxy.data(),
637660
TEXT_BUFFER);
638661
else {
639662
if (ImGui::BeginCombo(reinterpret_cast<const char *>(u8"远程代理"),
@@ -747,13 +770,15 @@ void Application::render_setting_box() {
747770
if (ImGui::CollapsingHeader("VITS")) {
748771
static char search_text[256] = "";
749772
ImGui::Checkbox(reinterpret_cast<const char *>(u8"启用Vits"), &configure.vits.enable);
750-
ImGui::InputText(reinterpret_cast<const char *>(u8"Vits 模型位置"), configure.vits.model.data(), TEXT_BUFFER);
773+
ImGui::InputText(reinterpret_cast<const char *>(u8"Vits 模型位置"), configure.vits.model.data(),
774+
TEXT_BUFFER);
751775
ImGui::InputText(reinterpret_cast<const char *>(u8"Vits 配置文件位置"), configure.vits.config.data(),
752776
TEXT_BUFFER);
753777
ImGui::InputText(reinterpret_cast<const char *>(u8"Vits的语言类型"), configure.vits.lanType.data(),
754778
TEXT_BUFFER);
755779
// 开始下拉列表
756-
if (ImGui::BeginCombo(reinterpret_cast<const char *>(u8"角色"), speakers[configure.vits.speaker_id].c_str())) {
780+
if (ImGui::BeginCombo(reinterpret_cast<const char *>(u8"角色"),
781+
speakers[configure.vits.speaker_id].c_str())) {
757782
// 在下拉框中添加一个文本输入框
758783
ImGui::InputTextWithHint("##Search", reinterpret_cast<const char *>(u8"搜索"), search_text,
759784
sizeof(search_text));
@@ -1363,7 +1388,8 @@ void Application::RuntimeDetector() {
13631388
reinterpret_cast<const char *>(u8"安装VitsConvertor成功"));
13641389
} else if (!mwhisper) {
13651390
ShowConfirmationDialog(reinterpret_cast<const char *>(u8"下载通知"),
1366-
reinterpret_cast<const char *>(u8"检测到你并没安装Whisper Model,是否下载?"), mwhisper,
1391+
reinterpret_cast<const char *>(u8"检测到你并没安装Whisper Model,是否下载?"),
1392+
mwhisper,
13671393
[this]() { WhisperModelDownload(Utils::GetFileName(whisperData.model)); },
13681394
reinterpret_cast<const char *>(u8"安装Whisper Model失败"),
13691395
reinterpret_cast<const char *>(u8"安装Whisper Model成功"));

sample/Application.h

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -37,13 +37,13 @@ enum State {
3737
class Application {
3838
private:
3939
#ifdef _WIN32
40-
const std::string VitsConvertUrl = "https://github.com/NGLSG/MoeGoe/releases/download/1.0/VitsConvertor-win64.tar.gz";
40+
const std::string VitsConvertUrl = "https://github.com/NGLSG/MoeGoe/releases/download/1.1/VitsConvertor-win64.tar.gz";
4141
const std::string whisperUrl = "https://github.com/ggerganov/whisper.cpp/releases/download/v1.3.0/whisper-bin-Win32.zip";
4242
const std::string vitsFile = "VitsConvertor.tar.gz";
4343
const std::string exeSuffix = ".exe";
4444
#else
4545
const std::string whisperUrl = "https://github.com/ggerganov/whisperData.cpp/releases/download/v1.3.0/whisper-bin-x64.zip";
46-
const std::string VitsConvertUrl = "https://github.com/NGLSG/MoeGoe/releases/download/1.0/VitsConvertor-linux64.tar.xz";
46+
const std::string VitsConvertUrl = "https://github.com/NGLSG/MoeGoe/releases/download/1.1/VitsConvertor-linux64.tar.xz";
4747
const std::string vitsFile = "VitsConvertor.tar.gz";
4848
const std::string exeSuffix = "";
4949
#endif
@@ -118,8 +118,7 @@ class Application {
118118
bool whisper = false;
119119
bool mwhisper = false;
120120
bool AppRunning = true;
121-
122-
long long FirstTime;
121+
long long FirstTime = 0;
123122

124123
void save(std::string name = "default", bool out = true);
125124

@@ -141,6 +140,8 @@ class Application {
141140

142141
}
143142

143+
void VitsListener();
144+
144145
// 渲染聊天框
145146
void render_chat_box();
146147

@@ -180,6 +181,7 @@ class Application {
180181

181182
void ClaudeHitsory() {
182183
thread([&]() {
184+
FirstTime = Utils::getCurrentTimestamp();
183185
while (AppRunning) {
184186
Chat botR;
185187
botR.flag = 1;
@@ -210,8 +212,8 @@ class Application {
210212
}),
211213
chat_history.end());
212214
save(convid, false);
213-
// 延迟1s
214-
std::this_thread::sleep_for(std::chrono::seconds(1));
215+
// 延迟0.002s
216+
std::this_thread::sleep_for(std::chrono::milliseconds(2));
215217
}
216218
}).detach();
217219
}

0 commit comments

Comments
 (0)