[IPLUG/EXAMPLES] fix IPlugChunks example
[wdl/wdl-ol.git] / IPlugExamples / IPlugChunks / IPlugChunks.cpp
blob817bdd34bd77b9d24a923c6f6eaf1007dd1faf78
1 #include "IPlugChunks.h"
2 #include "IPlug_include_in_plug_src.h"
3 #include "IControl.h"
4 #include "resource.h"
5 #include <math.h>
7 // The number of presets/programs
8 const int kNumPrograms = 8;
10 // An enumerated list for all the parameters of the plugin
11 enum EParams
13 kGain = 0,
14 kNumParams, // the last element is used to store the total number of parameters
17 const int kDummyParamForMultislider = -2;
19 enum ELayout
21 kW = 400,
22 kH = 300
25 class ITempPresetSaveButtonControl : public IPanelControl
27 public:
28 ITempPresetSaveButtonControl(IPlugBase *pPlug, IRECT pR)
29 : IPanelControl(pPlug, pR, &COLOR_RED) {}
31 void OnMouseDown(int x, int y, IMouseMod* pMod)
33 WDL_String presetFilePath;
34 mPlug->GetGUI()->DesktopPath(&presetFilePath);
35 #ifdef OS_WIN
36 presetFilePath.Append("\\IPlugChunksPreset.txt");
37 #else //OSX
38 presetFilePath.Append("IPlugChunksPreset.txt");
39 #endif
40 mPlug->DumpPresetBlob(presetFilePath.Get());
45 class PresetFunctionsMenu : public IPanelControl
47 private:
48 WDL_String mPreviousPath;
50 public:
51 PresetFunctionsMenu(IPlugBase *pPlug, IRECT pR)
52 : IPanelControl(pPlug, pR, &COLOR_BLUE)
55 bool Draw(IGraphics* pGraphics)
57 pGraphics->FillIRect(&COLOR_WHITE, &mRECT);
59 int ax = mRECT.R - 8;
60 int ay = mRECT.T + 4;
61 int bx = ax + 4;
62 int by = ay;
63 int cx = ax + 2;
64 int cy = ay + 2;
66 pGraphics->FillTriangle(&COLOR_GRAY, ax , ay, bx, by, cx, cy, &mBlend);
68 return true;
71 void OnMouseDown(int x, int y, IMouseMod* pMod)
73 if (pMod->L)
75 doPopupMenu();
78 Redraw(); // seems to need this
79 SetDirty();
82 void doPopupMenu()
84 IPopupMenu menu;
86 IGraphics* gui = mPlug->GetGUI();
88 menu.AddItem("Save Program...");
89 menu.AddItem("Save Bank...");
90 menu.AddSeparator();
91 menu.AddItem("Load Program...");
92 menu.AddItem("Load Bank...");
94 if(gui->CreateIPopupMenu(&menu, &mRECT))
96 int itemChosen = menu.GetChosenItemIdx();
97 WDL_String fileName;
99 //printf("chosen %i /n", itemChosen);
100 switch (itemChosen)
102 case 0: //Save Program
103 fileName.Set(mPlug->GetPresetName(mPlug->GetCurrentPresetIdx()));
104 GetGUI()->PromptForFile(&fileName, kFileSave, &mPreviousPath, "fxp");
105 mPlug->SaveProgramAsFXP(&fileName);
106 break;
107 case 1: //Save Bank
108 fileName.Set("IPlugChunksBank");
109 GetGUI()->PromptForFile(&fileName, kFileSave, &mPreviousPath, "fxb");
110 mPlug->SaveBankAsFXB(&fileName);
111 break;
112 case 3: //Load Preset
113 GetGUI()->PromptForFile(&fileName, kFileOpen, &mPreviousPath, "fxp");
114 mPlug->LoadProgramFromFXP(&fileName);
115 break;
116 case 4: // Load Bank
117 GetGUI()->PromptForFile(&fileName, kFileOpen, &mPreviousPath, "fxb");
118 mPlug->LoadBankFromFXB(&fileName);
119 break;
120 default:
121 break;
127 IPlugChunks::IPlugChunks(IPlugInstanceInfo instanceInfo)
128 : IPLUG_CTOR(kNumParams, kNumPrograms, instanceInfo), mGain(1.)
130 TRACE;
132 memset(mSteps, 0, NUM_SLIDERS*sizeof(double));
134 // Define parameter ranges, display units, labels.
135 //arguments are: name, defaultVal, minVal, maxVal, step, label
136 GetParam(kGain)->InitDouble("Gain", 0.0, -70.0, 12.0, 0.1, "dB");
138 MakePresetFromBlob("Ramp Up", "AAAAAJqZqT8AAAAAmpm5PwAAAIA9Csc/AAAAAAAA0D8AAABA4XrUPwAAAIDC9dg/AAAAwMzM3D8AAAAQ16PgPwAAALBH4eI/AAAA0MzM5D8AAADwUbjmPwAAAAjXo+g/AAAAKFyP6j8AAADMzMzsPwAAAOxRuO4/AAAAAAAA8D8AAAAAAAC8Pg==", 136);
139 MakePresetFromBlob("Ramp Down", "AAAA7FG47j8AAABI4XrsPwAAALBH4eo/AAAAGK5H6T8AAABwPQrnPwAAANDMzOQ/AAAAwB6F4z8AAAAghevhPwAAAAB7FN4/AAAAgOtR2D8AAABAuB7VPwAAAACuR9E/AAAAgEfhyj8AAAAAhevBPwAAAABSuK4/AAAAAOB6hD8AAAAAAAC8Pg==", 136);
140 MakePresetFromBlob("Triangle", "AAAAAIXrwT8AAACAR+HKPwAAAEBcj9I/AAAAgBSu1z8AAADA9SjcPwAAABDXo+A/AAAAsEfh4j8AAABQuB7lPwAAAGBmZuY/AAAAMDMz4z8AAAAAAADgPwAAAMD1KNw/AAAAQI/C1T8AAAAArkfRPwAAAICPwsU/AAAAAJqZuT8AAAAAAAAAAA==", 136);
141 MakePresetFromBlob("Inv Triangle", "AAAAAAAA8D8AAABQuB7tPwAAAKBwPeo/AAAAcD0K5z8AAABA4XrkPwAAAJDC9eA/AAAAwEfh2j8AAABAj8LVPwAAAECPwtU/AAAAwMzM3D8AAAAghevhPwAAANDMzOQ/AAAAgBSu5z8AAACYmZnpPwAAAFyPwu0/AAAAAAAA8D8AAAAAAAAAAA==", 136);
142 MakePresetFromBlob("Da Endz", "AAAAAAAA8D8AAAAA4HqEPwAAAADgeoQ/AAAAAOB6hD8AAAAA4HqEPwAAAADgeoQ/AAAAAOB6hD8AAAAA4HqEPwAAAADgeoQ/AAAAAOB6hD8AAAAA4HqEPwAAAADgeoQ/AAAAAOB6hD8AAAAA4HqEPwAAAADgeoQ/AAAAAAAA8D8AAAAAAAAAAA==", 136);
143 MakePresetFromBlob("Alternate", "AAAAAAAA8D8AAAAA4HqEPwAAAAAAAPA/AAAAAOB6hD8AAAAAAADwPwAAAADgeoQ/AAAAAAAA8D8AAAAA4HqEPwAAAAAAAPA/AAAAAOB6hD8AAAAAAADwPwAAAADgeoQ/AAAAAAAA8D8AAAAA4HqEPwAAAAAAAPA/AAAAAOB6hD8AAAAAAAAAAA==", 136);
144 MakePresetFromBlob("Alt Ramp Down", "AAAAAAAA8D8AAAAA4HqEPwAAALgehes/AAAAAOB6hD8AAACI61HoPwAAAADgeoQ/AAAAQArX4z8AAAAA4HqEPwAAAAAAAOA/AAAAAOB6hD8AAABAuB7VPwAAAADgeoQ/AAAAAKRwzT8AAAAA4HqEPwAAAAAzM8M/AAAAAOB6hD8AAAAAAAAAAA==", 136);
145 MakePresetFromBlob("Alt Ramp Up", "AAAAgJmZyT8AAAAA4HqEPwAAAIBmZtY/AAAAAOB6hD8AAAAAKVzfPwAAAADgeoQ/AAAAMFyP4j8AAAAA4HqEPwAAAEDheuQ/AAAAAOB6hD8AAADwKFznPwAAAADgeoQ/AAAAIIXr6T8AAAAA4HqEPwAAANijcO0/AAAAAOB6hD8AAAAAAAAAAA==", 136);
147 IGraphics* pGraphics = MakeGraphics(this, kW, kH);
148 pGraphics->AttachPanelBackground(&COLOR_BLUE);
150 mMSlider = new MultiSliderControlV(this, IRECT(10, 10, 170, 110), kDummyParamForMultislider, NUM_SLIDERS, 10, &COLOR_WHITE, &COLOR_BLACK, &COLOR_RED);
152 pGraphics->AttachControl(mMSlider);
153 pGraphics->AttachControl(new IVSliderControl(this, IRECT(200, 10, 220, 110), kGain, 20, &COLOR_WHITE, &COLOR_GREEN));
155 //pGraphics->AttachControl(new ITempPresetSaveButtonControl(this, IRECT(350, 250, 390, 290)));
156 pGraphics->AttachControl(new PresetFunctionsMenu(this, IRECT(350, 250, 390, 290)));
158 AttachGraphics(pGraphics);
160 // call RestorePreset(0) here which will initialize the multislider in the gui and the mSteps array
161 RestorePreset(0);
164 IPlugChunks::~IPlugChunks() {}
166 void IPlugChunks::ProcessDoubleReplacing(double** inputs, double** outputs, int nFrames)
168 // Mutex is already locked for us.
170 double* in1 = inputs[0];
171 double* in2 = inputs[1];
172 double* out1 = outputs[0];
173 double* out2 = outputs[1];
175 int samplesPerBeat = (int) GetSamplesPerBeat();
176 int samplePos = (int) GetSamplePos();
178 int count = mCount;
179 int prevcount = mPrevCount;
181 for (int s = 0; s < nFrames; ++s, ++in1, ++in2, ++out1, ++out2)
183 int mod = (samplePos + s) % (samplesPerBeat * BEAT_DIV);
185 count = mod / (samplesPerBeat / BEAT_DIV);
187 if (count >= NUM_SLIDERS)
189 count = 0;
192 if (count != prevcount)
194 if (GetGUI())
196 mMSlider->SetHighlight(count);
200 prevcount = count;
202 *out1 = *in1 * mSteps[count] * mGain;
203 *out2 = *in2 * mSteps[count] * mGain;
206 mCount = count;
207 mPrevCount = prevcount;
211 void IPlugChunks::Reset()
213 TRACE;
214 IMutexLock lock(this);
216 mCount = 0;
217 mPrevCount = ULONG_MAX;
220 void IPlugChunks::OnParamChange(int paramIdx)
222 TRACE;
224 IMutexLock lock(this);
226 switch (paramIdx)
228 case kDummyParamForMultislider:
229 mMSlider->GetLatestChange(mSteps);
230 break;
231 case kGain:
232 mGain = GetParam(kGain)->DBToAmp();
233 break;
234 default:
235 break;
239 // this over-ridden method is called when the host is trying to store the plug-in state and needs to get the current data from your algorithm
240 bool IPlugChunks::SerializeState(ByteChunk* pChunk)
242 TRACE;
243 IMutexLock lock(this);
244 double v;
246 // serialize the multi-slider state state before serializing the regular params
247 for (int i = 0; i< NUM_SLIDERS; i++)
249 v = mSteps[i];
250 pChunk->Put(&v);
253 return IPlugBase::SerializeParams(pChunk); // must remember to call SerializeParams at the end
256 // this over-ridden method is called when the host is trying to load the plug-in state and you need to unpack the data into your algorithm
257 int IPlugChunks::UnserializeState(ByteChunk* pChunk, int startPos)
259 TRACE;
260 IMutexLock lock(this);
261 double v = 0.0;
263 // unserialize the multi-slider state before unserializing the regular params
264 for (int i = 0; i< NUM_SLIDERS; i++)
266 v = 0.0;
267 startPos = pChunk->Get(&v, startPos);
268 mSteps[i] = v;
271 // update values in control, will set dirty
272 if(mMSlider)
273 mMSlider->SetState(mSteps);
275 return IPlugBase::UnserializeParams(pChunk, startPos); // must remember to call UnserializeParams at the end
278 bool IPlugChunks::CompareState(const unsigned char* incomingState, int startPos)
280 bool isEqual = true;
281 const double* data = (const double*) incomingState;
282 startPos = NUM_SLIDERS * sizeof(double);
283 isEqual = (memcmp(data, mSteps, startPos) == 0);
284 isEqual &= IPlugBase::CompareState(incomingState, startPos); // fuzzy compare regular params
286 return isEqual;
289 void IPlugChunks::PresetsChangedByHost()
291 TRACE;
292 IMutexLock lock(this);
294 if(mMSlider)
295 mMSlider->SetState(mSteps);
297 if(GetGUI())
298 GetGUI()->SetAllControlsDirty();