~ubuntu-branches/debian/sid/autotalent/sid

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
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
/* autotalent.c
   An auto-tuning LADSPA plugin.

   Free software by Thomas A. Baran.
   http://web.mit.edu/tbaran/www/autotalent.html
   VERSION 0.2
   March 20, 2010

   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 <stdlib.h>
#include <string.h>
#include <math.h>
#include <stdio.h>
#define PI (float)3.14159265358979323846
#define L2SC (float)3.32192809488736218171

/*****************************************************************************/

#include "ladspa.h"

#include "mayer_fft.h"

// Variables for FFT routine
typedef struct
{
  int nfft;        // size of FFT
  int numfreqs;    // number of frequencies represented (nfft/2 + 1)
  float* fft_data; // array for writing/reading to/from FFT function
} fft_vars;

// Constructor for FFT routine
fft_vars* fft_con(int nfft)
{
  fft_vars* membvars = (fft_vars*) malloc(sizeof(fft_vars));

  membvars->nfft = nfft;
  membvars->numfreqs = nfft/2 + 1;

  membvars->fft_data = (float*) calloc(nfft, sizeof(float));

  return membvars;
}

// Destructor for FFT routine
void fft_des(fft_vars* membvars)
{
  free(membvars->fft_data);

  free(membvars);
}

// Perform forward FFT of real data
// Accepts:
//   membvars - pointer to struct of FFT variables
//   input - pointer to an array of (real) input values, size nfft
//   output_re - pointer to an array of the real part of the output,
//     size nfft/2 + 1
//   output_im - pointer to an array of the imaginary part of the output,
//     size nfft/2 + 1
void fft_forward(fft_vars* membvars, float* input, float* output_re, float* output_im)
{
  int ti;
  int nfft;
  int hnfft;
  int numfreqs;

  nfft = membvars->nfft;
  hnfft = nfft/2;
  numfreqs = membvars->numfreqs;

  for (ti=0; ti<nfft; ti++) {
    membvars->fft_data[ti] = input[ti];
  }

  mayer_realfft(nfft, membvars->fft_data);

  output_im[0] = 0;
  for (ti=0; ti<hnfft; ti++) {
    output_re[ti] = membvars->fft_data[ti];
    output_im[ti+1] = membvars->fft_data[nfft-1-ti];
  }
  output_re[hnfft] = membvars->fft_data[hnfft];
  output_im[hnfft] = 0;
}

// Perform inverse FFT, returning real data
// Accepts:
//   membvars - pointer to struct of FFT variables
//   input_re - pointer to an array of the real part of the output,
//     size nfft/2 + 1
//   input_im - pointer to an array of the imaginary part of the output,
//     size nfft/2 + 1
//   output - pointer to an array of (real) input values, size nfft
void fft_inverse(fft_vars* membvars, float* input_re, float* input_im, float* output)
{
  int ti;
  int nfft;
  int hnfft;
  int numfreqs;

  nfft = membvars->nfft;
  hnfft = nfft/2;
  numfreqs = membvars->numfreqs;

  for (ti=0; ti<hnfft; ti++) {
    membvars->fft_data[ti] = input_re[ti];
    membvars->fft_data[nfft-1-ti] = input_im[ti+1];
  }
  membvars->fft_data[hnfft] = input_re[hnfft];

  mayer_realifft(nfft, membvars->fft_data);

  for (ti=0; ti<nfft; ti++) {
    output[ti] = membvars->fft_data[ti];
  }
}

// DONE WITH FFT CODE


// The port numbers

#define AT_TUNE 0
#define AT_FIXED 1
#define AT_PULL 2
#define AT_A 3
#define AT_Bb 4
#define AT_B 5
#define AT_C 6
#define AT_Db 7
#define AT_D 8
#define AT_Eb 9
#define AT_E 10
#define AT_F 11
#define AT_Gb 12
#define AT_G 13
#define AT_Ab 14
#define AT_AMOUNT 15
#define AT_SMOOTH 16
#define AT_SHIFT 17
#define AT_SCWARP 18
#define AT_LFOAMP 19
#define AT_LFORATE 20
#define AT_LFOSHAPE 21
#define AT_LFOSYMM 22
#define AT_LFOQUANT 23
#define AT_FCORR 24
#define AT_FWARP 25
#define AT_MIX 26
#define AT_PITCH 27
#define AT_CONF 28
#define AT_INPUT1  29
#define AT_OUTPUT1 30
#define AT_LATENCY 31



/*************************
 *  THE MEMBER VARIABLES *
 *************************/

typedef struct {

  LADSPA_Data* m_pfTune;
  LADSPA_Data* m_pfFixed;
  LADSPA_Data* m_pfPull;
  LADSPA_Data* m_pfA;
  LADSPA_Data* m_pfBb;
  LADSPA_Data* m_pfB;
  LADSPA_Data* m_pfC;
  LADSPA_Data* m_pfDb;
  LADSPA_Data* m_pfD;
  LADSPA_Data* m_pfEb;
  LADSPA_Data* m_pfE;
  LADSPA_Data* m_pfF;
  LADSPA_Data* m_pfGb;
  LADSPA_Data* m_pfG;
  LADSPA_Data* m_pfAb;
  LADSPA_Data* m_pfAmount;
  LADSPA_Data* m_pfSmooth;
  LADSPA_Data* m_pfShift;
  LADSPA_Data* m_pfScwarp;
  LADSPA_Data* m_pfLfoamp;
  LADSPA_Data* m_pfLforate;
  LADSPA_Data* m_pfLfoshape;
  LADSPA_Data* m_pfLfosymm;
  LADSPA_Data* m_pfLfoquant;
  LADSPA_Data* m_pfFcorr;
  LADSPA_Data* m_pfFwarp;
  LADSPA_Data* m_pfMix;
  LADSPA_Data* m_pfPitch;
  LADSPA_Data* m_pfConf;
  LADSPA_Data* m_pfInputBuffer1;
  LADSPA_Data* m_pfOutputBuffer1;
  LADSPA_Data* m_pfLatency;
  fft_vars* fmembvars; // member variables for fft routine

  unsigned long fs; // Sample rate

  unsigned long cbsize; // size of circular buffer
  unsigned long corrsize; // cbsize/2 + 1
  unsigned long cbiwr;
  unsigned long cbord;
  float* cbi; // circular input buffer
  float* cbf; // circular formant correction buffer
  float* cbo; // circular output buffer

  float* cbwindow; // hann of length N/2, zeros for the rest
  float* acwinv; // inverse of autocorrelation of window
  float* hannwindow; // length-N hann
  int noverlap;

  float* ffttime;
  float* fftfreqre;
  float* fftfreqim;

  // VARIABLES FOR LOW-RATE SECTION
  float aref; // A tuning reference (Hz)
  float inpitch; // Input pitch (semitones)
  float conf; // Confidence of pitch period estimate (between 0 and 1)
  float outpitch; // Output pitch (semitones)
  float vthresh; // Voiced speech threshold

  float pmax; // Maximum allowable pitch period (seconds)
  float pmin; // Minimum allowable pitch period (seconds)
  unsigned long nmax; // Maximum period index for pitch prd est
  unsigned long nmin; // Minimum period index for pitch prd est

  float lrshift; // Shift prescribed by low-rate section
  int ptarget; // Pitch target, between 0 and 11
  float sptarget; // Smoothed pitch target

  float lfophase;

  // VARIABLES FOR PITCH SHIFTER
  float phprdd; // default (unvoiced) phase period
  double inphinc; // input phase increment
  double outphinc; // input phase increment
  double phincfact; // factor determining output phase increment
  double phasein;
  double phaseout;
  float* frag; // windowed fragment of speech
  unsigned long fragsize; // size of fragment in samples

  // VARIABLES FOR FORMANT CORRECTOR
  int ford;
  float falph;
  float flamb;
  float* fk;
  float* fb;
  float* fc;
  float* frb;
  float* frc;
  float* fsig;
  float* fsmooth;
  float fhp;
  float flp;
  float flpa;
  float** fbuff;
  float* ftvec;
  float fmute;
  float fmutealph;

} Autotalent;



  /********************
   *  THE CONSTRUCTOR *
   ********************/

LADSPA_Handle 
instantiateAutotalent(const LADSPA_Descriptor * Descriptor,
		     unsigned long             SampleRate) {

  unsigned long ti;

  Autotalent* membvars = malloc(sizeof(Autotalent));

  membvars->aref = 440;
  
  membvars->fs = SampleRate;

  if (SampleRate>=88200) {
    membvars->cbsize = 4096;
  }
  else {
    membvars->cbsize = 2048;
  }
  membvars->corrsize = membvars->cbsize / 2 + 1;

  membvars->pmax = 1/(float)70;  // max and min periods (ms)
  membvars->pmin = 1/(float)700; // eventually may want to bring these out as sliders

  membvars->nmax = (unsigned long)(SampleRate * membvars->pmax);
  if (membvars->nmax > membvars->corrsize) {
    membvars->nmax = membvars->corrsize;
  }
  membvars->nmin = (unsigned long)(SampleRate * membvars->pmin);

  membvars->cbi = calloc(membvars->cbsize, sizeof(float));
  membvars->cbf = calloc(membvars->cbsize, sizeof(float));
  membvars->cbo = calloc(membvars->cbsize, sizeof(float));

  membvars->cbiwr = 0;
  membvars->cbord = 0;

  membvars->lfophase = 0;

  // Initialize formant corrector
  membvars->ford = 7; // should be sufficient to capture formants
  membvars->falph = pow(0.001, (float) 80 / (SampleRate));
  membvars->flamb = -(0.8517*sqrt(atan(0.06583*SampleRate))-0.1916); // or about -0.88 @ 44.1kHz
  membvars->fk = calloc(membvars->ford, sizeof(float));
  membvars->fb = calloc(membvars->ford, sizeof(float));
  membvars->fc = calloc(membvars->ford, sizeof(float));
  membvars->frb = calloc(membvars->ford, sizeof(float));
  membvars->frc = calloc(membvars->ford, sizeof(float));
  membvars->fsig = calloc(membvars->ford, sizeof(float));
  membvars->fsmooth = calloc(membvars->ford, sizeof(float));
  membvars->fhp = 0;
  membvars->flp = 0;
  membvars->flpa = pow(0.001, (float) 10 / (SampleRate));
  membvars->fbuff = (float**) malloc((membvars->ford)*sizeof(float*));
  for (ti=0; ti<membvars->ford; ti++) {
    membvars->fbuff[ti] = calloc(membvars->cbsize, sizeof(float));
  }
  membvars->ftvec = calloc(membvars->ford, sizeof(float));
  membvars->fmute = 1;
  membvars->fmutealph = pow(0.001, (float)1 / (SampleRate));

  // Standard raised cosine window, max height at N/2
  membvars->hannwindow = calloc(membvars->cbsize, sizeof(float));
  for (ti=0; ti<membvars->cbsize; ti++) {
    membvars->hannwindow[ti] = -0.5*cos(2*PI*ti/membvars->cbsize) + 0.5;
  }

  // Generate a window with a single raised cosine from N/4 to 3N/4
  membvars->cbwindow = calloc(membvars->cbsize, sizeof(float));
  for (ti=0; ti<(membvars->cbsize / 2); ti++) {
    membvars->cbwindow[ti+membvars->cbsize/4] = -0.5*cos(4*PI*ti/(membvars->cbsize - 1)) + 0.5;
  }

  membvars->noverlap = 4;

  membvars->fmembvars = fft_con(membvars->cbsize);

  membvars->ffttime = calloc(membvars->cbsize, sizeof(float));
  membvars->fftfreqre = calloc(membvars->corrsize, sizeof(float));
  membvars->fftfreqim = calloc(membvars->corrsize, sizeof(float));


  // ---- Calculate autocorrelation of window ----
  membvars->acwinv = calloc(membvars->cbsize, sizeof(float));
  for (ti=0; ti<membvars->cbsize; ti++) {
    membvars->ffttime[ti] = membvars->cbwindow[ti];
  }
  fft_forward(membvars->fmembvars, membvars->cbwindow, membvars->fftfreqre, membvars->fftfreqim);
  for (ti=0; ti<membvars->corrsize; ti++) {
    membvars->fftfreqre[ti] = (membvars->fftfreqre[ti])*(membvars->fftfreqre[ti]) + (membvars->fftfreqim[ti])*(membvars->fftfreqim[ti]);
    membvars->fftfreqim[ti] = 0;
  }
  fft_inverse(membvars->fmembvars, membvars->fftfreqre, membvars->fftfreqim, membvars->ffttime);
  for (ti=1; ti<membvars->cbsize; ti++) {
    membvars->acwinv[ti] = membvars->ffttime[ti]/membvars->ffttime[0];
    if (membvars->acwinv[ti] > 0.000001) {
      membvars->acwinv[ti] = (float)1/membvars->acwinv[ti];
    }
    else {
      membvars->acwinv[ti] = 0;
    }
  }
  membvars->acwinv[0] = 1;
  // ---- END Calculate autocorrelation of window ----
  

  membvars->lrshift = 0;
  membvars->ptarget = 0;
  membvars->sptarget = 0;

  membvars->vthresh = 0.7;  //  The voiced confidence (unbiased peak) threshold level

  // Pitch shifter initialization
  membvars->phprdd = 0.01; // Default period
  membvars->inphinc = (float)1/(membvars->phprdd * SampleRate);
  membvars->phincfact = 1;
  membvars->phasein = 0;
  membvars->phaseout = 0;
  membvars->frag = calloc(membvars->cbsize, sizeof(float));
  membvars->fragsize = 0;
  

  return membvars;
}


//  Connect port
void 
connectPortToAutotalent(LADSPA_Handle Instance,
		       unsigned long Port,
		       LADSPA_Data * DataLocation) {

  Autotalent * psAutotalent;

  psAutotalent = (Autotalent *)Instance;
  switch (Port) {
  case AT_TUNE:
    psAutotalent->m_pfTune = DataLocation;
    break;
  case AT_FIXED:
    psAutotalent->m_pfFixed = DataLocation;
    break;
  case AT_PULL:
    psAutotalent->m_pfPull = DataLocation;
    break;
  case AT_A:
    psAutotalent->m_pfA = DataLocation;
    break;
  case AT_Bb:
    psAutotalent->m_pfBb = DataLocation;
    break;
  case AT_B:
    psAutotalent->m_pfB = DataLocation;
    break;
  case AT_C:
    psAutotalent->m_pfC = DataLocation;
    break;
  case AT_Db:
    psAutotalent->m_pfDb = DataLocation;
    break;
  case AT_D:
    psAutotalent->m_pfD = DataLocation;
    break;
  case AT_Eb:
    psAutotalent->m_pfEb = DataLocation;
    break;
  case AT_E:
    psAutotalent->m_pfE = DataLocation;
    break;
  case AT_F:
    psAutotalent->m_pfF = DataLocation;
    break;
  case AT_Gb:
    psAutotalent->m_pfGb = DataLocation;
    break;
  case AT_G:
    psAutotalent->m_pfG = DataLocation;
    break;
  case AT_Ab:
    psAutotalent->m_pfAb = DataLocation;
    break;
  case AT_AMOUNT:
    psAutotalent->m_pfAmount = DataLocation;
    break;
  case AT_SMOOTH:
    psAutotalent->m_pfSmooth = DataLocation;
    break;
  case AT_SHIFT:
    psAutotalent->m_pfShift = DataLocation;
    break;
  case AT_SCWARP:
    psAutotalent->m_pfScwarp = DataLocation;
    break;
  case AT_LFOAMP:
    psAutotalent->m_pfLfoamp = DataLocation;
    break;
  case AT_LFORATE:
    psAutotalent->m_pfLforate = DataLocation;
    break;
  case AT_LFOSHAPE:
    psAutotalent->m_pfLfoshape = DataLocation;
    break;
  case AT_LFOSYMM:
    psAutotalent->m_pfLfosymm = DataLocation;
    break;
  case AT_LFOQUANT:
    psAutotalent->m_pfLfoquant = DataLocation;
    break;
  case AT_FCORR:
    psAutotalent->m_pfFcorr = DataLocation;
    break;
  case AT_FWARP:
    psAutotalent->m_pfFwarp = DataLocation;
    break;
  case AT_MIX:
    psAutotalent->m_pfMix = DataLocation;
    break;
  case AT_PITCH:
    psAutotalent->m_pfPitch = DataLocation;
    break;
  case AT_CONF:
    psAutotalent->m_pfConf = DataLocation;
    break;
  case AT_INPUT1:
    psAutotalent->m_pfInputBuffer1 = DataLocation;
    break;
  case AT_OUTPUT1:
    psAutotalent->m_pfOutputBuffer1 = DataLocation;
    break;
  case AT_LATENCY:
    psAutotalent->m_pfLatency = DataLocation;
    *(psAutotalent->m_pfLatency) = psAutotalent->cbsize - 1;
    break;
  }
}


// Called every time we get a new chunk of audio
void
runAutotalent(LADSPA_Handle Instance,
		 unsigned long SampleCount) {
  
  LADSPA_Data* pfInput;
  LADSPA_Data* pfOutput;
  float fAmount;
  float fSmooth;
  int iNotes[12];
  int iPitch2Note[12];
  int iNote2Pitch[12];
  int numNotes;
  float fTune;
  float fFixed;
  float fPull;
  float fShift;
  int iScwarp;
  float fLfoamp;
  float fLforate;
  float fLfoshape;
  float fLfosymm;
  int iLfoquant;
  int iFcorr;
  float fFwarp;
  float fMix;
  Autotalent* psAutotalent;
  unsigned long lSampleIndex;

  long int N;
  long int Nf;
  long int fs;
  float pmin;
  float pmax;
  unsigned long nmin;
  unsigned long nmax;

  long int ti;
  long int ti2;
  long int ti3;
  long int ti4;
  float tf;
  float tf2;

  // Variables for cubic spline interpolator
  float indd;
  int ind0;
  int ind1;
  int ind2;
  int ind3;
  float vald;
  float val0;
  float val1;
  float val2;
  float val3;

  int lowersnap;
  int uppersnap;
  float lfoval;

  float pperiod;
  float inpitch;
  float conf;
  float outpitch;
  float aref;
  float fa;
  float fb;
  float fc;
  float fk;
  float flamb;
  float frlamb;
  float falph;
  float foma;
  float f1resp;
  float f0resp;
  float flpa;
  int ford;
  psAutotalent = (Autotalent *)Instance;

  pfInput = psAutotalent->m_pfInputBuffer1;
  pfOutput = psAutotalent->m_pfOutputBuffer1;
  fAmount = (float) *(psAutotalent->m_pfAmount);
  fSmooth = (float) *(psAutotalent->m_pfSmooth) * 0.8; // Scales max to a more reasonable value
  fTune = (float) *(psAutotalent->m_pfTune);
  iNotes[0] = (int) *(psAutotalent->m_pfA);
  iNotes[1] = (int) *(psAutotalent->m_pfBb);
  iNotes[2] = (int) *(psAutotalent->m_pfB);
  iNotes[3] = (int) *(psAutotalent->m_pfC);
  iNotes[4] = (int) *(psAutotalent->m_pfDb);
  iNotes[5] = (int) *(psAutotalent->m_pfD);
  iNotes[6] = (int) *(psAutotalent->m_pfEb);
  iNotes[7] = (int) *(psAutotalent->m_pfE);
  iNotes[8] = (int) *(psAutotalent->m_pfF);
  iNotes[9] = (int) *(psAutotalent->m_pfGb);
  iNotes[10] = (int) *(psAutotalent->m_pfG);
  iNotes[11] = (int) *(psAutotalent->m_pfAb);
  fFixed = (float) *(psAutotalent->m_pfFixed);
  fPull = (float) *(psAutotalent->m_pfPull);
  fShift = (float) *(psAutotalent->m_pfShift);
  iScwarp = (int) *(psAutotalent->m_pfScwarp);
  fLfoamp = (float) *(psAutotalent->m_pfLfoamp);
  fLforate = (float) *(psAutotalent->m_pfLforate);
  fLfoshape = (float) *(psAutotalent->m_pfLfoshape);
  fLfosymm = (float) *(psAutotalent->m_pfLfosymm);
  iLfoquant = (int) *(psAutotalent->m_pfLfoquant);
  iFcorr = (int) *(psAutotalent->m_pfFcorr);
  fFwarp = (float) *(psAutotalent->m_pfFwarp);
  fMix = (float) *(psAutotalent->m_pfMix);

  // Some logic for the semitone->scale and scale->semitone conversion
  // If no notes are selected as being in the scale, instead snap to all notes
  ti2 = 0;
  for (ti=0; ti<12; ti++) {
    if (iNotes[ti]>=0) {
      iPitch2Note[ti] = ti2;
      iNote2Pitch[ti2] = ti;
      ti2 = ti2 + 1;
    }
    else {
      iPitch2Note[ti] = -1;
    }
  }
  numNotes = ti2;
  while (ti2<12) {
    iNote2Pitch[ti2] = -1;
    ti2 = ti2 + 1;
  }
  if (numNotes==0) {
    for (ti=0; ti<12; ti++) {
      iNotes[ti] = 1;
      iPitch2Note[ti] = ti;
      iNote2Pitch[ti] = ti;
    }
    numNotes = 12;
  }
  iScwarp = (iScwarp + numNotes*5)%numNotes;

  ford = psAutotalent->ford;
  falph = psAutotalent->falph;
  foma = (float)1 - falph;
  flpa = psAutotalent->flpa;
  flamb = psAutotalent->flamb;
  tf = pow((float)2,fFwarp/2)*(1+flamb)/(1-flamb);
  frlamb = (tf - 1)/(tf + 1);

  psAutotalent->aref = (float)fTune;

  N = psAutotalent->cbsize;
  Nf = psAutotalent->corrsize;
  fs = psAutotalent->fs;

  pmax = psAutotalent->pmax;
  pmin = psAutotalent->pmin;
  nmax = psAutotalent->nmax;
  nmin = psAutotalent->nmin;

  aref = psAutotalent->aref;
  pperiod = psAutotalent->pmax;
  inpitch = psAutotalent->inpitch;
  conf = psAutotalent->conf;
  outpitch = psAutotalent->outpitch;


  /*******************
   *  MAIN DSP LOOP  *
   *******************/
  for (lSampleIndex = 0; lSampleIndex < SampleCount; lSampleIndex++)  {
    
    // load data into circular buffer
    tf = (float) *(pfInput++);
    ti4 = psAutotalent->cbiwr;
    psAutotalent->cbi[ti4] = tf;

    if (iFcorr>=1) {
      // Somewhat experimental formant corrector
      //  formants are removed using an adaptive pre-filter and
      //  re-introduced after pitch manipulation using post-filter
      // tf is signal input
      fa = tf - psAutotalent->fhp; // highpass pre-emphasis filter
      psAutotalent->fhp = tf;
      fb = fa;
      for (ti=0; ti<ford; ti++) {
	psAutotalent->fsig[ti] = fa*fa*foma + psAutotalent->fsig[ti]*falph;
	fc = (fb-psAutotalent->fc[ti])*flamb + psAutotalent->fb[ti];
	psAutotalent->fc[ti] = fc;
	psAutotalent->fb[ti] = fb;
	fk = fa*fc*foma + psAutotalent->fk[ti]*falph;
	psAutotalent->fk[ti] = fk;
	tf = fk/(psAutotalent->fsig[ti] + 0.000001);
	tf = tf*foma + psAutotalent->fsmooth[ti]*falph;
	psAutotalent->fsmooth[ti] = tf;
	psAutotalent->fbuff[ti][ti4] = tf;
	fb = fc - tf*fa;
	fa = fa - tf*fc;
      }
      psAutotalent->cbf[ti4] = fa;
      // Now hopefully the formants are reduced
      // More formant correction code at the end of the DSP loop
    }
    else {
      psAutotalent->cbf[ti4] = tf;
    }


    // Input write pointer logic
    psAutotalent->cbiwr++;
    if (psAutotalent->cbiwr >= N) {
      psAutotalent->cbiwr = 0;
    }


    // ********************
    // * Low-rate section *
    // ********************

    // Every N/noverlap samples, run pitch estimation / manipulation code
    if ((psAutotalent->cbiwr)%(N/psAutotalent->noverlap) == 0) {

      // ---- Obtain autocovariance ----

      // Window and fill FFT buffer
      ti2 = psAutotalent->cbiwr;
      for (ti=0; ti<N; ti++) {
	psAutotalent->ffttime[ti] = (float)(psAutotalent->cbi[(ti2-ti+N)%N]*psAutotalent->cbwindow[ti]);
      }

      // Calculate FFT
      fft_forward(psAutotalent->fmembvars, psAutotalent->ffttime, psAutotalent->fftfreqre, psAutotalent->fftfreqim);

      // Remove DC
      psAutotalent->fftfreqre[0] = 0;
      psAutotalent->fftfreqim[0] = 0;

      // Take magnitude squared
      for (ti=1; ti<Nf; ti++) {
	psAutotalent->fftfreqre[ti] = (psAutotalent->fftfreqre[ti])*(psAutotalent->fftfreqre[ti]) + (psAutotalent->fftfreqim[ti])*(psAutotalent->fftfreqim[ti]);
	psAutotalent->fftfreqim[ti] = 0;
      }

      // Calculate IFFT
      fft_inverse(psAutotalent->fmembvars, psAutotalent->fftfreqre, psAutotalent->fftfreqim, psAutotalent->ffttime);

      // Normalize
      tf = (float)1/psAutotalent->ffttime[0];
      for (ti=1; ti<N; ti++) {
	psAutotalent->ffttime[ti] = psAutotalent->ffttime[ti] * tf;
      }
      psAutotalent->ffttime[0] = 1;

      //  ---- END Obtain autocovariance ----


      //  ---- Calculate pitch and confidence ----

      // Calculate pitch period
      //   Pitch period is determined by the location of the max (biased)
      //     peak within a given range
      //   Confidence is determined by the corresponding unbiased height
      tf2 = 0;
      pperiod = pmin;
      for (ti=nmin; ti<nmax; ti++) {
	ti2 = ti-1;
	ti3 = ti+1;
	if (ti2<0) {
	  ti2 = 0;
	}
	if (ti3>Nf) {
	  ti3 = Nf;
	}
	tf = psAutotalent->ffttime[ti];

	if (tf>psAutotalent->ffttime[ti2] && tf>=psAutotalent->ffttime[ti3] && tf>tf2) {
	  tf2 = tf;
	  ti4 = ti;
	}
      }
      if (tf2>0) {
	conf = tf2*psAutotalent->acwinv[ti4];
	if (ti4>0 && ti4<Nf) {
	  // Find the center of mass in the vicinity of the detected peak
	  tf = psAutotalent->ffttime[ti4-1]*(ti4-1);
	  tf = tf + psAutotalent->ffttime[ti4]*(ti4);
	  tf = tf + psAutotalent->ffttime[ti4+1]*(ti4+1);
	  tf = tf/(psAutotalent->ffttime[ti4-1] + psAutotalent->ffttime[ti4] + psAutotalent->ffttime[ti4+1]);
	  pperiod = tf/fs;
	}
	else {
	  pperiod = (float)ti4/fs;
	}
      }

      // Convert to semitones
      tf = (float) -12*log10((float)aref*pperiod)*L2SC;
      if (conf>=psAutotalent->vthresh) {
	inpitch = tf;
	psAutotalent->inpitch = tf; // update pitch only if voiced
      }
      psAutotalent->conf = conf;

      *(psAutotalent->m_pfPitch) = (LADSPA_Data) inpitch;
      *(psAutotalent->m_pfConf) = (LADSPA_Data) conf;

      //  ---- END Calculate pitch and confidence ----


      //  ---- Modify pitch in all kinds of ways! ----

      outpitch = inpitch;

      // Pull to fixed pitch
      outpitch = (1-fPull)*outpitch + fPull*fFixed;

      // -- Convert from semitones to scale notes --
      ti = (int)(outpitch/12 + 32) - 32; // octave
      tf = outpitch - ti*12; // semitone in octave
      ti2 = (int)tf;
      ti3 = ti2 + 1;
      // a little bit of pitch correction logic, since it's a convenient place for it
      if (iNotes[ti2%12]<0 || iNotes[ti3%12]<0) { // if between 2 notes that are more than a semitone apart
	lowersnap = 1;
	uppersnap = 1;
      }
      else {
	lowersnap = 0;
	uppersnap = 0;
	if (iNotes[ti2%12]==1) { // if specified by user
	  lowersnap = 1;
	}
	if (iNotes[ti3%12]==1) { // if specified by user
	  uppersnap = 1;
	}
      }
      // (back to the semitone->scale conversion)
      // finding next lower pitch in scale
      while (iNotes[(ti2+12)%12]<0) {
      	ti2 = ti2 - 1;
      }
      // finding next higher pitch in scale
      while (iNotes[ti3%12]<0) {
      	ti3 = ti3 + 1;
      }
      tf = (tf-ti2)/(ti3-ti2) + iPitch2Note[(ti2+12)%12];
      if (ti2<0) {
      	tf = tf - numNotes;
      }
      outpitch = tf + numNotes*ti;
      // -- Done converting to scale notes --

      // The actual pitch correction
      ti = (int)(outpitch+128) - 128;
      tf = outpitch - ti - 0.5;
      ti2 = ti3-ti2;
      if (ti2>2) { // if more than 2 semitones apart, put a 2-semitone-like transition halfway between
	tf2 = (float)ti2/2;
      }
      else {
	tf2 = (float)1;
      }
      if (fSmooth<0.001) {
	tf2 = tf*tf2/0.001;
      }
      else {
	tf2 = tf*tf2/fSmooth;
      }
      if (tf2<-0.5) tf2 = -0.5;
      if (tf2>0.5) tf2 = 0.5;
      tf2 = 0.5*sin(PI*tf2) + 0.5; // jumping between notes using horizontally-scaled sine segment
      tf2 = tf2 + ti;
      if ( (tf<0.5 && lowersnap) || (tf>=0.5 && uppersnap) ) {
	outpitch = fAmount*tf2 + ((float)1-fAmount)*outpitch;
      }

      // Add in pitch shift
      outpitch = outpitch + fShift;

      // LFO logic
      tf = fLforate*N/(psAutotalent->noverlap*fs);
      if (tf>1) tf=1;
      psAutotalent->lfophase = psAutotalent->lfophase + tf;
      if (psAutotalent->lfophase>1) psAutotalent->lfophase = psAutotalent->lfophase-1;
      lfoval = psAutotalent->lfophase;
      tf = (fLfosymm + 1)/2;
      if (tf<=0 || tf>=1) {
	if (tf<=0) lfoval = 1-lfoval;
      }
      else {
	if (lfoval<=tf) {
	  lfoval = lfoval/tf;
	}
	else {
	  lfoval = 1 - (lfoval-tf)/(1-tf);
	}
      }
      if (fLfoshape>=0) {
	// linear combination of cos and line
	lfoval = (0.5 - 0.5*cos(lfoval*PI))*fLfoshape + lfoval*(1-fLfoshape);
	lfoval = fLfoamp*(lfoval*2 - 1);
      }
      else {
	// smoosh the sine horizontally until it's squarish
	tf = 1 + fLfoshape;
	if (tf<0.001) {
	  lfoval = (lfoval - 0.5)*2/0.001;
	}
	else {
	  lfoval = (lfoval - 0.5)*2/tf;
	}
	if (lfoval>1) lfoval = 1;
	if (lfoval<-1) lfoval = -1;
	lfoval = fLfoamp*sin(lfoval*PI*0.5);
      }
      // add in quantized LFO
      if (iLfoquant>=1) {
	outpitch = outpitch + (int)(numNotes*lfoval + numNotes + 0.5) - numNotes;
      }


      // Convert back from scale notes to semitones
      outpitch = outpitch + iScwarp; // output scale rotate implemented here
      ti = (int)(outpitch/numNotes + 32) - 32;
      tf = outpitch - ti*numNotes;
      ti2 = (int)tf;
      ti3 = ti2 + 1;
      outpitch = iNote2Pitch[ti3%numNotes] - iNote2Pitch[ti2];
      if (ti3>=numNotes) {
	outpitch = outpitch + 12;
      }
      outpitch = outpitch*(tf - ti2) + iNote2Pitch[ti2];
      outpitch = outpitch + 12*ti;
      outpitch = outpitch - (iNote2Pitch[iScwarp] - iNote2Pitch[0]); //more scale rotation here

      // add in unquantized LFO
      if (iLfoquant<=0) {
	outpitch = outpitch + lfoval*2;
      }


      if (outpitch<-36) outpitch = -48;
      if (outpitch>24) outpitch = 24;

      psAutotalent->outpitch = outpitch;

      //  ---- END Modify pitch in all kinds of ways! ----

      // Compute variables for pitch shifter that depend on pitch
      psAutotalent->inphinc = aref*pow(2,inpitch/12)/fs;
      psAutotalent->outphinc = aref*pow(2,outpitch/12)/fs;
      psAutotalent->phincfact = psAutotalent->outphinc/psAutotalent->inphinc;
    }
    // ************************
    // * END Low-Rate Section *
    // ************************



    // *****************
    // * Pitch Shifter *
    // *****************

    // Pitch shifter (kind of like a pitch-synchronous version of Fairbanks' technique)
    //   Note: pitch estimate is naturally N/2 samples old
    psAutotalent->phasein = psAutotalent->phasein + psAutotalent->inphinc;
    psAutotalent->phaseout = psAutotalent->phaseout + psAutotalent->outphinc;

    //   When input phase resets, take a snippet from N/2 samples in the past
    if (psAutotalent->phasein >= 1) {
      psAutotalent->phasein = psAutotalent->phasein - 1;
      ti2 = psAutotalent->cbiwr - N/2;
      for (ti=-N/2; ti<N/2; ti++) {
	psAutotalent->frag[(ti+N)%N] = psAutotalent->cbf[(ti + ti2 + N)%N];
      }
    }

    //   When output phase resets, put a snippet N/2 samples in the future
    if (psAutotalent->phaseout >= 1) {
      psAutotalent->fragsize = psAutotalent->fragsize*2;
      if (psAutotalent->fragsize > N) {
	psAutotalent->fragsize = N;
      }
      psAutotalent->phaseout = psAutotalent->phaseout - 1;
      ti2 = psAutotalent->cbord + N/2;
      ti3 = (long int)(((float)psAutotalent->fragsize) / psAutotalent->phincfact);
      if (ti3>=N/2) {
	ti3 = N/2 - 1;
      }
      for (ti=-ti3/2; ti<(ti3/2); ti++) {
	tf = psAutotalent->hannwindow[(long int)N/2 + ti*(long int)N/ti3];
	// 3rd degree polynomial interpolator - based on eqns from Hal Chamberlin's book
	indd = psAutotalent->phincfact*ti;
	ind1 = (int)indd;
	ind2 = ind1+1;
	ind3 = ind1+2;
	ind0 = ind1-1;
	val0 = psAutotalent->frag[(ind0+N)%N];
	val1 = psAutotalent->frag[(ind1+N)%N];
	val2 = psAutotalent->frag[(ind2+N)%N];
	val3 = psAutotalent->frag[(ind3+N)%N];
	vald = 0;
	vald = vald - (float)0.166666666667 * val0 * (indd - ind1) * (indd - ind2) * (indd - ind3);
	vald = vald + (float)0.5 * val1 * (indd - ind0) * (indd - ind2) * (indd - ind3);
	vald = vald - (float)0.5 * val2 * (indd - ind0) * (indd - ind1) * (indd - ind3);
	vald = vald + (float)0.166666666667 * val3 * (indd - ind0) * (indd - ind1) * (indd - ind2);
	psAutotalent->cbo[(ti + ti2 + N)%N] = psAutotalent->cbo[(ti + ti2 + N)%N] + vald*tf;
      }
      psAutotalent->fragsize = 0;
    }
    psAutotalent->fragsize++;

    //   Get output signal from buffer
    tf = psAutotalent->cbo[psAutotalent->cbord]; // read buffer

    psAutotalent->cbo[psAutotalent->cbord] = 0; // erase for next cycle
    psAutotalent->cbord++; // increment read pointer
    if (psAutotalent->cbord >= N) {
      psAutotalent->cbord = 0;
    }

    // *********************
    // * END Pitch Shifter *
    // *********************

    ti4 = (psAutotalent->cbiwr + 2)%N;
    if (iFcorr>=1) {
      // The second part of the formant corrector
      // This is a post-filter that re-applies the formants, designed
      //   to result in the exact original signal when no pitch
      //   manipulation is performed.
      // tf is signal input
      // gotta run it 3 times because of a pesky delay free loop
      //  first time: compute 0-response
      tf2 = tf;
      fa = 0;
      fb = fa;
      for (ti=0; ti<ford; ti++) {
	fc = (fb-psAutotalent->frc[ti])*frlamb + psAutotalent->frb[ti];
	tf = psAutotalent->fbuff[ti][ti4];
	fb = fc - tf*fa;
	psAutotalent->ftvec[ti] = tf*fc;
	fa = fa - psAutotalent->ftvec[ti];
      }
      tf = -fa;
      for (ti=ford-1; ti>=0; ti--) {
	tf = tf + psAutotalent->ftvec[ti];
      }
      f0resp = tf;
      //  second time: compute 1-response
      fa = 1;
      fb = fa;
      for (ti=0; ti<ford; ti++) {
	fc = (fb-psAutotalent->frc[ti])*frlamb + psAutotalent->frb[ti];
	tf = psAutotalent->fbuff[ti][ti4];
	fb = fc - tf*fa;
	psAutotalent->ftvec[ti] = tf*fc;
	fa = fa - psAutotalent->ftvec[ti];
      }
      tf = -fa;
      for (ti=ford-1; ti>=0; ti--) {
	tf = tf + psAutotalent->ftvec[ti];
      }
      f1resp = tf;
      //  now solve equations for output, based on 0-response and 1-response
      tf = (float)2*tf2;
      tf2 = tf;
      tf = ((float)1 - f1resp + f0resp);
      if (tf!=0) {
	tf2 = (tf2 + f0resp) / tf;
      }
      else {
	tf2 = 0;
      }
      //  third time: update delay registers
      fa = tf2;
      fb = fa;
      for (ti=0; ti<ford; ti++) {
	fc = (fb-psAutotalent->frc[ti])*frlamb + psAutotalent->frb[ti];
	psAutotalent->frc[ti] = fc;
	psAutotalent->frb[ti] = fb;
	tf = psAutotalent->fbuff[ti][ti4];
	fb = fc - tf*fa;
	fa = fa - tf*fc;
      }
      tf = tf2;
      tf = tf + flpa*psAutotalent->flp;  // lowpass post-emphasis filter
      psAutotalent->flp = tf;
      // Bring up the gain slowly when formant correction goes from disabled
      // to enabled, while things stabilize.
      if (psAutotalent->fmute>0.5) {
	tf = tf*(psAutotalent->fmute - 0.5)*2;
      }
      else {
	tf = 0;
      }
      tf2 = psAutotalent->fmutealph;
      psAutotalent->fmute = (1-tf2) + tf2*psAutotalent->fmute;
      // now tf is signal output
      // ...and we're done messing with formants
    }
    else {
      psAutotalent->fmute = 0;
    }

    // Write audio to output of plugin
    // Mix (blend between original (delayed) =0 and processed =1)
    *(pfOutput++) = (LADSPA_Data) fMix*tf + (1-fMix)*psAutotalent->cbi[ti4];

  }

  // Tell the host the algorithm latency
  *(psAutotalent->m_pfLatency) = (LADSPA_Data) (N-1);
}



/********************
 *  THE DESTRUCTOR! *
 ********************/
void 
cleanupAutotalent(LADSPA_Handle Instance) {
  int ti;
  fft_des(((Autotalent*)Instance)->fmembvars);
  free(((Autotalent*)Instance)->cbi);
  free(((Autotalent*)Instance)->cbf);
  free(((Autotalent*)Instance)->cbo);
  free(((Autotalent*)Instance)->cbwindow);
  free(((Autotalent*)Instance)->hannwindow);
  free(((Autotalent*)Instance)->acwinv);
  free(((Autotalent*)Instance)->frag);
  free(((Autotalent*)Instance)->ffttime);
  free(((Autotalent*)Instance)->fftfreqre);
  free(((Autotalent*)Instance)->fftfreqim);
  free(((Autotalent*)Instance)->fk);
  free(((Autotalent*)Instance)->fb);
  free(((Autotalent*)Instance)->fc);
  free(((Autotalent*)Instance)->frb);
  free(((Autotalent*)Instance)->frc);
  free(((Autotalent*)Instance)->fsmooth);
  free(((Autotalent*)Instance)->fsig);
  for (ti=0; ti<((Autotalent*)Instance)->ford; ti++) {
    free(((Autotalent*)Instance)->fbuff[ti]);
  }
  free(((Autotalent*)Instance)->fbuff);
  free(((Autotalent*)Instance)->ftvec);
  free((Autotalent*)Instance);
}


LADSPA_Descriptor * g_psDescriptor;

// Called when first loaded
void 
_init() {

  char ** pcPortNames;
  LADSPA_PortDescriptor * piPortDescriptors;
  LADSPA_PortRangeHint * psPortRangeHints;

  g_psDescriptor
    = (LADSPA_Descriptor *)malloc(sizeof(LADSPA_Descriptor));

  if (g_psDescriptor) {
  
    g_psDescriptor->UniqueID
      = 4262;
    g_psDescriptor->Label
      = strdup("autotalent");
    g_psDescriptor->Properties
      = LADSPA_PROPERTY_HARD_RT_CAPABLE;
    g_psDescriptor->Name 
      = strdup("Autotalent");
    g_psDescriptor->Maker
      = strdup("Tom Baran");
    g_psDescriptor->Copyright
      = strdup("2010");
    g_psDescriptor->PortCount
      = 32;
    piPortDescriptors
      = (LADSPA_PortDescriptor *)calloc(32, sizeof(LADSPA_PortDescriptor));
    g_psDescriptor->PortDescriptors
      = (const LADSPA_PortDescriptor *)piPortDescriptors;
    piPortDescriptors[AT_TUNE]
      = LADSPA_PORT_INPUT | LADSPA_PORT_CONTROL;
    piPortDescriptors[AT_FIXED]
      = LADSPA_PORT_INPUT | LADSPA_PORT_CONTROL;
    piPortDescriptors[AT_PULL]
      = LADSPA_PORT_INPUT | LADSPA_PORT_CONTROL;
    piPortDescriptors[AT_A]
      = LADSPA_PORT_INPUT | LADSPA_PORT_CONTROL;
    piPortDescriptors[AT_Bb]
      = LADSPA_PORT_INPUT | LADSPA_PORT_CONTROL;
    piPortDescriptors[AT_B]
      = LADSPA_PORT_INPUT | LADSPA_PORT_CONTROL;
    piPortDescriptors[AT_C]
      = LADSPA_PORT_INPUT | LADSPA_PORT_CONTROL;
    piPortDescriptors[AT_Db]
      = LADSPA_PORT_INPUT | LADSPA_PORT_CONTROL;
    piPortDescriptors[AT_D]
      = LADSPA_PORT_INPUT | LADSPA_PORT_CONTROL;
    piPortDescriptors[AT_Eb]
      = LADSPA_PORT_INPUT | LADSPA_PORT_CONTROL;
    piPortDescriptors[AT_E]
      = LADSPA_PORT_INPUT | LADSPA_PORT_CONTROL;
    piPortDescriptors[AT_F]
      = LADSPA_PORT_INPUT | LADSPA_PORT_CONTROL;
    piPortDescriptors[AT_Gb]
      = LADSPA_PORT_INPUT | LADSPA_PORT_CONTROL;
    piPortDescriptors[AT_G]
      = LADSPA_PORT_INPUT | LADSPA_PORT_CONTROL;
    piPortDescriptors[AT_Ab]
      = LADSPA_PORT_INPUT | LADSPA_PORT_CONTROL;
    piPortDescriptors[AT_AMOUNT]
      = LADSPA_PORT_INPUT | LADSPA_PORT_CONTROL;
    piPortDescriptors[AT_SMOOTH]
      = LADSPA_PORT_INPUT | LADSPA_PORT_CONTROL;
    piPortDescriptors[AT_SHIFT]
      = LADSPA_PORT_INPUT | LADSPA_PORT_CONTROL;
    piPortDescriptors[AT_SCWARP]
      = LADSPA_PORT_INPUT | LADSPA_PORT_CONTROL;
    piPortDescriptors[AT_LFOAMP]
      = LADSPA_PORT_INPUT | LADSPA_PORT_CONTROL;
    piPortDescriptors[AT_LFORATE]
      = LADSPA_PORT_INPUT | LADSPA_PORT_CONTROL;
    piPortDescriptors[AT_LFOSHAPE]
      = LADSPA_PORT_INPUT | LADSPA_PORT_CONTROL;
    piPortDescriptors[AT_LFOSYMM]
      = LADSPA_PORT_INPUT | LADSPA_PORT_CONTROL;
    piPortDescriptors[AT_LFOQUANT]
      = LADSPA_PORT_INPUT | LADSPA_PORT_CONTROL;
    piPortDescriptors[AT_FCORR]
      = LADSPA_PORT_INPUT | LADSPA_PORT_CONTROL;
    piPortDescriptors[AT_FWARP]
      = LADSPA_PORT_INPUT | LADSPA_PORT_CONTROL;
    piPortDescriptors[AT_MIX]
      = LADSPA_PORT_INPUT | LADSPA_PORT_CONTROL;
    piPortDescriptors[AT_PITCH]
      = LADSPA_PORT_OUTPUT | LADSPA_PORT_CONTROL;
    piPortDescriptors[AT_CONF]
      = LADSPA_PORT_OUTPUT | LADSPA_PORT_CONTROL;
    piPortDescriptors[AT_INPUT1]
      = LADSPA_PORT_INPUT | LADSPA_PORT_AUDIO;
    piPortDescriptors[AT_OUTPUT1]
      = LADSPA_PORT_OUTPUT | LADSPA_PORT_AUDIO;
    piPortDescriptors[AT_LATENCY]
      = LADSPA_PORT_OUTPUT | LADSPA_PORT_CONTROL;
    pcPortNames
      = (char **)calloc(32, sizeof(char *));
    g_psDescriptor->PortNames 
      = (const char **)pcPortNames;
    pcPortNames[AT_TUNE]
      = strdup("Concert A (Hz)");
    pcPortNames[AT_FIXED]
      = strdup("Fixed pitch (semitones w.r.t. A)");
    pcPortNames[AT_PULL]
      = strdup("Pull to fixed pitch");
    pcPortNames[AT_A]
      = strdup("A");
    pcPortNames[AT_Bb]
      = strdup("Bb");
    pcPortNames[AT_B]
      = strdup("B");
    pcPortNames[AT_C]
      = strdup("C");
    pcPortNames[AT_Db]
      = strdup("Db");
    pcPortNames[AT_D]
      = strdup("D");
    pcPortNames[AT_Eb]
      = strdup("Eb");
    pcPortNames[AT_E]
      = strdup("E");
    pcPortNames[AT_F]
      = strdup("F");
    pcPortNames[AT_Gb]
      = strdup("Gb");
    pcPortNames[AT_G]
      = strdup("G");
    pcPortNames[AT_Ab]
      = strdup("Ab");
    pcPortNames[AT_AMOUNT]
      = strdup("Correction strength");
    pcPortNames[AT_SMOOTH]
      = strdup("Correction smoothness");
    pcPortNames[AT_SHIFT]
      = strdup("Pitch shift (scale notes)");
    pcPortNames[AT_SCWARP]
      = strdup("Output scale rotate (scale notes)");
    pcPortNames[AT_LFOAMP]
      = strdup("LFO depth");
    pcPortNames[AT_LFORATE]
      = strdup("LFO rate (Hz)");
    pcPortNames[AT_LFOSHAPE]
      = strdup("LFO shape (square->sine->tri)");
    pcPortNames[AT_LFOSYMM]
      = strdup("LFO symmetry");
    pcPortNames[AT_LFOQUANT]
      = strdup("LFO quantization");
    pcPortNames[AT_FCORR]
      = strdup("Formant correction");
    pcPortNames[AT_FWARP]
      = strdup("Formant warp");
    pcPortNames[AT_MIX]
      = strdup("Mix");
    pcPortNames[AT_PITCH]
      = strdup("Detected pitch (semitones w.r.t. A)");
    pcPortNames[AT_CONF]
      = strdup("Pitch detection confidence");
    pcPortNames[AT_INPUT1]
      = strdup("Input");
    pcPortNames[AT_OUTPUT1]
      = strdup("Output");
    pcPortNames[AT_LATENCY]
      = strdup("latency");
    psPortRangeHints = ((LADSPA_PortRangeHint *)
			calloc(32, sizeof(LADSPA_PortRangeHint)));
    g_psDescriptor->PortRangeHints
      = (const LADSPA_PortRangeHint *)psPortRangeHints;
    psPortRangeHints[AT_TUNE].HintDescriptor
      = (LADSPA_HINT_BOUNDED_BELOW |
	 LADSPA_HINT_BOUNDED_ABOVE |
	 LADSPA_HINT_DEFAULT_440);
    psPortRangeHints[AT_TUNE].LowerBound 
      = 400;
    psPortRangeHints[AT_TUNE].UpperBound 
      = 480;
    psPortRangeHints[AT_FIXED].HintDescriptor
      = (LADSPA_HINT_BOUNDED_BELOW |
	 LADSPA_HINT_BOUNDED_ABOVE |
	 LADSPA_HINT_DEFAULT_0);
    psPortRangeHints[AT_FIXED].LowerBound 
      = -36;
    psPortRangeHints[AT_FIXED].UpperBound 
      = 12;
    psPortRangeHints[AT_PULL].HintDescriptor
      = (LADSPA_HINT_BOUNDED_BELOW |
	 LADSPA_HINT_BOUNDED_ABOVE |
	 LADSPA_HINT_DEFAULT_0);
    psPortRangeHints[AT_PULL].LowerBound 
      = 0;
    psPortRangeHints[AT_PULL].UpperBound 
      = 1;
    psPortRangeHints[AT_A].HintDescriptor
      = (LADSPA_HINT_BOUNDED_BELOW |
	 LADSPA_HINT_BOUNDED_ABOVE |
	 LADSPA_HINT_INTEGER |
	 LADSPA_HINT_DEFAULT_0);
    psPortRangeHints[AT_A].LowerBound 
      = -1.1;
    psPortRangeHints[AT_A].UpperBound 
      = 1.1;
    psPortRangeHints[AT_Bb].HintDescriptor
      = (LADSPA_HINT_BOUNDED_BELOW |
	 LADSPA_HINT_BOUNDED_ABOVE |
	 LADSPA_HINT_INTEGER |
	 LADSPA_HINT_DEFAULT_MINIMUM);
    psPortRangeHints[AT_Bb].LowerBound 
      = -1.1;
    psPortRangeHints[AT_Bb].UpperBound 
      = 1.1;
    psPortRangeHints[AT_B].HintDescriptor
      = (LADSPA_HINT_BOUNDED_BELOW |
	 LADSPA_HINT_BOUNDED_ABOVE |
	 LADSPA_HINT_INTEGER |
	 LADSPA_HINT_DEFAULT_0);
    psPortRangeHints[AT_B].LowerBound 
      = -1.1;
    psPortRangeHints[AT_B].UpperBound 
      = 1.1;
    psPortRangeHints[AT_C].HintDescriptor
      = (LADSPA_HINT_BOUNDED_BELOW |
	 LADSPA_HINT_BOUNDED_ABOVE |
	 LADSPA_HINT_INTEGER |
	 LADSPA_HINT_DEFAULT_0);
    psPortRangeHints[AT_C].LowerBound 
      = -1.1;
    psPortRangeHints[AT_C].UpperBound 
      = 1.1;
    psPortRangeHints[AT_Db].HintDescriptor
      = (LADSPA_HINT_BOUNDED_BELOW |
	 LADSPA_HINT_BOUNDED_ABOVE |
	 LADSPA_HINT_INTEGER |
	 LADSPA_HINT_DEFAULT_MINIMUM);
    psPortRangeHints[AT_Db].LowerBound 
      = -1.1;
    psPortRangeHints[AT_Db].UpperBound 
      = 1.1;
    psPortRangeHints[AT_D].HintDescriptor
      = (LADSPA_HINT_BOUNDED_BELOW |
	 LADSPA_HINT_BOUNDED_ABOVE |
	 LADSPA_HINT_INTEGER |
	 LADSPA_HINT_DEFAULT_0);
    psPortRangeHints[AT_D].LowerBound 
      = -1.1;
    psPortRangeHints[AT_D].UpperBound 
      = 1.1;
    psPortRangeHints[AT_Eb].HintDescriptor
      = (LADSPA_HINT_BOUNDED_BELOW |
	 LADSPA_HINT_BOUNDED_ABOVE |
	 LADSPA_HINT_INTEGER |
	 LADSPA_HINT_DEFAULT_MINIMUM);
    psPortRangeHints[AT_Eb].LowerBound 
      = -1.1;
    psPortRangeHints[AT_Eb].UpperBound 
      = 1.1;
    psPortRangeHints[AT_E].HintDescriptor
      = (LADSPA_HINT_BOUNDED_BELOW |
	 LADSPA_HINT_BOUNDED_ABOVE |
	 LADSPA_HINT_INTEGER |
	 LADSPA_HINT_DEFAULT_0);
    psPortRangeHints[AT_E].LowerBound 
      = -1.1;
    psPortRangeHints[AT_E].UpperBound 
      = 1.1;
    psPortRangeHints[AT_F].HintDescriptor
      = (LADSPA_HINT_BOUNDED_BELOW |
	 LADSPA_HINT_BOUNDED_ABOVE |
	 LADSPA_HINT_INTEGER |
	 LADSPA_HINT_DEFAULT_0);
    psPortRangeHints[AT_F].LowerBound 
      = -1.1;
    psPortRangeHints[AT_F].UpperBound 
      = 1.1;
    psPortRangeHints[AT_Gb].HintDescriptor
      = (LADSPA_HINT_BOUNDED_BELOW |
	 LADSPA_HINT_BOUNDED_ABOVE |
	 LADSPA_HINT_INTEGER |
	 LADSPA_HINT_DEFAULT_MINIMUM);
    psPortRangeHints[AT_Gb].LowerBound 
      = -1.1;
    psPortRangeHints[AT_Gb].UpperBound 
      = 1.1;
    psPortRangeHints[AT_G].HintDescriptor
      = (LADSPA_HINT_BOUNDED_BELOW |
	 LADSPA_HINT_BOUNDED_ABOVE |
	 LADSPA_HINT_INTEGER |
	 LADSPA_HINT_DEFAULT_0);
    psPortRangeHints[AT_G].LowerBound 
      = -1.1;
    psPortRangeHints[AT_G].UpperBound 
      = 1.1;
    psPortRangeHints[AT_Ab].HintDescriptor
      = (LADSPA_HINT_BOUNDED_BELOW |
	 LADSPA_HINT_BOUNDED_ABOVE |
	 LADSPA_HINT_INTEGER |
	 LADSPA_HINT_DEFAULT_MINIMUM);
    psPortRangeHints[AT_Ab].LowerBound 
      = -1.1;
    psPortRangeHints[AT_Ab].UpperBound 
      = 1.1;
    psPortRangeHints[AT_AMOUNT].HintDescriptor
      = (LADSPA_HINT_BOUNDED_BELOW |
	 LADSPA_HINT_BOUNDED_ABOVE |
	 LADSPA_HINT_DEFAULT_1);
    psPortRangeHints[AT_AMOUNT].LowerBound 
      = 0;
    psPortRangeHints[AT_AMOUNT].UpperBound 
      = 1;
    psPortRangeHints[AT_SMOOTH].HintDescriptor
      = (LADSPA_HINT_BOUNDED_BELOW |
	 LADSPA_HINT_BOUNDED_ABOVE |
	 LADSPA_HINT_DEFAULT_0);
    psPortRangeHints[AT_SMOOTH].LowerBound 
      = 0;
    psPortRangeHints[AT_SMOOTH].UpperBound 
      = 1;
    psPortRangeHints[AT_SHIFT].HintDescriptor
      = (LADSPA_HINT_BOUNDED_BELOW |
	 LADSPA_HINT_BOUNDED_ABOVE |
	 LADSPA_HINT_DEFAULT_0);
    psPortRangeHints[AT_SHIFT].LowerBound 
      = -12;
    psPortRangeHints[AT_SHIFT].UpperBound 
      = 12;
    psPortRangeHints[AT_SCWARP].HintDescriptor
      = (LADSPA_HINT_BOUNDED_BELOW |
	 LADSPA_HINT_BOUNDED_ABOVE |
	 LADSPA_HINT_INTEGER |
	 LADSPA_HINT_DEFAULT_0);
    psPortRangeHints[AT_SCWARP].LowerBound 
      = -5.1;
    psPortRangeHints[AT_SCWARP].UpperBound 
      = 5.1;
    psPortRangeHints[AT_LFOAMP].HintDescriptor
      = (LADSPA_HINT_BOUNDED_BELOW |
	 LADSPA_HINT_BOUNDED_ABOVE |
	 LADSPA_HINT_DEFAULT_0);
    psPortRangeHints[AT_LFOAMP].LowerBound 
      = 0;
    psPortRangeHints[AT_LFOAMP].UpperBound 
      = 1;
    psPortRangeHints[AT_LFORATE].HintDescriptor
      = (LADSPA_HINT_BOUNDED_BELOW |
	 LADSPA_HINT_BOUNDED_ABOVE |
	 LADSPA_HINT_DEFAULT_MIDDLE);
    psPortRangeHints[AT_LFORATE].LowerBound 
      = 0;
    psPortRangeHints[AT_LFORATE].UpperBound 
      = 10;
    psPortRangeHints[AT_LFOSHAPE].HintDescriptor
      = (LADSPA_HINT_BOUNDED_BELOW |
	 LADSPA_HINT_BOUNDED_ABOVE |
	 LADSPA_HINT_DEFAULT_0);
    psPortRangeHints[AT_LFOSHAPE].LowerBound 
      = -1;
    psPortRangeHints[AT_LFOSHAPE].UpperBound 
      = 1;
    psPortRangeHints[AT_LFOSYMM].HintDescriptor
      = (LADSPA_HINT_BOUNDED_BELOW |
	 LADSPA_HINT_BOUNDED_ABOVE |
	 LADSPA_HINT_DEFAULT_0);
    psPortRangeHints[AT_LFOSYMM].LowerBound 
      = -1;
    psPortRangeHints[AT_LFOSYMM].UpperBound 
      = 1;
    psPortRangeHints[AT_LFOQUANT].HintDescriptor
      = (LADSPA_HINT_TOGGLED |
	 LADSPA_HINT_DEFAULT_0);
    psPortRangeHints[AT_FCORR].HintDescriptor
      = (LADSPA_HINT_TOGGLED |
	 LADSPA_HINT_DEFAULT_0);
    psPortRangeHints[AT_FWARP].HintDescriptor
      = (LADSPA_HINT_BOUNDED_BELOW |
	 LADSPA_HINT_BOUNDED_ABOVE |
	 LADSPA_HINT_DEFAULT_0);
    psPortRangeHints[AT_FWARP].LowerBound 
      = -1;
    psPortRangeHints[AT_FWARP].UpperBound 
      = 1;
    psPortRangeHints[AT_MIX].HintDescriptor
      = (LADSPA_HINT_BOUNDED_BELOW |
	 LADSPA_HINT_BOUNDED_ABOVE |
	 LADSPA_HINT_DEFAULT_1);
    psPortRangeHints[AT_MIX].LowerBound 
      = 0;
    psPortRangeHints[AT_MIX].UpperBound 
      = 1;
    psPortRangeHints[AT_PITCH].HintDescriptor
      = 0;
    psPortRangeHints[AT_CONF].HintDescriptor
      = 0;
    psPortRangeHints[AT_INPUT1].HintDescriptor
      = 0;
    psPortRangeHints[AT_OUTPUT1].HintDescriptor
      = 0;
    psPortRangeHints[AT_LATENCY].HintDescriptor
      = 0;

    g_psDescriptor->instantiate 
      = instantiateAutotalent;
    g_psDescriptor->connect_port 
      = connectPortToAutotalent;
    g_psDescriptor->activate
      = NULL;
    g_psDescriptor->run
      = runAutotalent;
    g_psDescriptor->run_adding
      = NULL;
    g_psDescriptor->set_run_adding_gain
      = NULL;
    g_psDescriptor->deactivate
      = NULL;
    g_psDescriptor->cleanup
      = cleanupAutotalent;
  }
  
}



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


// Called when library is unloaded
void
_fini() {
  deleteDescriptor(g_psDescriptor);
}


// Return the plugin descriptor (there's only one in this file)
const LADSPA_Descriptor * 
ladspa_descriptor(unsigned long Index) {
  switch (Index) {
  case 0:
    return g_psDescriptor;
  default:
    return NULL;
  }
}


// All done