From 0f949bc02b0264b9fb0154421075c4af768b64fa Mon Sep 17 00:00:00 2001 From: Robin Gareus Date: Sat, 17 Dec 2022 13:12:06 +0100 Subject: [PATCH] Add region polarity invert to Region menu --- gtk2_ardour/ardour.menus.in | 2 ++ gtk2_ardour/editor.h | 1 + gtk2_ardour/editor_actions.cc | 2 ++ gtk2_ardour/editor_ops.cc | 35 +++++++++++++++++++++++++++++++++ gtk2_ardour/editor_selection.cc | 10 ++++++++++ 5 files changed, 50 insertions(+) diff --git a/gtk2_ardour/ardour.menus.in b/gtk2_ardour/ardour.menus.in index 97484d0983..04a2202d76 100644 --- a/gtk2_ardour/ardour.menus.in +++ b/gtk2_ardour/ardour.menus.in @@ -385,6 +385,7 @@ + @@ -931,6 +932,7 @@ + diff --git a/gtk2_ardour/editor.h b/gtk2_ardour/editor.h index cb1eaa578d..84502834bf 100644 --- a/gtk2_ardour/editor.h +++ b/gtk2_ardour/editor.h @@ -2314,6 +2314,7 @@ private: void set_gain_envelope_visibility (); void set_region_gain_visibility (RegionView*); void toggle_gain_envelope_active (); + void toggle_region_polarity (); void reset_region_gain_envelopes (); void session_state_saved (std::string); diff --git a/gtk2_ardour/editor_actions.cc b/gtk2_ardour/editor_actions.cc index 5bfc7ca920..b8a1cd020f 100644 --- a/gtk2_ardour/editor_actions.cc +++ b/gtk2_ardour/editor_actions.cc @@ -1911,6 +1911,8 @@ Editor::register_region_actions () register_toggle_region_action (_region_actions, RegionActionTarget (SelectedRegions|EnteredRegions), "toggle-region-gain-envelope-active", _("Envelope Active"), sigc::mem_fun (*this, &Editor::toggle_gain_envelope_active)); + register_toggle_region_action (_region_actions, RegionActionTarget (SelectedRegions|EnteredRegions), "toggle-region-polarity", _("Invert Polarity"), sigc::mem_fun (*this, &Editor::toggle_region_polarity)); + register_region_action (_region_actions, RegionActionTarget (SelectedRegions|EnteredRegions), "quantize-region", _("Quantize..."), sigc::mem_fun (*this, &Editor::quantize_region)); register_region_action (_region_actions, RegionActionTarget (SelectedRegions|EnteredRegions), "legatize-region", _("Legatize"), sigc::bind(sigc::mem_fun (*this, &Editor::legatize_region), false)); register_region_action (_region_actions, RegionActionTarget (SelectedRegions|EnteredRegions), "deinterlace-midi", _("Deinterlace Into Layers"), sigc::mem_fun (*this, &Editor::deinterlace_selected_midi_regions)); diff --git a/gtk2_ardour/editor_ops.cc b/gtk2_ardour/editor_ops.cc index 6f920ac6a1..68392be790 100644 --- a/gtk2_ardour/editor_ops.cc +++ b/gtk2_ardour/editor_ops.cc @@ -6409,6 +6409,41 @@ Editor::set_gain_envelope_visibility () } } +void +Editor::toggle_region_polarity () +{ + if (_ignore_region_action) { + return; + } + + RegionSelection rs = get_regions_from_selection_and_entered (); + + if (!_session || rs.empty()) { + return; + } + + bool in_command = false; + + for (RegionSelection::iterator i = rs.begin(); i != rs.end(); ++i) { + AudioRegionView* const arv = dynamic_cast(*i); + if (arv) { + arv->region()->clear_changes (); + gain_t scale_amplitude = arv->audio_region()->scale_amplitude (); + arv->audio_region()->set_scale_amplitude (-1 * scale_amplitude); + + if (!in_command) { + begin_reversible_command (_("region polarity invery")); + in_command = true; + } + _session->add_command (new StatefulDiffCommand (arv->region())); + } + } + + if (in_command) { + commit_reversible_command (); + } +} + void Editor::toggle_gain_envelope_active () { diff --git a/gtk2_ardour/editor_selection.cc b/gtk2_ardour/editor_selection.cc index 0f57542e97..a80d762554 100644 --- a/gtk2_ardour/editor_selection.cc +++ b/gtk2_ardour/editor_selection.cc @@ -1428,6 +1428,8 @@ Editor::sensitize_the_right_region_actions (bool because_canvas_crossing) bool have_active_fade_in = false; bool have_active_fade_out = false; bool have_transients = false; + bool have_inverted_polarity = false; + bool have_non_inverted_polarity = false; for (list::const_iterator i = rs.begin(); i != rs.end(); ++i) { @@ -1509,6 +1511,9 @@ Editor::sensitize_the_right_region_actions (bool because_canvas_crossing) } else { have_inactive_fade_out = true; } + + have_inverted_polarity = ar->scale_amplitude () < 0; + have_non_inverted_polarity = ar->scale_amplitude () >= 0; } } @@ -1565,6 +1570,10 @@ Editor::sensitize_the_right_region_actions (bool because_canvas_crossing) // Glib::RefPtr::cast_dynamic (_region_actions->get_action("toggle-region-gain-envelope-active"))->set_inconsistent (); } + if (have_inverted_polarity && !have_non_inverted_polarity) { + Glib::RefPtr::cast_dynamic (_region_actions->get_action("toggle-region-polarity"))->set_active (); + } + } else { _region_actions->get_action("loudness-analyze-region")->set_sensitive (false); @@ -1574,6 +1583,7 @@ Editor::sensitize_the_right_region_actions (bool because_canvas_crossing) _region_actions->get_action("pitch-shift-region")->set_sensitive (false); _region_actions->get_action("strip-region-silence")->set_sensitive (false); _region_actions->get_action("show-rhythm-ferret")->set_sensitive (false); + _region_actions->get_action("toggle-region-polarity")->set_sensitive (false); }