-
Notifications
You must be signed in to change notification settings - Fork 3
/
UClapInstrument.pas
338 lines (291 loc) · 9.51 KB
/
UClapInstrument.pas
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
unit UClapInstrument;
interface
uses UClapInstrumentBase,UClapBridge,Classes,UClapBase,Forms;
type //TClapInstrument = class;
TClapParameter = record
id,steps:integer;
title,shorttitle,units:string;
min,max,defVal,value:double;
automate,isProgram,dirty,sendToHost:boolean; // visible in Host ?
end;
TClapParameterArray = TArray<TClapParameter>;
// TClapInstrument extends TClapInstrumentBase with parameters, midi handling, programs (not yet as the are not implemented in Clap)
// and makes it compatible with my TClapInstrument interface which unifies several plugin technologies to a simple framework
TClapInstrument = class(TClapInstrumentBase)
private
Fparameters:TClapParameterArray;
procedure OnInit;override;
procedure OnExit;override;
function GetParamInfo(parm_index: integer; var min, max, def: double; var id: integer; var name: string): boolean;override;
function ParamCount: integer;override;
function ParamGetValue(id: integer): double;override;
procedure ParamSetValue(id:integer;value:double);override;
function ParamGetText(id:integer;value:double):string;override;
function paramIdToIndex(id:integer):integer;
procedure SetSampleRate(samplerate:double);override;
procedure OnTimer;override;
function SaveToStream(proc:TonSaveToStream):boolean;override;
function LoadFromStream(proc:TonLoadFromStream):boolean;override;
procedure OnSysExEvent(p:pointer;size:integer); overload;override;
procedure GetHostParameterChanges(proc:TParameterChangesProcedure);override;final;
procedure GuiSetVisible(visible: boolean);override;
protected
///////////////////////// From ClapInstrumentBase ////////////////////////////////////////
function EditorForm:TForm;
/////////////////// To ClapInstrument ///////////////////////////////////////////////////////////////////
procedure OnSysExEvent(s:string);overload;
procedure OnMidiEvent(byte0, byte1, byte2: integer);virtual;
// TODO not implemented procedure MidiOut(byte0, byte1, byte2: integer);
// DONE implemented in ClapInstrumentBase procedure Process32(startsample,samples,channels:integer;inputs,outputs:PPsingle);virtual;
procedure OnSampleRateChanged(samplerate:double);virtual;
// ONREQUEST not implemented procedure OnPlayStateChanged(playing:boolean;ppq:integer);
// ONREQUEST not implemented procedure OnTempoChanged(tempo:single);
procedure UpdateProcessorParameter(id:integer;value:double);virtual;
procedure OnInitialize;virtual;
procedure AddParameter(id:integer;title,shorttitle,units:string;min,max,val:double;automate:boolean=true;steps:integer=0;ProgramChange:boolean=false);virtual;
// DONE function getParameterAsString ALIAS ParamGetText
procedure UpdateHostParameter(id:integer;value:double);
procedure UpdateEditorParameter(id:integer;value:double);virtual;
procedure ResendParameters;
// DONE implemented in ClapInstrumentBase getEditorClass:TFormClass
procedure OnEditOpen;virtual;
procedure OnEditClose;virtual;
// TODO: Wait for Clap to implement Presets
procedure OnProgramChange(prgm:integer);virtual;
procedure OnEditIdle;virtual;
procedure OnFinalize;virtual;
end;
implementation
uses SysUtils;
procedure TClapInstrument.OnTimer;
begin
// update dirty parameters to ui
if EditorForm=NIL then exit;
VAR count:=length(Fparameters);
for VAR i:=0 to count-1 do if Fparameters[i].dirty then
begin
VAR id:=Fparameters[i].id;
VAR value:=Fparameters[i].value;
UpdateEditorParameter(id,value);
Fparameters[i].dirty:=false;
end;
end;
procedure TClapInstrument.ParamSetValue(id:integer;value:double);
begin
VAR index:=paramIdToIndex(id);
if index=-1 then exit;
FParameters[index].value:=value;
FParameters[index].dirty:=true;
// See Document OnAutomateUpdateParameter
// if FeditorForm<>NIL then
// UpdateEditorParameter(FParameters[index].id,value);
updateProcessorParameter(FParameters[index].id,value);
end;
procedure TClapInstrument.AddParameter(id: integer; title, shorttitle, units: string; min, max, val: double; automate: boolean; steps: integer; ProgramChange: boolean);
VAR n:integer;
params:TClapParameter;
begin
params.id:=id;
params.title:=title;
params.shorttitle:=shorttitle;
params.units:=units;
params.min:=min;
params.max:=max;
if (max<=min) then params.max:=params.min+1;
if (val<params.min) then val:=params.min;
if (val>params.max) then val:=params.max;
val:=(val-min)/(max-min);
params.defval:=val;
params.value:=val;
params.automate:=automate;
params.steps:=steps;
params.isProgram:=ProgramChange;
n:=Length(Fparameters);
SetLength(Fparameters,n+1);
FParameters[n]:=params;
end;
function TClapInstrument.EditorForm: TForm;
begin
result:=FGUI;
end;
procedure TClapInstrument.OnEditClose;
begin
// virtual
end;
procedure TClapInstrument.OnEditIdle;
begin
// virtual
end;
procedure TClapInstrument.OnEditOpen;
begin
// virtual
end;
procedure TClapInstrument.OnExit;
begin
OnFinalize;
end;
procedure TClapInstrument.OnFinalize;
begin
// virtual
end;
function TClapInstrument.ParamCount: integer;
begin
result:=length(FParameters);
end;
function TClapInstrument.ParamGetText(id: integer; value: double): string;
begin
VAR index:=paramIdToIndex(id);
if index=-1 then exit('');
VAR range:=FParameters[index].max-FParameters[index].min;
value:=FParameters[index].min+value*range;
VAR s:string;
if (range<2) and (value<10) then
s:=Copy(floattostr(value),1,4)
else
s:=inttostr(round(value));
result:=s+' '+FParameters[index].units;
end;
function TClapInstrument.ParamGetValue(id: integer): double;
begin
VAR index:=paramIdToIndex(id);
if index=-1 then exit(0);
result:=FParameters[index].value;
end;
function TClapInstrument.paramIdToIndex(id: integer): integer;
begin
for VAR i:=0 to ParamCount-1 do
if Fparameters[i].id=id then exit(i);
result:=-1;
end;
procedure TClapInstrument.ResendParameters;
begin
for VAR i:=0 to ParamCount-1 do
Fparameters[i].dirty := true;
end;
// just a quick implementation withou further units/helpers
const MAGIC_VERSION = 20221119;
function TClapInstrument.SaveToStream(proc: TonSaveToStream): boolean;
procedure SaveSlToStream(sl:TstringList);
begin
VAR p:pbyte;
VAR s:=sl.text;
VAR len:=length(s);
GetMem(p,len);
for var i:=0 to len-1 do p[i]:=ord(s[i+1]);
proc(p,len);
FreeMem(p);
end;
VAR pc:PByte;
begin
VAR sl:=TStringList.Create;
sl.Add(inttostr(MAGIC_VERSION));
for VAR i:=0 to ParamCount-1 do
sl.add(inttostr(Fparameters[i].id)+';'+inttostr(round(Fparameters[i].value*127)));
SaveSlToStream(sl);
result:=true;
sl.Free;
end;
procedure TClapInstrument.SetSampleRate(samplerate: double);
begin
OnSampleRateChanged(samplerate);
end;
// just a quick implementation without further units/helpers
function TClapInstrument.LoadFromStream(proc: TonLoadFromStream): boolean;
procedure LoadSlFromStream(sl:TstringList);
begin
VAR p:pbyte;
VAR size:=40*length(Fparameters);
GetMem(p,size);
size:=proc(p,size);
VAR s:='';
for var i:=0 to size-1 do s:=s+chr(p[i]);
FreeMem(p);
sl.Text:=s;
end;
begin
VAR sl:=TStringList.Create;
LoadSlFromStream(sl);
if (sl.Count>0) and (strtointdef(sl[0],0)=MAGIC_VERSION) then
for VAR i:=1 to sl.count-1 do
begin
VAR s:=sl[i];
VAR p:=pos(';',s);
VAR id:=StrToIntDef(Copy(s,1,p-1),-1);
VAR val:=StrToIntDef(Copy(s,p+1),0);
ParamSetValue(id,val/127);
end;
sl.Free;
end;
procedure TClapInstrument.UpdateEditorParameter(id: integer; value: double);
begin
// virtual !
end;
procedure TClapInstrument.UpdateHostParameter(id: integer; value: double);
begin
ParamSetValue(id,value);
VAR index:=paramIdToIndex(id);
if index=-1 then exit;
Fparameters[index].sendToHost:=true;
Fparameters[index].dirty:=true;
Fparameters[index].value:=value;
updateProcessorParameter(FParameters[index].id,value);
end;
procedure TClapInstrument.UpdateProcessorParameter(id: integer; value: double);
begin
// virtual !
end;
procedure TClapInstrument.GetHostParameterChanges( proc: TParameterChangesProcedure);
begin
for var i:=0 to length(FParameters)-1 do
if FParameters[i].sendToHost then
begin
FParameters[i].sendToHost:=false;
proc(FParameters[i].id,FParameters[i].value);
end;
end;
function TClapInstrument.GetParamInfo(parm_index: integer; var min, max, def: double; var id: integer;VAR name:string): boolean;
begin
if parm_index>=paramCount then exit(false);
id:=FParameters[parm_index].id;
min:=0;//
max:=1;//FParameters[parm_index].max;
def:=(FParameters[parm_index].defVal-FParameters[parm_index].min) / (FParameters[parm_index].max-FParameters[parm_index].min);
result:=true;
name:=FParameters[parm_index].title;
end;
procedure TClapInstrument.GuiSetVisible(visible: boolean);
begin
inherited;
if visible then OnEditOpen;
end;
procedure TClapInstrument.OnInit;
begin
OnInitialize;
end;
procedure TClapInstrument.OnInitialize;
begin
end;
procedure TClapInstrument.OnMidiEvent(byte0, byte1, byte2: integer);
begin
end;
procedure TClapInstrument.OnProgramChange(prgm: integer);
begin
end;
procedure TClapInstrument.OnSampleRateChanged(samplerate: double);
begin
// virtual
end;
procedure TClapInstrument.OnSysExEvent(s: string);
begin
end;
{$POINTERMATH ON}
procedure TClapInstrument.OnSysExEvent(p: pointer; size: integer);
VAR pp:^char;
begin
pp:=p;
VAR s:='';
for VAR i:=0 TO size-1 do
s:=s+pp[i];
OnSysExEvent(s);
end;
end.