From 27fe526383809390c12f1645ce1d52d189c8ac08 Mon Sep 17 00:00:00 2001 From: yum Date: Thu, 31 Aug 2023 17:42:38 -0700 Subject: Clean up UI stoi pattern wxWidgets encodes text inputs & multiple-choice inputs as strings. I frequently have to convert these into ints & apply a range check. Encapsulate that in a function and use a shitty little ASSIGN_OR_RETURN macro to make the parsing as concise as possible. Also delete unused WhisperCPP config settings. --- GUI/GUI/GUI/Config.cpp | 50 +--------------- GUI/GUI/GUI/Config.h | 18 ------ GUI/GUI/GUI/Frame.cpp | 151 +++++++++++++++++++------------------------------ 3 files changed, 58 insertions(+), 161 deletions(-) (limited to 'GUI') diff --git a/GUI/GUI/GUI/Config.cpp b/GUI/GUI/GUI/Config.cpp index 3ba6be5..40ac92a 100644 --- a/GUI/GUI/GUI/Config.cpp +++ b/GUI/GUI/GUI/Config.cpp @@ -95,23 +95,7 @@ AppConfig::AppConfig(wxTextCtrl* out) params_path(), menu_path(), unity_generated_dir("TaSTT_Generated"), - clear_osc(true), - - whisper_model("ggml-medium.bin"), - whisper_mic(0), - whisper_decode_method("greedy"), - whisper_max_ctxt(100), - whisper_beam_width(5), - whisper_beam_n_best(5), - whisper_vad_min_duration(0.5), - whisper_vad_max_duration(5.0), - whisper_vad_drop_start_silence(0.5), - whisper_vad_pause_duration(0.2), - whisper_vad_retain_duration(0.2), - - whisper_enable_builtin(false), - whisper_enable_custom(false), - whisper_enable_browser_src(true) + clear_osc(true) {} bool AppConfig::Serialize(const std::filesystem::path& path) { @@ -152,22 +136,6 @@ bool AppConfig::Serialize(const std::filesystem::path& path) { cm.Set("unity_generated_dir", unity_generated_dir); cm.Set("clear_osc", clear_osc); - cm.Set("whisper_model", whisper_model); - cm.Set("whisper_mic", whisper_mic); - cm.Set("whisper_decode_method", whisper_decode_method); - cm.Set("whisper_max_ctxt", whisper_max_ctxt); - cm.Set("whisper_beam_width", whisper_beam_width); - cm.Set("whisper_beam_n_best", whisper_beam_n_best); - cm.Set("whisper_vad_min_duration", whisper_vad_min_duration); - cm.Set("whisper_vad_max_duration", whisper_vad_max_duration); - cm.Set("whisper_vad_drop_start_silence", whisper_vad_drop_start_silence); - cm.Set("whisper_vad_pause_duration", whisper_vad_pause_duration); - cm.Set("whisper_vad_retain_duration", whisper_vad_retain_duration); - - cm.Set("whisper_enable_builtin", whisper_enable_builtin); - cm.Set("whisper_enable_custom", whisper_enable_custom); - cm.Set("whisper_enable_browser_src", whisper_enable_browser_src); - return Config::Serialize(path, cm); } @@ -220,22 +188,6 @@ bool AppConfig::Deserialize(const std::filesystem::path& path) { cm.Get("unity_generated_dir", c.unity_generated_dir); cm.Get("clear_osc", c.clear_osc); - cm.Get("whisper_model", c.whisper_model); - cm.Get("whisper_mic", c.whisper_mic); - cm.Get("whisper_decode_method", c.whisper_decode_method); - cm.Get("whisper_max_ctxt", c.whisper_max_ctxt); - cm.Get("whisper_beam_width", c.whisper_beam_width); - cm.Get("whisper_beam_n_best", c.whisper_beam_n_best); - cm.Get("whisper_vad_min_duration", c.whisper_vad_min_duration); - cm.Get("whisper_vad_max_duration", c.whisper_vad_max_duration); - cm.Get("whisper_vad_drop_start_silence", c.whisper_vad_drop_start_silence); - cm.Get("whisper_vad_pause_duration", c.whisper_vad_pause_duration); - cm.Get("whisper_vad_retain_duration", c.whisper_vad_retain_duration); - - cm.Get("whisper_enable_builtin", c.whisper_enable_builtin); - cm.Get("whisper_enable_custom", c.whisper_enable_custom); - cm.Get("whisper_enable_browser_src", c.whisper_enable_browser_src); - *this = std::move(c); return true; } diff --git a/GUI/GUI/GUI/Config.h b/GUI/GUI/GUI/Config.h index bcf4267..88d9b69 100644 --- a/GUI/GUI/GUI/Config.h +++ b/GUI/GUI/GUI/Config.h @@ -84,23 +84,5 @@ public: std::string menu_path; std::string unity_generated_dir; bool clear_osc; - - // WhisperCPP-specific settings. - std::string whisper_model; - int whisper_mic; - std::string whisper_decode_method; - int whisper_max_ctxt; - int whisper_beam_width; - int whisper_beam_n_best; - float whisper_vad_min_duration; - float whisper_vad_max_duration; - float whisper_vad_drop_start_silence; - float whisper_vad_pause_duration; - float whisper_vad_retain_duration; - - // Browser source-specific settings. - bool whisper_enable_builtin; - bool whisper_enable_custom; - bool whisper_enable_browser_src; }; diff --git a/GUI/GUI/GUI/Frame.cpp b/GUI/GUI/GUI/Frame.cpp index 63962df..bc8e3ee 100644 --- a/GUI/GUI/GUI/Frame.cpp +++ b/GUI/GUI/GUI/Frame.cpp @@ -15,6 +15,24 @@ #include #include +// Does `lhs_type lhs = rhs`, where rhs returns `std::optional`. +// If the optional doesn't return a value, this returns. +// TODO(yum) do this without creating a named temporary. +// Example: +// ASSIGN_OR_RETURN(int, foo, 1) +#define ASSIGN_OR_RETURN_VOID(lhs_type, lhs, rhs) \ + std::optional lhs ## _tmp = rhs; \ + if (!lhs ## _tmp.has_value()) return; \ + lhs_type lhs = std::move(lhs ## _tmp).value() + +#define ASSIGN_OR_RETURN_BOOL(lhs_type, lhs, rhs) \ + std::optional lhs ## _tmp = rhs; \ + if (!lhs ## _tmp.has_value()) return false; \ + lhs_type lhs = std::move(lhs ## _tmp).value() + +using ::Logging::DrainAsyncOutput; +using ::Logging::Log; + namespace { enum FrameIds { ID_MAIN_PANEL, @@ -512,10 +530,30 @@ namespace { return default_index; } -} // namespace + std::optional stoiInRange(wxTextCtrl* out, const std::string& int_s, const std::string int_name, int min, int max) { + int res; + try { + res = std::stoi(int_s); + } + catch (const std::invalid_argument&) { + Log(out, "Could not parse {} \"{}\" as an integer: invalid\n", + int_name, int_s); + return {}; + } + catch (const std::out_of_range&) { + Log(out, "Could not parse {} \"{}\" as an integer: out of " + "range\n", int_name, int_s); + return {}; + } + if (res < min || res > max) { + Log(out, "Int argument {} is out of the allowed range [{},{}]\n", + int_name, min, max); + return {}; + } + return res; + } -using ::Logging::DrainAsyncOutput; -using ::Logging::Log; +} // namespace Frame::Frame() : wxFrame(nullptr, wxID_ANY, "TaSTT"), @@ -1728,35 +1766,15 @@ void Frame::OnGenerateFX(wxCommandEvent& event) if (chars_per_sync_idx == wxNOT_FOUND) { chars_per_sync_idx = kCharsDefault; } - std::string chars_per_sync_str = - kCharsPerSync[chars_per_sync_idx].ToStdString(); int bytes_per_char_idx = unity_bytes_per_char_->GetSelection(); if (bytes_per_char_idx == wxNOT_FOUND) { bytes_per_char_idx = kBytesDefault; } - std::string bytes_per_char_str = - kBytesPerChar[bytes_per_char_idx].ToStdString(); - std::string rows_str = unity_rows_->GetValue().ToStdString(); - std::string cols_str = unity_cols_->GetValue().ToStdString(); - int rows, cols, bytes_per_char, chars_per_sync; - try { - rows = std::stoi(rows_str); - cols = std::stoi(cols_str); - bytes_per_char = std::stoi(bytes_per_char_str); - chars_per_sync = std::stoi(chars_per_sync_str); - } - catch (const std::invalid_argument&) { - Log(unity_out_, "Could not parse rows \"{}\", cols \"{}\", bytes per " - "char \"{}\", or chars per sync \"{}\" as an integer\n", - rows_str, cols_str, bytes_per_char_str, chars_per_sync_str); - return false; - } - catch (const std::out_of_range&) { - Log(unity_out_, "Rows \"{}\" or cols \"{}\" are out of range\n", - rows_str, cols_str); - return false; - } + ASSIGN_OR_RETURN_BOOL(int, rows, stoiInRange(transcribe_out_, py_app_rows_->GetValue().ToStdString(), "rows", 1, 10)); + ASSIGN_OR_RETURN_BOOL(int, cols, stoiInRange(transcribe_out_, py_app_cols_->GetValue().ToStdString(), "cols", 1, 120)); + ASSIGN_OR_RETURN_BOOL(int, chars_per_sync, stoiInRange(transcribe_out_, kCharsPerSync[chars_per_sync_idx].ToStdString(), "chars_per_sync", 5, 24)); + ASSIGN_OR_RETURN_BOOL(int, bytes_per_char, stoiInRange(transcribe_out_, kBytesPerChar[bytes_per_char_idx].ToStdString(), "bytes_per_char", 1, 2)); app_c_->assets_path = unity_assets_path.string(); app_c_->fx_path = unity_animator_path.string(); @@ -2096,14 +2114,12 @@ void Frame::OnUnityParamChangeImpl() { if (chars_per_sync_idx == wxNOT_FOUND) { chars_per_sync_idx = kCharsDefault; } - std::string chars_per_sync_str = kCharsPerSync[chars_per_sync_idx].ToStdString(); - int chars_per_sync = std::stoi(chars_per_sync_str); + ASSIGN_OR_RETURN_VOID(int, chars_per_sync, stoiInRange(transcribe_out_, kCharsPerSync[chars_per_sync_idx].ToStdString(), "chars_per_sync", 5, 24)); int bytes_per_char_idx = unity_bytes_per_char_->GetSelection(); if (bytes_per_char_idx == wxNOT_FOUND) { bytes_per_char_idx = kBytesDefault; } - std::string bytes_per_char_str = kBytesPerChar[bytes_per_char_idx].ToStdString(); - int bytes_per_char = std::stoi(bytes_per_char_str); + ASSIGN_OR_RETURN_VOID(int, bytes_per_char, stoiInRange(transcribe_out_, kBytesPerChar[bytes_per_char_idx].ToStdString(), "bytes_per_char", 1, 2)); // Used to select which region is being updated. int select_bits = 8; @@ -2179,6 +2195,7 @@ void Frame::OnAppStart(wxCommandEvent& event) { if (button_idx == wxNOT_FOUND) { button_idx = kBytesDefault; } + const bool enable_local_beep = py_app_enable_local_beep_->GetValue(); const bool enable_browser_src = py_app_enable_browser_src_->GetValue(); const bool use_cpu = py_app_use_cpu_->GetValue(); @@ -2190,69 +2207,15 @@ void Frame::OnAppStart(wxCommandEvent& event) { const bool enable_profanity_filter = py_app_enable_profanity_filter_->GetValue(); const bool enable_debug_mode = py_app_enable_debug_mode_->GetValue(); const bool reset_on_toggle = py_app_reset_on_toggle_->GetValue(); - std::string rows_str = py_app_rows_->GetValue().ToStdString(); - std::string cols_str = py_app_cols_->GetValue().ToStdString(); - std::string chars_per_sync_str = - kCharsPerSync[chars_per_sync_idx].ToStdString(); - std::string bytes_per_char_str = - kBytesPerChar[bytes_per_char_idx].ToStdString(); - std::string gpu_idx_str = - py_app_gpu_idx_->GetValue().ToStdString(); - std::string keybind = - py_app_keybind_->GetValue().ToStdString(); - std::string browser_src_port_str = - py_app_browser_src_port_->GetValue().ToStdString(); - int rows, cols, chars_per_sync, bytes_per_char, gpu_idx, browser_src_port; - try { - rows = std::stoi(rows_str); - cols = std::stoi(cols_str); - chars_per_sync = std::stoi(chars_per_sync_str); - bytes_per_char = std::stoi(bytes_per_char_str); - gpu_idx = std::stoi(gpu_idx_str); - browser_src_port = std::stoi(browser_src_port_str); - } - catch (const std::invalid_argument&) { - Log(transcribe_out_, "Could not parse rows \"{}\", cols \"{}\", chars " - "per sync \"{}\", " - "bytes per char \"{}\", " - "gpu_idx \"{}\", " - "or browser src port \"{}\"" - "as an integer\n", rows_str, cols_str, chars_per_sync_str, - bytes_per_char_str, gpu_idx_str, browser_src_port_str); - return; - } - catch (const std::out_of_range&) { - Log(transcribe_out_, "Rows \"{}\", " - "cols \"{}\", " - "chars per sync \"{}\", " - "bytes per char \"{}\", " - "gpu idx \"{}\", " - "or browser src port \"{}\" " - "are out of range\n", rows_str, cols_str, chars_per_sync_str, - bytes_per_char_str, gpu_idx_str, browser_src_port_str); - return; - } - const int max_rows = 10; - const int max_cols = 240; - const int min_gpu_idx = 0; - const int max_gpu_idx = 10; - const int min_browser_src_port = 1024; - const int max_browser_src_port = 65535; - if (rows < 0 || rows > max_rows || - cols < 0 || cols > max_cols || - gpu_idx < min_gpu_idx || gpu_idx > max_gpu_idx || - browser_src_port < min_browser_src_port || browser_src_port > max_browser_src_port) { - Log(transcribe_out_, "Rows not on [{},{}] or cols not on [{},{}] or " - "gpu_idx not on [{}, {}] or " - "browser src port not on [{}, {}] or " - "commit_fuzz_threshold not on [{}, {}] " - "\n", - 0, max_rows, - 0, max_cols, - min_gpu_idx, max_gpu_idx, - min_browser_src_port, max_browser_src_port); - return; - } + + ASSIGN_OR_RETURN_VOID(int, rows, stoiInRange(transcribe_out_, py_app_rows_->GetValue().ToStdString(), "rows", 1, 10)); + ASSIGN_OR_RETURN_VOID(int, cols, stoiInRange(transcribe_out_, py_app_cols_->GetValue().ToStdString(), "cols", 1, 120)); + ASSIGN_OR_RETURN_VOID(int, chars_per_sync, stoiInRange(transcribe_out_, kCharsPerSync[chars_per_sync_idx].ToStdString(), "chars_per_sync", 5, 24)); + ASSIGN_OR_RETURN_VOID(int, bytes_per_char, stoiInRange(transcribe_out_, kBytesPerChar[bytes_per_char_idx].ToStdString(), "bytes_per_char", 1, 2)); + ASSIGN_OR_RETURN_VOID(int, gpu_idx, stoiInRange(transcribe_out_, py_app_gpu_idx_->GetValue().ToStdString(), "gpu_idx", 0, 10)); + ASSIGN_OR_RETURN_VOID(int, browser_src_port, stoiInRange(transcribe_out_, py_app_browser_src_port_->GetValue().ToStdString(), "browser_src_port", 1024, 65535)); + + std::string keybind = py_app_keybind_->GetValue().ToStdString(); app_c_->microphone = kMicChoices[which_mic].ToStdString(); app_c_->language = kLangChoices[which_lang].ToStdString(); -- cgit v1.2.3