aboutsummaryrefslogtreecommitdiff
path: root/externals/grill/xsample/source/inter.h
blob: f8a0881baee0253788abfe58e6ba10ecfab56ff1 (plain)
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
/* 
xsample - extended sample objects for Max/MSP and pd (pure data)

Copyright (c) 2001-2006 Thomas Grill (gr@grrrr.org)
For information on usage and redistribution, and for a DISCLAIMER OF ALL
WARRANTIES, see the file, "license.txt," in this distribution.  
*/

#ifndef __INTER_H
#define __INTER_H

TMPLDEF void xinter::st_play0(const t_sample *,const int ,const int ,const int n,const int inchns,const int outchns,t_sample *const *invecs,t_sample *const *outvecs,bool looped)
{
	// stopped/invalid buffer -> output zero
	for(int ci = 0; ci < outchns; ++ci) ZeroSamples(outvecs[ci],n);
}

TMPLDEF void xinter::st_play1(const t_sample *bdt,const int smin,const int smax,const int n,const int inchns,const int outchns,t_sample *const *invecs,t_sample *const *outvecs,bool looped)
{
	SIGCHNS(BCHNS,inchns,OCHNS,outchns);

	// position info are frame units
	const t_sample *pos = invecs[0];
	t_sample *const *sig = outvecs;
	
	// no interpolation
	// ----------------

    if(UNLIKELY(smin == smax)) {
        // zero loop length -> assume that smin is a valid sample position...

        int ci;
        for(ci = 0; ci < OCHNS; ++ci) SetSamples(sig[ci],n,bdt[smin*BCHNS]);
	    // clear rest of output channels (if buffer has less channels)
	    for(; ci < outchns; ++ci) ZeroSamples(sig[ci],n);
    }
    else if(OCHNS == 1) {
        t_sample *sig0 = sig[0];
	    for(int i = 0; i < n; ++i) {	
		    register long oint = CASTINT<long>(*(pos++));

            // for xplay oint can be out of bounds -> check
		    if(LIKELY(oint >= smin))
			    if(LIKELY(oint < smax)) {
				    // normal
				    *(sig0++) = bdt[oint*BCHNS];
			    }
			    else {
				    // position > last sample ... take only last sample
				    *(sig0++) = bdt[(smax-1)*BCHNS];
			    }
		    else {
			    // position < 0 ... take only 0th sample
			    *(sig0++) = bdt[smin*BCHNS];
		    }
	    }
    }
    else {
	    for(int i = 0,si = 0; i < n; ++i,++si) {	
		    register long oint = CASTINT<long>(*(pos++));
		    register const t_sample *fp;

            // for xplay oint can be out of bounds -> check
		    if(LIKELY(oint >= smin))
			    if(LIKELY(oint < smax)) {
				    // normal
				    fp = bdt+oint*BCHNS;
			    }
			    else {
				    // position > last sample ... take only last sample
				    fp = bdt+(smax-1)*BCHNS;
			    }
		    else {
			    // position < 0 ... take only 0th sample
			    fp = bdt+smin*BCHNS;
		    }

            for(int ci = 0; ci < OCHNS; ++ci)
			    sig[ci][si] = fp[ci];
        }

	    // clear rest of output channels (if buffer has less channels)
	    for(int ci = OCHNS; ci < outchns; ++ci) ZeroSamples(sig[ci],n);
    }
}

TMPLDEF void xinter::st_play2(const t_sample *bdt,const int smin,const int smax,const int n,const int inchns,const int outchns,t_sample *const *invecs,t_sample *const *outvecs,bool looped)
{
	const int plen = smax-smin;
	if(UNLIKELY(plen < 2)) {
		st_play1 TMPLCALL (bdt,smin,smax,n,inchns,outchns,invecs,outvecs,looped);
		return;
	}

	SIGCHNS(BCHNS,inchns,OCHNS,outchns);

	// position info are frame units
	const t_sample *pos = invecs[0];
	t_sample *const *sig = outvecs;
	
	// linear interpolation
	// --------------------

	const int maxo = smax-1;  // last sample in buffer

    if(OCHNS == 1) {
        t_sample *sig0 = sig[0];
	    for(int i = 0; i < n; ++i) {	
		    const float o = *(pos++);
		    register long oint = CASTINT<long>(o);
			const float frac = o-oint;
			t_sample fp0,fp1;

		    if(LIKELY(oint >= smin))
			    if(LIKELY(oint < maxo)) {
				    // normal interpolation
			        fp0 = bdt[oint*BCHNS];
			        fp1 = bdt[(oint+1)*BCHNS];
			    }
			    else {
				    // position is past last sample
                    if(looped) {
        				oint = smin+(oint-smin)%plen;
                        fp0 = bdt[oint*BCHNS];
                        fp1 = oint >= maxo?bdt[smin]:fp0;
                    }
                    else
    	                fp0 = fp1 = bdt[maxo*BCHNS]; 
                }
		    else {
			    // position is before first sample
                if(looped) {
        			oint = smax-(smin-oint)%plen;
                    fp0 = bdt[oint*BCHNS]; 
                    fp1 = oint >= maxo?bdt[smin]:fp0;
                }
                else
		            fp0 = fp1 = bdt[smin*BCHNS]; 
		    }

            *(sig0++) = fp0+frac*(fp1-fp0);
	    }
    }
    else {
	    for(int i = 0,si = 0; i < n; ++i,++si) {	
		    const float o = *(pos++);
		    register long oint = CASTINT<long>(o);
			const t_sample *fp0,*fp1;
			const float frac = o-oint;

		    if(LIKELY(oint >= smin))
			    if(LIKELY(oint < maxo)) {
				    // normal interpolation
			        fp0 = bdt+oint*BCHNS;
			        fp1 = fp0+BCHNS;
			    }
			    else {
				    // position is past last sample
                    if(looped) {
        				oint = smin+(oint-smin)%plen;
                        fp0 = bdt+oint*BCHNS;
                        fp1 = oint >= maxo?bdt+smin:fp0;
                    }
                    else
		                fp0 = fp1 = bdt+maxo*BCHNS;
			    }
		    else {
			    // position is before first sample
                if(looped) {
        			oint = smax-(smin-oint)%plen;
                    fp0 = bdt+oint*BCHNS;
                    fp1 = oint >= maxo?bdt+smin:fp0;
                }
                else
		            fp0 = fp1 = bdt+smin*BCHNS;
		    }

			for(int ci = 0; ci < OCHNS; ++ci) 
				sig[ci][si] = fp0[ci]+frac*(fp1[ci]-fp0[ci]);
	    }

    	// clear rest of output channels (if buffer has less channels)
	    for(int ci = OCHNS; ci < outchns; ++ci) ZeroSamples(sig[ci],n);
    }
}

TMPLDEF void xinter::st_play4(const t_sample *bdt,const int smin,const int smax,const int n,const int inchns,const int outchns,t_sample *const *invecs,t_sample *const *outvecs,bool looped)
{
	const int plen = smax-smin; //curlen;
	if(UNLIKELY(plen < 4)) {
		if(plen < 2) st_play1 TMPLCALL (bdt,smin,smax,n,inchns,outchns,invecs,outvecs,looped);
		else st_play2 TMPLCALL (bdt,smin,smax,n,inchns,outchns,invecs,outvecs,looped);
		return;
	}

	SIGCHNS(BCHNS,inchns,OCHNS,outchns);

	// position info are frame units
	const t_sample *pos = invecs[0];
	t_sample *const *sig = outvecs;
	
	// 4-point interpolation
	// ---------------------
	const int maxo = smax-1; // last sample in play region

    if(OCHNS == 1) {
        t_sample *sig0 = sig[0];
	    for(int i = 0; i < n; ++i) {	
		    float o = pos[i];
		    register long oint = CASTINT<long>(o);
		    register t_sample fa,fb,fc,fd;
		    const float frac = o-oint;
            register const t_sample *ptr = bdt+oint*BCHNS;

            if(LIKELY(oint > smin)) {
			    if(LIKELY(oint < maxo-2)) {
				    // normal case
				    fa = ptr[-BCHNS];
				    fb = ptr[0];
				    fc = ptr[BCHNS];
				    fd = ptr[BCHNS*2];
                }
			    else {
				    // not enough space at the end

                    if(looped) {
                        // normalize position
                        oint = smin+(oint-smin)%plen;
                        goto looped1;
                    }
                    else {
                        // last sample is outside in any case
                        fd = bdt[maxo*BCHNS];

                        if(oint-1 >= maxo)
                            // if first is outside, all are outside
                            fa = fb = fc = fd;
                        else {
                            fa = ptr[-BCHNS];
                            if(oint >= maxo) 
                                fb = fc = fd;
                            else {
                                fb = ptr[0];
                                fc = oint+1 < maxo?ptr[BCHNS]:fd;
                            }
                        }
                    }
                }
            }
		    else {
			    // not enough space at the beginning

                if(looped) {
                    // normalize position
                    oint = smax-(smin-oint)%plen;
looped1:
                    ptr = bdt+oint*BCHNS;

                    // inside in any case
                    fb = ptr[0];

                    if(oint < maxo-1) {
                        fa = oint > smin?ptr[-BCHNS]:bdt[maxo*BCHNS];
                        fc = ptr[BCHNS];
                        fd = ptr[BCHNS*2];
                    }
                    else {
                        fa = ptr[-BCHNS];
                        fc = oint < maxo?ptr[BCHNS]:ptr[(1-plen)*BCHNS];
                        fd = ptr[(2-plen)*BCHNS];
                    }
                }
                else {
                    // first sample is outside in any case
                    fa = bdt[smin*BCHNS];

                    if(oint+2 < smin)
                        // if last is outside, all are outside
                        fb = fc = fd = fa;
                    else {
                        fd = ptr[BCHNS*2];
                        if(oint+1 < smin) 
                            fb = fc = fa;
                        else {
                            fc = ptr[BCHNS];
                            fb = oint < smin?fa:ptr[0];
                        }
                    }
                }
		    }
    		
		    const float f1 = frac*0.5f-0.5f;
		    const float f3 = frac*3.0f-1.0f;
    		
			const float amdf = (fa-fd)*frac;
			const float cmb = fc-fb;
			const float bma = fb-fa;
			sig0[i] = fb + frac*( cmb - f1 * ( amdf+bma+cmb*f3 ) );
	    }
    }
    else {
	    for(int i = 0,si = 0; i < n; ++i,++si) {	
		    float o = *(pos++);
		    register long oint = CASTINT<long>(o);
		    const float frac = o-oint;
            register const t_sample *ptr = bdt+oint*BCHNS;
		    register const t_sample *fa,*fb,*fc,*fd;

		    if(LIKELY(oint > smin))
			    if(LIKELY(oint < maxo-2)) {
				    // normal case
    				fb = ptr;
				    fa = fb-BCHNS;
				    fc = fb+BCHNS;
				    fd = fc+BCHNS;
			    }
			    else {
				    // not enough space at the end

                    if(looped) {
                        // normalize position
                        oint = smin+(oint-smin)%plen;
                        goto looped2;
                    }
                    else {
                        // last sample is outside in any case
                        fd = bdt+maxo*BCHNS;

                        if(oint-1 >= maxo)
                            // if first is outside, all are outside
                            fa = fb = fc = fd;
                        else {
                            fa = ptr-BCHNS;
                            if(oint >= maxo) 
                                fb = fc = fd;
                            else {
                                fb = ptr;
                                fc = oint+1 < maxo?ptr+BCHNS:fd;
                            }
                        }
                    }
			    }
		    else {
			    // not enough space at the beginning

                if(looped) {
                    // normalize position
                    oint = smax-(smin-oint)%plen;
looped2:
                    // inside in any case
                    fb = bdt+oint*BCHNS;

                    if(oint < maxo-1) {
                        fa = oint > smin?fb-BCHNS:bdt+maxo*BCHNS;
                        fc = fb+BCHNS;
                        fd = fc+BCHNS;
                    }
                    else {
                        fa = fb-BCHNS;
                        fc = oint < maxo?fb+BCHNS:bdt+(oint-plen+1)*BCHNS;
                        fd = bdt+(oint-plen+2)*BCHNS;
                    }
                }
                else {
                    // first sample is outside in any case
                    fa = bdt+smin*BCHNS;

                    if(oint+2 < smin)
                        // if last is outside, all are outside
                        fb = fc = fd = fa;
                    else {
                        fd = ptr+BCHNS*2;
                        if(oint+1 < smin) 
                            fb = fc = fa;
                        else {
                            fc = ptr+BCHNS;
                            fb = oint < smin?fa:ptr;
                        }
                    }
                }
		    }
    		
		    const float f1 = 0.5f*(frac-1.0f);
		    const float f3 = frac*3.0f-1.0f;
    		
		    for(int ci = 0; ci < OCHNS; ++ci) {
			    const float amdf = (fa[ci]-fd[ci])*frac;
			    const float cmb = fc[ci]-fb[ci];
			    const float bma = fb[ci]-fa[ci];
			    sig[ci][si] = fb[ci] + frac*( cmb - f1 * ( amdf+bma+cmb*f3 ) );
		    }
	    }

	    // clear rest of output channels (if buffer has less channels)
	    for(int ci = OCHNS; ci < outchns; ++ci) ZeroSamples(sig[ci],n);
    }
}


TMPLDEF inline void xinter::s_play0(int n,t_sample *const *invecs,t_sample *const *outvecs)
{
	st_play0 TMPLCALL (buf.Data(),curmin,curmax,n,buf.Channels(),outchns,invecs,outvecs,loopmode == xsl_loop);
}

TMPLDEF inline void xinter::s_play1(int n,t_sample *const *invecs,t_sample *const *outvecs)
{
	st_play1 TMPLCALL (buf.Data(),curmin,curmax,n,buf.Channels(),outchns,invecs,outvecs,loopmode == xsl_loop);
}

TMPLDEF inline void xinter::s_play2(int n,t_sample *const *invecs,t_sample *const *outvecs)
{
	st_play2 TMPLCALL (buf.Data(),curmin,curmax,n,buf.Channels(),outchns,invecs,outvecs,loopmode == xsl_loop);
}

TMPLDEF inline void xinter::s_play4(int n,t_sample *const *invecs,t_sample *const *outvecs)
{
	st_play4 TMPLCALL (buf.Data(),curmin,curmax,n,buf.Channels(),outchns,invecs,outvecs,loopmode == xsl_loop);
}

#endif