-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathfp_cplug.cpp
More file actions
561 lines (441 loc) · 12 KB
/
fp_cplug.cpp
File metadata and controls
561 lines (441 loc) · 12 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
#ifdef _USE_AFX
//#include "stdafx.h"
#include <afxwin.h>
#endif
#ifdef __BORLANDC__
#include <vcl/SysInit.hpp>
#endif
#include <stdio.h>
#include "math.h"
#include "fp_cplug.h"
#include "fp_extra.h"
#include <string.h>
#ifdef __APPLE__
#include <stdlib.h>
#endif
char AppHint[256];
char PluginPath[MaxPath]; // path to this DLL plugin (for loading of resources)
#ifndef __BORLANDC__
HINSTANCE HInstance;
#endif
void GetPluginPath()
{
#ifndef __APPLE__
char p[MaxPath];
GetModuleFileNameA(HInstance, p, MaxPath);
strcpy(PluginPath, p);
#endif
}
// 4 byte alignment
inline int AlignBlock_4(int Value)
{
int n = 3;
return (Value+3) & (~n);
}
// 16 byte alignment
inline int AlignBlock_16(int Value)
{
int n = 15;
return (Value+15) & (~n);
}
// 32 byte alignment
inline int AlignBlock_32(int Value)
{
int n = 31;
return (Value+31) & (~n);
}
// some constants for TAudioRenderer
const float Ramp_Res_Unit = 1.0f/1000.0f; // will take this number of samples to go from 0 to 1 at 44100Hz
const float Ramp_Res_Norm = Ramp_Res_Unit; // source is -1..1 normalized
const float Ramp_Res_Min = Ramp_Res_Unit*10.0f; // source is -1..1 normalized, but quicker ramping, with minimum safety
const float Ramp_Res_FromInt = Ramp_Res_Unit*32768.0f; // source is integer
#define M_PI 3.14159265358979323846
// --- TAudioRenderer ---
TAudioRenderer::TAudioRenderer(TAudioRenderer *source)
{
if (source)
setSmpRate(source->getSmpRate());
else
setSmpRate(MixSmpRate_Default);
}
double TAudioRenderer::SamplesToMS(double Time)
{
return Time*1000.0/fSmpRate;
}
double TAudioRenderer::MSToSamples(double Time)
{
return Time*fSmpRate/1000.0;
}
int TAudioRenderer::getSmpRate()
{
return fSmpRate;
}
void TAudioRenderer::setSmpRate(int newValue)
{
fSmpRate = newValue;
SmpRateScale = 44100.0f/fSmpRate;
// filters
OmegaMul = M_PI*2.0/fSmpRate;
FilterMaxFreq = fSmpRate*0.4999f;
// ramping
Ramp_Res_Norm = Ramp_Res_Unit*SmpRateScale;
Ramp_Res_Min = Ramp_Res_Norm*10;
Ramp_Res_FromInt = Ramp_Res_Norm*32768;
}
// --- TCPPFruityPlug ---
// create the object
TCPPFruityPlug::TCPPFruityPlug(intptr_t SetHostTag, TFruityPlugHost *SetPlugHost, HINSTANCE instance)
: TFruityPlug()
{
HostTag = SetHostTag;
PlugHost = SetPlugHost;
HInstance = instance; // call before GetPluginPath()!
GetPluginPath();
#ifndef __APPLE__
InitializeCriticalSection(&ThreadSafeCS);
#endif
AudioRenderer.setSmpRate(MixSmpRate_Default);
ProcessTempBuffer = NULL;
MaxProcessLength = 0;
#ifdef __BORLANDC__
PopupParam = -1;
#endif
#ifndef __APPLE__
char *path = (char *)(PlugHost->Dispatcher(HostTag,FHD_GetPath, GP_ProgPath, 0));
strcpy_s(ProgPath, MAX_PATH, path);
#endif
}
void _stdcall TCPPFruityPlug::DestroyObject()
{
#ifndef __APPLE__
DeleteCriticalSection(&ThreadSafeCS);
#endif
if (ProcessTempBuffer)
free(ProcessTempBuffer);
// inherited function (deletes this object!)
TFruityPlug::DestroyObject();
}
// returns true if freshly reallocated
bool TCPPFruityPlug::AllocTempBuffer(int Length, int NumBuffers)
{
if (Length>MaxProcessLength)
{
MaxProcessLength = AlignBlock_4(Length);
ProcessTempBuffer = (PWAV32FM)realloc(ProcessTempBuffer, MaxProcessLength*8*NumBuffers);
return true;
}
else
return false;
}
void TCPPFruityPlug::SetInstanceHandle(HINSTANCE instance)
{
HInstance = instance;
}
void TCPPFruityPlug::ProcessAllParams()
{
for (int n = 0; n < Info->NumParams; n++)
ProcessParam(n, ProcessParam(n, 0, REC_GetValue), REC_UpdateValue | REC_UpdateControl);
}
// to safely skip a rendering (for plugins that don't render)
void TCPPFruityPlug::SkipRendering(void *SourceBuffer, void *DestBuffer, int Length)
{
if (SourceBuffer != DestBuffer)
memmove(DestBuffer, SourceBuffer, Length << 3);
}
// get the speed step to go through the LFO table at a given pitch (cents) (tuned around C5)
int TCPPFruityPlug::GetStep_Cents(int Pitch)
{
return (int)(PitchMul * exp(NoteMul*Pitch));
}
float TCPPFruityPlug::GetStep_Cents(float Pitch)
{
return (float)(PitchMul * exp(NoteMul*Pitch));
}
int TCPPFruityPlug::GetStep_Freq(int Freq)
{
int x = this->AudioRenderer.getSmpRate();
return Freq/x;
}
// thread synchronization for safety
void TCPPFruityPlug::Lock()
{
#ifndef __APPLE__
EnterCriticalSection(&ThreadSafeCS);
#endif
}
void TCPPFruityPlug::Unlock()
{
#ifndef __APPLE__
LeaveCriticalSection(&ThreadSafeCS);
#endif
}
// hints
void TCPPFruityPlug::ShowHintMsg(char *Msg)
{
PlugHost->OnHint(HostTag, Msg);
}
void TCPPFruityPlug::ShowHintMsg_Direct(char *Msg)
{
PlugHost->Dispatcher(HostTag, FHD_OnHint_Direct, 0, (intptr_t)Msg);
}
void TCPPFruityPlug::ShowHintMsg_Percent(int Value, int Max)
{
char tempc[256];
sprintf(tempc, "%d%%", Value*100 / Max);
PlugHost->OnHint(HostTag, tempc);
}
//const PitchTypeT:Array[-1..2] of String=(' octave',' semitone',' cent',' Hz');
void TCPPFruityPlug::ShowHintMsg_Pitch(int Value, int PitchType)
{
const char *PitchTypeT[4];
PitchTypeT[0] = "octave";
PitchTypeT[1] = "semitone";
PitchTypeT[2] = "cent";
PitchTypeT[3] = "Hz";
char Msg[256];
char M2[256];
sprintf(M2, "%d %s", Value, PitchTypeT[PitchType+1]); // PitchType = -1..2
if (Value == 0 || Value == 1)
sprintf(Msg, "+%s", M2);
else if (Value > 1)
sprintf(Msg, "+%ss", M2);
else if (abs(Value) > 1)
sprintf(Msg, "%ss", M2);
else
strcpy(Msg, M2);
PlugHost->OnHint(HostTag, Msg);
}
void TCPPFruityPlug::ShowHintMsg_Pitch(float Value, int PitchType, int Digits)
{
const char *PitchTypeT[4];
PitchTypeT[0] = "octave";
PitchTypeT[1] = "semitone";
PitchTypeT[2] = "cent";
PitchTypeT[3] = "Hz";
char Msg[256];
char M2[256];
sprintf(M2, "%%%d.f %%s", Digits); // create the format string
sprintf(M2, M2, Value, PitchTypeT[PitchType+1]);
if (fabs(Value) > 1 && PitchType < 2)
sprintf(Msg, "+%ss", M2);
else if (Value >= 0)
sprintf(Msg, "+%s", M2);
PlugHost->OnHint(HostTag, Msg);
}
void TCPPFruityPlug::ShowHintMsg_Pan(int Value)
{
char Msg[128];
int n;
n = Value * 100 / 64;
if (n == 0)
strcpy(Msg, "Centered");
else
{
sprintf(Msg, "%d%% ", abs(n));
if (n < 0)
strcpy(Msg, strcat(Msg, "left"));
else
strcpy(Msg, strcat(Msg, "right"));
}
PlugHost->OnHint(HostTag, Msg);
}
// show the time in 0:00 format at 192 PPQ
void TCPPFruityPlug::ShowHintMsg_Time(int Value)
{
char Msg[128];
sprintf(Msg, "%d:%.2d", Value / AbsPPN, Value % AbsPPN);
PlugHost->OnHint(HostTag, Msg);
}
void TCPPFruityPlug::CheckItem(HMENU Menu, int Item, int ParamNum, int Index)
{
intptr_t funcresult = PlugHost->Dispatcher(HostTag, FHD_GetParamMenuFlags, ParamNum, Index);
unsigned int check = 0;
#ifndef __APPLE__
if ((funcresult & FHP_Checked) != 0) check = MF_CHECKED;
else check = MF_UNCHECKED;
CheckMenuItem(Menu, Item, MF_BYPOSITION | check);
if ((funcresult & FHP_Disabled) == 0) check = MF_ENABLED;
else check = MF_DISABLED;
EnableMenuItem(Menu, Item, MF_BYPOSITION | check);
#endif
}
// adjust a common param popup (VCL version)
#ifdef __BORLANDC__
void TCPPFruityPlug::AdjustParamPopup(TMenuItem *Item, int ParamNum, int FirstItemIndex)
{
if (!Item)
return;
// delete the old entries
int count = Item->Count;
while (count > FirstItemIndex) {
Item->Delete(count-1);
count--;
}
// add (append) new ones
int n = 0;
unsigned int flags;
unsigned int id;
PParamMenuEntry MenuEntry = 0;
TMenuItem *NewItem;
PopupParam = ParamNum;
do {
// get menu entry
MenuEntry = (PParamMenuEntry)(PlugHost->Dispatcher(HostTag, FHD_GetParamMenuEntry, ParamNum, n));
if (MenuEntry) {
// create, fill & add item
NewItem = new TMenuItem(Item);
NewItem->Caption = MenuEntry->Name;
NewItem->Tag = n;
NewItem->Checked = (MenuEntry->Flags & FHP_Checked);
NewItem->Enabled = !(MenuEntry->Flags & FHP_Disabled);
NewItem->OnClick = MenuClick;
Item->Add(NewItem);
n++;
}
}
while (MenuEntry);
}
#endif
// adjust a common param popup (Windows API version)
void TCPPFruityPlug::AdjustParamPopup(HMENU Item, int ParamNum, int FirstItemIndex, int FirstID)
{
#ifndef __APPLE__
if (!Item)
return;
// delete the old entries
int count = GetMenuItemCount(Item);
while (count > FirstItemIndex) {
DeleteMenu(Item, count-1, MF_BYPOSITION);
count--;
}
// add (append) new ones
int n = 0;
unsigned int flags;
unsigned int id;
PParamMenuEntry MenuEntry = 0;
do {
// get menu entry
MenuEntry = (PParamMenuEntry)(PlugHost->Dispatcher(HostTag, FHD_GetParamMenuEntry, ParamNum, n));
if (MenuEntry) {
// create, fill & add item
flags = MF_STRING;
if (MenuEntry->Flags & FHP_Disabled)
flags = flags | MF_GRAYED;
else
flags = flags | MF_ENABLED;
if (MenuEntry->Flags & FHP_Checked)
flags = flags | MF_CHECKED;
else
flags = flags | MF_UNCHECKED;
if (strcmp(MenuEntry->Name, "-") == 0)
flags = flags | MF_SEPARATOR;
id = FirstID + n;
AppendMenuA(Item, flags, id, MenuEntry->Name);
n++;
}
}
while (MenuEntry);
#endif
}
void _stdcall TCPPFruityPlug::NewTick()
{
}
intptr_t _stdcall TCPPFruityPlug::Dispatcher(intptr_t ID, intptr_t Index, intptr_t Value)
{
intptr_t v = 0;
switch (ID)
{
case FPD_UseIncreasedMIDIResolution: v = 1;
}
return v;
}
void _stdcall TCPPFruityPlug::Eff_Render(PWAV32FS SourceBuffer, PWAV32FS DestBuffer, int Length)
{
}
void _stdcall TCPPFruityPlug::Gen_Render(PWAV32FS DestBuffer, int &Length)
{
}
void _stdcall TCPPFruityPlug::GetName(int Section, int Index, int Value, char *Name)
{
}
void _stdcall TCPPFruityPlug::Idle_Public()
{
}
int _stdcall TCPPFruityPlug::ProcessParam(int Index, int Value, int RECFlags)
{
return Value;
}
void _stdcall TCPPFruityPlug::SaveRestoreState(IStream *Stream, BOOL Save)
{
}
TVoiceHandle _stdcall TCPPFruityPlug::TriggerVoice(PVoiceParams VoiceParams, intptr_t SetTag)
{
return 0;
}
void _stdcall TCPPFruityPlug::Voice_Kill(TVoiceHandle Handle)
{
}
int _stdcall TCPPFruityPlug::Voice_ProcessEvent(TVoiceHandle Handle, intptr_t EventID, intptr_t EventValue, intptr_t Flags)
{
return 0;
}
void _stdcall TCPPFruityPlug::Voice_Release(TVoiceHandle Handle)
{
}
int _stdcall TCPPFruityPlug::Voice_Render(TVoiceHandle Handle, PWAV32FS DestBuffer, int &Length)
{
return 0;
}
int _stdcall TCPPFruityPlug::ProcessEvent(int EventID, int EventValue, int Flags)
{
return 0;
}
void _stdcall TCPPFruityPlug::MIDITick()
{
}
void _stdcall TCPPFruityPlug::MIDIIn(int &Msg)
{
}
#ifdef __BORLANDC__
void __fastcall TCPPFruityPlug::MenuClick(TObject *Sender)
{
if (PopupParam >= 0 && Sender) {
TMenuItem *item = (TMenuItem *)Sender;
PlugHost->Dispatcher(HostTag, FHD_ParamMenu, PopupParam, item->Tag);
PopupParam = -1;
}
}
#endif
void _stdcall TCPPFruityPlug::MsgIn(intptr_t Msg)
{
}
// translate a controller value (0..FromMIDI_Max)
int TranslateMidi(int Value, int Min, int Max)
{
return Min + (int)(Value * FromMIDI_Div * (Max - Min));
}
const int HintPBMax = 20; // 20 steps for the hint progress bar
const int HintPBFirst = 'a';
const int HintPBLast = HintPBFirst+HintPBMax;
void TCPPFruityPlug::ShowHintMsg_Gauge(const char* Msg , int Value, int Max)
{
// limit value to 0..Max
if (Value < 0) Value = 0;
if (Value > Max) Value = Max;
if (Max < 1)
Max = 1;
char s[1024];
int v = HintPBFirst;
v = v + (Value * (HintPBMax+1) / Max);
if (v > HintPBLast)
v = HintPBLast;
#ifdef __APPLE__
snprintf(s, 1024, "^.%d%s", v, Msg);
#else
sprintf_s(s, 1024, "^.%d%s", v, Msg);
#endif
ShowHintMsg_Direct(s);
}
void TCPPFruityPlug::Idle()
{
}