-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfiddle~.c
1805 lines (1626 loc) · 52.6 KB
/
fiddle~.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
/*
Copyright 1997-1999 Miller Puckette, Music Department, UCSD ([email protected])
and Ted Apel, CRCA, UCSD ([email protected]).
Permission is granted to use this software for any noncommercial purpose.
For commercial licensing contact the UCSD Technology Transfer Office.
UC MAKES NO WARRANTY, EXPRESS OR IMPLIED, IN CONNECTION WITH THIS SOFTWARE!
This file is downloadable from http://crca.ucsd.edu/~msp
*/
/*
* Fiddle is a pitch tracker hardwired to have hop size ("H") equal to
* half its window size ("N").
*
* This version should compile for Max "0.26," JMAX, Pd, or Max/MSP.
*
* The "lastanalysis" field holds the shifted FT of the previous H
* samples. The buffer contains in effect points 1/2, 3/2, ..., (N-1)/2
* of the DTFT of a real vector of length N, half of whose points are zero,
* i.e., only the first H points are used. Put another way, we get the
* the odd-numbered points of the FFT of the H points, zero padded to 4*H in
* length. The integer points 0, 1, ..., H-1
* are found by interpolating these others, using the fact that the
* half-integer points are band-limited (they only have positive frequencies.)
* To facilitate the interpolation the "lastanalysis" buffer contains
* FILTSIZE extra points (1/2-FILTSIZE, ..., -1/2) at the beginning and
* FILTSIZE again at the end ((N+1)/2, ..., FILTSIZE+(N-1)/2). The buffer
* therefore has N+4*FILTSIZE floating-point numbers in it.
*
* after doing this I found out that you can just do a real FFT
* of the H new points, zero-padded to contain N points, and using a similar
* but simpler interpolation scheme you can still get 2N points of the DTFT
* of the N points. Jean Laroche is a big fat hen.
*
*/
#define MSP
#ifdef NT
#define flog log
#define fexp exp
#define fsqrt sqrt
#pragma warning (disable: 4305 4244)
#endif
#ifdef __linux__
#define flog log
#define fexp exp
#define fsqrt sqrt
#endif
char fiddle_version[] = "fiddle~ v1.2 Test";
#ifdef FTS1X
#include "mess.h"
#include "ext_mess.h"
#include "dsp.h"
#define CLASSNAME "fiddle"
#define OUTLETpower 5
#define OUTLETmicropitch1 4
#define OUTLETmicropitch2 3
#define OUTLETmicropitch3 2
#define OUTLETattack 1
#define OUTLETpitch 0
static fts_symbol_t *dsp_symbol = 0;
#endif /* FTS */
#ifdef MAX26
#define t_floatarg double
#include "m_extern.h"
#include "d_graph.h"
#include "d_ugen.h"
#endif /* MAX26 */
#ifdef PD
#include "m_pd.h"
#endif /* PD */
#ifdef MSP
#define flog log
#define fexp exp
#define fsqrt sqrt
#endif /* MSP */
#ifdef MSP
#define t_floatarg double // this is a guess based on MAX26
#include "ext.h"
#include "ext_mess.h"
#include "z_dsp.h"
#include "fft_mayer.proto.h"
//#include "fiddle_header.h"
// #include "MacHeaders.h"
//#include <MacHeadersPPC>
//#include "fiddledoit.h"
#endif /* MSP */
#include <math.h>
#define MINBIN 3
#define DEFAMPLO 40
#define DEFAMPHI 50
#define DEFATTACKTIME 100
#define DEFATTACKTHRESH 10
#define DEFVIBTIME 50
#define DEFVIBDEPTH 0.5
#define GLISS 0.7f
#define DBFUDGE 30.8f
#define MINFREQINBINS 5 /* minimum frequency in bins for reliable output */
#define MAXNPITCH 3
#define MAXHIST 3 /* find N hottest peaks in histogram */
#define MAXPOINTS 8192
#define MINPOINTS 128
#define DEFAULTPOINTS 1024
#define HISTORY 20
#define MAXPEAK 100 /* maximum number of peaks */
#define DEFNPEAK 20 /* default number of peaks */
#define MAXNPEAK (MAXLOWPEAK + MAXSTRONGPEAK)
#define MINBW (0.03f) /* consider BW >= 0.03 FFT bins */
#define BINPEROCT 48 /* bins per octave */
#define BPERO_OVER_LOG2 69.24936196f /* BINSPEROCT/log(2) */
#define FACTORTOBINS (float)(4/0.0145453) /* 4 / (pow(2.,1/48.) - 1) */
#define BINGUARD 10 /* extra bins to throw in front */
#define PARTIALDEVIANCE 0.023f /* acceptable partial detuning in % */
#define LOGTODB 4.34294481903f /* 20/log(10) */
#define KNOCKTHRESH 10.f /* don't know how to describe this */
#define SETLONG(ap, x) ((ap)->a_type = A_LONG, (ap)->a_w.w_long = (x))
#define SETFLOAT(ap, x) ((ap)->a_type = A_FLOAT, (ap)->a_w.w_float = (x))
static float sigfiddle_partialonset[] =
{
0,
48,
76.0782000346154967102,
96,
111.45254855459339269887,
124.07820003461549671089,
134.75303625876499715823,
144,
152.15640006923099342109,
159.45254855459339269887,
166.05271769459026829915,
172.07820003461549671088,
177.62110647077242370064,
182.75303625876499715892,
187.53074858920888940907,
192,
};
#define NPARTIALONSET ((int)(sizeof(sigfiddle_partialonset)/sizeof(float)))
static int sigfiddle_intpartialonset[] =
{
0,
48,
76,
96,
111,
124,
135,
144,
152,
159,
166,
172,
178,
183,
188,
192,
};
/* these coefficients, which come from the "upsamp" subdirectory,
are a filter kernel for upsampling by a factor of two, assuming
the sound to be upsampled has no energy above half the Nyquist, i.e.,
that it's already 2x oversampled compared to the theoretically possible
sample rate. I got these by trial and error. */
#define FILT1 ((float)(.5 * 1.227054))
#define FILT2 ((float)(.5 * -0.302385))
#define FILT3 ((float)(.5 * 0.095326))
#define FILT4 ((float)(.5 * -0.022748))
#define FILT5 ((float)(.5 * 0.002533))
#define FILTSIZE 5
typedef struct peakout /* a peak for output */
{
float po_freq; /* frequency in hz */
float po_amp; /* amplitude */
} t_peakout;
typedef struct peak /* a peak for analysis */
{
float p_freq; /* frequency in bins */
float p_width; /* peak width in bins */
float p_pow; /* peak power */
float p_loudness; /* 4th root of power */
float *p_fp; /* pointer back to spectrum */
} t_peak;
typedef struct histopeak
{
float h_pitch; /* estimated pitch */
float h_value; /* value of peak */
float h_loud; /* combined strength of found partials */
int h_index; /* index of bin holding peak */
int h_used; /* true if an x_hist entry points here */
} t_histopeak;
typedef struct pitchhist /* struct for keeping history by pitch */
{
float h_pitch; /* pitch to output */
float h_amps[HISTORY]; /* past amplitudes */
float h_pitches[HISTORY]; /* past pitches */
float h_noted; /* last pitch output */
int h_age; /* number of frames pitch has been there */
t_histopeak *h_wherefrom; /* new histogram peak to incorporate */
void *h_outlet;
} t_pitchhist;
typedef struct sigfiddle /* instance struct */
{
#ifdef FTS1X
fts_object_t x_h; /* object header */
fts_alarm_t x_clock; /* callback for timeouts */
#endif
#ifdef MAX26
t_head x_h; /* header for tilde objects */
t_sig *x_io[IN1+OUT0]; /* number of signal inputs and outputs */
void *x_clock; /* a "clock" object */
#endif
#ifdef PD
t_object x_ob; /* object header */
t_clock *x_clock; /* callback for timeouts */
#endif
#ifdef MSP
t_pxobject x_obj;
void *x_clock;
long x_downsample; // downsample feature because of
// MSP's large sig vector sizes
#endif
float *x_inbuf; /* buffer to analyze, npoints/2 elems */
float *x_lastanalysis; /* FT of last buffer (see main comment) */
float *x_spiral; /* 1/4-wave complex exponential */
t_peakout *x_peakbuf; /* spectral peaks for output */
int x_npeakout; /* number of spectral peaks to output */
int x_npeakanal; /* number of spectral peaks to analyze */
int x_phase; /* number of points since last output */
int x_histphase; /* phase into amplitude history vector */
int x_hop; /* period of output, npoints/2 */
float x_sr; /* sample rate */
t_pitchhist x_hist[MAXNPITCH]; /* history of current pitches */
int x_nprint; /* how many periods to print */
int x_npitch; /* number of simultaneous pitches */
float x_dbs[HISTORY]; /* DB history, indexed by "histphase" */
float x_peaked; /* peak since last attack */
int x_dbage; /* number of bins DB has met threshold */
int x_auto; /* true if generating continuous output */
/* parameters */
float x_amplo;
float x_amphi;
int x_attacktime;
int x_attackbins;
float x_attackthresh;
int x_vibtime;
int x_vibbins;
float x_vibdepth;
float x_npartial;
/* outlets & clock */
void *x_envout;
int x_attackvalue;
void *x_attackout;
void *x_noteout;
void *x_peakout;
} t_sigfiddle;
#if CHECKER
float fiddle_checker[1024];
#endif
#ifdef MSP
// Mac compiler requires prototypes for everything
int sigfiddle_ilog2(int n);
float fiddle_mtof(float f);
float fiddle_ftom(float f);
void sigfiddle_doit(t_sigfiddle *x);
void sigfiddle_debug(t_sigfiddle *x);
void sigfiddle_print(t_sigfiddle *x);
void sigfiddle_assist(t_sigfiddle *x, void *b, long m, long a, char *s);
void sigfiddle_amprange(t_sigfiddle *x, double amplo, double amphi);
void sigfiddle_reattack(t_sigfiddle *x, t_floatarg attacktime, t_floatarg
attackthresh);
void sigfiddle_vibrato(t_sigfiddle *x, t_floatarg vibtime, t_floatarg
vibdepth);
void sigfiddle_npartial(t_sigfiddle *x, double npartial);
void sigfiddle_auto(t_sigfiddle *x, t_floatarg f);
int sigfiddle_doinit(t_sigfiddle *x, long npoints, long npitch, long
npeakanal, long npeakout);
static t_int *fiddle_perform(t_int *w);
void sigfiddle_dsp(t_sigfiddle *x, t_signal **sp);
void sigfiddle_tick(t_sigfiddle *x);
void sigfiddle_bang(t_sigfiddle *x);
void sigfiddle_ff(t_sigfiddle *x);
//void *sigfiddle_new(long npoints, long npitch);
void *sigfiddle_new(long npoints, long npitch,
long npeakanal, long npeakout);
void msp_fft(float *buf, long np, long inv);
float msp_ffttemp[MAXPOINTS*2];
int errno;
#endif
int sigfiddle_ilog2(int n)
{
int ret = -1;
while (n)
{
n >>= 1;
ret++;
}
return (ret);
}
float fiddle_mtof(float f)
{
return (8.17579891564 * exp(.0577622650 * f));
}
float fiddle_ftom(float f)
{
return (17.3123405046 * log(.12231220585 * f));
}
#define ftom fiddle_ftom
#define mtof fiddle_mtof
void sigfiddle_doit(t_sigfiddle *x)
{
#ifdef MSP
// prevents interrupt-level stack overflow crash with Netscape.
static float spect1[4*MAXPOINTS];
static float spect2[MAXPOINTS + 4*FILTSIZE];
#else
float spect1[4*MAXPOINTS];
float spect2[MAXPOINTS + 4*FILTSIZE];
#endif
#if CHECKER
float checker3[4*MAXPOINTS];
#endif
t_peak peaklist[MAXPEAK + 1], *pk1;
t_peakout *pk2;
t_histopeak histvec[MAXHIST], *hp1;
int i, j, k, hop = x->x_hop, n = 2*hop, npeak, npitch,
logn = sigfiddle_ilog2(n), newphase, oldphase;
float *fp, *fp1, *fp2, *fp3, total_power, total_loudness, total_db;
float maxbin = BINPEROCT * (logn-2), *histogram = spect2 + BINGUARD;
t_pitchhist *phist;
float hzperbin = x->x_sr / (2.0f * n);
int npeakout = x->x_npeakout, npeakanal = x->x_npeakanal;
int npeaktot = (npeakout > npeakanal ? npeakout : npeakanal);
oldphase = x->x_histphase;
newphase = x->x_histphase + 1;
if (newphase == HISTORY) newphase = 0;
x->x_histphase = newphase;
/*
* multiply the H points by a 1/4-wave complex exponential,
* and take FFT of the result.
*/
for (i = 0, fp1 = x->x_inbuf, fp2 = x->x_spiral, fp3 = spect1;
i < hop; i++, fp1++, fp2 += 2, fp3 += 2)
fp3[0] = fp1[0] * fp2[0], fp3[1] = fp1[0] * fp2[1];
#ifdef MAX26
fft(spect1, hop, 0);
#endif
#ifdef PD
pd_fft(spect1, hop, 0);
#endif
#ifdef FTS1X
fts_cfft_inplc((complex *)spect1, hop);
#endif
#ifdef MSP
msp_fft(spect1,hop,0);
#endif
/*
* now redistribute the points to get in effect the odd-numbered
* points of the FFT of the H points, zero padded to 4*H in length.
*/
for (i = 0, fp1 = spect1, fp2 = spect2 + (2*FILTSIZE);
i < (hop>>1); i++, fp1 += 2, fp2 += 4)
fp2[0] = fp1[0], fp2[1] = fp1[1];
for (i = 0, fp1 = spect1 + n - 2, fp2 = spect2 + (2*FILTSIZE+2);
i < (hop>>1); i++, fp1 -= 2, fp2 += 4)
fp2[0] = fp1[0], fp2[1] = -fp1[1];
for (i = 0, fp1 = spect2 + (2*FILTSIZE), fp2 = spect2 + (2*FILTSIZE-2);
i<FILTSIZE; i++, fp1+=2, fp2-=2)
fp2[0] = fp1[0], fp2[1] = -fp1[1];
for (i = 0, fp1 = spect2 + (2*FILTSIZE+n-2), fp2 = spect2 + (2*FILTSIZE+n);
i<FILTSIZE; i++, fp1-=2, fp2+=2)
fp2[0] = fp1[0], fp2[1] = -fp1[1];
#if 0
{
fp = spect2 + 2*FILTSIZE;
post("x1 re %12.4f %12.4f %12.4f %12.4f %12.4f",
fp[0], fp[2], fp[4], fp[6], fp[8]);
post("x1 im %12.4f %12.4f %12.4f %12.4f %12.4f",
fp[1], fp[3], fp[5], fp[7], fp[9]);
}
#endif
/* spect2 is now prepared; now combine spect2 and lastanalysis into
* spect1. Odd-numbered points of spect1 are the points of "last"
* plus (-i, i, -i, ...) times spect1. Even-numbered points are
* the interpolated points of "last" plus (1, -1, 1, ...) times the
* interpolated points of spect1.
*
* To interpolate, take FILT1 exp(-pi/4) times
* the previous point, FILT2*exp(-3*pi/4) times 3 bins before,
* etc, and FILT1 exp(pi/4), FILT2 exp(3pi/4), etc., to weight
* the +1, +3, etc., points.
*
* In this calculation, we take (1, i, -1, -i, 1) times the
* -9, -7, ..., -1 points, and (i, -1, -i, 1, i) times the 1, 3,..., 9
* points of the OLD spectrum, alternately adding and subtracting
* the new spectrum to the old; then we multiply the whole thing
* by exp(-i pi/4).
*/
for (i = 0, fp1 = spect1, fp2 = x->x_lastanalysis + 2*FILTSIZE,
fp3 = spect2 + 2*FILTSIZE;
i < (hop>>1); i++)
{
float re, im;
re= FILT1 * ( fp2[ -2] -fp2[ 1] +fp3[ -2] -fp3[ 1]) +
FILT2 * ( fp2[ -3] -fp2[ 2] +fp3[ -3] -fp3[ 2]) +
FILT3 * (-fp2[ -6] +fp2[ 5] -fp3[ -6] +fp3[ 5]) +
FILT4 * (-fp2[ -7] +fp2[ 6] -fp3[ -7] +fp3[ 6]) +
FILT5 * ( fp2[-10] -fp2[ 9] +fp3[-10] -fp3[ 9]);
im= FILT1 * ( fp2[ -1] +fp2[ 0] +fp3[ -1] +fp3[ 0]) +
FILT2 * (-fp2[ -4] -fp2[ 3] -fp3[ -4] -fp3[ 3]) +
FILT3 * (-fp2[ -5] -fp2[ 4] -fp3[ -5] -fp3[ 4]) +
FILT4 * ( fp2[ -8] +fp2[ 7] +fp3[ -8] +fp3[ 7]) +
FILT5 * ( fp2[ -9] +fp2[ 8] +fp3[ -9] +fp3[ 8]);
fp1[0] = 0.7071f * (re + im);
fp1[1] = 0.7071f * (im - re);
fp1[4] = fp2[0] + fp3[1];
fp1[5] = fp2[1] - fp3[0];
fp1 += 8, fp2 += 2, fp3 += 2;
re= FILT1 * ( fp2[ -2] -fp2[ 1] -fp3[ -2] +fp3[ 1]) +
FILT2 * ( fp2[ -3] -fp2[ 2] -fp3[ -3] +fp3[ 2]) +
FILT3 * (-fp2[ -6] +fp2[ 5] +fp3[ -6] -fp3[ 5]) +
FILT4 * (-fp2[ -7] +fp2[ 6] +fp3[ -7] -fp3[ 6]) +
FILT5 * ( fp2[-10] -fp2[ 9] -fp3[-10] +fp3[ 9]);
im= FILT1 * ( fp2[ -1] +fp2[ 0] -fp3[ -1] -fp3[ 0]) +
FILT2 * (-fp2[ -4] -fp2[ 3] +fp3[ -4] +fp3[ 3]) +
FILT3 * (-fp2[ -5] -fp2[ 4] +fp3[ -5] +fp3[ 4]) +
FILT4 * ( fp2[ -8] +fp2[ 7] -fp3[ -8] -fp3[ 7]) +
FILT5 * ( fp2[ -9] +fp2[ 8] -fp3[ -9] -fp3[ 8]);
fp1[0] = 0.7071f * (re + im);
fp1[1] = 0.7071f * (im - re);
fp1[4] = fp2[0] - fp3[1];
fp1[5] = fp2[1] + fp3[0];
fp1 += 8, fp2 += 2, fp3 += 2;
}
#if 0
if (x->x_nprint)
{
for (i = 0, fp = spect1; i < 16; i++, fp+= 4)
post("spect %d %f %f --> %f", i, fp[0], fp[1],
sqrt(fp[0] * fp[0] + fp[1] * fp[1]));
}
#endif
/* copy new spectrum out */
for (i = 0, fp1 = spect2, fp2 = x->x_lastanalysis;
i < n + 4*FILTSIZE; i++) *fp2++ = *fp1++;
for (i = 0; i < MINBIN; i++) spect1[4*i + 2] = spect1[4*i + 3] = 0;
/* starting at bin MINBIN, compute hanning windowed power spectrum */
for (i = MINBIN, fp1 = spect1+4*MINBIN, total_power = 0;
i < n-2; i++, fp1 += 4)
{
float re = fp1[0] - 0.5f * (fp1[-8] + fp1[8]);
float im = fp1[1] - 0.5f * (fp1[-7] + fp1[9]);
fp1[3] = (total_power += (fp1[2] = re * re + im * im));
}
if (total_power > 1e-9f)
{
total_db = (100.f - DBFUDGE) + LOGTODB * log(total_power/n);
total_loudness = fsqrt(fsqrt(total_power));
if (total_db < 0) total_db = 0;
}
else total_db = total_loudness = 0;
/* store new db in history vector */
x->x_dbs[newphase] = total_db;
if (total_db < x->x_amplo) goto nopow;
#if 1
if (x->x_nprint) post("power %f", total_power);
#endif
#if CHECKER
/* verify that our FFT resampling thing is putting out good results */
for (i = 0; i < hop; i++)
{
checker3[2*i] = fiddle_checker[i];
checker3[2*i + 1] = 0;
checker3[n + 2*i] = fiddle_checker[i] = x->x_inbuf[i];
checker3[n + 2*i + 1] = 0;
}
for (i = 2*n; i < 4*n; i++) checker3[i] = 0;
fft(checker3, 2*n, 0);
if (x->x_nprint)
{
for (i = 0, fp = checker3; i < 16; i++, fp += 2)
post("spect %d %f %f --> %f", i, fp[0], fp[1],
sqrt(fp[0] * fp[0] + fp[1] * fp[1]));
}
#endif
npeak = 0;
/* search for peaks */
for (i = MINBIN, fp = spect1+4*MINBIN, pk1 = peaklist;
i < n-2 && npeak < npeaktot; i++, fp += 4)
{
float height = fp[2], h1 = fp[-2], h2 = fp[6];
float totalfreq, pfreq, f1, f2, m, var, stdev;
if (height < h1 || height < h2 ||
h1 < 0.00001f*total_power || h2 < 0.00001f*total_power)
continue;
/* use an informal phase vocoder to estimate the frequency.
Do this for the two adjacent bins too. */
pfreq= ((fp[-8] - fp[8]) * (2.0f * fp[0] - fp[8] - fp[-8]) +
(fp[-7] - fp[9]) * (2.0f * fp[1] - fp[9] - fp[-7])) /
(2.0f * height);
f1= ((fp[-12] - fp[4]) * (2.0f * fp[-4] - fp[4] - fp[-12]) +
(fp[-11] - fp[5]) * (2.0f * fp[-3] - fp[5] - fp[-11])) /
(2.0f * h1) - 1;
f2= ((fp[-4] - fp[12]) * (2.0f * fp[4] - fp[12] - fp[-4]) +
(fp[-3] - fp[13]) * (2.0f * fp[5] - fp[13] - fp[-3])) /
(2.0f * h2) + 1;
/* get sample mean and variance of the three */
m = 0.333333f * (pfreq + f1 + f2);
var = 0.5f * ((pfreq-m)*(pfreq-m) + (f1-m)*(f1-m) + (f2-m)*(f2-m));
totalfreq = i + m;
if (var * total_power > KNOCKTHRESH * height || var < 1e-30)
{
#if 0
if (x->x_nprint)
post("cancel: %.2f hz, index %.1f, power %.5f, stdev=%.2f",
totalfreq * hzperbin, BPERO_OVER_LOG2 * log(totalfreq) - 96,
height, sqrt(var));
#endif
continue;
}
stdev = fsqrt(var);
if (totalfreq < 4)
{
if (x->x_nprint) post("oops: was %d, freq %f, m %f, stdev %f h %f",
i, totalfreq, m, stdev, height);
totalfreq = 4;
}
pk1->p_width = stdev;
pk1->p_pow = height;
pk1->p_loudness = fsqrt(fsqrt(height));
pk1->p_fp = fp;
pk1->p_freq = totalfreq;
npeak++;
#if 1
if (x->x_nprint)
{
post("peak: %.2f hz. index %.1f, power %.5f, stdev=%.2f",
pk1->p_freq * hzperbin,
BPERO_OVER_LOG2 * log(pk1->p_freq) - 96,
height, stdev);
}
#endif
pk1++;
}
/* prepare the raw peaks for output */
for (i = 0, pk1 = peaklist, pk2 = x->x_peakbuf; i < npeak;
i++, pk1++, pk2++)
{
float loudness = pk1->p_loudness;
if (i >= npeakout) break;
pk2->po_freq = hzperbin * pk1->p_freq;
pk2->po_amp = (2.f / (float)n) * (loudness * loudness);
}
for (; i < npeakout; i++, pk2++) pk2->po_amp = pk2->po_freq = 0;
/* now, working back into spect2, make a sort of "liklihood"
* spectrum. Proceeding in 48ths of an octave, from 2 to
* n/2 (in bins), the likelihood of each pitch range is contributed
* to by every peak in peaklist that's an integer multiple of it
* in frequency.
*/
if (npeak > npeakanal) npeak = npeakanal; /* max # peaks to analyze */
for (i = 0, fp1 = histogram; i < maxbin; i++) *fp1++ = 0;
for (i = 0, pk1 = peaklist; i < npeak; i++, pk1++)
{
float pit = BPERO_OVER_LOG2 * flog(pk1->p_freq) - 96.0f;
float binbandwidth = FACTORTOBINS * pk1->p_width/pk1->p_freq;
float putbandwidth = (binbandwidth < 2 ? 2 : binbandwidth);
float weightbandwidth = (binbandwidth < 1.0f ? 1.0f : binbandwidth);
/* float weightamp = 1.0f + 3.0f * pk1->p_pow / pow; */
float weightamp = 4. * pk1->p_loudness / total_loudness;
for (j = 0, fp2 = sigfiddle_partialonset; j < NPARTIALONSET; j++, fp2++)
{
float bin = pit - *fp2;
if (bin < maxbin)
{
float para, pphase, score = 30.0f * weightamp /
((j+x->x_npartial) * weightbandwidth);
int firstbin = bin + 0.5f - 0.5f * putbandwidth;
int lastbin = bin + 0.5f + 0.5f * putbandwidth;
int ibw = lastbin - firstbin;
if (firstbin < -BINGUARD) break;
para = 1.0f / (putbandwidth * putbandwidth);
for (k = 0, fp3 = histogram + firstbin,
pphase = firstbin-bin; k <= ibw;
k++, fp3++, pphase += 1.0f)
{
*fp3 += score * (1.0f - para * pphase * pphase);
}
}
}
}
#if 1
if (x->x_nprint)
{
for (i = 0; i < 6*5; i++)
{
float fhz = hzperbin * exp ((8*i + 96) * (1./BPERO_OVER_LOG2));
if (!(i % 6)) post("-- bin %d pitch %f freq %f----", 8*i,
ftom(fhz), fhz);;
post("%3d %3d %3d %3d %3d %3d %3d %3d",
(int)(histogram[8*i]),
(int)(histogram[8*i+1]),
(int)(histogram[8*i+2]),
(int)(histogram[8*i+3]),
(int)(histogram[8*i+4]),
(int)(histogram[8*i+5]),
(int)(histogram[8*i+6]),
(int)(histogram[8*i+7]));
}
}
#endif
/*
* Next we find up to NPITCH strongest peaks in the histogram.
* if a peak is related to a stronger one via an interval in
* the sigfiddle_partialonset array, we suppress it.
*/
for (npitch = 0; npitch < x->x_npitch; npitch++)
{
int index;
float best;
if (npitch)
{
for (best = 0, index = -1, j=1; j < maxbin-1; j++)
{
if (histogram[j] > best && histogram[j] > histogram[j-1] &&
histogram[j] > histogram[j+1])
{
for (k = 0; k < npitch; k++)
if (histvec[k].h_index == j)
goto peaknogood;
for (k = 0; k < NPARTIALONSET; k++)
{
if (j - sigfiddle_intpartialonset[k] < 0) break;
if (histogram[j - sigfiddle_intpartialonset[k]]
> histogram[j]) goto peaknogood;
}
for (k = 0; k < NPARTIALONSET; k++)
{
if (j + sigfiddle_intpartialonset[k] >= maxbin) break;
if (histogram[j + sigfiddle_intpartialonset[k]]
> histogram[j]) goto peaknogood;
}
index = j;
best = histogram[j];
}
peaknogood: ;
}
}
else
{
for (best = 0, index = -1, j=0; j < maxbin; j++)
if (histogram[j] > best)
index = j, best = histogram[j];
}
if (index < 0) break;
histvec[npitch].h_value = best;
histvec[npitch].h_index = index;
}
#if 1
if (x->x_nprint)
{
for (i = 0; i < npitch; i++)
{
post("index %d freq %f --> value %f", histvec[i].h_index,
exp((1./BPERO_OVER_LOG2) * (histvec[i].h_index + 96)),
histvec[i].h_value);
post("next %f , prev %f",
exp((1./BPERO_OVER_LOG2) * (histvec[i].h_index + 97)),
exp((1./BPERO_OVER_LOG2) * (histvec[i].h_index + 95)) );
}
}
#endif
/* for each histogram peak, we now search back through the
* FFT peaks. A peak is a pitch if either there are several
* harmonics that match it, or else if (a) the fundamental is
* present, and (b) the sum of the powers of the contributing peaks
* is at least 1/100 of the total power.
*
* A peak is a contributor if its frequency is within 25 cents of
* a partial from 1 to 16.
*
* Finally, we have to be at least 5 bins in frequency, which
* corresponds to 2-1/5 periods fitting in the analysis window.
*/
for (i = 0; i < npitch; i++)
{
float cumpow = 0, cumstrength = 0, freqnum = 0, freqden = 0;
int npartials = 0, nbelow8 = 0;
/* guessed-at frequency in bins */
float putfreq = fexp((1.0f / BPERO_OVER_LOG2) *
(histvec[i].h_index + 96.0f));
for (j = 0; j < npeak; j++)
{
float fpnum = peaklist[j].p_freq/putfreq;
int pnum = fpnum + 0.5f;
float fipnum = pnum;
float deviation;
if (pnum > 16 || pnum < 1) continue;
deviation = 1.0f - fpnum/fipnum;
if (deviation > -PARTIALDEVIANCE && deviation < PARTIALDEVIANCE)
{
/*
* we figure this is a partial since it's within 1/4 of
* a halftone of a multiple of the putative frequency.
*/
float stdev, weight;
npartials++;
if (pnum < 8) nbelow8++;
cumpow += peaklist[j].p_pow;
cumstrength += fsqrt(fsqrt(peaklist[j].p_pow));
stdev = (peaklist[j].p_width > MINBW ?
peaklist[j].p_width : MINBW);
weight = 1.0f / ((stdev*fipnum) * (stdev*fipnum));
freqden += weight;
freqnum += weight * peaklist[j].p_freq/fipnum;
#if 1
if (x->x_nprint)
{
post("peak %d partial %d f=%f w=%f",
j, pnum, peaklist[j].p_freq/fipnum, weight);
}
#endif
}
#if 1
else if (x->x_nprint) post("peak %d partial %d dev %f",
j, pnum, deviation);
#endif
}
if ((nbelow8 < 4 || npartials < 7) && cumpow < 0.01f * total_power)
histvec[i].h_value = 0;
else
{
float pitchpow = (cumstrength * cumstrength) *
(cumstrength * cumstrength);
float freqinbins = freqnum/freqden;
/* check for minimum output frequency */
if (freqinbins < MINFREQINBINS)
histvec[i].h_value = 0;
else
{
/* we passed all tests... save the values we got */
histvec[i].h_pitch = ftom(hzperbin * freqnum/freqden);
histvec[i].h_loud = (100.0f -DBFUDGE) +
(LOGTODB) * log(pitchpow/n);
}
}
}
#if 1
if (x->x_nprint)
{
for (i = 0; i < npitch; i++)
{
if (histvec[i].h_value > 0)
post("index %d pit %f loud %f", histvec[i].h_index,
histvec[i].h_pitch, histvec[i].h_loud);
else post("-- cancelled --");
}
}
#endif
/* now try to find continuous pitch tracks that match the new
* pitches. First mark each peak unmatched.
*/
for (i = 0, hp1 = histvec; i < npitch; i++, hp1++)
hp1->h_used = 0;
/* for each old pitch, try to match a new one to it. */
for (i = 0, phist = x->x_hist; i < x->x_npitch; i++, phist++)
{
float thispitch = phist->h_pitches[oldphase];
phist->h_pitch = 0; /* no output, thanks */
phist->h_wherefrom = 0;
if (thispitch == 0.0f) continue;
for (j = 0, hp1 = histvec; j < npitch; j++, hp1++)
if ((hp1->h_value > 0) && hp1->h_pitch > thispitch - GLISS
&& hp1->h_pitch < thispitch + GLISS)
{
phist->h_wherefrom = hp1;
hp1->h_used = 1;
}
}
for (i = 0, hp1 = histvec; i < npitch; i++, hp1++)
if ((hp1->h_value > 0) && !hp1->h_used)
{
for (j = 0, phist = x->x_hist; j < x->x_npitch; j++, phist++)
if (!phist->h_wherefrom)
{
phist->h_wherefrom = hp1;
phist->h_age = 0;
phist->h_noted = 0;
hp1->h_used = 1;
goto happy;
}
break;
happy: ;
}
/* copy the pitch info into the history vector */
for (i = 0, phist = x->x_hist; i < x->x_npitch; i++, phist++)
{
if (phist->h_wherefrom)
{
phist->h_amps[newphase] = phist->h_wherefrom->h_loud;
phist->h_pitches[newphase] =
phist->h_wherefrom->h_pitch;
(phist->h_age)++;
}
else
{
phist->h_age = 0;
phist->h_amps[newphase] = phist->h_pitches[newphase] = 0;
}
}
#if 1
if (x->x_nprint)
{
post("vibrato %d %f", x->x_vibbins, x->x_vibdepth);
for (i = 0, phist = x->x_hist; i < x->x_npitch; i++, phist++)
{
post("noted %f, age %d", phist->h_noted, phist->h_age);
#ifndef I860
post("values %f %f %f %f %f",
phist->h_pitches[newphase],
phist->h_pitches[(newphase + HISTORY-1)%HISTORY],
phist->h_pitches[(newphase + HISTORY-2)%HISTORY],
phist->h_pitches[(newphase + HISTORY-3)%HISTORY],
phist->h_pitches[(newphase + HISTORY-4)%HISTORY]);
#endif
}
}
#endif
/* look for envelope attacks */
x->x_attackvalue = 0;
if (x->x_peaked)
{
if (total_db > x->x_amphi)
{
int binlook = newphase - x->x_attackbins;
if (binlook < 0) binlook += HISTORY;
if (total_db > x->x_dbs[binlook] + x->x_attackthresh)
{
x->x_attackvalue = 1;
x->x_peaked = 0;
}
}
}
else
{
int binlook = newphase - x->x_attackbins;
if (binlook < 0) binlook += HISTORY;
if (x->x_dbs[binlook] > x->x_amphi && x->x_dbs[binlook] > total_db)
x->x_peaked = 1;
}
/* for each current frequency track, test for a new note using a
* stability criterion. Later perhaps we should also do as in
* pitch~ and check for unstable notes a posteriori when
* there's a new attack with no note found since the last onset;
* but what's an attack &/or onset when we're polyphonic?
*/
for (i = 0, phist = x->x_hist; i < x->x_npitch; i++, phist++)
{
/*
* if we've found a pitch but we've now strayed from it turn
* it off.
*/
if (phist->h_noted)
{
if (phist->h_pitches[newphase] > phist->h_noted + x->x_vibdepth
|| phist->h_pitches[newphase] < phist->h_noted - x->x_vibdepth)
phist->h_noted = 0;
}
else
{
if (phist->h_wherefrom && phist->h_age >= x->x_vibbins)
{
float centroid = 0;
int not = 0;
for (j = 0, k = newphase; j < x->x_vibbins; j++)
{
centroid += phist->h_pitches[k];
k--;
if (k < 0) k = HISTORY-1;
}
centroid /= x->x_vibbins;
for (j = 0, k = newphase; j < x->x_vibbins; j++)
{
/* calculate deviation from norm */
float dev = centroid - phist->h_pitches[k];
k--;
if (k < 0) k = HISTORY-1;
if (dev > x->x_vibdepth ||
-dev > x->x_vibdepth) not = 1;
}
if (!not)
{
phist->h_pitch = phist->h_noted = centroid;
}
}
}
}
return;
nopow:
for (i = 0; i < x->x_npitch; i++)
{
x->x_hist[i].h_pitch = x->x_hist[i].h_noted =
x->x_hist[i].h_pitches[newphase] =
x->x_hist[i].h_amps[newphase] = 0;
x->x_hist[i].h_age = 0;
}
x->x_peaked = 1;
x->x_dbage = 0;
}
void sigfiddle_debug(t_sigfiddle *x)
{
x->x_nprint = 1;
}
void sigfiddle_print(t_sigfiddle *x)
{
post("amp-range %f %f,", x->x_amplo, x->x_amphi);
post("reattack %d %f,", x->x_attacktime, x->x_attackthresh);
post("vibrato %d %f", x->x_vibtime, x->x_vibdepth);