From 9924a141b0b1266671915be12e21df6c8f4c5366 Mon Sep 17 00:00:00 2001 From: yum Date: Sat, 9 Sep 2023 23:18:32 -0700 Subject: Browser source now shows preview text as slightly transparent Improves viewer experience. --- GUI/GUI/GUI/BrowserSource.cpp | 12 ++++++++++++ GUI/GUI/GUI/Frame.cpp | 10 ++++++++++ GUI/GUI/GUI/Transcript.cpp | 11 +++++++++++ GUI/GUI/GUI/Transcript.h | 3 +++ 4 files changed, 36 insertions(+) (limited to 'GUI') diff --git a/GUI/GUI/GUI/BrowserSource.cpp b/GUI/GUI/GUI/BrowserSource.cpp index 45ca7f9..fce1bee 100644 --- a/GUI/GUI/GUI/BrowserSource.cpp +++ b/GUI/GUI/GUI/BrowserSource.cpp @@ -55,11 +55,23 @@ void BrowserSource::Run(volatile bool* run) transcript_oss << segment; } + std::ostringstream preview_oss; + std::vector preview = transcript_->GetPreview(); + // Hack: escape transcription to work inside JSON blob. + for (auto& segment : preview) { + size_t pos; + while ((pos = segment.find('"')) != std::string::npos) { + segment[pos] = '\''; + } + preview_oss << segment; + } + bool is_final = transcript_->IsFinalized(); std::ostringstream resp_oss; resp_oss << "{"; resp_oss << "\"transcript\":\"" << transcript_oss.str() << "\","; + resp_oss << "\"preview\":\"" << preview_oss.str() << "\","; resp_oss << "\"is_final\":" << std::to_string(is_final ? 1 : 0) << ""; resp_oss << "}"; payload = resp_oss.str(); diff --git a/GUI/GUI/GUI/Frame.cpp b/GUI/GUI/GUI/Frame.cpp index f2fb140..384f2a2 100644 --- a/GUI/GUI/GUI/Frame.cpp +++ b/GUI/GUI/GUI/Frame.cpp @@ -2383,6 +2383,16 @@ void Frame::OnAppStart(wxCommandEvent& event) { //Log(transcribe_out_, "Got transcription line! Transcript: \"{}\"", filtered_transcript); transcript_.Set(std::move(filtered_transcript)); } + + pattern = std::regex("^Preview: "); + 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_.SetPreview(std::move(filtered_transcript)); + } } }; auto in_cb = [&](std::string& in) { diff --git a/GUI/GUI/GUI/Transcript.cpp b/GUI/GUI/GUI/Transcript.cpp index e635343..eb798d9 100644 --- a/GUI/GUI/GUI/Transcript.cpp +++ b/GUI/GUI/GUI/Transcript.cpp @@ -11,6 +11,12 @@ void Transcript::Set(std::string&& segment) { segments_.push_back(std::move(segment)); } +void Transcript::SetPreview(std::string&& segment) { + std::scoped_lock l(mu_); + previews_.clear(); + previews_.push_back(std::move(segment)); +} + void Transcript::Clear() { std::scoped_lock l(mu_); segments_.clear(); @@ -21,6 +27,11 @@ std::vector Transcript::Get() { return segments_; } +std::vector Transcript::GetPreview() { + std::scoped_lock l(mu_); + return previews_; +} + void Transcript::SetFinalized(bool is_finalized) { // Accessing anything smaller than a word is always atomic. is_finalized_ = is_finalized; diff --git a/GUI/GUI/GUI/Transcript.h b/GUI/GUI/GUI/Transcript.h index 07cf6c0..1c18afe 100644 --- a/GUI/GUI/GUI/Transcript.h +++ b/GUI/GUI/GUI/Transcript.h @@ -11,6 +11,7 @@ public: void Append(std::string&& segment); void Set(std::string&& segment); + void SetPreview(std::string&& segment); void Clear(); // Indicate whether the transcript is "finalized", i.e. the transcription @@ -19,10 +20,12 @@ public: void SetFinalized(bool is_finalized); std::vector Get(); + std::vector GetPreview(); bool IsFinalized(); private: std::mutex mu_; std::vector segments_; + std::vector previews_; bool is_finalized_{ false }; }; -- cgit v1.2.3