File: LoopBuf.cpp

package info (click to toggle)
supercollider-sc3-plugins 3.7.1~repack-2
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 14,332 kB
  • ctags: 11,704
  • sloc: cpp: 140,180; lisp: 14,746; ansic: 2,133; xml: 86; makefile: 82; haskell: 21; sh: 8
file content (514 lines) | stat: -rw-r--r-- 11,567 bytes parent folder | download
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
/*
 *  LoopBuf.cpp
 *  xSC3plugins
 *
 *  Created by Lance Putnam on Sun Jun 27 2004.
 *  Copyright (c) 2004 __MyCompanyName__. All rights reserved.
 *
 */

#include "SC_PlugIn.h"

static InterfaceTable *ft;

struct LoopBuf : public Unit
{
	double m_phase;
	float m_prevgate;
	float m_fbufnum;
	bool m_playThrough;
	SndBuf *m_buf;
};

struct FLoopBuf : public Unit
{
	uint32 m_phase;
	float m_prevgate;
	float m_fbufnum;
	bool m_playThrough;
	SndBuf *m_buf;
};

extern "C"
{
	void load(InterfaceTable *inTable);

//	void LoopBuf_next_aa(LoopBuf *unit, int inNumSamples);
//	void LoopBuf_next_ak(LoopBuf *unit, int inNumSamples);
//	void LoopBuf_next_ka(LoopBuf *unit, int inNumSamples);
	void LoopBuf_next_kk(LoopBuf *unit, int inNumSamples);
	void LoopBuf_Ctor(LoopBuf* unit);

	void FLoopBuf_next(FLoopBuf *unit, int inNumSamples);
	void FLoopBuf_Ctor(FLoopBuf* unit);
}

//////////////////////////////////////////////////////////////////////////////////////////////////
/*
static float cubicinterp(float x, float y0, float y1, float y2, float y3)
{
	// 4-point, 3rd-order Hermite (x-form)
	// 8 mul, 11 add
	float c0 = y1;
	float c1 = 0.5f * (y2 - y0);
	float c2 = y0 - (2.5f * y1) + y2 + y2 - 0.5f * y3;
	float c3 = 0.5f * (y3 - y0) + 1.5f * (y1 - y2);

	return ((c3 * x + c2) * x + c1) * x + c0;

//	float result = (0.5f * (y3 - y0) + 1.5f * (y1 - y2)) * x;
//	result = (result + (y0 - (2.5f * y1) + y2 + y2 - 0.5f * y3)) * x;
//	result = (result + ( 0.5f * (y2 - y0) )) * x;
//
//	return result + y1;

//	float hy0 = 0.5f * y0;
//	float hy1 = 0.5f * y1;
//	float hy2 = 0.5f * y2;
//	float hy3 = 0.5f * y3;
//	float c1 = hy2 - hy0;
//	float c2 = y0 + y2 + y2 - y1 - y1 - hy1 - hy3;
//	float c3 = hy3 - hy0 + y1 + hy1 - y2 - hy2;
//
//	return ((c3 * x + c2) * x + c1) * x + y1;

	// 4 mul, 15 add
	//float ty0 = y0 + y0;
//	float ty1 = y1 + y1;
//	float ty2 = y2 + y2;
//	float ty2mty1 = ty2 - ty1;
//	float y1py3 = y1 + y3;
//	//float c1 = y2 - y0;
//	//float c2 = y0 + y0 + ty2mty1 + ty2mty1 - y1py3;
//	//float c3 = y1py3 - y0 - y2 - ty2mty1;
//
//	//return 0.5f * (((c3 * x + c2) * x + c1) * x + ty1);
//	return 0.5f * ((( (y1py3 - y0 - y2 - ty2mty1) * x + (y0 + y0 + ty2mty1 + ty2mty1 - y1py3) ) * x + (y2 - y0) ) * x + ty1);

	// 6 mul, 9 add
//	float y0my3 = y0 - y3;
//	float y1my2 = y1 - y2;
//	float y0my2 = y0 - y2;
//	// -y0 + 3*y1 - 3*y2 + y3
//	float result = (3.f*y1my2 - y0my3) * x;
//
//	// 2*y0 - 5*y1 + 4*y2 - y3
//	result = (result + -5.f*y1my2 + y0my3 + y0my2) * x;
//
//	result = (result - y0my2) * x;
//	return (0.5f * result) + y1;

}
*/
//////////////////////////////////////////////////////////////////////////////////////////////////

//inline double sc_loop(Unit *unit, double in, double hi, int loop)
//{
//	// avoid the divide if possible
//	if (in >= hi) {			// forward playback
//		if (!loop) {
//			unit->mDone = true;
//			return hi;
//		}
//		in -= hi;
//		if (in < hi) return in;
//	} else if (in < 0.) {   // reverse playback
//		if (!loop) {
//			unit->mDone = true;
//			return 0.;
//		}
//		in += hi;
//		if (in >= 0.) return in;
//	} else return in;
//
//	return in - hi * floor(in/hi);
//}

// intel asm
//inline int truncate(float flt)
//{
//	int i;
//	_asm
//	{
//	fld flt
//	fistp i
//	}
//	return i
//}

#define CHECK_LOOP \
	if(endLoop >= fbufFrames)\
		endLoop = fbufFrames - 1.f;\
	else if(endLoop < 0.f)\
		endLoop = 0.f;\
	if(startLoop < 0.f)\
		startLoop = 0.f;\
	else if(startLoop >= fbufFrames)\
		startLoop = fbufFrames - 1.f;\
	if(startLoop > endLoop){\
		float temp = endLoop;\
		endLoop = startLoop;\
		startLoop = temp;\
	}\
	float loopFrames = endLoop - startLoop;\
	if(loopFrames < 1.f){\
		loopFrames = 1.f;\
		if(endLoop >= fbufFrames - 1.f)\
			startLoop = endLoop - 1.f;\
		else endLoop = startLoop + 1.f;\
	}

#define CHECK_GATE\
	bool gateIsPos = gate > 0.f;\
	bool gateWasNeg = unit->m_prevgate <= 0.f;\
	if (gateIsPos && gateWasNeg) {\
		unit->mDone = false;\
		unit->m_playThrough = false;\
		phase = ZIN0(3);\
	}\
	else if (!(gateIsPos || gateWasNeg))\
		unit->m_playThrough = true;\
	unit->m_prevgate = gate;\

#define WRAP_PHASE_FWD \
	if(phase > endLoop){\
		if(unit->m_playThrough){\
			unit->mDone = true;\
			phase = endLoop;\
		}\
		else{\
			phase -= loopFrames;\
			if(phase > endLoop){\
				phase -= loopFrames * floorf((phase-startLoop)/loopFrames);\
			}\
		}\
	}\
	int32 iphase = (int32)phase;

#define WRAP_PHASE_BWD \
	if(phase < startLoop){\
		if(unit->m_playThrough){\
			unit->mDone = true;\
			phase = startLoop;\
		}\
		else{\
			phase += loopFrames;\
			if(phase < startLoop){\
				phase += loopFrames * floorf((endLoop-phase)/loopFrames);\
			}\
		}\
	}\
	int32 iphase = (int32)phase;


#define CHECK_BUF \
	if (!bufData) { \
                unit->mDone = true; \
		ClearUnitOutputs(unit, inNumSamples); \
		return; \
	}

#define SETUP_OUT \
	uint32 numOutputs = unit->mNumOutputs; \
	if (numOutputs > bufChannels) { \
                unit->mDone = true; \
		ClearUnitOutputs(unit, inNumSamples); \
		return; \
	} \
	float *out[16]; \
	for (uint32 i=0; i<numOutputs; ++i) out[i] = ZOUT(i);

#define LOOP_BODY_4 \
		float* table1 = bufData + iphase * bufChannels; \
		float* table0 = table1 - bufChannels; \
		float* table2 = table1 + bufChannels; \
		float* table3 = table2 + bufChannels; \
		if (iphase == 0) { \
			table0 += bufSamples; \
		} else if (iphase >= guardFrame) { \
			if (iphase == guardFrame) { \
				table3 -= bufSamples; \
			} else { \
				table2 -= bufSamples; \
				table3 -= bufSamples; \
			} \
		} \
		int32 index = 0; \
		float fracphase = phase - (double)iphase; \
		for (uint32 i=0; i<numOutputs; ++i) { \
			float a = table0[index]; \
			float b = table1[index]; \
			float c = table2[index]; \
			float d = table3[index]; \
			*++(out[i]) = cubicinterp(fracphase, a, b, c, d); \
			index++; \
		}\
		phase += rate;

#define LOOP_BODY_2 \
		float* table1 = bufData + iphase * bufChannels; \
		float* table2 = table1 + bufChannels; \
		if (iphase > guardFrame) { \
			table2 -= bufSamples; \
		} \
		int32 index = 0; \
		float fracphase = phase - (double)iphase; \
		for (uint32 i=0; i<numOutputs; ++i) { \
			float b = table1[index]; \
			float c = table2[index]; \
			*++(out[i]) = b + fracphase * (c - b); \
			index++; \
		}\
		phase += rate;

#define LOOP_BODY_1 \
		float* table1 = bufData + iphase * bufChannels; \
		int32 index = 0; \
		for (uint32 i=0; i<numOutputs; ++i) { \
			*++(out[i]) = table1[index++]; \
		}\
		phase += rate;

void LoopBuf_Ctor(LoopBuf *unit)
{
// input 1 => rate
// input 2 => gate
//	if (INRATE(1) == calc_FullRate) {
//		if (INRATE(2) == calc_FullRate) {
//			SETCALC(LoopBuf_next_aa);
//		} else {
//			SETCALC(LoopBuf_next_ak);
//		}
//	} else {
//		if (INRATE(2) == calc_FullRate) {
//			SETCALC(LoopBuf_next_ka);
//		} else {
			SETCALC(LoopBuf_next_kk);
//		}
//	}

	unit->m_fbufnum = -1e9f;
	unit->m_prevgate = 0.;
	unit->m_phase = ZIN0(3);
	unit->m_playThrough = false;

	ClearUnitOutputs(unit, 1);
}

void LoopBuf_next_kk(LoopBuf *unit, int inNumSamples)
{
	float rate		= ZIN0(1);
	float gate		= ZIN0(2);
	float startLoop = ZIN0(4);
	float endLoop   = ZIN0(5);
	int32 ipol		= (int32)ZIN0(6);

	double phase = unit->m_phase;
	bool isPlayingForward = rate >= 0.f;

	GET_BUF
	CHECK_BUF
	SETUP_OUT

	float fbufFrames = (float)bufFrames;

	CHECK_LOOP
	CHECK_GATE

	if(isPlayingForward){
		if( unit->m_playThrough )
			endLoop = fbufFrames;

		switch(ipol){
		case 2:
			for (int i=0; i<inNumSamples; ++i) {
				WRAP_PHASE_FWD
				LOOP_BODY_2
			}
			break;
		case 4:
			for (int i=0; i<inNumSamples; ++i) {
				WRAP_PHASE_FWD
				LOOP_BODY_4
			}
			break;
		default:
			for (int i=0; i<inNumSamples; ++i) {
				WRAP_PHASE_FWD
				LOOP_BODY_1
			}
			break;
		};
	}
	else {
		if( unit->m_playThrough )
			startLoop = 0.f;

		switch(ipol){
		case 2:
			for (int i=0; i<inNumSamples; ++i) {
				WRAP_PHASE_BWD
				LOOP_BODY_2
			}
			break;
		case 4:
			for (int i=0; i<inNumSamples; ++i) {
				WRAP_PHASE_BWD
				LOOP_BODY_4
			}
			break;
		default:
			for (int i=0; i<inNumSamples; ++i) {
				WRAP_PHASE_BWD
				LOOP_BODY_1
			}
			break;
		};
	}
	unit->m_phase = phase;
}

void FLoopBuf_Ctor(FLoopBuf *unit)
{
	SETCALC(FLoopBuf_next);

	unit->m_fbufnum = -1e9f;
	unit->m_prevgate = 0.;
	unit->m_phase = (uint32)ZIN0(3);
	unit->m_playThrough = false;

	ClearUnitOutputs(unit, 1);
}

void FLoopBuf_next(FLoopBuf *unit, int inNumSamples)
{
	float rate		= ZIN0(1);
	float gate		= ZIN0(2);
	//int32 ipol		= (int32)ZIN0(6);

	uint32 phase = unit->m_phase;
	//bool isPlayingForward = rate >= 0.f;

	GET_BUF
	CHECK_BUF
	SETUP_OUT

	uint32 intsPerFrame = 4294967295UL/bufFrames;
	double fintsPerFrame = (double)intsPerFrame;
	double framesPerInt = 1.f / fintsPerFrame;
	uint32 phaseInc = (uint32)(fintsPerFrame * rate);
	uint32 startLoop = (uint32)ZIN0(4) * intsPerFrame;
	uint32 endLoop   = (uint32)ZIN0(5) * intsPerFrame;

	// CHECK_LOOP
	if(startLoop > endLoop){	// ensure startLoop is always less than endLoop
		uint32 temp = endLoop;
		endLoop = startLoop;
		startLoop = temp;
	}

	uint32 loopFrames = endLoop - startLoop;

	if(loopFrames == 0){   // the loop needs to be at least 1 frame
		loopFrames = intsPerFrame;
		if(endLoop >= 4294967295UL - intsPerFrame)
			startLoop = endLoop - intsPerFrame;
		else endLoop = startLoop + intsPerFrame;
	}
	// END CHECK_LOOP

	// CHECK_GATE
	bool gateIsPos = gate > 0.f;
	bool gateWasNeg = unit->m_prevgate <= 0.f;
	if (gateIsPos && gateWasNeg) {			// gate +zc
		unit->mDone = false;
		unit->m_playThrough = false;
		phase = (uint32)ZIN0(3) * intsPerFrame;
	}
	else if (!(gateIsPos || gateWasNeg))	// gate -zc
		unit->m_playThrough = true;
	// END CHECK_GATE

	unit->m_prevgate = gate;

//	if(isPlayingForward){
		if( unit->m_playThrough )
			endLoop = 4294967295UL;

		for (int i=0; i<inNumSamples; ++i) {
			//IWRAP_PHASE_FWD
			if(phase > endLoop){
				if(unit->m_playThrough){
					unit->mDone = true;
					phase = endLoop;
				}
				else{
					phase -= loopFrames;
					if(phase > endLoop){
						phase -= loopFrames * (phase-startLoop)/loopFrames;
					}
				}
			}

			//ILOOP_BODY_1
//			uint32 iphase = phase / intsPerFrame;
//			float* table1 = bufData + iphase * bufChannels;
//			int32 index = 0;
//			for (uint32 i=0; i<numOutputs; ++i) {
//				*++(out[i]) = table1[index++];
//			}
//			phase += phaseInc;

			//ILOOP_BODY_2
			uint32 bufIndex = phase / intsPerFrame;
			float* table1 = bufData + bufIndex * bufChannels;
			float* table2 = table1 + bufChannels;
			if (bufIndex > guardFrame) {
				table2 -= bufSamples;
			}
			int32 index = 0;
			float fracphase = (double)(phase-(bufIndex*intsPerFrame)) * framesPerInt;
			//float fracphase = (double)(phase % intsPerFrame) * framesPerInt;
			for (uint32 i=0; i<numOutputs; ++i) {
				float b = table1[index];
				float c = table2[index];
				*++(out[i]) = b + fracphase * (c - b);
				index++;
			}
			phase += phaseInc;

//		float* table1 = bufData + iphase * bufChannels;
//		float* table2 = table1 + bufChannels;
//		if (iphase > guardFrame) {
//			table2 -= bufSamples;
//		}
//		int32 index = 0;
//		float fracphase = phase - (double)iphase;
//		for (uint32 i=0; i<numOutputs; ++i) {
//			float b = table1[index];
//			float c = table2[index];
//			*++(out[i]) = b + fracphase * (c - b);
//			index++;
//		}
//		phase += rate;
		}

//	}
//
//	else {
//		if( unit->m_playThrough )
//			startLoop = 0;
//
//		IWRAP_PHASE_BWD
//		ILOOP_BODY_1
//		phase += phaseInc;
//	}
	unit->m_phase = phase;
}

PluginLoad(LoopBuf)
{
	ft = inTable;
	DefineSimpleUnit(LoopBuf);
	DefineSimpleUnit(FLoopBuf);
}