From 4f3131b4a36d8e1557edb31d3754a431717dab7b Mon Sep 17 00:00:00 2001 From: yum Date: Fri, 30 Jun 2023 19:44:27 -0700 Subject: Add visual commit indicator to OBS browser source Circle goes red when speaking, grey when done. Ideally it would be in the top right portion of the browser source, but this is a good start. Also, hard-cap transcripts to 4096 chars. This prevents the STT from lagging during long sessions. --- GUI/GUI/GUI/BrowserSource.cpp | 5 ++++- GUI/GUI/GUI/Frame.cpp | 40 ++++++++++++++++++++++++++-------------- GUI/GUI/GUI/Transcript.cpp | 10 ++++++++++ GUI/GUI/GUI/Transcript.h | 7 +++++++ 4 files changed, 47 insertions(+), 15 deletions(-) (limited to 'GUI') diff --git a/GUI/GUI/GUI/BrowserSource.cpp b/GUI/GUI/GUI/BrowserSource.cpp index 62e3e43..45ca7f9 100644 --- a/GUI/GUI/GUI/BrowserSource.cpp +++ b/GUI/GUI/GUI/BrowserSource.cpp @@ -55,9 +55,12 @@ void BrowserSource::Run(volatile bool* run) transcript_oss << segment; } + bool is_final = transcript_->IsFinalized(); + std::ostringstream resp_oss; resp_oss << "{"; - resp_oss << "\"transcript\":\"" << transcript_oss.str() << "\""; + resp_oss << "\"transcript\":\"" << transcript_oss.str() << "\","; + resp_oss << "\"is_final\":" << std::to_string(is_final ? 1 : 0) << ""; resp_oss << "}"; payload = resp_oss.str(); type = WebServer::JSON; diff --git a/GUI/GUI/GUI/Frame.cpp b/GUI/GUI/GUI/Frame.cpp index 236d375..e6506bd 100644 --- a/GUI/GUI/GUI/Frame.cpp +++ b/GUI/GUI/GUI/Frame.cpp @@ -8,6 +8,7 @@ #include #include #include +#include #include #include #include @@ -2164,20 +2165,31 @@ void Frame::OnAppStart(wxCommandEvent& event) { app_c_->keybind = keybind; app_c_->Serialize(AppConfig::kConfigPath); - auto out_cb = [&](const std::string& out, const std::string& err) { - Log(transcribe_out_, "{}", out); - Log(transcribe_out_, "{}", err); - - std::regex pattern("^Transcription \\(([0-9]*\\.[0-9]+) seconds\\):"); - if (std::regex_search(out, pattern)) { - std::string filtered_transcript = std::regex_replace(out, pattern, ""); - filtered_transcript.erase(std::remove_if(filtered_transcript.begin(), filtered_transcript.end(), [](char c) { - return c == '\n' || c == '\r'; - }), filtered_transcript.end()); - //Log(transcribe_out_, "Got transcription line! Transcript: \"{}\"", filtered_transcript); - transcript_.Set(std::move(filtered_transcript)); - } - }; + auto out_cb = [&](const std::string& out, const std::string& err) { + Log(transcribe_out_, "{}", out); + Log(transcribe_out_, "{}", err); + + std::istringstream out_iss(out); + std::string out_line; + while (std::getline(out_iss, out_line)) { + if (out_line.starts_with("Finalized: 1")) { + transcript_.SetFinalized(true); + } + else if (out_line.starts_with("Finalized: 0")) { + transcript_.SetFinalized(false); + } + + std::regex pattern("^Transcription \\(([0-9]*\\.[0-9]+) seconds\\):"); + if (std::regex_search(out_line, pattern)) { + std::string filtered_transcript = std::regex_replace(out_line, pattern, ""); + filtered_transcript.erase(std::remove_if(filtered_transcript.begin(), filtered_transcript.end(), [](char c) { + return c == '\n' || c == '\r'; + }), filtered_transcript.end()); + //Log(transcribe_out_, "Got transcription line! Transcript: \"{}\"", filtered_transcript); + transcript_.Set(std::move(filtered_transcript)); + } + } + }; auto in_cb = [&](std::string& in) {}; auto run_cb = [&]() { return run_py_app_; diff --git a/GUI/GUI/GUI/Transcript.cpp b/GUI/GUI/GUI/Transcript.cpp index 9ef607f..e635343 100644 --- a/GUI/GUI/GUI/Transcript.cpp +++ b/GUI/GUI/GUI/Transcript.cpp @@ -20,3 +20,13 @@ std::vector Transcript::Get() { std::scoped_lock l(mu_); return segments_; } + +void Transcript::SetFinalized(bool is_finalized) { + // Accessing anything smaller than a word is always atomic. + is_finalized_ = is_finalized; +} + +bool Transcript::IsFinalized() { + // Accessing anything smaller than a word is always atomic. + return is_finalized_; +} diff --git a/GUI/GUI/GUI/Transcript.h b/GUI/GUI/GUI/Transcript.h index fae2bad..07cf6c0 100644 --- a/GUI/GUI/GUI/Transcript.h +++ b/GUI/GUI/GUI/Transcript.h @@ -13,9 +13,16 @@ public: void Set(std::string&& segment); void Clear(); + // Indicate whether the transcript is "finalized", i.e. the transcription + // engine has committed the entirety of the transcript and will no longer + // change it. + void SetFinalized(bool is_finalized); + std::vector Get(); + bool IsFinalized(); private: std::mutex mu_; std::vector segments_; + bool is_finalized_{ false }; }; -- cgit v1.2.3