From ff2cf496cd6f9a1a5bf47078b7d5585937fc78b4 Mon Sep 17 00:00:00 2001 From: Paul Licameli Date: Wed, 12 Jun 2019 23:03:13 -0400 Subject: [PATCH] Move DoEffect out of PluginMenus --- lib-src/mod-nyq-bench/NyqBench.cpp | 3 +- src/BatchCommands.cpp | 4 +- src/Menus.h | 6 -- src/ProjectFileManager.cpp | 2 +- src/effects/Effect.cpp | 2 +- src/effects/EffectManager.cpp | 138 ++++++++++++++++++++++++++++ src/effects/EffectManager.h | 3 + src/effects/EffectRack.cpp | 2 +- src/menus/PluginMenus.cpp | 141 +---------------------------- src/menus/TrackMenus.cpp | 2 +- 10 files changed, 151 insertions(+), 152 deletions(-) diff --git a/lib-src/mod-nyq-bench/NyqBench.cpp b/lib-src/mod-nyq-bench/NyqBench.cpp index a2d69d004..948eaaf25 100755 --- a/lib-src/mod-nyq-bench/NyqBench.cpp +++ b/lib-src/mod-nyq-bench/NyqBench.cpp @@ -26,7 +26,6 @@ #include "AudioIOBase.h" #include "CommonCommandFlags.h" #include "LabelTrack.h" -#include "Menus.h" #include "ModuleManager.h" #include "Prefs.h" #include "Project.h" @@ -1405,7 +1404,7 @@ void NyqBench::OnGo(wxCommandEvent & e) mRunning = true; UpdateWindowUI(); - PluginActions::DoEffect(ID, CommandContext(*p), 0); + EffectManager::DoEffect(ID, CommandContext(*p), 0); mRunning = false; UpdateWindowUI(); diff --git a/src/BatchCommands.cpp b/src/BatchCommands.cpp index 0ac91e8fa..6d86daccf 100644 --- a/src/BatchCommands.cpp +++ b/src/BatchCommands.cpp @@ -785,7 +785,7 @@ bool MacroCommands::ApplyEffectCommand( EffectManager::kDontRepeatLast); else // and apply the effect... - res = PluginActions::DoEffect(ID, + res = EffectManager::DoEffect(ID, Context, EffectManager::kConfigured | EffectManager::kSkipState | @@ -820,7 +820,7 @@ bool MacroCommands::HandleTextualCommand( CommandManager &commandManager, { if (em.GetCommandIdentifier(plug->GetID()) == Str) { - return PluginActions::DoEffect( + return EffectManager::DoEffect( plug->GetID(), context, EffectManager::kConfigured); } diff --git a/src/Menus.h b/src/Menus.h index 34cf4cf36..b8b1faa80 100644 --- a/src/Menus.h +++ b/src/Menus.h @@ -138,12 +138,6 @@ void DoTogglePinnedHead( AudacityProject & ); void DoRecord( AudacityProject & ); } -/// Namespace for helper functions to do with plug ins -namespace PluginActions { -bool DoEffect( - const PluginID & ID, const CommandContext & context, unsigned flags ); -} - /// Namespace for functions for Help menu namespace HelpActions { void DoHelpWelcome( AudacityProject & ); diff --git a/src/ProjectFileManager.cpp b/src/ProjectFileManager.cpp index 2a98dcbe9..54050de53 100644 --- a/src/ProjectFileManager.cpp +++ b/src/ProjectFileManager.cpp @@ -1712,7 +1712,7 @@ bool ProjectFileManager::Import( SelectUtilities::SelectNone( project ); SelectUtilities::SelectAllIfNone( project ); const CommandContext context( project ); - PluginActions::DoEffect( + EffectManager::DoEffect( EffectManager::Get().GetEffectByIdentifier(wxT("Normalize")), context, EffectManager::kConfigured); diff --git a/src/effects/Effect.cpp b/src/effects/Effect.cpp index cd7002ecf..52bf7b6f8 100644 --- a/src/effects/Effect.cpp +++ b/src/effects/Effect.cpp @@ -777,7 +777,7 @@ bool Effect::Apply() // This is absolute hackage...but easy and I can't think of another way just now. // // It should callback to the EffectManager to kick off the processing - return PluginActions::DoEffect(GetID(), context, + return EffectManager::DoEffect(GetID(), context, EffectManager::kConfigured); } diff --git a/src/effects/EffectManager.cpp b/src/effects/EffectManager.cpp index c473389d9..c5cccc4b6 100644 --- a/src/effects/EffectManager.cpp +++ b/src/effects/EffectManager.cpp @@ -37,8 +37,18 @@ effects. #include "../ShuttleGetDefinition.h" #include "../commands/CommandContext.h" +#include "../commands/CommandManager.h" #include "../commands/Command.h" +#include "../Menus.h" +#include "../MissingAliasFileDialog.h" #include "../PluginManager.h" +#include "../ProjectHistory.h" +#include "../ProjectSettings.h" +#include "../ProjectWindow.h" +#include "../SelectUtilities.h" +#include "../TrackPanel.h" +#include "../ViewInfo.h" +#include "../WaveTrack.h" /******************************************************************************* @@ -88,6 +98,134 @@ void EffectManager::UnregisterEffect(const PluginID & ID) mEffects.erase(id); } +/// DoEffect() takes a PluginID and has the EffectManager execute the associated +/// effect. +/// +/// At the moment flags are used only to indicate whether to prompt for +// parameters, whether to save the state to history and whether to allow +/// 'Repeat Last Effect'. + +/* static */ bool EffectManager::DoEffect( + const PluginID & ID, const CommandContext &context, unsigned flags ) +{ + AudacityProject &project = context.project; + const auto &settings = ProjectSettings::Get( project ); + auto &tracks = TrackList::Get( project ); + auto &trackPanel = TrackPanel::Get( project ); + auto &trackFactory = TrackFactory::Get( project ); + auto rate = settings.GetRate(); + auto &selectedRegion = ViewInfo::Get( project ).selectedRegion; + auto &commandManager = CommandManager::Get( project ); + auto &window = ProjectWindow::Get( project ); + + const PluginDescriptor *plug = PluginManager::Get().GetPlugin(ID); + if (!plug) + return false; + + EffectType type = plug->GetEffectType(); + + // Make sure there's no activity since the effect is about to be applied + // to the project's tracks. Mainly for Apply during RTP, but also used + // for batch commands + if (flags & EffectManager::kConfigured) + { + TransportActions::DoStop(project); + SelectUtilities::SelectAllIfNone( project ); + } + + MissingAliasFilesDialog::SetShouldShow(true); + + auto nTracksOriginally = tracks.size(); + wxWindow *focus = wxWindow::FindFocus(); + wxWindow *parent = nullptr; + if (focus != nullptr) { + parent = focus->GetParent(); + } + + bool success = false; + auto cleanup = finally( [&] { + + if (!success) { + // For now, we're limiting realtime preview to a single effect, so + // make sure the menus reflect that fact that one may have just been + // opened. + MenuManager::Get(project).UpdateMenus( false ); + } + + } ); + + int count = 0; + bool clean = true; + for (auto t : tracks.Selected< const WaveTrack >()) { + if (t->GetEndTime() != 0.0) + clean = false; + count++; + } + + EffectManager & em = EffectManager::Get(); + + success = em.DoEffect(ID, &window, rate, + &tracks, &trackFactory, &selectedRegion, + (flags & EffectManager::kConfigured) == 0); + + if (!success) + return false; + + if (em.GetSkipStateFlag()) + flags = flags | EffectManager::kSkipState; + + if (!(flags & EffectManager::kSkipState)) + { + wxString shortDesc = em.GetCommandName(ID); + wxString longDesc = em.GetCommandDescription(ID); + ProjectHistory::Get( project ).PushState(longDesc, shortDesc); + } + + if (!(flags & EffectManager::kDontRepeatLast)) + { + // Only remember a successful effect, don't remember insert, + // or analyze effects. + if (type == EffectTypeProcess) { + wxString shortDesc = em.GetCommandName(ID); + MenuManager::Get(project).mLastEffect = ID; + wxString lastEffectDesc; + /* i18n-hint: %s will be the name of the effect which will be + * repeated if this menu item is chosen */ + lastEffectDesc.Printf(_("Repeat %s"), shortDesc); + commandManager.Modify(wxT("RepeatLastEffect"), lastEffectDesc); + } + } + + //STM: + //The following automatically re-zooms after sound was generated. + // IMO, it was disorienting, removing to try out without re-fitting + //mchinen:12/14/08 reapplying for generate effects + if (type == EffectTypeGenerate) + { + if (count == 0 || (clean && selectedRegion.t0() == 0.0)) + ViewActions::DoZoomFit(project); + // trackPanel->Refresh(false); + } + window.RedrawProject(); + if (focus != nullptr && focus->GetParent()==parent) { + focus->SetFocus(); + } + + // A fix for Bug 63 + // New tracks added? Scroll them into view so that user sees them. + // Don't care what track type. An analyser might just have added a + // Label track and we want to see it. + if( tracks.size() > nTracksOriginally ){ + // 0.0 is min scroll position, 1.0 is max scroll position. + trackPanel.VerticalScroll( 1.0 ); + } else { + trackPanel.EnsureVisible(trackPanel.GetFirstSelectedTrack()); + trackPanel.Refresh(false); + } + + return true; +} + bool EffectManager::DoEffect(const PluginID & ID, wxWindow *parent, double projectRate, diff --git a/src/effects/EffectManager.h b/src/effects/EffectManager.h index 2a371604c..584e94068 100644 --- a/src/effects/EffectManager.h +++ b/src/effects/EffectManager.h @@ -67,6 +67,9 @@ public: // them by index number, usually when the user selects one from a menu. // public: + static bool DoEffect( + const PluginID & ID, const CommandContext &context, unsigned flags ); + EffectManager(); virtual ~EffectManager(); diff --git a/src/effects/EffectRack.cpp b/src/effects/EffectRack.cpp index ef4a8c84b..fac228fff 100644 --- a/src/effects/EffectRack.cpp +++ b/src/effects/EffectRack.cpp @@ -309,7 +309,7 @@ void EffectRack::OnApply(wxCommandEvent & WXUNUSED(evt)) { if (mPowerState[i]) { - if (!PluginActions::DoEffect(mEffects[i]->GetID(), + if (!EffectManager::DoEffect(mEffects[i]->GetID(), *project, EffectManager::kConfigured)) // If any effect fails (or throws), then stop. diff --git a/src/menus/PluginMenus.cpp b/src/menus/PluginMenus.cpp index afb374eb5..1fd4bedad 100644 --- a/src/menus/PluginMenus.cpp +++ b/src/menus/PluginMenus.cpp @@ -7,18 +7,11 @@ #include "../CommonCommandFlags.h" #include "../FreqWindow.h" #include "../Menus.h" -#include "../MissingAliasFileDialog.h" #include "../PluginManager.h" #include "../Prefs.h" #include "../Project.h" -#include "../ProjectHistory.h" -#include "../ProjectSettings.h" #include "../ProjectWindow.h" #include "../Screenshot.h" -#include "../SelectUtilities.h" -#include "../TrackPanel.h" -#include "../ViewInfo.h" -#include "../WaveTrack.h" #include "../commands/CommandContext.h" #include "../commands/CommandManager.h" #include "../commands/ScreenshotCommand.h" @@ -417,135 +410,6 @@ MenuTable::BaseItemPtrs PopulateMacrosMenu( CommandFlag flags ); namespace PluginActions { -// exported helper functions - -/// DoEffect() takes a PluginID and has the EffectManager execute the associated -/// effect. -/// -/// At the moment flags are used only to indicate whether to prompt for -// parameters, whether to save the state to history and whether to allow -/// 'Repeat Last Effect'. -bool DoEffect( - const PluginID & ID, const CommandContext &context, unsigned flags ) -{ - AudacityProject &project = context.project; - const auto &settings = ProjectSettings::Get( project ); - auto &tracks = TrackList::Get( project ); - auto &trackPanel = TrackPanel::Get( project ); - auto &trackFactory = TrackFactory::Get( project ); - auto rate = settings.GetRate(); - auto &selectedRegion = ViewInfo::Get( project ).selectedRegion; - auto &commandManager = CommandManager::Get( project ); - auto &window = ProjectWindow::Get( project ); - - const PluginDescriptor *plug = PluginManager::Get().GetPlugin(ID); - if (!plug) - return false; - - EffectType type = plug->GetEffectType(); - - // Make sure there's no activity since the effect is about to be applied - // to the project's tracks. Mainly for Apply during RTP, but also used - // for batch commands - if (flags & EffectManager::kConfigured) - { - TransportActions::DoStop(project); - SelectUtilities::SelectAllIfNone( project ); - } - - MissingAliasFilesDialog::SetShouldShow(true); - - auto nTracksOriginally = tracks.size(); - wxWindow *focus = wxWindow::FindFocus(); - wxWindow *parent = nullptr; - if (focus != nullptr) { - parent = focus->GetParent(); - } - - bool success = false; - auto cleanup = finally( [&] { - - if (!success) { - // For now, we're limiting realtime preview to a single effect, so - // make sure the menus reflect that fact that one may have just been - // opened. - MenuManager::Get(project).UpdateMenus( false ); - } - - } ); - - int count = 0; - bool clean = true; - for (auto t : tracks.Selected< const WaveTrack >()) { - if (t->GetEndTime() != 0.0) - clean = false; - count++; - } - - EffectManager & em = EffectManager::Get(); - - success = em.DoEffect(ID, &window, rate, - &tracks, &trackFactory, &selectedRegion, - (flags & EffectManager::kConfigured) == 0); - - if (!success) - return false; - - if (em.GetSkipStateFlag()) - flags = flags | EffectManager::kSkipState; - - if (!(flags & EffectManager::kSkipState)) - { - wxString shortDesc = em.GetCommandName(ID); - wxString longDesc = em.GetCommandDescription(ID); - ProjectHistory::Get( project ).PushState(longDesc, shortDesc); - } - - if (!(flags & EffectManager::kDontRepeatLast)) - { - // Only remember a successful effect, don't remember insert, - // or analyze effects. - if (type == EffectTypeProcess) { - wxString shortDesc = em.GetCommandName(ID); - MenuManager::Get(project).mLastEffect = ID; - wxString lastEffectDesc; - /* i18n-hint: %s will be the name of the effect which will be - * repeated if this menu item is chosen */ - lastEffectDesc.Printf(_("Repeat %s"), shortDesc); - commandManager.Modify(wxT("RepeatLastEffect"), lastEffectDesc); - } - } - - //STM: - //The following automatically re-zooms after sound was generated. - // IMO, it was disorienting, removing to try out without re-fitting - //mchinen:12/14/08 reapplying for generate effects - if (type == EffectTypeGenerate) - { - if (count == 0 || (clean && selectedRegion.t0() == 0.0)) - ViewActions::DoZoomFit(project); - // trackPanel->Refresh(false); - } - window.RedrawProject(); - if (focus != nullptr && focus->GetParent()==parent) { - focus->SetFocus(); - } - - // A fix for Bug 63 - // New tracks added? Scroll them into view so that user sees them. - // Don't care what track type. An analyser might just have added a - // Label track and we want to see it. - if( tracks.size() > nTracksOriginally ){ - // 0.0 is min scroll position, 1.0 is max scroll position. - trackPanel.VerticalScroll( 1.0 ); - } else { - trackPanel.EnsureVisible(trackPanel.GetFirstSelectedTrack()); - trackPanel.Refresh(false); - } - - return true; -} - // Menu handler functions struct Handler : CommandHandlerObject { @@ -559,7 +423,7 @@ void OnManageGenerators(const CommandContext &context) void OnEffect(const CommandContext &context) { // using GET to interpret parameter as a PluginID - DoEffect(context.parameter.GET(), context, 0); + EffectManager::DoEffect(context.parameter.GET(), context, 0); } void OnManageEffects(const CommandContext &context) @@ -573,7 +437,8 @@ void OnRepeatLastEffect(const CommandContext &context) auto lastEffect = MenuManager::Get(context.project).mLastEffect; if (!lastEffect.empty()) { - DoEffect( lastEffect, context, EffectManager::kConfigured ); + EffectManager::DoEffect( + lastEffect, context, EffectManager::kConfigured ); } } diff --git a/src/menus/TrackMenus.cpp b/src/menus/TrackMenus.cpp index e830f1f98..07ed175e6 100644 --- a/src/menus/TrackMenus.cpp +++ b/src/menus/TrackMenus.cpp @@ -674,7 +674,7 @@ void OnNewTimeTrack(const CommandContext &context) void OnStereoToMono(const CommandContext &context) { - PluginActions::DoEffect( + EffectManager::DoEffect( EffectManager::Get().GetEffectByIdentifier(wxT("StereoToMono")), context, EffectManager::kConfigured);