aboutsummaryrefslogtreecommitdiff
path: root/sc4pd/source/PitchShift.cpp
blob: 8a3f265b7c42788414f5e12d4502c2afe70ccb78 (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
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
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
/* sc4pd 
   PitchShift~ 

   Copyright (c) 2004 Tim Blechmann.                                       

   This code is derived from:
	SuperCollider real time audio synthesis system
    Copyright (c) 2002 James McCartney. All rights reserved.
	http://www.audiosynth.com


   This program is free software; you can redistribute it and/or
   modify it under the terms of the GNU General Public License
   as published by the Free Software Foundation; either version 2
   of the License, or (at your option) any later version.

   This program is distributed in the hope that it will be useful,             
   but WITHOUT ANY WARRANTY; without even the implied warranty of         
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.
   
   You should have received a copy of the GNU General Public License
   along with this program; if not, write to the Free Software
   Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.

   Based on:
     PureData by Miller Puckette and others.
         http://www.crca.ucsd.edu/~msp/software.html
     FLEXT by Thomas Grill
         http://www.parasitaere-kapazitaeten.net/ext
     SuperCollider by James McCartney
         http://www.audiosynth.com
     
   Coded while listening to: Bernhard Lang: Differenz / Wiederholung 2
   
*/

#include "sc4pd.hpp"

/* ------------------------ PitchShift~ -----------------------------*/

class PitchShift_ar
    :public sc4pd_dsp
{
    FLEXT_HEADER(PitchShift_ar,sc4pd_dsp);

public:
    PitchShift_ar(int argc,t_atom * argv);

protected:
    virtual void m_signal(int n, t_sample *const *in, t_sample *const *out)
    {
	m_signal_fun(n,in,out);
    }
    
    virtual void m_dsp(int n, t_sample *const *in, t_sample *const *out);

    void m_set_pitchratio (float f)
    {
	m_pitchratio = f;
    }

    void m_set_pitchdispersion (float f)
    {
	m_pitchdispersion = f;
    }
    
    void m_set_timedispersion (float f)
    {
	m_timedispersion = f;
    }

private:
    float m_windowsize,m_pitchratio,m_pitchdispersion,m_timedispersion;
    RGen rgen;


    float *m_dlybuf;
    float m_dsamp1, m_dsamp1_slope, m_ramp1, m_ramp1_slope;
    float m_dsamp2, m_dsamp2_slope, m_ramp2, m_ramp2_slope;
    float m_dsamp3, m_dsamp3_slope, m_ramp3, m_ramp3_slope;
    float m_dsamp4, m_dsamp4_slope, m_ramp4, m_ramp4_slope;
    float m_fdelaylen, m_slope;
    long m_iwrphase, m_idelaylen, m_mask;
    long m_counter, m_stage, m_numoutput, m_framesize;

    DEFSIGCALL(m_signal_fun);
    DEFSIGFUN(m_signal_);
    DEFSIGFUN(m_signal_z);
    
    FLEXT_CALLBACK_F(m_set_pitchratio);
    FLEXT_CALLBACK_F(m_set_pitchdispersion);
    FLEXT_CALLBACK_F(m_set_timedispersion);
};

FLEXT_LIB_DSP_V("PitchShift~",PitchShift_ar);

PitchShift_ar::PitchShift_ar(int argc,t_atom * argv)
{
    FLEXT_ADDMETHOD_(0,"pitchRatio",m_set_pitchratio);
    FLEXT_ADDMETHOD_(0,"pitchDispersion",m_set_pitchdispersion);
    FLEXT_ADDMETHOD_(0,"timeDispersion",m_set_timedispersion);

    AtomList Args(argc,argv);

    if (Args.Count() != 4)
    {
	post("4 arguments needed");
	return;
    }

    m_windowsize = sc_getfloatarg(Args,0);
    m_pitchratio = sc_getfloatarg(Args,1);
    m_pitchdispersion = sc_getfloatarg(Args,2);
    m_timedispersion = sc_getfloatarg(Args,3);

    rgen.init(timeseed());
    
    AddOutSignal();
	
    SETSIGFUN(m_signal_fun,SIGFUN(m_signal_z));
}

void PitchShift_ar::m_dsp(int n, t_sample *const *in, t_sample *const *out)
{
    /* initialization from PitchShift_Ctor(PitchShift *unit) */

    long delaybufsize;
    float *dlybuf;
    float pchratio;
    float fdelaylen, slope;
    long framesize, last;
    
    //out = ZOUT(0);
    //in = ZIN(0);
    pchratio = m_pitchratio;
	
    delaybufsize = (long)ceil(m_windowsize * SAMPLERATE * 3.f + 3.f);
    fdelaylen = delaybufsize - 3;

    delaybufsize = delaybufsize + BUFLENGTH;
    delaybufsize = NEXTPOWEROFTWO(delaybufsize);  // round up to next power of two
    dlybuf = new float[delaybufsize];
         //(float*)RTAlloc(unit->mWorld, delaybufsize * sizeof(float));
	
    *dlybuf = 0;

    m_dlybuf = dlybuf;
    m_idelaylen = delaybufsize;
    m_fdelaylen = fdelaylen;
    m_iwrphase = 0;
    m_numoutput = 0;
    m_mask = last = (delaybufsize - 1);
	
    m_framesize = framesize = ((long)(m_windowsize * SAMPLERATE) + 2) & ~3;
    m_slope = slope = 2.f / framesize;
    m_stage = 3;
    m_counter = framesize >> 2;
    m_ramp1 = 0.5;
    m_ramp2 = 1.0;
    m_ramp3 = 0.5;
    m_ramp4 = 0.0;

    m_ramp1_slope = -slope;
    m_ramp2_slope = -slope;
    m_ramp3_slope =  slope;
    m_ramp4_slope =  slope;
    
    dlybuf[last  ] = 0.f; // put a few zeroes where we start the read heads
    dlybuf[last-1] = 0.f;
    dlybuf[last-2] = 0.f;
    
    m_numoutput = 0;

    // start all read heads 2 samples behind the write head
    m_dsamp1 = m_dsamp2 = m_dsamp3 = m_dsamp4 = 2.f;
    // pch ratio is initially zero for the read heads
    m_dsamp1_slope = m_dsamp2_slope = m_dsamp3_slope = m_dsamp4_slope = 1.f;
}


void PitchShift_ar::m_signal_z(int n, t_sample *const *in, 
		      t_sample *const *out)
{
    
    float *nout, *nin, *dlybuf;
    float disppchratio, pchratio, pchratio1, value;
    float dsamp1, dsamp1_slope, ramp1, ramp1_slope;
    float dsamp2, dsamp2_slope, ramp2, ramp2_slope;
    float dsamp3, dsamp3_slope, ramp3, ramp3_slope;
    float dsamp4, dsamp4_slope, ramp4, ramp4_slope;
    float fdelaylen, d1, d2, frac, slope, samp_slope, startpos, 
	pchdisp, timedisp;
    long remain, nsmps, idelaylen, irdphase, irdphaseb, iwrphase;
    long mask, idsamp;
    long counter, stage, framesize, numoutput;
	
    RGET;
		
    nout = *out;
    nin = *in;
    
    pchratio = m_pitchratio;
    pchdisp = m_pitchdispersion;
    timedisp = m_timedispersion;
    timedisp = sc_clip(timedisp, 0.f, m_windowsize) * SAMPLERATE;
	
    dlybuf = m_dlybuf;
    fdelaylen = m_fdelaylen;
    idelaylen = m_idelaylen;
    iwrphase = m_iwrphase;
    numoutput = m_numoutput;

    counter = m_counter;
    stage = m_stage;
    mask = m_mask;
    framesize = m_framesize;
    
    dsamp1 = m_dsamp1;
    dsamp2 = m_dsamp2;
    dsamp3 = m_dsamp3;
    dsamp4 = m_dsamp4;
	
    dsamp1_slope = m_dsamp1_slope;
    dsamp2_slope = m_dsamp2_slope;
    dsamp3_slope = m_dsamp3_slope;
    dsamp4_slope = m_dsamp4_slope;
	
    ramp1 = m_ramp1;
    ramp2 = m_ramp2;
    ramp3 = m_ramp3;
    ramp4 = m_ramp4;
    
    ramp1_slope = m_ramp1_slope;
    ramp2_slope = m_ramp2_slope;
    ramp3_slope = m_ramp3_slope;
    ramp4_slope = m_ramp4_slope;
    
    slope = m_slope;
	
    remain = n;
    while (remain) 
    {
	if (counter <= 0) 
	{
	    counter = framesize >> 2;
	    m_stage = stage = (stage + 1) & 3;
	    disppchratio = pchratio;
	    if (pchdisp != 0.f) 
	    {
		disppchratio += (pchdisp * frand2(s1,s2,s3));
	    }
	    disppchratio = sc_clip(disppchratio, 0.f, 4.f);
	    pchratio1 = disppchratio - 1.f;
	    samp_slope = -pchratio1;
	    startpos = pchratio1 < 0.f ? 2.f : framesize * pchratio1 + 2.f;
	    startpos += (timedisp * frand(s1,s2,s3));
	    switch(stage) 
	    {
	    case 0 :
		m_dsamp1_slope = dsamp1_slope = samp_slope;
		dsamp1 = startpos;
		ramp1 = 0.0;
		m_ramp1_slope = ramp1_slope =  slope;
		m_ramp3_slope = ramp3_slope = -slope;
		break;
	    case 1 :
		m_dsamp2_slope = dsamp2_slope = samp_slope;
		dsamp2 = startpos;
		ramp2 = 0.0;
		m_ramp2_slope = ramp2_slope =  slope;
		m_ramp4_slope = ramp4_slope = -slope;
		break;
	    case 2 :
		m_dsamp3_slope = dsamp3_slope = samp_slope;
		dsamp3 = startpos;
		ramp3 = 0.0;
		m_ramp3_slope = ramp3_slope =  slope;
		m_ramp1_slope = ramp1_slope = -slope;
		break;
	    case 3 :
		m_dsamp4_slope = dsamp4_slope = samp_slope;
		dsamp4 = startpos;
		ramp4 = 0.0;
		m_ramp2_slope = ramp2_slope = -slope;
		m_ramp4_slope = ramp4_slope =  slope;
		break;
	    }				
	}
	nsmps = sc_min(remain, counter);
	remain -= nsmps;
	counter -= nsmps;
	
	while (nsmps--) {
	    numoutput++;
	    iwrphase = (iwrphase + 1) & mask;
	    
	    dsamp1 += dsamp1_slope;
	    idsamp = (long)dsamp1;
	    frac = dsamp1 - idsamp;
	    irdphase = (iwrphase - idsamp) & mask;
	    irdphaseb = (irdphase - 1) & mask;
	    if (numoutput < idelaylen) 
	    {
		if (irdphase > iwrphase) 
		{
		    value = 0.f;
		} 
		else if (irdphaseb > iwrphase) 
		{
		    d1 = dlybuf[irdphase];
		    value = (d1 - frac * d1) * ramp1;
		} 
		else 
		{
		    d1 = dlybuf[irdphase];
		    d2 = dlybuf[irdphaseb];
		    value = (d1 + frac * (d2 - d1)) * ramp1;
		}
	    }
	    else
	    {
		d1 = dlybuf[irdphase];
		d2 = dlybuf[irdphaseb];
		value = (d1 + frac * (d2 - d1)) * ramp1;
	    }
	    ramp1 += ramp1_slope;
	    
	    dsamp2 += dsamp2_slope;
	    idsamp = (long)dsamp2;
	    frac = dsamp2 - idsamp;
	    irdphase = (iwrphase - idsamp) & mask;
	    irdphaseb = (irdphase - 1) & mask;
	    if (numoutput < idelaylen) 
	    {
		if (irdphase > iwrphase) 
		{
		    //value += 0.f;
		} 
		else if (irdphaseb > iwrphase) 
		{
		    d1 = dlybuf[irdphase];
		    value += (d1 - frac * d1) * ramp2;
		}
		else 
		{
		    d1 = dlybuf[irdphase];
		    d2 = dlybuf[irdphaseb];
		    value += (d1 + frac * (d2 - d1)) * ramp2;
		
		}
	    } 
	    else 
	    {
		d1 = dlybuf[irdphase];
		d2 = dlybuf[irdphaseb];
		value += (d1 + frac * (d2 - d1)) * ramp2;
	    }
	    ramp2 += ramp2_slope;
	    
	    dsamp3 += dsamp3_slope;
	    idsamp = (long)dsamp3;
	    frac = dsamp3 - idsamp;
	    irdphase = (iwrphase - idsamp) & mask;
	    irdphaseb = (irdphase - 1) & mask;
	    if (numoutput < idelaylen) 
	    {
		if (irdphase > iwrphase) 
		{
		    //value += 0.f;
		} 
		else if (irdphaseb > iwrphase) 
		{
		    d1 = dlybuf[irdphase];
		    value += (d1 - frac * d1) * ramp3;
		} 
		else 
		{
		    d1 = dlybuf[irdphase];
		    d2 = dlybuf[irdphaseb];
		    value += (d1 + frac * (d2 - d1)) * ramp3;
		}
	    }
	    else
	    {
		d1 = dlybuf[irdphase];
		d2 = dlybuf[irdphaseb];
		value += (d1 + frac * (d2 - d1)) * ramp3;
	    }
	    ramp3 += ramp3_slope;
			
	    dsamp4 += dsamp4_slope;
	    idsamp = (long)dsamp4;
	    frac = dsamp4 - idsamp;
	    irdphase = (iwrphase - idsamp) & mask;
	    irdphaseb = (irdphase - 1) & mask;
	    
	    if (numoutput < idelaylen) 
	    {
		if (irdphase > iwrphase) 
		{
		    //value += 0.f;
		} else if (irdphaseb > iwrphase) {
		    d1 = dlybuf[irdphase];
		    value += (d1 - frac * d1) * ramp4;
		}
		else 
		{
		    d1 = dlybuf[irdphase];
		    d2 = dlybuf[irdphaseb];
		    value += (d1 + frac * (d2 - d1)) * ramp4;
		}
	    }
	    else 
	    {
		d1 = dlybuf[irdphase];
		d2 = dlybuf[irdphaseb];
		value += (d1 + frac * (d2 - d1)) * ramp4;
	    }
	    ramp4 += ramp4_slope;
			
	    dlybuf[iwrphase] = ZXP(nin);
	    ZXP(nout) = value *= 0.5;
	}
    }
    
    m_counter = counter;
    m_stage = stage;
    m_mask = mask;
    
    m_dsamp1 = dsamp1;
    m_dsamp2 = dsamp2;
    m_dsamp3 = dsamp3;
    m_dsamp4 = dsamp4;
    
    m_ramp1 = ramp1;
    m_ramp2 = ramp2;
    m_ramp3 = ramp3;
    m_ramp4 = ramp4;
	
    m_numoutput = numoutput;
    m_iwrphase = iwrphase;
	
    if (numoutput >= idelaylen) 
    {
	SETSIGFUN(m_signal_fun,SIGFUN(m_signal_z));
    }
    RPUT;
}
    
void PitchShift_ar::m_signal_(int n, t_sample *const *in, 
		      t_sample *const *out)
{
    float *nout, *nin, *dlybuf;
    float disppchratio, pchratio, pchratio1, value;
    float dsamp1, dsamp1_slope, ramp1, ramp1_slope;
    float dsamp2, dsamp2_slope, ramp2, ramp2_slope;
    float dsamp3, dsamp3_slope, ramp3, ramp3_slope;
    float dsamp4, dsamp4_slope, ramp4, ramp4_slope;
    float fdelaylen, d1, d2, frac, slope, samp_slope, startpos, 
	pchdisp, timedisp;
    long remain, nsmps, idelaylen, irdphase, irdphaseb, iwrphase, mask, idsamp;
    long counter, stage, framesize;
    
    RGET;
    
    nout = *out;
    nin = *in;
    
    pchratio = m_pitchratio;
    pchdisp = m_pitchdispersion;
    timedisp = m_timedispersion;
    
    timedisp = sc_clip(timedisp, 0.f, m_windowsize) * SAMPLERATE;
    
    dlybuf = m_dlybuf;
    fdelaylen = m_fdelaylen;
    idelaylen = m_idelaylen;
    iwrphase = m_iwrphase;
    
    counter = m_counter;
    stage = m_stage;
    mask = m_mask;
    framesize = m_framesize;
    
    dsamp1 = m_dsamp1;
    dsamp2 = m_dsamp2;
    dsamp3 = m_dsamp3;
    dsamp4 = m_dsamp4;
    
    dsamp1_slope = m_dsamp1_slope;
    dsamp2_slope = m_dsamp2_slope;
    dsamp3_slope = m_dsamp3_slope;
    dsamp4_slope = m_dsamp4_slope;
    
    ramp1 = m_ramp1;
    ramp2 = m_ramp2;
    ramp3 = m_ramp3;
    ramp4 = m_ramp4;
    
    ramp1_slope = m_ramp1_slope;
    ramp2_slope = m_ramp2_slope;
    ramp3_slope = m_ramp3_slope;
    ramp4_slope = m_ramp4_slope;
    
    slope = m_slope;
    
    remain = n;
    while (remain) 
    {
	if (counter <= 0) 
	{
	    counter = framesize >> 2;
	    m_stage = stage = (stage + 1) & 3;
	    disppchratio = pchratio;
	    if (pchdisp != 0.f) 
	    {
		disppchratio += (pchdisp * frand2(s1,s2,s3));
	    }
	    disppchratio = sc_clip(disppchratio, 0.f, 4.f);
	    pchratio1 = disppchratio - 1.f;
	    samp_slope = -pchratio1;
	    startpos = pchratio1 < 0.f ? 2.f : framesize * pchratio1 + 2.f;
	    startpos += (timedisp * frand(s1,s2,s3));
	    switch(stage) 
	    {
	    case 0 :
		m_dsamp1_slope = dsamp1_slope = samp_slope;
		dsamp1 = startpos;
		ramp1 = 0.0;
		m_ramp1_slope = ramp1_slope =  slope;
		m_ramp3_slope = ramp3_slope = -slope;
		break;
	    case 1 :
		m_dsamp2_slope = dsamp2_slope = samp_slope;
		dsamp2 = startpos;
		ramp2 = 0.0;
		m_ramp2_slope = ramp2_slope =  slope;
		m_ramp4_slope = ramp4_slope = -slope;
		break;
	    case 2 :
		m_dsamp3_slope = dsamp3_slope = samp_slope;
		dsamp3 = startpos;
		ramp3 = 0.0;
		m_ramp3_slope = ramp3_slope =  slope;
		m_ramp1_slope = ramp1_slope = -slope;
		break;
	    case 3 :
		m_dsamp4_slope = dsamp4_slope = samp_slope;
		dsamp4 = startpos;
		ramp4 = 0.0;
		m_ramp2_slope = ramp2_slope = -slope;
		m_ramp4_slope = ramp4_slope =  slope;
		break;
	    }
	}
	
	nsmps = sc_min(remain, counter);
	remain -= nsmps;
	counter -= nsmps;
	
	for (int i = 0; i!= nsmps;++i)
	{
	    iwrphase = (iwrphase + 1) & mask;
	    
	    dsamp1 += dsamp1_slope;
	    idsamp = (long)dsamp1;
	    frac = dsamp1 - idsamp;
	    irdphase = (iwrphase - idsamp) & mask;
	    irdphaseb = (irdphase - 1) & mask;
	    d1 = dlybuf[irdphase];
	    d2 = dlybuf[irdphaseb];
	    value = (d1 + frac * (d2 - d1)) * ramp1;
	    ramp1 += ramp1_slope;
	    
	    dsamp2 += dsamp2_slope;
	    idsamp = (long)dsamp2;
	    frac = dsamp2 - idsamp;
	    irdphase = (iwrphase - idsamp) & mask;
	    irdphaseb = (irdphase - 1) & mask;
	    d1 = dlybuf[irdphase];
	    d2 = dlybuf[irdphaseb];
	    value += (d1 + frac * (d2 - d1)) * ramp2;
	    ramp2 += ramp2_slope;
	    
	    dsamp3 += dsamp3_slope;
	    idsamp = (long)dsamp3;
	    frac = dsamp3 - idsamp;
	    irdphase = (iwrphase - idsamp) & mask;
	    irdphaseb = (irdphase - 1) & mask;
	    d1 = dlybuf[irdphase];
	    d2 = dlybuf[irdphaseb];
	    value += (d1 + frac * (d2 - d1)) * ramp3;
	    ramp3 += ramp3_slope;
	
	    dsamp4 += dsamp4_slope;
	    idsamp = (long)dsamp4;
	    frac = dsamp4 - idsamp;
	    irdphase = (iwrphase - idsamp) & mask;
	    irdphaseb = (irdphase - 1) & mask;
	    d1 = dlybuf[irdphase];
	    d2 = dlybuf[irdphaseb];
	    value += (d1 + frac * (d2 - d1)) * ramp4;
	    ramp4 += ramp4_slope;
	    
	    dlybuf[iwrphase] = ZXP(nin);
	    ZXP(nout) = value *= 0.5;
	}
    }
    
    m_counter = counter;
    
    m_dsamp1 = dsamp1;
    m_dsamp2 = dsamp2;
    m_dsamp3 = dsamp3;
    m_dsamp4 = dsamp4;
    
    m_ramp1 = ramp1;
    m_ramp2 = ramp2;
    m_ramp3 = ramp3;
    m_ramp4 = ramp4;
    
    m_iwrphase = iwrphase;
    
    RPUT;
}
    

/* a control rate PitchShift doesn't make sense */