-
Notifications
You must be signed in to change notification settings - Fork 20
/
autotalent.c
1491 lines (1315 loc) · 40.2 KB
/
autotalent.c
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
/* 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
Modified by Ethan Chen
http://github.com/intervigilium/autotalent-harness
VERSION 0.1x
July 9, 2010
Modified by Eng Eder Souza (ederwander)
http://github.com/ederwander/PyAutoTune
VERSION 0.1b
May 18, 2012
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.
*/
/*****************************************************************************/
#include "autotalent.h"
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <stdio.h>
#define PI (float)3.14159265358979323846
#define L2SC (float)3.32192809488736218171
/*****************************************************************************/
#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];
}
}
#define CONCERT_A 440
#define AT_A 0
#define AT_Bb 1
#define AT_B 2
#define AT_C 3
#define AT_Db 4
#define AT_D 5
#define AT_Eb 6
#define AT_E 7
#define AT_F 8
#define AT_Gb 9
#define AT_G 10
#define AT_Ab 11
#define KEY_Ab_A -1
#define KEY_Ab_Bb 1
#define KEY_Ab_B -1
#define KEY_Ab_C 1
#define KEY_Ab_Db 1
#define KEY_Ab_D -1
#define KEY_Ab_Eb 1
#define KEY_Ab_E -1
#define KEY_Ab_F 1
#define KEY_Ab_Gb -1
#define KEY_Ab_G 1
#define KEY_Ab_Ab 1
#define KEY_A_A 1
#define KEY_A_Bb -1
#define KEY_A_B 1
#define KEY_A_C -1
#define KEY_A_Db 1
#define KEY_A_D 1
#define KEY_A_Eb -1
#define KEY_A_E 1
#define KEY_A_F -1
#define KEY_A_Gb 1
#define KEY_A_G -1
#define KEY_A_Ab 1
#define KEY_Bb_A 1
#define KEY_Bb_Bb 1
#define KEY_Bb_B -1
#define KEY_Bb_C 1
#define KEY_Bb_Db -1
#define KEY_Bb_D 1
#define KEY_Bb_Eb 1
#define KEY_Bb_E -1
#define KEY_Bb_F 1
#define KEY_Bb_Gb -1
#define KEY_Bb_G 1
#define KEY_Bb_Ab -1
#define KEY_B_A -1
#define KEY_B_Bb 1
#define KEY_B_B 1
#define KEY_B_C -1
#define KEY_B_Db 1
#define KEY_B_D -1
#define KEY_B_Eb 1
#define KEY_B_E 1
#define KEY_B_F -1
#define KEY_B_Gb 1
#define KEY_B_G -1
#define KEY_B_Ab 1
#define KEY_C_A 1
#define KEY_C_Bb -1
#define KEY_C_B 1
#define KEY_C_C 1
#define KEY_C_Db -1
#define KEY_C_D 1
#define KEY_C_Eb -1
#define KEY_C_E 1
#define KEY_C_F 1
#define KEY_C_Gb -1
#define KEY_C_G 1
#define KEY_C_Ab -1
#define KEY_Db_A -1
#define KEY_Db_Bb 1
#define KEY_Db_B -1
#define KEY_Db_C 1
#define KEY_Db_Db 1
#define KEY_Db_D -1
#define KEY_Db_Eb 1
#define KEY_Db_E -1
#define KEY_Db_F 1
#define KEY_Db_Gb 1
#define KEY_Db_G -1
#define KEY_Db_Ab 1
#define KEY_D_A 1
#define KEY_D_Bb -1
#define KEY_D_B 1
#define KEY_D_C -1
#define KEY_D_Db 1
#define KEY_D_D 1
#define KEY_D_Eb -1
#define KEY_D_E 1
#define KEY_D_F -1
#define KEY_D_Gb 1
#define KEY_D_G 1
#define KEY_D_Ab -1
#define KEY_Eb_A -1
#define KEY_Eb_Bb 1
#define KEY_Eb_B -1
#define KEY_Eb_C 1
#define KEY_Eb_Db -1
#define KEY_Eb_D 1
#define KEY_Eb_Eb 1
#define KEY_Eb_E -1
#define KEY_Eb_F 1
#define KEY_Eb_Gb -1
#define KEY_Eb_G 1
#define KEY_Eb_Ab 1
#define KEY_E_A 1
#define KEY_E_Bb -1
#define KEY_E_B 1
#define KEY_E_C -1
#define KEY_E_Db 1
#define KEY_E_D -1
#define KEY_E_Eb 1
#define KEY_E_E 1
#define KEY_E_F -1
#define KEY_E_Gb 1
#define KEY_E_G -1
#define KEY_E_Ab 1
#define KEY_F_A 1
#define KEY_F_Bb 1
#define KEY_F_B -1
#define KEY_F_C 1
#define KEY_F_Db -1
#define KEY_F_D 1
#define KEY_F_Eb -1
#define KEY_F_E 1
#define KEY_F_F 1
#define KEY_F_Gb -1
#define KEY_F_G 1
#define KEY_F_Ab -1
#define KEY_Gb_A -1
#define KEY_Gb_Bb 1
#define KEY_Gb_B 1
#define KEY_Gb_C -1
#define KEY_Gb_Db 1
#define KEY_Gb_D -1
#define KEY_Gb_Eb 1
#define KEY_Gb_E -1
#define KEY_Gb_F 1
#define KEY_Gb_Gb 1
#define KEY_Gb_G -1
#define KEY_Gb_Ab 1
#define KEY_G_A 1
#define KEY_G_Bb -1
#define KEY_G_B 1
#define KEY_G_C 1
#define KEY_G_Db -1
#define KEY_G_D 1
#define KEY_G_Eb -1
#define KEY_G_E 1
#define KEY_G_F -1
#define KEY_G_Gb 1
#define KEY_G_G 1
#define KEY_G_Ab -1
// chromatic scale, X because it's unique
#define KEY_X_A 1
#define KEY_X_Bb 1
#define KEY_X_B 1
#define KEY_X_C 1
#define KEY_X_Db 1
#define KEY_X_D 1
#define KEY_X_Eb 1
#define KEY_X_E 1
#define KEY_X_F 1
#define KEY_X_Gb 1
#define KEY_X_G 1
#define KEY_X_Ab 1
/*************************
* THE MEMBER VARIABLES *
*************************/
typedef struct {
float* m_pfTune;
float* m_pfFixed;
float* m_pfPull;
int* m_pfKey;
float* m_pfAmount;
float* m_pfSmooth;
float* m_pfShift;
int* m_pfScwarp;
float* m_pfLfoamp;
float* m_pfLforate;
float* m_pfLfoshape;
float* m_pfLfosymm;
int* m_pfLfoquant;
int* m_pfFcorr;
float* m_pfFwarp;
float* m_pfMix;
// don't know what these are yet
float* m_pfPitch;
float* m_pfConf;
float* m_pfInputBuffer1;
float* m_pfOutputBuffer1;
long int* 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 *
********************/
Autotalent * instantiateAutotalent(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;
}
/********************
* THE SETTERS *
********************/
// Set autotalent key
void setAutotalentKey(Autotalent * autotalent, char * keyPtr) {
int* key;
key = calloc(12, sizeof(int));
switch (*keyPtr) {
case 'a':
key[AT_A] = KEY_Ab_A;
key[AT_Bb] = KEY_Ab_Bb;
key[AT_B] = KEY_Ab_B;
key[AT_C] = KEY_Ab_C;
key[AT_Db] = KEY_Ab_Db;
key[AT_D] = KEY_Ab_D;
key[AT_Eb] = KEY_Ab_Eb;
key[AT_E] = KEY_Ab_E;
key[AT_F] = KEY_Ab_F;
key[AT_Gb] = KEY_Ab_Gb;
key[AT_G] = KEY_Ab_G;
key[AT_Ab] = KEY_Ab_Ab;
break;
case 'A':
key[AT_A] = KEY_A_A;
key[AT_Bb] = KEY_A_Bb;
key[AT_B] = KEY_A_B;
key[AT_C] = KEY_A_C;
key[AT_Db] = KEY_A_Db;
key[AT_D] = KEY_A_D;
key[AT_Eb] = KEY_A_Eb;
key[AT_E] = KEY_A_E;
key[AT_F] = KEY_A_F;
key[AT_Gb] = KEY_A_Gb;
key[AT_G] = KEY_A_G;
key[AT_Ab] = KEY_A_Ab;
break;
case 'b':
key[AT_A] = KEY_Bb_A;
key[AT_Bb] = KEY_Bb_Bb;
key[AT_B] = KEY_Bb_B;
key[AT_C] = KEY_Bb_C;
key[AT_Db] = KEY_Bb_Db;
key[AT_D] = KEY_Bb_D;
key[AT_Eb] = KEY_Bb_Eb;
key[AT_E] = KEY_Bb_E;
key[AT_F] = KEY_Bb_F;
key[AT_Gb] = KEY_Bb_Gb;
key[AT_G] = KEY_Bb_G;
key[AT_Ab] = KEY_Bb_Ab;
break;
case 'B':
key[AT_A] = KEY_B_A;
key[AT_Bb] = KEY_B_Bb;
key[AT_B] = KEY_B_B;
key[AT_C] = KEY_B_C;
key[AT_Db] = KEY_B_Db;
key[AT_D] = KEY_B_D;
key[AT_Eb] = KEY_B_Eb;
key[AT_E] = KEY_B_E;
key[AT_F] = KEY_B_F;
key[AT_Gb] = KEY_B_Gb;
key[AT_G] = KEY_B_G;
key[AT_Ab] = KEY_B_Ab;
break;
case 'C':
key[AT_A] = KEY_C_A;
key[AT_Bb] = KEY_C_Bb;
key[AT_B] = KEY_C_B;
key[AT_C] = KEY_C_C;
key[AT_Db] = KEY_C_Db;
key[AT_D] = KEY_C_D;
key[AT_Eb] = KEY_C_Eb;
key[AT_E] = KEY_C_E;
key[AT_F] = KEY_C_F;
key[AT_Gb] = KEY_C_Gb;
key[AT_G] = KEY_C_G;
key[AT_Ab] = KEY_C_Ab;
break;
case 'd':
key[AT_A] = KEY_Db_A;
key[AT_Bb] = KEY_Db_Bb;
key[AT_B] = KEY_Db_B;
key[AT_C] = KEY_Db_C;
key[AT_Db] = KEY_Db_Db;
key[AT_D] = KEY_Db_D;
key[AT_Eb] = KEY_Db_Eb;
key[AT_E] = KEY_Db_E;
key[AT_F] = KEY_Db_F;
key[AT_Gb] = KEY_Db_Gb;
key[AT_G] = KEY_Db_G;
key[AT_Ab] = KEY_Db_Ab;
break;
case 'D':
key[AT_A] = KEY_D_A;
key[AT_Bb] = KEY_D_Bb;
key[AT_B] = KEY_D_B;
key[AT_C] = KEY_D_C;
key[AT_Db] = KEY_D_Db;
key[AT_D] = KEY_D_D;
key[AT_Eb] = KEY_D_Eb;
key[AT_E] = KEY_D_E;
key[AT_F] = KEY_D_F;
key[AT_Gb] = KEY_D_Gb;
key[AT_G] = KEY_D_G;
key[AT_Ab] = KEY_D_Ab;
break;
case 'e':
key[AT_A] = KEY_Eb_A;
key[AT_Bb] = KEY_Eb_Bb;
key[AT_B] = KEY_Eb_B;
key[AT_C] = KEY_Eb_C;
key[AT_Db] = KEY_Eb_Db;
key[AT_D] = KEY_Eb_D;
key[AT_Eb] = KEY_Eb_Eb;
key[AT_E] = KEY_Eb_E;
key[AT_F] = KEY_Eb_F;
key[AT_Gb] = KEY_Eb_Gb;
key[AT_G] = KEY_Eb_G;
key[AT_Ab] = KEY_Eb_Ab;
break;
case 'E':
key[AT_A] = KEY_E_A;
key[AT_Bb] = KEY_E_Bb;
key[AT_B] = KEY_E_B;
key[AT_C] = KEY_E_C;
key[AT_Db] = KEY_E_Db;
key[AT_D] = KEY_E_D;
key[AT_Eb] = KEY_E_Eb;
key[AT_E] = KEY_E_E;
key[AT_F] = KEY_E_F;
key[AT_Gb] = KEY_E_Gb;
key[AT_G] = KEY_E_G;
key[AT_Ab] = KEY_E_Ab;
break;
case 'F':
key[AT_A] = KEY_F_A;
key[AT_Bb] = KEY_F_Bb;
key[AT_B] = KEY_F_B;
key[AT_C] = KEY_F_C;
key[AT_Db] = KEY_F_Db;
key[AT_D] = KEY_F_D;
key[AT_Eb] = KEY_F_Eb;
key[AT_E] = KEY_F_E;
key[AT_F] = KEY_F_F;
key[AT_Gb] = KEY_F_Gb;
key[AT_G] = KEY_F_G;
key[AT_Ab] = KEY_F_Ab;
break;
case 'g':
key[AT_A] = KEY_Gb_A;
key[AT_Bb] = KEY_Gb_Bb;
key[AT_B] = KEY_Gb_B;
key[AT_C] = KEY_Gb_C;
key[AT_Db] = KEY_Gb_Db;
key[AT_D] = KEY_Gb_D;
key[AT_Eb] = KEY_Gb_Eb;
key[AT_E] = KEY_Gb_E;
key[AT_F] = KEY_Gb_F;
key[AT_Gb] = KEY_Gb_Gb;
key[AT_G] = KEY_Gb_G;
key[AT_Ab] = KEY_Gb_Ab;
break;
case 'G':
key[AT_A] = KEY_G_A;
key[AT_Bb] = KEY_G_Bb;
key[AT_B] = KEY_G_B;
key[AT_C] = KEY_G_C;
key[AT_Db] = KEY_G_Db;
key[AT_D] = KEY_G_D;
key[AT_Eb] = KEY_G_Eb;
key[AT_E] = KEY_G_E;
key[AT_F] = KEY_G_F;
key[AT_Gb] = KEY_G_Gb;
key[AT_G] = KEY_G_G;
key[AT_Ab] = KEY_G_Ab;
break;
case 'X':
key[AT_A] = KEY_X_A;
key[AT_Bb] = KEY_X_Bb;
key[AT_B] = KEY_X_B;
key[AT_C] = KEY_X_C;
key[AT_Db] = KEY_X_Db;
key[AT_D] = KEY_X_D;
key[AT_Eb] = KEY_X_Eb;
key[AT_E] = KEY_X_E;
key[AT_F] = KEY_X_F;
key[AT_Gb] = KEY_X_Gb;
key[AT_G] = KEY_X_G;
key[AT_Ab] = KEY_X_Ab;
break;
}
autotalent->m_pfKey = key;
}
// Set autotalent parameters
void setAutotalentParameters(Autotalent * autotalent, float * concertA, float * fixedPitch, float * fixedPull,
float * correctStrength, float * correctSmooth,float * pitchShift, int * scaleRotate,
float * lfoDepth, float * lfoRate, float * lfoShape, float * lfoSym, int * lfoQuant,
int * formCorr, float * formWarp, float * mix) {
// set concert A
autotalent->m_pfTune = concertA;
// printf("Concert A: %f\n", *(autotalent->m_pfTune));
// set pitch correction parameters
autotalent->m_pfFixed = fixedPitch;
autotalent->m_pfPull = fixedPull;
autotalent->m_pfAmount = correctStrength;
autotalent->m_pfSmooth = correctSmooth;
autotalent->m_pfShift = pitchShift;
autotalent->m_pfScwarp = scaleRotate;
//printf("FixedPitch: %f, FixedPull: %f, CorrectStr: %f, CorrectSmooth: %f, PitchShift: %f, ScaleRotate: %d\n",
// *(autotalent->m_pfFixed), *(autotalent->m_pfPull), *(autotalent->m_pfAmount), *(autotalent->m_pfSmooth), *(autotalent->m_pfShift), *(autotalent->m_pfScwarp));
// set LFO parameters
autotalent->m_pfLfoamp = lfoDepth;
autotalent->m_pfLforate = lfoRate;
autotalent->m_pfLfoshape = lfoShape;
autotalent->m_pfLfosymm = lfoSym;
autotalent->m_pfLfoquant = lfoQuant;
autotalent->m_pfFcorr = formCorr;
autotalent->m_pfFwarp = formWarp;
autotalent->m_pfMix = mix;
//printf("LFODepth: %f, LFORate: %f, LFOShape %f, LFOSym: %f, //LFOQuant: %d, FormCorr: %d, FormWarp: %f, Mix: %f\n",
// *(autotalent->m_pfLfoamp), *(autotalent->m_pfLforate), *(autotalent->m_pfLfoshape), *(autotalent->m_pfLfosymm), *(autotalent->m_pfLfoquant), *(autotalent->m_pfFcorr), *(autotalent->m_pfFwarp), *(autotalent->m_pfMix));
// set output parameters, note these aren't used by us
autotalent->m_pfPitch = calloc(1, sizeof(float));
autotalent->m_pfConf = calloc(1, sizeof(float));
autotalent->m_pfLatency = calloc(1, sizeof(long int));
}
// Set input and output buffers
void setAutotalentBuffers(Autotalent * autotalent, float * inputBuffer, float * outputBuffer) {
autotalent->m_pfInputBuffer1 = inputBuffer;
autotalent->m_pfOutputBuffer1 = outputBuffer;
}
/********************
* THE PROCESSOR *
********************/
// Called every time we get a new chunk of audio
void runAutotalent(Autotalent * Instance, unsigned long SampleCount) {
// some kind of buffer, need to find out the type, looks like floats
float* pfInput;
float* 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] = psAutotalent->m_pfKey[AT_A];
iNotes[1] = psAutotalent->m_pfKey[AT_Bb];
iNotes[2] = psAutotalent->m_pfKey[AT_B];
iNotes[3] = psAutotalent->m_pfKey[AT_C];
iNotes[4] = psAutotalent->m_pfKey[AT_Db];
iNotes[5] = psAutotalent->m_pfKey[AT_D];
iNotes[6] = psAutotalent->m_pfKey[AT_Eb];
iNotes[7] = psAutotalent->m_pfKey[AT_E];
iNotes[8] = psAutotalent->m_pfKey[AT_F];
iNotes[9] = psAutotalent->m_pfKey[AT_Gb];
iNotes[10] = psAutotalent->m_pfKey[AT_G];
iNotes[11] = psAutotalent->m_pfKey[AT_Ab];
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;
}