forked from audacity/audacity
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEffectManager.cpp
More file actions
840 lines (691 loc) · 19.9 KB
/
Copy pathEffectManager.cpp
File metadata and controls
840 lines (691 loc) · 19.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
/**********************************************************************
Audacity: A Digital Audio Editor
EffectManager.cpp
Audacity(R) is copyright (c) 1999-2008 Audacity Team.
License: GPL v2. See License.txt.
******************************************************************//**
\class EffectManager
\brief EffectManager is the class that handles effects and effect categories.
It maintains a graph of effect categories and subcategories,
registers and unregisters effects and can return filtered lists of
effects.
*//*******************************************************************/
#include "../Audacity.h"
#include "EffectManager.h"
#include "Effect.h"
#include <algorithm>
#include <wx/tokenzr.h>
#include "../widgets/AudacityMessageBox.h"
#include "../ShuttleGetDefinition.h"
#include "../commands/CommandContext.h"
#include "../commands/AudacityCommand.h"
#include "../PluginManager.h"
/*******************************************************************************
Creates a singleton and returns reference
(Thread-safe...no active threading during construction or after destruction)
*******************************************************************************/
EffectManager & EffectManager::Get()
{
static EffectManager em;
return em;
}
EffectManager::EffectManager()
{
mSkipStateFlag = false;
}
EffectManager::~EffectManager()
{
}
// Here solely for the purpose of Nyquist Workbench until
// a better solution is devised.
const PluginID & EffectManager::RegisterEffect(Effect *f)
{
const PluginID & ID = PluginManager::Get().RegisterPlugin(f, PluginTypeEffect);
mEffects[ID] = f;
return ID;
}
// Here solely for the purpose of Nyquist Workbench until
// a better solution is devised.
void EffectManager::UnregisterEffect(const PluginID & ID)
{
PluginID id = ID;
PluginManager::Get().UnregisterPlugin(id);
mEffects.erase(id);
}
bool EffectManager::DoAudacityCommand(const PluginID & ID,
const CommandContext &context,
wxWindow *parent,
bool shouldPrompt /* = true */)
{
this->SetSkipStateFlag(false);
AudacityCommand *command = GetAudacityCommand(ID);
if (!command)
{
return false;
}
bool res = command->DoAudacityCommand(parent, context, shouldPrompt);
return res;
}
ComponentInterfaceSymbol EffectManager::GetCommandSymbol(const PluginID & ID)
{
return PluginManager::Get().GetSymbol(ID);
}
TranslatableString EffectManager::GetCommandName(const PluginID & ID)
{
return GetCommandSymbol(ID).Msgid();
}
TranslatableString EffectManager::GetEffectFamilyName(const PluginID & ID)
{
auto effect = GetEffect(ID);
if (effect)
return effect->GetFamily().Msgid();
return {};
}
TranslatableString EffectManager::GetVendorName(const PluginID & ID)
{
auto effect = GetEffect(ID);
if (effect)
return effect->GetVendor().Msgid();
return {};
}
CommandID EffectManager::GetCommandIdentifier(const PluginID & ID)
{
wxString name = PluginManager::Get().GetSymbol(ID).Internal();
return Effect::GetSquashedName(name);
}
TranslatableString EffectManager::GetCommandDescription(const PluginID & ID)
{
if (GetEffect(ID))
return XO("Applied effect: %s").Format( GetCommandName(ID) );
if (GetAudacityCommand(ID))
return XO("Applied command: %s").Format( GetCommandName(ID) );
return {};
}
wxString EffectManager::GetCommandUrl(const PluginID & ID)
{
Effect* pEff = GetEffect(ID);
if( pEff )
return pEff->ManualPage();
AudacityCommand * pCom = GetAudacityCommand(ID);
if( pCom )
return pCom->ManualPage();
return wxEmptyString;
}
TranslatableString EffectManager::GetCommandTip(const PluginID & ID)
{
Effect* pEff = GetEffect(ID);
if( pEff )
return pEff->GetDescription();
AudacityCommand * pCom = GetAudacityCommand(ID);
if( pCom )
return pCom->GetDescription();
return {};
}
void EffectManager::GetCommandDefinition(const PluginID & ID, const CommandContext & context, int flags)
{
ComponentInterface *command;
command = GetEffect(ID);
if( !command )
command = GetAudacityCommand( ID );
if( !command )
return;
ShuttleParams NullShuttle;
// Test if it defines any parameters at all.
bool bHasParams = command->DefineParams( NullShuttle );
if( (flags ==0) && !bHasParams )
return;
// This is capturing the output context into the shuttle.
ShuttleGetDefinition S( *context.pOutput.get()->mStatusTarget.get() );
S.StartStruct();
// using GET to expose a CommandID to the user!
// Macro command details are one place that we do expose Identifier
// to (more sophisticated) users
S.AddItem( GetCommandIdentifier( ID ).GET(), "id" );
S.AddItem( GetCommandName( ID ).Translation(), "name" );
if( bHasParams ){
S.StartField( "params" );
S.StartArray();
command->DefineParams( S );
S.EndArray();
S.EndField();
}
S.AddItem( GetCommandUrl( ID ), "url" );
// The tip is a translated string!
S.AddItem( GetCommandTip( ID ).Translation(), "tip" );
S.EndStruct();
}
bool EffectManager::IsHidden(const PluginID & ID)
{
Effect *effect = GetEffect(ID);
if (effect)
{
return effect->IsHidden();
}
return false;
}
void EffectManager::SetSkipStateFlag(bool flag)
{
mSkipStateFlag = flag;
}
bool EffectManager::GetSkipStateFlag()
{
return mSkipStateFlag;
}
bool EffectManager::SupportsAutomation(const PluginID & ID)
{
const PluginDescriptor *plug = PluginManager::Get().GetPlugin(ID);
if (plug)
{
return plug->IsEffectAutomatable();
}
return false;
}
wxString EffectManager::GetEffectParameters(const PluginID & ID)
{
Effect *effect = GetEffect(ID);
if (effect)
{
wxString parms;
effect->GetAutomationParameters(parms);
// Some effects don't have automatable parameters and will not return
// anything, so try to get the active preset (current or factory).
if (parms.empty())
{
parms = GetDefaultPreset(ID);
}
return parms;
}
AudacityCommand *command = GetAudacityCommand(ID);
if (command)
{
wxString parms;
command->GetAutomationParameters(parms);
// Some effects don't have automatable parameters and will not return
// anything, so try to get the active preset (current or factory).
if (parms.empty())
{
parms = GetDefaultPreset(ID);
}
return parms;
}
return wxEmptyString;
}
bool EffectManager::SetEffectParameters(const PluginID & ID, const wxString & params)
{
Effect *effect = GetEffect(ID);
if (effect)
{
CommandParameters eap(params);
if (eap.HasEntry(wxT("Use Preset")))
{
return effect->SetAutomationParameters(eap.Read(wxT("Use Preset")));
}
return effect->SetAutomationParameters(params);
}
AudacityCommand *command = GetAudacityCommand(ID);
if (command)
{
// Set defaults (if not initialised) before setting values.
command->Init();
CommandParameters eap(params);
if (eap.HasEntry(wxT("Use Preset")))
{
return command->SetAutomationParameters(eap.Read(wxT("Use Preset")));
}
return command->SetAutomationParameters(params);
}
return false;
}
bool EffectManager::PromptUser(
const PluginID & ID,
const EffectClientInterface::EffectDialogFactory &factory, wxWindow &parent)
{
bool result = false;
Effect *effect = GetEffect(ID);
if (effect)
{
result = effect->ShowInterface(
parent, factory, effect->IsBatchProcessing() );
return result;
}
AudacityCommand *command = GetAudacityCommand(ID);
if (command)
{
result = command->PromptUser(&parent);
return result;
}
return result;
}
bool EffectManager::HasPresets(const PluginID & ID)
{
Effect *effect = GetEffect(ID);
if (!effect)
{
return false;
}
return effect->GetUserPresets().size() > 0 ||
effect->GetFactoryPresets().size() > 0 ||
effect->HasCurrentSettings() ||
effect->HasFactoryDefaults();
}
#include <wx/choice.h>
#include <wx/listbox.h>
#include "../ShuttleGui.h"
namespace {
///////////////////////////////////////////////////////////////////////////////
//
// EffectPresetsDialog
//
///////////////////////////////////////////////////////////////////////////////
class EffectPresetsDialog final : public wxDialogWrapper
{
public:
EffectPresetsDialog(wxWindow *parent, Effect *effect);
virtual ~EffectPresetsDialog();
wxString GetSelected() const;
void SetSelected(const wxString & parms);
private:
void SetPrefix(const TranslatableString & type, const wxString & prefix);
void UpdateUI();
void OnType(wxCommandEvent & evt);
void OnOk(wxCommandEvent & evt);
void OnCancel(wxCommandEvent & evt);
private:
wxChoice *mType;
wxListBox *mPresets;
RegistryPaths mFactoryPresets;
RegistryPaths mUserPresets;
wxString mSelection;
DECLARE_EVENT_TABLE()
};
enum
{
ID_Type = 10000
};
BEGIN_EVENT_TABLE(EffectPresetsDialog, wxDialogWrapper)
EVT_CHOICE(ID_Type, EffectPresetsDialog::OnType)
EVT_LISTBOX_DCLICK(wxID_ANY, EffectPresetsDialog::OnOk)
EVT_BUTTON(wxID_OK, EffectPresetsDialog::OnOk)
EVT_BUTTON(wxID_CANCEL, EffectPresetsDialog::OnCancel)
END_EVENT_TABLE()
EffectPresetsDialog::EffectPresetsDialog(wxWindow *parent, Effect *effect)
: wxDialogWrapper(parent, wxID_ANY, XO("Select Preset"))
{
ShuttleGui S(this, eIsCreating);
S.StartVerticalLay();
{
S.StartTwoColumn();
S.SetStretchyCol(1);
{
S.AddPrompt(XXO("Type:"));
mType = S.Id(ID_Type).AddChoice( {}, {}, 0 );
S.AddPrompt(XXO("&Preset:"));
mPresets = S
.Style( wxLB_SINGLE | wxLB_NEEDED_SB )
.AddListBox( {} );
}
S.EndTwoColumn();
S.AddStandardButtons();
}
S.EndVerticalLay();
mUserPresets = effect->GetUserPresets();
mFactoryPresets = effect->GetFactoryPresets();
if (mUserPresets.size() > 0)
{
mType->Append(_("User Presets"));
}
if (mFactoryPresets.size() > 0)
{
mType->Append(_("Factory Presets"));
}
if (effect->HasCurrentSettings())
{
mType->Append(_("Current Settings"));
}
if (effect->HasFactoryDefaults())
{
mType->Append(_("Factory Defaults"));
}
UpdateUI();
}
EffectPresetsDialog::~EffectPresetsDialog()
{
}
wxString EffectPresetsDialog::GetSelected() const
{
return mSelection;
}
void EffectPresetsDialog::SetSelected(const wxString & parms)
{
wxString preset = parms;
if (preset.StartsWith(Effect::kUserPresetIdent))
{
preset.Replace(Effect::kUserPresetIdent, wxEmptyString, false);
SetPrefix(XO("User Presets"), preset);
}
else if (preset.StartsWith(Effect::kFactoryPresetIdent))
{
preset.Replace(Effect::kFactoryPresetIdent, wxEmptyString, false);
SetPrefix(XO("Factory Presets"), preset);
}
else if (preset.StartsWith(Effect::kCurrentSettingsIdent))
{
SetPrefix(XO("Current Settings"), wxEmptyString);
}
else if (preset.StartsWith(Effect::kFactoryDefaultsIdent))
{
SetPrefix(XO("Factory Defaults"), wxEmptyString);
}
}
void EffectPresetsDialog::SetPrefix(
const TranslatableString & type, const wxString & prefix)
{
mType->SetStringSelection(type.Translation());
if (type == XO("User Presets"))
{
mPresets->Clear();
for (const auto &preset : mUserPresets)
mPresets->Append(preset);
mPresets->Enable(true);
mPresets->SetStringSelection(prefix);
if (mPresets->GetSelection() == wxNOT_FOUND)
{
mPresets->SetSelection(0);
}
mSelection = Effect::kUserPresetIdent + mPresets->GetStringSelection();
}
else if (type == XO("Factory Presets"))
{
mPresets->Clear();
for (size_t i = 0, cnt = mFactoryPresets.size(); i < cnt; i++)
{
auto label = mFactoryPresets[i];
if (label.empty())
{
label = _("None");
}
mPresets->Append(label);
}
mPresets->Enable(true);
mPresets->SetStringSelection(prefix);
if (mPresets->GetSelection() == wxNOT_FOUND)
{
mPresets->SetSelection(0);
}
mSelection = Effect::kFactoryPresetIdent + mPresets->GetStringSelection();
}
else if (type == XO("Current Settings"))
{
mPresets->Clear();
mPresets->Enable(false);
mSelection = Effect::kCurrentSettingsIdent;
}
else if (type == XO("Factory Defaults"))
{
mPresets->Clear();
mPresets->Enable(false);
mSelection = Effect::kFactoryDefaultsIdent;
}
}
void EffectPresetsDialog::UpdateUI()
{
int selected = mType->GetSelection();
if (selected == wxNOT_FOUND)
{
selected = 0;
mType->SetSelection(selected);
}
wxString type = mType->GetString(selected);
if (type == _("User Presets"))
{
selected = mPresets->GetSelection();
if (selected == wxNOT_FOUND)
{
selected = 0;
}
mPresets->Clear();
for (const auto &preset : mUserPresets)
mPresets->Append(preset);
mPresets->Enable(true);
mPresets->SetSelection(selected);
mSelection = Effect::kUserPresetIdent + mPresets->GetString(selected);
}
else if (type == _("Factory Presets"))
{
selected = mPresets->GetSelection();
if (selected == wxNOT_FOUND)
{
selected = 0;
}
mPresets->Clear();
for (size_t i = 0, cnt = mFactoryPresets.size(); i < cnt; i++)
{
auto label = mFactoryPresets[i];
if (label.empty())
{
label = _("None");
}
mPresets->Append(label);
}
mPresets->Enable(true);
mPresets->SetSelection(selected);
mSelection = Effect::kFactoryPresetIdent + mPresets->GetString(selected);
}
else if (type == _("Current Settings"))
{
mPresets->Clear();
mPresets->Enable(false);
mSelection = Effect::kCurrentSettingsIdent;
}
else if (type == _("Factory Defaults"))
{
mPresets->Clear();
mPresets->Enable(false);
mSelection = Effect::kFactoryDefaultsIdent;
}
}
void EffectPresetsDialog::OnType(wxCommandEvent & WXUNUSED(evt))
{
UpdateUI();
}
void EffectPresetsDialog::OnOk(wxCommandEvent & WXUNUSED(evt))
{
UpdateUI();
EndModal(true);
}
void EffectPresetsDialog::OnCancel(wxCommandEvent & WXUNUSED(evt))
{
mSelection = wxEmptyString;
EndModal(false);
}
}
wxString EffectManager::GetPreset(const PluginID & ID, const wxString & params, wxWindow * parent)
{
Effect *effect = GetEffect(ID);
if (!effect)
{
return wxEmptyString;
}
CommandParameters eap(params);
wxString preset;
if (eap.HasEntry(wxT("Use Preset")))
{
preset = eap.Read(wxT("Use Preset"));
}
{
EffectPresetsDialog dlg(parent, effect);
dlg.Layout();
dlg.Fit();
dlg.SetSize(dlg.GetMinSize());
dlg.CenterOnParent();
dlg.SetSelected(preset);
if (dlg.ShowModal())
preset = dlg.GetSelected();
else
preset = wxEmptyString;
}
if (preset.empty())
{
return preset;
}
eap.DeleteAll();
eap.Write(wxT("Use Preset"), preset);
eap.GetParameters(preset);
return preset;
}
wxString EffectManager::GetDefaultPreset(const PluginID & ID)
{
Effect *effect = GetEffect(ID);
if (!effect)
{
return wxEmptyString;
}
wxString preset;
if (effect->HasCurrentSettings())
{
preset = Effect::kCurrentSettingsIdent;
}
else if (effect->HasFactoryDefaults())
{
preset = Effect::kFactoryDefaultsIdent;
}
if (!preset.empty())
{
CommandParameters eap;
eap.Write(wxT("Use Preset"), preset);
eap.GetParameters(preset);
}
return preset;
}
void EffectManager::SetBatchProcessing(const PluginID & ID, bool start)
{
Effect *effect = GetEffect(ID);
if (effect)
{
effect->SetBatchProcessing(start);
return;
}
AudacityCommand *command = GetAudacityCommand(ID);
if (command)
{
command->SetBatchProcessing(start);
return;
}
}
Effect *EffectManager::GetEffect(const PluginID & ID)
{
// Must have a "valid" ID
if (ID.empty())
{
return NULL;
}
// If it is actually a command then refuse it (as an effect).
if( mCommands.find( ID ) != mCommands.end() )
return NULL;
// TODO: This is temporary and should be redone when all effects are converted
if (mEffects.find(ID) == mEffects.end())
{
// This will instantiate the effect client if it hasn't already been done
EffectDefinitionInterface *ident = dynamic_cast<EffectDefinitionInterface *>(PluginManager::Get().GetInstance(ID));
if (ident && ident->IsLegacy())
{
auto effect = dynamic_cast<Effect *>(ident);
if (effect && effect->Startup(NULL))
{
mEffects[ID] = effect;
return effect;
}
}
auto effect = std::make_shared<Effect>(); // TODO: use make_unique and store in std::unordered_map
if (effect)
{
EffectClientInterface *client = dynamic_cast<EffectClientInterface *>(ident);
if (client && effect->Startup(client))
{
auto pEffect = effect.get();
mEffects[ID] = pEffect;
mHostEffects[ID] = std::move(effect);
return pEffect;
}
}
auto command = dynamic_cast<AudacityCommand *>(PluginManager::Get().GetInstance(ID));
if( !command )
AudacityMessageBox(
XO(
"Attempting to initialize the following effect failed:\n\n%s\n\nMore information may be available in 'Help > Diagnostics > Show Log'")
.Format( GetCommandName(ID) ),
XO("Effect failed to initialize"));
return NULL;
}
return mEffects[ID];
}
AudacityCommand *EffectManager::GetAudacityCommand(const PluginID & ID)
{
// Must have a "valid" ID
if (ID.empty())
{
return NULL;
}
// TODO: This is temporary and should be redone when all effects are converted
if (mCommands.find(ID) == mCommands.end())
{
// This will instantiate the effect client if it hasn't already been done
auto command = dynamic_cast<AudacityCommand *>(PluginManager::Get().GetInstance(ID));
if (command )//&& command->Startup(NULL))
{
command->Init();
mCommands[ID] = command;
return command;
}
/*
if (ident && ident->IsLegacy())
{
auto command = dynamic_cast<AudacityCommand *>(ident);
if (commandt && command->Startup(NULL))
{
mCommands[ID] = command;
return command;
}
}
auto command = std::make_shared<AudacityCommand>(); // TODO: use make_unique and store in std::unordered_map
if (command)
{
AudacityCommand *client = dynamic_cast<AudacityCommand *>(ident);
if (client && command->Startup(client))
{
auto pCommand = command.get();
mEffects[ID] = pCommand;
mHostEffects[ID] = std::move(effect);
return pEffect;
}
}
*/
AudacityMessageBox(
XO(
"Attempting to initialize the following command failed:\n\n%s\n\nMore information may be available in 'Help > Diagnostics > Show Log'")
.Format( GetCommandName(ID) ),
XO("Command failed to initialize"));
return NULL;
}
return mCommands[ID];
}
const PluginID & EffectManager::GetEffectByIdentifier(const CommandID & strTarget)
{
static PluginID empty;
if (strTarget.empty()) // set GetCommandIdentifier to wxT("") to not show an effect in Batch mode
{
return empty;
}
PluginManager & pm = PluginManager::Get();
// Effects OR Generic commands...
const PluginDescriptor *plug = pm.GetFirstPlugin(PluginTypeEffect | PluginTypeAudacityCommand);
while (plug)
{
if (GetCommandIdentifier(plug->GetID()) == strTarget)
{
return plug->GetID();
}
plug = pm.GetNextPlugin(PluginTypeEffect | PluginTypeAudacityCommand);
}
return empty;;
}