File: tap_rotspeak.c

package info (click to toggle)
tap-plugins 1.0.0-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, buster
  • size: 604 kB
  • sloc: ansic: 9,700; makefile: 57
file content (766 lines) | stat: -rw-r--r-- 25,189 bytes parent folder | download | duplicates (2)
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
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
/*                                                     -*- linux-c -*-
    Copyright (C) 2004 Tom Szilagyi

    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., 675 Mass Ave, Cambridge, MA 02139, USA.
*/


#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>

#include <ladspa.h>
#include "tap_utils.h"

/* The Unique ID of the plugin: */

#define ID_STEREO         2149

/* The port numbers for the plugin: */

#define BASSFREQ        0
#define HORNFREQ        1
#define STWIDTH         2
#define HRBAL           3
#define LATENCY         4
#define INPUT_L         5
#define INPUT_R         6
#define OUTPUT_L        7
#define OUTPUT_R        8


/* Total number of ports */

#define PORTCOUNT_STEREO   9

/*
 * This has to be bigger than 0.3f * sample_rate / (2*PI) for any sample rate.
 * At 192 kHz 9168 is needed so this should be enough.
 */
#define PM_DEPTH 9200

/* maximum phase mod freq */
#define PM_FREQ 30.0f


/* splitting input signals into low and high freq components */
#define SPLIT_FREQ 1000.0f
#define SPLIT_BW 1.0f


/* approx. sound velocity in air [m/s] */
#define C_AIR 340.0f

/* coefficient between rotating frequency and pitch mod depth (aka. Doppler effect) */
#define FREQ_PITCH 1.6f

/* cosine table for fast computations */
LADSPA_Data cos_table[1024];


/* The structure used to hold port connection information and state */

typedef struct {
	LADSPA_Data * hornfreq;
	LADSPA_Data * bassfreq;
	LADSPA_Data * stwidth;
	LADSPA_Data * hrbal;
	LADSPA_Data * latency;
	LADSPA_Data * input_L;
	LADSPA_Data * input_R;
	LADSPA_Data * output_L;
	LADSPA_Data * output_R;

        LADSPA_Data * ringbuffer_h_L;
        unsigned long buflen_h_L;
        unsigned long pos_h_L;
        LADSPA_Data * ringbuffer_h_R;
        unsigned long buflen_h_R;
        unsigned long pos_h_R;

        LADSPA_Data * ringbuffer_b_L;
        unsigned long buflen_b_L;
        unsigned long pos_b_L;
        LADSPA_Data * ringbuffer_b_R;
        unsigned long buflen_b_R;
        unsigned long pos_b_R;

	biquad * eq_filter_L;
	biquad * lp_filter_L;
	biquad * hp_filter_L;

	biquad * eq_filter_R;
	biquad * lp_filter_R;
	biquad * hp_filter_R;

	unsigned long sample_rate;
	LADSPA_Data phase_h;
	LADSPA_Data phase_b;

	LADSPA_Data run_adding_gain;
} RotSpkr;


void cleanup_RotSpkr(LADSPA_Handle Instance);

/* Construct a new plugin instance. */
LADSPA_Handle
instantiate_RotSpkr(const LADSPA_Descriptor * Descriptor,
		    unsigned long             sample_rate) {

	LADSPA_Handle * ptr;

	if ((ptr = calloc(1, sizeof(RotSpkr))) != NULL) {
		RotSpkr* rotSpeak = (RotSpkr*)ptr;
		rotSpeak->sample_rate = sample_rate;
		rotSpeak->run_adding_gain = 1.0;

		if ((rotSpeak->ringbuffer_h_L =
		     calloc(2 * PM_DEPTH, sizeof(LADSPA_Data))) == NULL)
		{
			cleanup_RotSpkr((LADSPA_Handle)rotSpeak);
			return NULL;
		}
		if ((rotSpeak->ringbuffer_h_R =
		     calloc(2 * PM_DEPTH, sizeof(LADSPA_Data))) == NULL)
		{
			cleanup_RotSpkr((LADSPA_Handle)rotSpeak);
			return NULL;
		}
		rotSpeak->buflen_h_L = ceil(0.3f * sample_rate / M_PI);
		rotSpeak->buflen_h_R = ceil(0.3f * sample_rate / M_PI);
		rotSpeak->pos_h_L = 0;
		rotSpeak->pos_h_R = 0;

		if ((rotSpeak->ringbuffer_b_L =
		     calloc(2 * PM_DEPTH, sizeof(LADSPA_Data))) == NULL)
		{
			cleanup_RotSpkr((LADSPA_Handle)rotSpeak);
			return NULL;
		}
		if ((rotSpeak->ringbuffer_b_R =
		     calloc(2 * PM_DEPTH, sizeof(LADSPA_Data))) == NULL)
		{
			cleanup_RotSpkr((LADSPA_Handle)rotSpeak);
			return NULL;
		}
		rotSpeak->buflen_b_L = ceil(0.3f * sample_rate / M_PI);
		rotSpeak->buflen_b_R = ceil(0.3f * sample_rate / M_PI);
		rotSpeak->pos_b_L = 0;
		rotSpeak->pos_b_R = 0;

		if ((rotSpeak->eq_filter_L = calloc(1, sizeof(biquad))) == NULL)
		{
			cleanup_RotSpkr((LADSPA_Handle)rotSpeak);
			return NULL;
		}
		if ((rotSpeak->lp_filter_L = calloc(1, sizeof(biquad))) == NULL)
		{
			cleanup_RotSpkr((LADSPA_Handle)rotSpeak);
			return NULL;
		}
		if ((rotSpeak->hp_filter_L = calloc(1, sizeof(biquad))) == NULL)
		{
			cleanup_RotSpkr((LADSPA_Handle)rotSpeak);
			return NULL;
		}

		if ((rotSpeak->eq_filter_R = calloc(1, sizeof(biquad))) == NULL)
		{
			cleanup_RotSpkr((LADSPA_Handle)rotSpeak);
			return NULL;
		}
		if ((rotSpeak->lp_filter_R = calloc(1, sizeof(biquad))) == NULL)
		{
			cleanup_RotSpkr((LADSPA_Handle)rotSpeak);
			return NULL;
		}
		if ((rotSpeak->hp_filter_R = calloc(1, sizeof(biquad))) == NULL)
		{
			cleanup_RotSpkr((LADSPA_Handle)rotSpeak);
			return NULL;
		}

		return ptr;
	}

	return NULL;
}


void
activate_RotSpkr(LADSPA_Handle Instance) {

	int i;
	RotSpkr * ptr;

	ptr = (RotSpkr *)Instance;

	for (i = 0; i < 2 * PM_DEPTH; i++) {
		ptr->ringbuffer_h_L[i] = 0.0f;
		ptr->ringbuffer_h_R[i] = 0.0f;
		ptr->ringbuffer_b_L[i] = 0.0f;
		ptr->ringbuffer_b_R[i] = 0.0f;
	}

	ptr->phase_h = 0.0f;
	ptr->phase_b = 0.0f;

	biquad_init(ptr->eq_filter_L);
	biquad_init(ptr->lp_filter_L);
	biquad_init(ptr->hp_filter_L);
	biquad_init(ptr->eq_filter_R);
	biquad_init(ptr->lp_filter_R);
	biquad_init(ptr->hp_filter_R);

	eq_set_params(ptr->eq_filter_L, SPLIT_FREQ, +8.0f, SPLIT_BW, ptr->sample_rate);
	eq_set_params(ptr->eq_filter_R, SPLIT_FREQ, +8.0f, SPLIT_BW, ptr->sample_rate);
	lp_set_params(ptr->lp_filter_L, SPLIT_FREQ, SPLIT_BW, ptr->sample_rate);
	lp_set_params(ptr->lp_filter_R, SPLIT_FREQ, SPLIT_BW, ptr->sample_rate);
	hp_set_params(ptr->hp_filter_L, SPLIT_FREQ, SPLIT_BW, ptr->sample_rate);
	hp_set_params(ptr->hp_filter_R, SPLIT_FREQ, SPLIT_BW, ptr->sample_rate);
}



/* Connect a port to a data location. */
void
connect_port_RotSpkr(LADSPA_Handle Instance,
		     unsigned long Port,
		     LADSPA_Data * DataLocation) {

	RotSpkr * ptr;

	ptr = (RotSpkr *)Instance;
	switch (Port) {
	case HORNFREQ:
		ptr->hornfreq = DataLocation;
		break;
	case BASSFREQ:
		ptr->bassfreq = DataLocation;
		break;
	case STWIDTH:
		ptr->stwidth = DataLocation;
		break;
	case HRBAL:
		ptr->hrbal = DataLocation;
		break;
	case LATENCY:
		ptr->latency = DataLocation;
                *(ptr->latency) = ptr->buflen_h_L / 2;  /* IS THIS LEGAL? YES, ONLY IF DataLocation points to valid memory location on stack/heap*/
		break;
	case INPUT_L:
		ptr->input_L = DataLocation;
		break;
	case INPUT_R:
		ptr->input_R = DataLocation;
		break;
	case OUTPUT_L:
		ptr->output_L = DataLocation;
		break;
	case OUTPUT_R:
		ptr->output_R = DataLocation;
		break;
	}
}



void
run_RotSpkr(LADSPA_Handle Instance,
	    unsigned long SampleCount) {

	RotSpkr * ptr = (RotSpkr *)Instance;

	LADSPA_Data * input_L = ptr->input_L;
	LADSPA_Data * input_R = ptr->input_R;
	LADSPA_Data * output_L = ptr->output_L;
	LADSPA_Data * output_R = ptr->output_R;
	LADSPA_Data freq_h = LIMIT(*(ptr->hornfreq),0.0f,PM_FREQ);
	LADSPA_Data freq_b = LIMIT(*(ptr->bassfreq),0.0f,PM_FREQ);
	LADSPA_Data stwidth = LIMIT(*(ptr->stwidth),0.0f,100.0f);
	LADSPA_Data hrbal = LIMIT(*(ptr->hrbal),0.0f,1.0f);
	LADSPA_Data pmdepth_h =
		LIMIT(1.0f/(1.0f+FREQ_PITCH*freq_h/C_AIR) * ptr->sample_rate
		      / 200.0f / M_PI / freq_h, 0, ptr->buflen_h_L / 2);
	LADSPA_Data pmdepth_b =
		LIMIT(1.0f/(1.0f+FREQ_PITCH*freq_b/C_AIR) * ptr->sample_rate
		      / 200.0f / M_PI / freq_b, 0, ptr->buflen_b_L / 2);
	unsigned long sample_index;

	LADSPA_Data in_L = 0.0f, in_R = 0.0f;
	LADSPA_Data lo_L = 0.0f, lo_R = 0.0f;
	LADSPA_Data hi_L = 0.0f, hi_R = 0.0f;

	LADSPA_Data phase_h_L = 0.0f, phase_b_L = 0.0f;
	LADSPA_Data phase_h_R = 0.0f, phase_b_R = 0.0f;
	LADSPA_Data phase_pm_h_L = 0.0f, phase_pm_b_L = 0.0f;
	LADSPA_Data phase_pm_h_R = 0.0f, phase_pm_b_R = 0.0f;
	LADSPA_Data pm_h_L = 0.0f, pm_b_L = 0.0f;
	LADSPA_Data pm_h_R = 0.0f, pm_b_R = 0.0f;

	LADSPA_Data fpos_h_L = 0.0f, fpos_b_L = 0.0f, fpos_h_R = 0.0f, fpos_b_R = 0.0f;
	LADSPA_Data n_h_L = 0.0f, n_b_L = 0.0f, n_h_R = 0.0f, n_b_R = 0.0f;
	LADSPA_Data rem_h_L = 0.0f, rem_b_L = 0.0f, rem_h_R = 0.0f, rem_b_R = 0.0f;
	LADSPA_Data sa_h_L = 0.0f, sa_b_L = 0.0f, sb_h_L = 0.0f, sb_b_L = 0.0f;
	LADSPA_Data sa_h_R = 0.0f, sa_b_R = 0.0f, sb_h_R = 0.0f, sb_b_R = 0.0f;
	

	for (sample_index = 0; sample_index < SampleCount; sample_index++) {

		in_L = *(input_L++);
		in_R = *(input_R++);

		in_L = biquad_run(ptr->eq_filter_L, in_L);
		in_R = biquad_run(ptr->eq_filter_R, in_R);
		lo_L = biquad_run(ptr->lp_filter_L, in_L);
		lo_R = biquad_run(ptr->lp_filter_R, in_R);
		hi_L = biquad_run(ptr->hp_filter_L, in_L);
		hi_R = biquad_run(ptr->hp_filter_R, in_R);
	

		phase_h_L = 1024.0f * freq_h * sample_index / ptr->sample_rate + ptr->phase_h;
		while (phase_h_L >= 1024.0f)
		        phase_h_L -= 1024.0f;  
		phase_pm_h_L = phase_h_L + 256.0f;
		while (phase_pm_h_L >= 1024.0f)
			phase_pm_h_L -= 1024.0f;
 		phase_h_R = phase_h_L + 512.0f;
		while (phase_h_R >= 1024.0f)
		        phase_h_R -= 1024.0f;
		phase_pm_h_R = phase_h_R + 256.0f;
		while (phase_pm_h_R >= 1024.0f)
			phase_pm_h_R -= 1024.0f;

		phase_b_L = 1024.0f * freq_b * sample_index / ptr->sample_rate + ptr->phase_b;
		while (phase_b_L >= 1024.0f)
		        phase_b_L -= 1024.0f;  
		phase_pm_b_L = phase_b_L + 256.0f;
		while (phase_pm_b_L >= 1024.0f)
			phase_pm_b_L -= 1024.0f;
 		phase_b_R = phase_b_L + 512.0f;
		while (phase_b_R >= 1024.0f)
		        phase_b_R -= 1024.0f;
		phase_pm_b_R = phase_b_R + 256.0f;
		while (phase_pm_b_R >= 1024.0f)
			phase_pm_b_R -= 1024.0f;

                push_buffer(hi_L, ptr->ringbuffer_h_L, ptr->buflen_h_L, &(ptr->pos_h_L));
                push_buffer(hi_R, ptr->ringbuffer_h_R, ptr->buflen_h_R, &(ptr->pos_h_R));
                push_buffer(lo_L, ptr->ringbuffer_b_L, ptr->buflen_b_L, &(ptr->pos_b_L));
                push_buffer(lo_R, ptr->ringbuffer_b_R, ptr->buflen_b_R, &(ptr->pos_b_R));

                fpos_h_L = pmdepth_h * (1.0f - cos_table[(unsigned long) phase_pm_h_L]);
                n_h_L = floorf(fpos_h_L);
                rem_h_L = fpos_h_L - n_h_L;
                sa_h_L = read_buffer(ptr->ringbuffer_h_L,
				     ptr->buflen_h_L, ptr->pos_h_L, (unsigned long) n_h_L);
                sb_h_L = read_buffer(ptr->ringbuffer_h_L,
				     ptr->buflen_h_L, ptr->pos_h_L, (unsigned long) n_h_L + 1);
                pm_h_L = (1 - rem_h_L) * sa_h_L + rem_h_L * sb_h_L;

                fpos_h_R = pmdepth_h * (1.0f - cos_table[(unsigned long) phase_pm_h_R]);
                n_h_R = floorf(fpos_h_R);
                rem_h_R = fpos_h_R - n_h_R;
                sa_h_R = read_buffer(ptr->ringbuffer_h_R,
				     ptr->buflen_h_R, ptr->pos_h_R, (unsigned long) n_h_R);
                sb_h_R = read_buffer(ptr->ringbuffer_h_R,
				     ptr->buflen_h_R, ptr->pos_h_R, (unsigned long) n_h_R + 1);
                pm_h_R = (1 - rem_h_R) * sa_h_R + rem_h_R * sb_h_R;


                fpos_b_L = pmdepth_b * (1.0f - cos_table[(unsigned long) phase_pm_b_L]);
                n_b_L = floorf(fpos_b_L);
                rem_b_L = fpos_b_L - n_b_L;
                sa_b_L = read_buffer(ptr->ringbuffer_b_L,
				     ptr->buflen_b_L, ptr->pos_b_L, (unsigned long) n_b_L);
                sb_b_L = read_buffer(ptr->ringbuffer_b_L,
				     ptr->buflen_b_L, ptr->pos_b_L, (unsigned long) n_b_L + 1);
                pm_b_L = (1 - rem_b_L) * sa_b_L + rem_b_L * sb_b_L;

                fpos_b_R = pmdepth_b * (1.0f - cos_table[(unsigned long) phase_pm_b_R]);
                n_b_R = floorf(fpos_b_R);
                rem_b_R = fpos_b_R - n_b_R;
                sa_b_R = read_buffer(ptr->ringbuffer_b_R,
				     ptr->buflen_b_R, ptr->pos_b_R, (unsigned long) n_b_R);
                sb_b_R = read_buffer(ptr->ringbuffer_b_R,
				     ptr->buflen_b_R, ptr->pos_b_R, (unsigned long) n_b_R + 1);
                pm_b_R = (1 - rem_b_R) * sa_b_R + rem_b_R * sb_b_R;


		*(output_L++) =
			hrbal * pm_h_L * (1.0f + 0.5f * stwidth/100.0f *
					  cos_table[(unsigned long) phase_h_L]) +
			(1.0f - hrbal) * pm_b_L * (1.0f + 0.5f * stwidth/100.0f *
						   cos_table[(unsigned long) phase_b_L]);

		*(output_R++) =
			hrbal * pm_h_R * (1.0f + 0.5f * stwidth/100.0f *
					  cos_table[(unsigned long) phase_h_R]) +
			(1.0f - hrbal) * pm_b_R * (1.0f + 0.5f * stwidth/100.0f *
						   cos_table[(unsigned long) phase_b_R]);
	}

        ptr->phase_h += 1024.0f * freq_h * sample_index / ptr->sample_rate;
	while (ptr->phase_h >= 1024.0f)
		ptr->phase_h -= 1024.0f;
        ptr->phase_b += 1024.0f * freq_b * sample_index / ptr->sample_rate;
	while (ptr->phase_b >= 1024.0f)
		ptr->phase_b -= 1024.0f;

	*(ptr->latency) = ptr->buflen_h_L / 2;
}


void
set_run_adding_gain_RotSpkr(LADSPA_Handle Instance, LADSPA_Data gain) {

	RotSpkr * ptr = (RotSpkr *)Instance;

	ptr->run_adding_gain = gain;
}



void 
run_adding_RotSpkr(LADSPA_Handle Instance,
		   unsigned long SampleCount) {
  
	RotSpkr * ptr = (RotSpkr *)Instance;

	LADSPA_Data * input_L = ptr->input_L;
	LADSPA_Data * input_R = ptr->input_R;
	LADSPA_Data * output_L = ptr->output_L;
	LADSPA_Data * output_R = ptr->output_R;
	LADSPA_Data freq_h = LIMIT(*(ptr->hornfreq),0.0f,PM_FREQ);
	LADSPA_Data freq_b = LIMIT(*(ptr->bassfreq),0.0f,PM_FREQ);
	LADSPA_Data stwidth = LIMIT(*(ptr->stwidth),0.0f,100.0f);
	LADSPA_Data hrbal = LIMIT(*(ptr->hrbal),0.0f,1.0f);
	LADSPA_Data pmdepth_h =
		LIMIT(1.0f/(1.0f+FREQ_PITCH*freq_h/C_AIR) * ptr->sample_rate
		      / 200.0f / M_PI / freq_h, 0, ptr->buflen_h_L / 2);
	LADSPA_Data pmdepth_b =
		LIMIT(1.0f/(1.0f+FREQ_PITCH*freq_b/C_AIR) * ptr->sample_rate
		      / 200.0f / M_PI / freq_b, 0, ptr->buflen_b_L / 2);
	unsigned long sample_index;

	LADSPA_Data in_L = 0.0f, in_R = 0.0f;
	LADSPA_Data lo_L = 0.0f, lo_R = 0.0f;
	LADSPA_Data hi_L = 0.0f, hi_R = 0.0f;

	LADSPA_Data phase_h_L = 0.0f, phase_b_L = 0.0f;
	LADSPA_Data phase_h_R = 0.0f, phase_b_R = 0.0f;
	LADSPA_Data phase_pm_h_L = 0.0f, phase_pm_b_L = 0.0f;
	LADSPA_Data phase_pm_h_R = 0.0f, phase_pm_b_R = 0.0f;
	LADSPA_Data pm_h_L = 0.0f, pm_b_L = 0.0f;
	LADSPA_Data pm_h_R = 0.0f, pm_b_R = 0.0f;

	LADSPA_Data fpos_h_L = 0.0f, fpos_b_L = 0.0f, fpos_h_R = 0.0f, fpos_b_R = 0.0f;
	LADSPA_Data n_h_L = 0.0f, n_b_L = 0.0f, n_h_R = 0.0f, n_b_R = 0.0f;
	LADSPA_Data rem_h_L = 0.0f, rem_b_L = 0.0f, rem_h_R = 0.0f, rem_b_R = 0.0f;
	LADSPA_Data sa_h_L = 0.0f, sa_b_L = 0.0f, sb_h_L = 0.0f, sb_b_L = 0.0f;
	LADSPA_Data sa_h_R = 0.0f, sa_b_R = 0.0f, sb_h_R = 0.0f, sb_b_R = 0.0f;
	

	for (sample_index = 0; sample_index < SampleCount; sample_index++) {

		in_L = *(input_L++);
		in_R = *(input_R++);

		in_L = biquad_run(ptr->eq_filter_L, in_L);
		in_R = biquad_run(ptr->eq_filter_R, in_R);
		lo_L = biquad_run(ptr->lp_filter_L, in_L);
		lo_R = biquad_run(ptr->lp_filter_R, in_R);
		hi_L = biquad_run(ptr->hp_filter_L, in_L);
		hi_R = biquad_run(ptr->hp_filter_R, in_R);
	

		phase_h_L = 1024.0f * freq_h * sample_index / ptr->sample_rate + ptr->phase_h;
		while (phase_h_L >= 1024.0f)
		        phase_h_L -= 1024.0f;  
		phase_pm_h_L = phase_h_L + 256.0f;
		while (phase_pm_h_L >= 1024.0f)
			phase_pm_h_L -= 1024.0f;
 		phase_h_R = phase_h_L + 512.0f;
		while (phase_h_R >= 1024.0f)
		        phase_h_R -= 1024.0f;
		phase_pm_h_R = phase_h_R + 256.0f;
		while (phase_pm_h_R >= 1024.0f)
			phase_pm_h_R -= 1024.0f;

		phase_b_L = 1024.0f * freq_b * sample_index / ptr->sample_rate + ptr->phase_b;
		while (phase_b_L >= 1024.0f)
		        phase_b_L -= 1024.0f;  
		phase_pm_b_L = phase_b_L + 256.0f;
		while (phase_pm_b_L >= 1024.0f)
			phase_pm_b_L -= 1024.0f;
 		phase_b_R = phase_b_L + 512.0f;
		while (phase_b_R >= 1024.0f)
		        phase_b_R -= 1024.0f;
		phase_pm_b_R = phase_b_R + 256.0f;
		while (phase_pm_b_R >= 1024.0f)
			phase_pm_b_R -= 1024.0f;

                push_buffer(hi_L, ptr->ringbuffer_h_L, ptr->buflen_h_L, &(ptr->pos_h_L));
                push_buffer(hi_R, ptr->ringbuffer_h_R, ptr->buflen_h_R, &(ptr->pos_h_R));
                push_buffer(lo_L, ptr->ringbuffer_b_L, ptr->buflen_b_L, &(ptr->pos_b_L));
                push_buffer(lo_R, ptr->ringbuffer_b_R, ptr->buflen_b_R, &(ptr->pos_b_R));

                fpos_h_L = pmdepth_h * (1.0f - cos_table[(unsigned long) phase_pm_h_L]);
                n_h_L = floorf(fpos_h_L);
                rem_h_L = fpos_h_L - n_h_L;
                sa_h_L = read_buffer(ptr->ringbuffer_h_L,
				     ptr->buflen_h_L, ptr->pos_h_L, (unsigned long) n_h_L);
                sb_h_L = read_buffer(ptr->ringbuffer_h_L,
				     ptr->buflen_h_L, ptr->pos_h_L, (unsigned long) n_h_L + 1);
                pm_h_L = (1 - rem_h_L) * sa_h_L + rem_h_L * sb_h_L;

                fpos_h_R = pmdepth_h * (1.0f - cos_table[(unsigned long) phase_pm_h_R]);
                n_h_R = floorf(fpos_h_R);
                rem_h_R = fpos_h_R - n_h_R;
                sa_h_R = read_buffer(ptr->ringbuffer_h_R,
				     ptr->buflen_h_R, ptr->pos_h_R, (unsigned long) n_h_R);
                sb_h_R = read_buffer(ptr->ringbuffer_h_R,
				     ptr->buflen_h_R, ptr->pos_h_R, (unsigned long) n_h_R + 1);
                pm_h_R = (1 - rem_h_R) * sa_h_R + rem_h_R * sb_h_R;


                fpos_b_L = pmdepth_b * (1.0f - cos_table[(unsigned long) phase_pm_b_L]);
                n_b_L = floorf(fpos_b_L);
                rem_b_L = fpos_b_L - n_b_L;
                sa_b_L = read_buffer(ptr->ringbuffer_b_L,
				     ptr->buflen_b_L, ptr->pos_b_L, (unsigned long) n_b_L);
                sb_b_L = read_buffer(ptr->ringbuffer_b_L,
				     ptr->buflen_b_L, ptr->pos_b_L, (unsigned long) n_b_L + 1);
                pm_b_L = (1 - rem_b_L) * sa_b_L + rem_b_L * sb_b_L;

                fpos_b_R = pmdepth_b * (1.0f - cos_table[(unsigned long) phase_pm_b_R]);
                n_b_R = floorf(fpos_b_R);
                rem_b_R = fpos_b_R - n_b_R;
                sa_b_R = read_buffer(ptr->ringbuffer_b_R,
				     ptr->buflen_b_R, ptr->pos_b_R, (unsigned long) n_b_R);
                sb_b_R = read_buffer(ptr->ringbuffer_b_R,
				     ptr->buflen_b_R, ptr->pos_b_R, (unsigned long) n_b_R + 1);
                pm_b_R = (1 - rem_b_R) * sa_b_R + rem_b_R * sb_b_R;


		*(output_L++) += ptr->run_adding_gain *
			hrbal * pm_h_L * (1.0f + 0.5f * stwidth/100.0f *
					  cos_table[(unsigned long) phase_h_L]) +
			(1.0f - hrbal) * pm_b_L * (1.0f + 0.5f * stwidth/100.0f *
						   cos_table[(unsigned long) phase_b_L]);

		*(output_R++) += ptr->run_adding_gain *
			hrbal * pm_h_R * (1.0f + 0.5f * stwidth/100.0f *
					  cos_table[(unsigned long) phase_h_R]) +
			(1.0f - hrbal) * pm_b_R * (1.0f + 0.5f * stwidth/100.0f *
						   cos_table[(unsigned long) phase_b_R]);
	}

        ptr->phase_h += 1024.0f * freq_h * sample_index / ptr->sample_rate;
	while (ptr->phase_h >= 1024.0f)
		ptr->phase_h -= 1024.0f;
        ptr->phase_b += 1024.0f * freq_b * sample_index / ptr->sample_rate;
	while (ptr->phase_b >= 1024.0f)
		ptr->phase_b -= 1024.0f;

	*(ptr->latency) = ptr->buflen_h_L / 2;
}



/*
   Throw away an RotSpkr effect instance.
   This function should be called only when RotSpkr was allocated with calloc.
 */
void
cleanup_RotSpkr(LADSPA_Handle Instance) {

	RotSpkr * ptr = (RotSpkr *)Instance;
	if (!ptr)
		return;
	if (ptr->ringbuffer_h_L)
		free(ptr->ringbuffer_h_L);
	if (ptr->ringbuffer_h_R)
		free(ptr->ringbuffer_h_R);
	if (ptr->ringbuffer_b_L)
		free(ptr->ringbuffer_b_L);
	if (ptr->ringbuffer_b_R)
		free(ptr->ringbuffer_b_R);
	if (ptr->eq_filter_L)
		free(ptr->eq_filter_L);
	if (ptr->eq_filter_R)
		free(ptr->eq_filter_R);
	if (ptr->lp_filter_L)
		free(ptr->lp_filter_L);
	if (ptr->lp_filter_R)
		free(ptr->lp_filter_R);
	if (ptr->hp_filter_L)
		free(ptr->hp_filter_L);
	if (ptr->hp_filter_R)
		free(ptr->hp_filter_R);
	if (Instance)
		free(Instance);
}



LADSPA_Descriptor * stereo_descriptor = NULL;



/* __attribute__((constructor)) tap_init() is called automatically when the plugin library is first
   loaded. */
void 
__attribute__((constructor)) tap_init() {
	
	int i;
	char ** port_names;
	LADSPA_PortDescriptor * port_descriptors;
	LADSPA_PortRangeHint * port_range_hints;
	
	if ((stereo_descriptor = 
	     (LADSPA_Descriptor *)malloc(sizeof(LADSPA_Descriptor))) == NULL)
		exit(1);
	
	for (i = 0; i < 1024; i++)
		cos_table[i] = cosf(i * M_PI / 512.0f);


	stereo_descriptor->UniqueID = ID_STEREO;
	stereo_descriptor->Label = strdup("tap_rotspeak");
	stereo_descriptor->Properties = LADSPA_PROPERTY_HARD_RT_CAPABLE;
	stereo_descriptor->Name = strdup("TAP Rotary Speaker");
	stereo_descriptor->Maker = strdup("Tom Szilagyi");
	stereo_descriptor->Copyright = strdup("GPL");
	stereo_descriptor->PortCount = PORTCOUNT_STEREO;

	if ((port_descriptors =
	     (LADSPA_PortDescriptor *)calloc(PORTCOUNT_STEREO, sizeof(LADSPA_PortDescriptor))) == NULL)
		exit(1);

	stereo_descriptor->PortDescriptors = (const LADSPA_PortDescriptor *)port_descriptors;
	port_descriptors[HORNFREQ] = LADSPA_PORT_INPUT | LADSPA_PORT_CONTROL;
	port_descriptors[BASSFREQ] = LADSPA_PORT_INPUT | LADSPA_PORT_CONTROL;
	port_descriptors[STWIDTH] = LADSPA_PORT_INPUT | LADSPA_PORT_CONTROL;
	port_descriptors[HRBAL] = LADSPA_PORT_INPUT | LADSPA_PORT_CONTROL;
	port_descriptors[LATENCY] = LADSPA_PORT_OUTPUT | LADSPA_PORT_CONTROL;
	port_descriptors[INPUT_L] = LADSPA_PORT_INPUT | LADSPA_PORT_AUDIO;
	port_descriptors[INPUT_R] = LADSPA_PORT_INPUT | LADSPA_PORT_AUDIO;
	port_descriptors[OUTPUT_L] = LADSPA_PORT_OUTPUT | LADSPA_PORT_AUDIO;
	port_descriptors[OUTPUT_R] = LADSPA_PORT_OUTPUT | LADSPA_PORT_AUDIO;

	if ((port_names = 
	     (char **)calloc(PORTCOUNT_STEREO, sizeof(char *))) == NULL)
		exit(1);

	stereo_descriptor->PortNames = (const char **)port_names;
	port_names[HORNFREQ] = strdup("Horn Frequency [Hz]");
	port_names[BASSFREQ] = strdup("Rotor Frequency [Hz]");
	port_names[STWIDTH] = strdup("Mic Distance [%]");
	port_names[HRBAL] = strdup("Rotor/Horn Mix");
	port_names[LATENCY] = strdup("latency");
	port_names[INPUT_L] = strdup("Input L");
	port_names[INPUT_R] = strdup("Input R");
	port_names[OUTPUT_L] = strdup("Output L");
	port_names[OUTPUT_R] = strdup("Output R");

	if ((port_range_hints = 
	     ((LADSPA_PortRangeHint *)calloc(PORTCOUNT_STEREO, sizeof(LADSPA_PortRangeHint)))) == NULL)
		exit(1);

	stereo_descriptor->PortRangeHints	= (const LADSPA_PortRangeHint *)port_range_hints;
	port_range_hints[HORNFREQ].HintDescriptor = 
		(LADSPA_HINT_BOUNDED_BELOW |
		 LADSPA_HINT_BOUNDED_ABOVE |
		 LADSPA_HINT_DEFAULT_0);
	port_range_hints[BASSFREQ].HintDescriptor = 
		(LADSPA_HINT_BOUNDED_BELOW |
		 LADSPA_HINT_BOUNDED_ABOVE |
		 LADSPA_HINT_DEFAULT_0);
	port_range_hints[STWIDTH].HintDescriptor = 
		(LADSPA_HINT_BOUNDED_BELOW |
		 LADSPA_HINT_BOUNDED_ABOVE |
		 LADSPA_HINT_DEFAULT_LOW);
	port_range_hints[HRBAL].HintDescriptor = 
		(LADSPA_HINT_BOUNDED_BELOW |
		 LADSPA_HINT_BOUNDED_ABOVE |
		 LADSPA_HINT_DEFAULT_MIDDLE);
	port_range_hints[LATENCY].HintDescriptor = 
		(LADSPA_HINT_BOUNDED_BELOW |
		 LADSPA_HINT_BOUNDED_ABOVE |
		 LADSPA_HINT_DEFAULT_MAXIMUM);
	port_range_hints[HORNFREQ].LowerBound = 0;
	port_range_hints[HORNFREQ].UpperBound = PM_FREQ;
	port_range_hints[BASSFREQ].LowerBound = 0;
	port_range_hints[BASSFREQ].UpperBound = PM_FREQ;
	port_range_hints[STWIDTH].LowerBound = 0;
	port_range_hints[STWIDTH].UpperBound = 100.0f;
	port_range_hints[HRBAL].LowerBound = 0;
	port_range_hints[HRBAL].UpperBound = 1.0f;
	port_range_hints[LATENCY].LowerBound = 0;
	port_range_hints[LATENCY].UpperBound = PM_DEPTH;
	port_range_hints[INPUT_L].HintDescriptor = 0;
	port_range_hints[INPUT_R].HintDescriptor = 0;
	port_range_hints[OUTPUT_L].HintDescriptor = 0;
	port_range_hints[OUTPUT_R].HintDescriptor = 0;
	stereo_descriptor->instantiate = instantiate_RotSpkr;
	stereo_descriptor->connect_port = connect_port_RotSpkr;
	stereo_descriptor->activate = activate_RotSpkr;
	stereo_descriptor->run = run_RotSpkr;
	stereo_descriptor->run_adding = run_adding_RotSpkr;
	stereo_descriptor->set_run_adding_gain = set_run_adding_gain_RotSpkr;
	stereo_descriptor->deactivate = NULL;
	stereo_descriptor->cleanup = cleanup_RotSpkr;
}


void
delete_descriptor(LADSPA_Descriptor * descriptor) {
	unsigned long index;
	if (descriptor) {
		free((char *)descriptor->Label);
		free((char *)descriptor->Name);
		free((char *)descriptor->Maker);
		free((char *)descriptor->Copyright);
		free((LADSPA_PortDescriptor *)descriptor->PortDescriptors);
		for (index = 0; index < descriptor->PortCount; index++)
			free((char *)(descriptor->PortNames[index]));
		free((char **)descriptor->PortNames);
		free((LADSPA_PortRangeHint *)descriptor->PortRangeHints);
		free(descriptor);
	}
}


/* __attribute__((destructor)) tap_fini() is called automatically when the library is unloaded. */
void
__attribute__((destructor)) tap_fini() {
	delete_descriptor(stereo_descriptor);
}


/* Return a descriptor of the requested plugin type. */
const LADSPA_Descriptor * 
ladspa_descriptor(unsigned long Index) {

	switch (Index) {
	case 0:
		return stereo_descriptor;
	default:
		return NULL;
	}
}