forked from grzegorz-kraszewski/libdigibooster3
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dsp_wavetable.c
391 lines (311 loc) · 11.6 KB
/
dsp_wavetable.c
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
/*-----------------*/
/* libdigibooster3 */
/*-----------------*/
/*
Copyright (c) 2014, Grzegorz Kraszewski
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
1. Redistributions of source code must retain the above copyright notice, this
list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.
This software is provided by the copyright holders and contributors "as is" and
any express or implied warranties, including, but not limited to the implied
warranties of merchantability and fitness for a particular purpose are
disclaimed. In no event shall the copyright owner or contributors be liable for
any direct, indirect, incidental, special, exemplary, or consequential damages
(including, but not limited to, procurement of substitute goods or services;
loss of use, data, or profits; or business interruption) however caused and
on any theory of liability, whether in contract strict liability or tort
(including negligence or otherwise) arising in any way out of the use of this
software, even if advised of the possibility of such damage.
*/
/* Wavetable source. Includes loop unroller. */
#include "libdigibooster3.h"
#include "dsp.h"
// This structure defines one loop point.
struct TPoint
{
int32_t Position; // ordinal number of the first sample after the turnpoint
int32_t JumpOffset; // offset of executed jump
int ActDir; // direction which triggers the turnpoint
int ResDir; // direction after turnpoint execution
int Counter; // activation counter, trurnpoint disabled if 0
};
// Main wavetable DSP object structure.
struct SampledInstrument
{
struct DSPObject object;
int16_t *AudioData;
int32_t AudioLength;
int32_t CurPos; // current position, the first sample to be sent in the next chunk
int32_t LoopFirst; // the first sample in loop
int32_t LoopLast; // the last sample in loop
int CurDir; // current direction of unrolling
int LoopType; // type of loop
int BackwardsPlay; // true if instrument has been triggered with E3x
struct TPoint *Tp1;
struct TPoint *Tp2;
struct TPoint TpA;
struct TPoint TpB;
};
#define TPDIR_FWD 1
#define TPDIR_REV 2
#define is_in_range_fwd(pos, start, len) (((pos) >= (start)) && ((pos) < (start) + (len)))
#define is_in_range_rev(pos, start, len) (((pos) > (start) - (len)) && ((pos) <= (start)))
//==============================================================================================
// dsp_sampled_instr_regenerate()
//==============================================================================================
void dsp_sampled_instr_regenerate(struct SampledInstrument *smi)
{
if (smi->LoopType == IF_NO_LOOP)
{
smi->Tp1 = NULL;
smi->Tp2 = NULL;
}
else if (smi->LoopType == IF_FORWARD_LOOP)
{
if (smi->BackwardsPlay)
{
smi->TpA.Position = smi->LoopFirst;
smi->TpA.ActDir = TPDIR_REV;
smi->TpA.ResDir = TPDIR_REV;
smi->TpA.JumpOffset = smi->LoopLast - smi->LoopFirst + 1;
smi->TpA.Counter = 0x7FFFFFFF;
smi->Tp1 = &smi->TpA;
smi->Tp2 = NULL;
}
else
{
smi->TpA.Position = smi->LoopLast + 1;
smi->TpA.ActDir = TPDIR_FWD;
smi->TpA.ResDir = TPDIR_FWD;
smi->TpA.JumpOffset = smi->LoopFirst - smi->LoopLast - 1;
smi->TpA.Counter = 0x7FFFFFFF;
smi->Tp1 = &smi->TpA;
smi->Tp2 = NULL;
}
}
else // IF_PINGPONG_LOOP
{
smi->TpA.Position = smi->LoopFirst;
smi->TpA.ActDir = TPDIR_REV;
smi->TpA.ResDir = TPDIR_FWD;
smi->TpA.JumpOffset = 0;
smi->TpA.Counter = 0x7FFFFFFF;
smi->Tp1 = &smi->TpA;
smi->TpB.Position = smi->LoopLast + 1;
smi->TpB.ActDir = TPDIR_FWD;
smi->TpB.ResDir = TPDIR_REV;
smi->TpB.JumpOffset = 0;
smi->TpB.Counter = 0x7FFFFFFF;
smi->Tp2 = &smi->TpB;
}
}
//==============================================================================================
// dsp_sampled_instr_set()
//==============================================================================================
void dsp_sampled_instr_set(struct DSPObject *obj, struct DSPTag *tags)
{
struct SampledInstrument *smi = (struct SampledInstrument*)obj;
int regenerate = FALSE;
while (tags->dspt_tag)
{
switch (tags->dspt_tag)
{
case DSPA_ReversePlay:
smi->BackwardsPlay = tags->dspt_data;
smi->CurDir = tags->dspt_data ? TPDIR_REV : TPDIR_FWD;
regenerate = TRUE;
break;
case DSPA_LoopFirst:
smi->LoopFirst = tags->dspt_data;
regenerate = TRUE;
break;
case DSPA_LoopLast:
smi->LoopLast = tags->dspt_data;
regenerate = TRUE;
break;
case DSPA_LoopType:
smi->LoopType = tags->dspt_data;
regenerate = TRUE;
break;
case DSPA_SampleOffset:
if (smi->BackwardsPlay) smi->CurPos = smi->AudioLength - tags->dspt_data;
else smi->CurPos = tags->dspt_data;
if (smi->CurPos > smi->AudioLength) smi->CurPos = smi->AudioLength;
else if (smi->CurPos < 0) smi->CurPos = 0;
break;
case DSPA_LimitOffsetToLoop:
if (tags->dspt_data)
{
if (smi->CurPos < smi->LoopFirst) smi->CurPos = smi->LoopFirst;
if (smi->CurPos > smi->LoopLast) smi->CurPos = smi->LoopLast;
}
break;
}
tags++;
}
if (regenerate) dsp_sampled_instr_regenerate(smi);
return;
}
//==============================================================================================
// dsp_sampled_instr_pull()
//==============================================================================================
int dsp_sampled_instr_pull(struct DSPObject *obj, int16_t *dest, int32_t samples)
{
struct SampledInstrument *smi = (struct SampledInstrument*)obj;
int stays_on = TRUE;
int32_t zero_padding = 0;
dest--; // PowerPC optimization, I use ++dest instead of dest++ later.
// The main loop is driven by number of samples requested.
while (samples)
{
int32_t chunk = samples; // number of samples before turnpoint
struct TPoint *execute = NULL; // turnpoint to execute
int16_t *s;
int32_t i;
// Steps taken are different depending on the current unroller direction. The general
// rule is to search for nearest active turnpoint in reqested range of samples, cut
// the request to the turnpoint and copy samples, then execute the turnpoint. The first
// step is to search the turnpoint.
if (smi->CurDir == TPDIR_FWD)
{
// For forward direction the turnpoint must be inside the range and be activated
// with forward direction. Both turnpoints are checked. If the second one is
// nearer, or at the same position as the first, it takes precedence.
if (smi->Tp1)
{
if (is_in_range_fwd(smi->Tp1->Position, smi->CurPos, chunk) && (smi->Tp1->ActDir == TPDIR_FWD))
{
execute = smi->Tp1;
chunk = smi->Tp1->Position - smi->CurPos;
}
}
if (smi->Tp2)
{
if (is_in_range_fwd(smi->Tp2->Position, smi->CurPos, chunk) && (smi->Tp2->ActDir == TPDIR_FWD))
{
execute = smi->Tp2;
chunk = smi->Tp2->Position - smi->CurPos;
}
}
// Check now if the end of instrument data is on the way. If so, the channel should
// be switched off, and request padded with zeros.
zero_padding = smi->CurPos + chunk - smi->AudioLength;
if (zero_padding > 0)
{
stays_on = FALSE;
chunk -= zero_padding;
}
// Trimmed request execution. Audio samples first, zero padding then if needed.
s = &smi->AudioData[smi->CurPos] - 1; // - 1 is for PPC optimization to use ++s below
for (i = 0; i < chunk; i++) { *++dest = *++s; }
for (i = 0; i < zero_padding; i++) { *++dest = 0; }
smi->CurPos += chunk;
}
else
{
// Backward direction. Again the turnpoint must be located inside the range, but
// tre range calculation is slightly different. Both the turnpoints are checked.
// The same as for forward, if the second one is neared the current position, or
// placed at the same position as the first, the second one takes precedence.
if (smi->Tp1)
{
if (is_in_range_rev(smi->Tp1->Position, smi->CurPos, chunk) && (smi->Tp1->ActDir == TPDIR_REV))
{
execute = smi->Tp1;
chunk = smi->CurPos - smi->Tp1->Position;
}
}
if (smi->Tp2)
{
if (is_in_range_rev(smi->Tp2->Position, smi->CurPos, chunk) && (smi->Tp2->ActDir == TPDIR_REV))
{
execute = smi->Tp2;
chunk = smi->CurPos - smi->Tp2->Position;
}
}
// Check now if the start of instrument data is on the way. If so, the channel should
// be switched off, and request padded with zeros.
zero_padding = chunk - smi->CurPos;
if (zero_padding > 0)
{
stays_on = FALSE;
chunk -= zero_padding;
}
// Trimmed request execution. Audio samples first, zero padding then if needed.
s = &smi->AudioData[smi->CurPos];
for (i = 0; i < chunk; i++) { *++dest = *--s; }
for (i = 0; i < zero_padding; i++) { *++dest = 0; }
smi->CurPos -= chunk;
}
samples -= chunk;
if (zero_padding > 0) samples -= zero_padding; // substract only real padding
// Turnpoint execution. It is already checked that activation direction is matched.
if (execute && (execute->Counter > 0))
{
smi->CurPos += execute->JumpOffset;
smi->CurDir = execute->ResDir;
execute->Counter--;
}
}
return stays_on;
}
//==============================================================================================
// dsp_sampled_instr_dispose()
//==============================================================================================
void dsp_sampled_instr_dispose(struct DSPObject *obj)
{
if (obj) db3_free(obj);
}
//==============================================================================================
// dsp_sampled_instr_flush()
//==============================================================================================
void dsp_sampled_instr_flush(UNUSED struct DSPObject *obj0)
{
// struct SampledInstrument *obj = (struct SampledInstrument*)obj0;
return;
}
//==============================================================================================
// dsp_sampled_instr_get()
//==============================================================================================
int dsp_sampled_instr_get(struct DSPObject *obj, uint32_t attr, int32_t *storage)
{
struct SampledInstrument *dsp = (struct SampledInstrument*)obj;
switch (attr)
{
case DSPA_SampleOffset: *storage = dsp->CurPos; return 1;
}
return 0;
}
//==============================================================================================
// dsp_sampled_instr_new()
//==============================================================================================
struct DSPObject *dsp_sampled_instr_new(int16_t *data, int32_t loop_start,
int32_t loop_len, UNUSED int32_t loop_count, int32_t total_frames, int loop_type)
{
struct SampledInstrument *smi;
if ((smi = db3_malloc(sizeof(struct SampledInstrument))) != NULL)
{
smi->object.dsp_type = DSPTYPE_UNROLLER;
smi->object.dsp_pull = dsp_sampled_instr_pull;
smi->object.dsp_dispose = dsp_sampled_instr_dispose;
smi->object.dsp_set = dsp_sampled_instr_set;
smi->object.dsp_get = dsp_sampled_instr_get;
smi->object.dsp_flush = dsp_sampled_instr_flush;
smi->AudioData = data;
smi->AudioLength = total_frames;
smi->BackwardsPlay = FALSE;
smi->LoopFirst = loop_start;
smi->LoopLast = loop_start + loop_len - 1;
smi->LoopType = loop_type;
smi->CurPos = 0;
smi->CurDir = TPDIR_FWD;
dsp_sampled_instr_regenerate(smi);
return &smi->object;
}
return NULL;
}