File: DrumTrack.cpp

package info (click to toggle)
supercollider-sc3-plugins 3.7.1~repack-2
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 14,332 kB
  • ctags: 11,704
  • sloc: cpp: 140,180; lisp: 14,746; ansic: 2,133; xml: 86; makefile: 82; haskell: 21; sh: 8
file content (2076 lines) | stat: -rw-r--r-- 55,872 bytes parent folder | download
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
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
/*
	SuperCollider real time audio synthesis system
 Copyright (c) 2002 James McCartney. All rights reserved.
	http://www.audiosynth.com
 
 This program is free software; you can redistribute it and/or modify
 it under the terms of the GNU General Public License as published by
 the Free Software Foundation; either version 2 of the License, or
 (at your option) any later version.
 
 This program is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 GNU General Public License for more details.
 
 You should have received a copy of the GNU General Public License
 along with this program; if not, write to the Free Software
 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

//This file is part of The BBCut Library. Copyright (C) 2001  Nick M.Collins distributed under the terms of the GNU General Public License full notice in file BBCutLibrary.help

//last revisions pre release 1.0 Nick Collins 15 June 2004

#include "SC_PlugIn.h"
#include "SC_fftlib.h"
#include <stdio.h>

//helpful constants
#define PI 3.1415926535898f
#define TWOPI 6.28318530717952646f 

//FFT data 
#define N 1024  //FFT size
#define NOVER2 512  //FFT size
#define NOVER4 256  //FFT size
#define OVERLAP 512
#define OVERLAPINDEX 512
#define HOPSIZE 512  
#define FS 44100 //assumes fixed sampling rate
#define FRAMESR 86.1328
#define FRAMEPERIOD 0.01161 //seconds

//algorithm quantities NR tempo hypotheses, ND num phase hypotheses

//used to be TEMPOLOW 90 TEMPOHIGH 180 TEMPOSTEP 0.9 INTEGRATIONTIME 3.4 LOUDNESSFRAMESSTORED 293 
//used to be TEMPOLOW 80 TEMPOHIGH 200 TEMPOSTEP 1.2 INTEGRATIONTIME 4 LOUDNESSFRAMESSTORED 350
#define TEMPOLOW 90
//#define TEMPOHIGH 190
#define NR 100
#define ND 20
#define NDOVER2 10
#define NRD 2000
#define NDRECIP 0.05
#define TEMPOSTEP 1.0
#define INTEGRATIONTIME 3.4 
//#define NUMBEATS 4

//MINPERIOD 0.33 MAXPERIOD 0.66 MINDIFF 28 MAXDIFF 58
//MINPERIOD 0.3 MAXPERIOD 0.75 MINDIFF 25 MAXDIFF 65
#define MINPERIOD 0.315
#define MAXPERIOD 0.66
#define MINDIFF 27
#define MAXDIFF 58


//ceil(INTEGRATIONTIME*FRAMESR)
#define LOUDNESSFRAMESSTORED 293
//can calculate after frame 293 then

//these values assume TEMPOLOW 90 and sample rate 44100 with 1024 FFT
//#define FFTSTOREMEM 350
//#define FFTSTORESIZE 8400
//about 4 seconds of FFT powers stored between 0 and 1000 Hz (bin indices 1 to 24 for aforementioned FFT size)

//about once per second, now made quicker update

//old values SKIP 28 TIMEELAPSED 0.3250794
#define SKIP 24
//28/86.1328 
#define TIMEELAPSED 0.2786395
//just quicker than one beat at 180bpm 1.9969164 //0.9984582 

#define tempoconsistency 2
#define phaseconsistency 2
//#define tempotestvalue 0.25
//#define phasetestvalue 0.4

#define MAXONSETS 50

//const int toptempi=5;

const float subbeats[8]= {0.0,0.5,1.0,1.5,2.0,2.5,3.0,3.5};
//const float subbeats[2][16]= {{0.0,0.25,0.5,0.75,1.0,1.25,1.5,1.75,2.0,2.25,2.5,2.75,3.0,3.25,3.5,3.75},{0.0,0.32,0.5,0.82,1.0,1.32,1.5,1.82,2.0,2.32,2.5,2.82,3.0,3.32,3.5,3.82}};
//const float subbeats[16]={0.0,0.25,0.5,0.75,1.0,1.25,1.5,1.75,2.0,2.25,2.5,2.75,3.0,3.25,3.5,3.75};
const float beats[4]= {0.0,1.0,2.0,3.0};

//weights
const float sbtemplate[8]= {1.0,0.5,1.0,0.5,1.0,0.5,1.0,0.5};

//could be different templates based on a parameter
//const float sbtemplate[16]= {1.0,0.1,0.8,0.2,1.0,0.1,0.8,0.2,1.0,0.1,0.8,0.2,1.0,0.1,0.8,0.2};

//phase matching conditions
//float required[4]={1.0, 0.5, 0.3, 0.5};


//use integrationtime for maxwindow
//#define MAXWINDOW    //(numbeats+1)*lowperiod; 


//for hanning window, constructed in plugin initialisation
float hanning[N]; 


int eqlbandbins[43]= {1,2,3,4,5,6,7,8,9,11,13,15,17,19,22,25,28,32,36,41,46,52,58,65,73,82,92,103,116,129,144,161,180,201,225,251,280,312,348,388,433,483,513}; 
int eqlbandsizes[42]= {1,1,1,1,1,1,1,1,2,2,2,2,2,3,3,3,4,4,5,5,6,6,7,8,9,10,11,13,13,15,17,19,21,24,26,29,32,36,40,45,50,30}; 

//this loudness conversion was removed since it worked better without transient detection but just using current energy
//float contours[42][11]= {{ 47.88, 59.68, 68.55, 75.48, 81.71, 87.54, 93.24, 98.84,104.44,109.94,115.31},{ 29.04, 41.78, 51.98, 60.18, 67.51, 74.54, 81.34, 87.97, 94.61,101.21,107.74},{ 20.72, 32.83, 43.44, 52.18, 60.24, 67.89, 75.34, 82.70, 89.97, 97.23,104.49},{ 15.87, 27.14, 37.84, 46.94, 55.44, 63.57, 71.51, 79.34, 87.14, 94.97,102.37},{ 12.64, 23.24, 33.91, 43.27, 52.07, 60.57, 68.87, 77.10, 85.24, 93.44,100.90},{ 10.31, 20.43, 31.03, 40.54, 49.59, 58.33, 66.89, 75.43, 83.89, 92.34,100.80},{  8.51, 18.23, 28.83, 38.41, 47.65, 56.59, 65.42, 74.16, 82.89, 91.61,100.33},{  7.14, 16.55, 27.11, 36.79, 46.16, 55.27, 64.29, 73.24, 82.15, 91.06, 99.97},{  5.52, 14.58, 25.07, 34.88, 44.40, 53.73, 62.95, 72.18, 81.31, 90.44, 99.57},{  3.98, 12.69, 23.10, 32.99, 42.69, 52.27, 61.66, 71.15, 80.54, 89.93, 99.31},{  2.99, 11.43, 21.76, 31.73, 41.49, 51.22, 60.88, 70.51, 80.11, 89.70, 99.30},{  2.35, 10.58, 20.83, 30.86, 40.68, 50.51, 60.33, 70.08, 79.83, 89.58, 99.32},{  2.05, 10.12, 20.27, 30.35, 40.22, 50.10, 59.97, 69.82, 79.67, 89.52, 99.38},{  2.00,  9.93, 20.00, 30.07, 40.00, 49.93, 59.87, 69.80, 79.73, 89.67, 99.60},{  2.19, 10.00, 20.00, 30.00, 40.00, 50.00, 59.99, 69.99, 79.98, 89.98, 99.97},{  2.71, 10.56, 20.61, 30.71, 40.76, 50.81, 60.86, 70.96, 81.01, 91.06,101.17},{  3.11, 11.05, 21.19, 31.41, 41.53, 51.64, 61.75, 71.95, 82.05, 92.15,102.33},{  2.39, 10.69, 21.14, 31.52, 41.73, 51.95, 62.11, 72.31, 82.46, 92.56,102.59},{  1.50, 10.11, 20.82, 31.32, 41.62, 51.92, 62.12, 72.32, 82.52, 92.63,102.56},{ -0.17,  8.50, 19.27, 29.77, 40.07, 50.37, 60.57, 70.77, 80.97, 91.13,101.23},{ -1.80,  6.96, 17.77, 28.29, 38.61, 48.91, 59.13, 69.33, 79.53, 89.71, 99.86},{ -3.42,  5.49, 16.36, 26.94, 37.31, 47.61, 57.88, 68.08, 78.28, 88.41, 98.39},{ -4.73,  4.38, 15.34, 25.99, 36.39, 46.71, 57.01, 67.21, 77.41, 87.51, 97.41},{ -5.73,  3.63, 14.74, 25.48, 35.88, 46.26, 56.56, 66.76, 76.96, 87.06, 96.96},{ -6.24,  3.33, 14.59, 25.39, 35.84, 46.22, 56.52, 66.72, 76.92, 87.04, 97.00},{ -6.09,  3.62, 15.03, 25.83, 36.37, 46.70, 57.00, 67.20, 77.40, 87.57, 97.68},{ -5.32,  4.44, 15.90, 26.70, 37.28, 47.60, 57.90, 68.10, 78.30, 88.52, 98.78},{ -3.49,  6.17, 17.52, 28.32, 38.85, 49.22, 59.52, 69.72, 79.92, 90.20,100.61},{ -0.81,  8.58, 19.73, 30.44, 40.90, 51.24, 61.52, 71.69, 81.87, 92.15,102.63},{  2.91, 11.82, 22.64, 33.17, 43.53, 53.73, 63.96, 74.09, 84.22, 94.45,104.89},{  6.68, 15.19, 25.71, 36.03, 46.25, 56.31, 66.45, 76.49, 86.54, 96.72,107.15},{ 10.43, 18.65, 28.94, 39.02, 49.01, 58.98, 68.93, 78.78, 88.69, 98.83,109.36},{ 13.56, 21.65, 31.78, 41.68, 51.45, 61.31, 71.07, 80.73, 90.48,100.51,111.01},{ 14.36, 22.91, 33.19, 43.09, 52.71, 62.37, 71.92, 81.38, 90.88,100.56,110.56},{ 15.06, 23.90, 34.23, 44.05, 53.48, 62.90, 72.21, 81.43, 90.65, 99.93,109.34},{ 15.36, 23.90, 33.89, 43.31, 52.40, 61.42, 70.29, 79.18, 88.00, 96.69,105.17},{ 15.60, 23.90, 33.60, 42.70, 51.50, 60.20, 68.70, 77.30, 85.80, 94.00,101.70},{ 15.60, 23.90, 33.60, 42.70, 51.50, 60.20, 68.70, 77.30, 85.80, 94.00,101.70},{ 15.60, 23.90, 33.60, 42.70, 51.50, 60.20, 68.70, 77.30, 85.80, 94.00,101.70},{ 15.60, 23.90, 33.60, 42.70, 51.50, 60.20, 68.70, 77.30, 85.80, 94.00,101.70},{ 15.60, 23.90, 33.60, 42.70, 51.50, 60.20, 68.70, 77.30, 85.80, 94.00,101.70},{ 15.60, 23.90, 33.60, 42.70, 51.50, 60.20, 68.70, 77.30, 85.80, 94.00,101.70}};
//double phons[11]={2,10,20,30,40,50,60,70,80,90,100};


extern "C"
{
	void load(InterfaceTable *inTable);
}

InterfaceTable *ft;



struct DrumTrack : Unit {
	
	//FFT data
	int m_bufWritePos;
	float * m_prepareFFTBuf;
	float * m_FFTBuf;

	scfft *m_scfft;
	
	//time positions
	long m_frame;
	
	//loudness measure
	float m_loudness[LOUDNESSFRAMESSTORED];
	int m_loudnesscounter;
	float m_loudbands[40]; 
	
	//for bass onset detections
	float m_bassonsets[LOUDNESSFRAMESSTORED];
	
	//tempo
	
	float m_currtempo;
	float m_lasttempo; 
	float m_tempodiff[tempoconsistency]; //initialise to ones
	int m_tdindex;
	
	//phase
	
	float m_currphase;
	float m_lastphasewinner;
	float m_phasediffs[phaseconsistency]; //initialise to ones
	int m_pdindex;
	
	//phasor, trigger beat and modulo when >1.0
	float m_phase, m_phaseperblock;
	
	//phasor output separate so can have it lock and keep going when don't want to track
	float m_outputphase, m_outputtempo, m_outputphaseperblock;
	
	int halftrig;
    int q1trig;
	int q2trig;
	
	//can make twice as many including groove, or just test eighth note positions for now
	
	//dynamic programming 
	float m_lastscore[40];
	float m_lastcandidatetempo[10]; //indices
	float m_lastcandidatephase[40];
	
	//candidate scores
	//float m_scores[NRD]; //2000 possibles
	float m_bestscores[10];
	float m_besttemposcores[10]; //store tempi
	float m_bestphasescores[40]; //store phases four for each, always best two and two antiphase 
	float m_phasescores[40]; //store the actual scores for use in the the dyn prog cost function
	
	//amortization
	int m_amortisationstate;
	int m_storeloudnesscounter;
	float m_scorenorm; //normalise all scores in a sweep by the maximum
	
	//chord detection- Goto histogramming requires storing fft bins 1 to 24 over last 4 seconds (see workbook 15 dec 04)
//	float * m_fftstore;
//	int m_fftstorepos;
//	int m_fftstoreposhold;
//	float m_chordhistogram[24];
//	float m_chordhistogramprev[24];
//	


	//bass/chord normalisation
	float m_bassmax; 
	
	//,m_chordmax;
	
	//final consistency requirement
	float m_prevphase,m_prevtempo;
	float m_consistency;
	
	
	//goto onset spotting
	float * m_powerbuf[3];
	int m_powerbufcount;
	
	float m_maxsnaresig;
	long m_lastsnaredetect;
	
	float m_maxkicksig;
	long m_lastkickdetect;
	
	//0 for nothing, 1 for snare, 2 for kick, same counter as m_loudnesscounter 
	int m_onsets[LOUDNESSFRAMESSTORED];
	//int onsetposition[50];
	int m_onsetposition[MAXONSETS]; //no more than this
	float m_patternphase; //if 
	float m_patternscore;
	float m_patterntempo;
	
	float * m_prior;
	
	int m_debugmode;
	
};



extern "C"
{
	//required interface functions
	void DrumTrack_next(DrumTrack *unit, int wrongNumSamples);
	void DrumTrack_Ctor(DrumTrack *unit);
	void DrumTrack_Dtor(DrumTrack *unit);
}

//other functions
void preparefft(DrumTrack *unit, float* in, int n);
void dofft(DrumTrack *unit);
void calculateloudness(DrumTrack *unit);
void updatetempophase(DrumTrack *unit, float tempowinner,float phasewinner);
float phasematchesbassonsets(DrumTrack *unit, float phase, float bps);
//float comparechordetect(DrumTrack *unit, float phase, float bps);

void snaredetection(DrumTrack *unit);
void kickdetection(DrumTrack *unit);
int findarchetype(DrumTrack *unit, float tempowinner);
void testpattern(DrumTrack * unit, int &bestindex, float &bestscore, int &besttempo, int first, int diff, int firstval);

//amortisation
void do100thscoring(DrumTrack *unit,int j);
float calcphasediff(float prevphase, float phase, float prevtempo, float tempo);
void do40thpath(DrumTrack *unit,int i);
void finaldecision(DrumTrack *unit);

//old
void tempoassess(DrumTrack *unit, float *, float *);

void DrumTrack_Ctor(DrumTrack* unit) {
	int i,j;
	
	unit->m_debugmode=(int)ZIN0(10);
	
	////prior//////
	
	unit->m_prior=NULL;
	
	World *world = unit->mWorld;
    
	int bufnum = (int)ZIN0(7);
	
	//if (bufnum >= world->mNumSndBufs) bufnum = 0;
	
	//printf("bufnum%d\n",bufnum);
	
	if (bufnum>=0) {
	SndBuf *buf = world->mSndBufs + bufnum; 
	
	int priorbufsize = buf->samples;

	if(priorbufsize!=100) {printf("buffer for tempo weightings prior not size 100  bufnum%d\n",bufnum);}
	else {
	unit->m_prior= buf->data; 
	
	//for (int h=0; h<100; ++h) {
//	printf("%f\n",unit->m_prior[h]);
	//}
	}
	}
	
	
	////////FFT data///////////
	
	unit->m_prepareFFTBuf = (float*)RTAlloc(unit->mWorld, N * sizeof(float));
	unit->m_FFTBuf = (float*)RTAlloc(unit->mWorld, N * sizeof(float));
	unit->m_bufWritePos = 0;	

	//N=1024
	SCWorld_Allocator alloc(ft, unit->mWorld);
	//no overlap
	unit->m_scfft = scfft_create(N, N, kHannWindow, unit->m_FFTBuf, unit->m_FFTBuf, kForward, alloc);
	
	////////time positions//////////
	unit->m_frame=0;
	
	/////////loudness measure////////
	unit->m_loudnesscounter=LOUDNESSFRAMESSTORED-1;
	//zero loudness store 
	for(j=0;j<LOUDNESSFRAMESSTORED;++j) {
		unit->m_loudness[j]=0.0;
		unit->m_bassonsets[j]=0.0;
	}
	
	//zero previous specific loudness in Bark bands
	for(j=0;j<40;++j) {
		unit->m_loudbands[j]=0.0;
	}
	
	/////////tempo assess///////////
	unit->m_currtempo=2;
	unit->m_lasttempo=2; 
	
	for(j=0;j<tempoconsistency;++j)
		unit->m_tempodiff[j]=1.0;		//if 0.0 biases taking first tempo estimate it can! 
	
	unit->m_tdindex=0;
	
	
	////////phase assess///////////
	
	unit->m_currphase=0.0;
	
	unit->m_lastphasewinner=0.0;
	
	for(j=0;j<phaseconsistency;++j)
		unit->m_phasediffs[j]=1.0; //take first phase you can? No, wait
	
	unit->m_pdindex=0;
	
	unit->m_phase=0.0;
	
	//default of 2bps
	unit->m_phaseperblock= (unit->mWorld->mFullRate.mBufLength*2)/(FS);
	
	unit->m_outputphase= unit->m_phase;
	unit->m_outputtempo= unit->m_currtempo;
	unit->m_outputphaseperblock= unit->m_phaseperblock;
	
	unit->mCalcFunc = (UnitCalcFunc)&DrumTrack_next;
	
	unit->halftrig=0;
    unit->q1trig=0;
	unit->q2trig=0;
	
	//amortisation
	unit->m_amortisationstate=142; //off
	
	//dyn prog initialise playing field
	float * lastscore= unit->m_lastscore;
	float * lastcandtempo= unit->m_lastcandidatetempo;
	float * lastcandphase= unit->m_lastcandidatephase;
	
	for(j=0;j<10;++j) {
		
		lastcandtempo[j]=1.5+j*0.15; //even spread
		
		for (i=0;i<4;++i) {
			lastcandphase[4*j+i]=i*0.25; //even spread
			lastscore[4*j+i]=0;
			
		}
	}
	
	
	
	unit->m_scorenorm= 1.0;
	
	//int storesize= FFTSTORESIZE; //24*FFTSTOREMEM;
//	
//	unit->m_fftstore = (float*)RTAlloc(unit->mWorld, storesize * sizeof(float));
//	
//	float * fftstore= unit->m_fftstore;
//	
//	//zero initialise
//	for(j=0;j<storesize;++j) fftstore[j]=0;
//	
//	unit->m_fftstorepos=FFTSTOREMEM-1;
//	
	unit->m_bassmax=1.0;
	//unit->m_chordmax=1.0;
	
	unit->m_prevphase=0.0;
	unit->m_prevtempo=2;
	unit->m_consistency=0.0;
	
	
	//snare detection
	for (i=0;i<3;++i)
		unit->m_powerbuf[i] = (float*)RTAlloc(unit->mWorld, NOVER4 * sizeof(float)); 
	
	//zero buffers
	for (i=0;i<3;++i)
		for (j=0;j<NOVER4;++j)
			unit->m_powerbuf[i][j]=0.0;
	
	unit->m_powerbufcount=0;
	
	unit->m_maxsnaresig=ZIN0(9); //*15.0; //15.0; //40.0; //from testing, taken as the fair value
	unit->m_lastsnaredetect=0;
	
	unit->m_maxkicksig=ZIN0(8)*20000.0; //1.0
	
	//blank out onsets
	for(j=0;j<LOUDNESSFRAMESSTORED;++j) {
		unit->m_onsets[j]=0;
	}
	
}



void DrumTrack_Dtor(DrumTrack *unit)
{
	
	RTFree(unit->mWorld, unit->m_prepareFFTBuf);
	RTFree(unit->mWorld, unit->m_FFTBuf);
	//RTFree(unit->mWorld, unit->m_fftstore);
		
	for (int i=0;i<3;++i)
		RTFree(unit->mWorld, unit->m_powerbuf[i]); 
	
	if(unit->m_scfft) {
		SCWorld_Allocator alloc(ft, unit->mWorld);
		scfft_destroy(unit->m_scfft, alloc);
	}
}


void DrumTrack_next(DrumTrack *unit, int wrongNumSamples)
{
	//would normally be float,will be cast to int for Tristan's optimisation
	float *in = IN(0);
	
	//updated at control rate
	unit->m_maxsnaresig=ZIN0(9); //*15.0; //15.0; //40.0; //from testing, taken as the fair value
	unit->m_maxkicksig=ZIN0(8)*20000.0; //1.0
	
	//printf("%d \n",wrongNumSamples);
	
	int numSamples = unit->mWorld->mFullRate.mBufLength;
	
	//conditions in reverse order to avoid immediate spillover
	
	//printf("state %d \n",unit->m_amortisationstate);	
	
	//final update phase decision
	if(unit->m_amortisationstate==140) {
		
		//printf("finaldecision \n");	
		
		unit->m_amortisationstate=unit->m_amortisationstate+1; //141 is off, nothing to do
		
		finaldecision(unit);
	}
	
	//then path assessments, 40*40= 1600, do 40 each time for conceptual clarity
	if((unit->m_amortisationstate>99) && (unit->m_amortisationstate<140)) {
		
		//printf("do40thpath %d \n",unit->m_amortisationstate);	
		
		if(unit->m_amortisationstate==100) {
			
			//maybe 
			//	if((1.0/unit->m_bestscores[0])>0.00001) {
			if((1.0/unit->m_bestscores[0])<unit->m_scorenorm) {
				unit->m_scorenorm= 1.0/unit->m_bestscores[0];
				
				//printf("%f \n",unit->m_scorenorm);
				 } 
			//else
			//	unit->m_scorenorm= 1.0;
			
			//set weakest candidate(s) to be the patten phase, tempo
			
			//dangerous
			//unit->m_bestscores[9]=unit->m_patternscore;
			unit->m_bestscores[9]=unit->m_bestscores[0];

		unit->m_besttemposcores[9]= unit->m_patterntempo;
		unit->m_bestphasescores[36]= unit->m_patternphase;
			
			
		}
		
		do40thpath(unit, unit->m_amortisationstate-100);
		unit->m_amortisationstate=unit->m_amortisationstate+1;
		
	}
	
	//calculation spread out in time
	if(unit->m_amortisationstate<100) {
		
		//printf("do100thscore %d \n",unit->m_amortisationstate);	
		
		do100thscoring(unit,unit->m_amortisationstate);
		
		unit->m_amortisationstate=unit->m_amortisationstate+1;
		
	}
	
	preparefft(unit, in, numSamples);
	
	//test if impulse to output
	
	unit->m_phase+=unit->m_phaseperblock;
	
	//if not locked, update output phase from model phase, else keep a separate output phase
	
	float lock= ZIN0(1);
	
	//printf("lock %f \n",lock);
	
	if(lock<0.5) {
	
	unit->m_outputphase= unit->m_phase;
	unit->m_outputtempo= unit->m_currtempo;
	unit->m_outputphaseperblock= unit->m_phaseperblock;
	} else {
	unit->m_outputphase+=unit->m_outputphaseperblock;
	}
	
	if (unit->m_phase >= 1.f) {unit->m_phase-= 1.f;}

	//0 is beat, 1 is quaver, 2 is semiquaver, 3 is actual current tempo in bps
	//so no audio accuracy with beats, just asap, may as well be control rate
	ZOUT0(0)=0.0;
	ZOUT0(1)=0.0;
	ZOUT0(2)=0.0;
	ZOUT0(3)=unit->m_outputtempo; //*0.016666667;
	
	//output beat
	if (unit->m_outputphase >= 1.f) {
		
		//printf("beat \n");
		
		unit->m_outputphase -= 1.f;
		ZOUT0(0)=1.0;
		ZOUT0(1)=1.0;
		ZOUT0(2)=1.0;
		unit->halftrig=0;
		unit->q1trig=0;
		unit->q2trig=0;
	}
	
	if (unit->m_outputphase>=0.5 && unit->halftrig==0) {
		ZOUT0(1)=1.0;
		ZOUT0(2)=1.0;
		unit->halftrig=1;
	}
	
	if (unit->m_outputphase>=0.25 && unit->q1trig==0) {
		ZOUT0(2)=1.0;
		unit->q1trig=1;
	}
	
	if (unit->m_outputphase>=0.75 && unit->q2trig==0) {
		ZOUT0(2)=1.0;
		unit->q2trig=1;
	}
	
	//for (int i=0; i<numSamples; ++i) {
	//		*++output = 0.0;
	//	}
	//	
	
	
}


//Tristan recommends copying ints rather than floats- I say negligible compared to other algorithm costs for the moment
// TO TREAT, check, update, probably replace entirely with pre allocated buffer based scheme? 
void preparefft(DrumTrack *unit, float* in, int n) {
	
	int i, index = 0, cpt = n, maxindex;
	
	int bufpos= unit->m_bufWritePos;
	
	float * preparefftbuf=unit->m_prepareFFTBuf;
	float * fftbuf= unit->m_FFTBuf;
	
	// Copy input samples into prepare buffer	
	while ((bufpos < N) && (cpt > 0)) {
		preparefftbuf[bufpos] = in[index];
		bufpos++;
		index++;
		cpt--;
	}
	
	// When Buffer is full...
	if (bufpos >= N) {
		
		// Make a copy of prepared buffer into FFT buffer for computation
		for (i=0; i<N; i++) 
			fftbuf[i] = preparefftbuf[i];
		
		// Save overlapping samples back into buffer- no danger since no indices overwritten
		for (i=0; i<OVERLAP; i++) 
			preparefftbuf[i] = preparefftbuf[OVERLAPINDEX+i];
		
		maxindex = n - index + OVERLAPINDEX;
		
		//blockSize less than N-OVERLAPINDEX so no problem
		// Copy the rest of incoming samples into prepareFFTBuffer
		for (i=OVERLAPINDEX; i<maxindex; i++) {
			preparefftbuf[i] = in[index];
			index++;
		}
		
		bufpos = maxindex;
		
		//FFT buffer ready- calculate away!
		unit->m_frame= unit->m_frame+1;
		dofft(unit);
	}
	
	
	unit->m_bufWritePos= bufpos;
	//printf("%d \n",bufpos);
	
}



//calculation function once FFT data ready
void dofft(DrumTrack *unit) {
	
	int i;
	
	float * fftbuf= unit->m_FFTBuf;
	
	for (i=0; i<N; ++i)
		fftbuf[i] *= hanning[i];

	scfft_dofft(unit->m_scfft);
	
	float val1, val2;
	// Squared Absolute so get power
	for (i=2; i<N; i+=2) {
		val1 = fftbuf[i];
		val2 = fftbuf[i+1];
		//i>>1 is i/2
		fftbuf[i>>1] = (val1*val1)+(val2*val2);
	}
	
	//calculate loudness first, increments loudnesscounter needed in checkforonsets
	calculateloudness(unit);
	
	//for snare and kick detection
	///keeping a record of last three fftbufs
	//update last three powers in required bands
	unit->m_powerbufcount=(unit->m_powerbufcount+1)%3;
	
	float * storebuf= unit->m_powerbuf[unit->m_powerbufcount];
	
	//only stores lower bands under 11kHz
	for (i=0;i<NOVER4;++i)
		storebuf[i]=fftbuf[i];
	
	//zero out now, not two ago like before
	int onsetframe= (unit->m_loudnesscounter+LOUDNESSFRAMESSTORED)%LOUDNESSFRAMESSTORED;
	unit->m_onsets[onsetframe]=0;  //zero out old values		
	
	//high detections
	//gotodetectioninband(unit,unit->m_gotobands[1]);	
	
	//low detections second so these onsets always overpower the hgh onsets	
	//gotodetectioninband(unit,unit->m_gotobands[0]);
	
	//snare detections (not overwritable by kicks)
	snaredetection(unit);
	kickdetection(unit);
	
	//avoid snare overwrite, trust snare detector
	
	//	//check for any twos in last ten, checking ten either side
	//	int basepos= (unit->m_loudnesscounter+LOUDNESSFRAMESSTORED-2)%LOUDNESSFRAMESSTORED;
	//	
	//	//if any kicks near recent snares, zero the snares
	//	for (j=0;j<5;++j) {
	//	 int pos=(basepos+LOUDNESSFRAMESSTORED-j)%LOUDNESSFRAMESSTORED;
	//	 
	//	 if(unit->m_onsets[pos]==1) {
	//		for (i=0;i<5;++i) { //checking 5 previous frames from current position, will gradually test 5 either side
	//		 int pos2=(basepos+LOUDNESSFRAMESSTORED-i)%LOUDNESSFRAMESSTORED;
	//	
	//		if(unit->m_onsets[pos2]==2) {
	//		unit->m_onsets[pos]=0;
	//		//break //could break because know no 2 can come till 10 after a 2 onset, but safer to leave alone
	//		}
	//		
	//		}
	//	 
	//	 }
	//	 
	//	} 
	
	//int onsetval= unit->m_onsets[(unit->m_loudnesscounter+LOUDNESSFRAMESSTORED-2)%LOUDNESSFRAMESSTORED];
	//if(onsetval) printf("onset %d \n",onsetval);
	
	
	if (unit->m_frame%SKIP==0) {
		
		//printf("amortisation time \n");
		
		//amortisation- 8 control periods in a frame
		//have 2000 calcs to do, split over 100 control periods = 6400 samples, ie one tempo per control period
		
		unit->m_amortisationstate=0;
		
		//fix time reference for calculations, so it doesn't update during the amortisation
		unit->m_storeloudnesscounter= unit->m_loudnesscounter;  
		
		//unit->m_fftstoreposhold= unit->m_fftstorepos;
		
		unit->m_currphase=unit->m_phase;
		
		//reset scoring
		//float * scores=  unit->m_scores;
		float * bestscores= unit->m_bestscores;
		//float * besttemposcores=unit->m_besttemposcores;
		//	float * bestphasescores=unit->m_bestphasescores;
		//	
		for (i=0; i<10; ++i) { 
			
			//no need to zero, gets written into during this process
			//for (j=0; j<ND; ++j)
			//scores[i][j]; //2000 possibles
			
			bestscores[i]=-1000;
			//besttemposcores[i]=0.0; 
			//	bestphasescores[i*4]=0.0; 
			//	bestphasescores[i*4+1]=0.0;
			//	bestphasescores[i*4+2]=0.0;
			//	bestphasescores[i*4+3]=0.0;
			//	no need to reset these since will automatically get written over
		}
		
		//assess pattern matches for scoring 
		int match= findarchetype(unit,0.0); //don't know tempowinner yet 
	
		//if (match && (unit->m_patternscore>1.75)) {
//	
//		printf("pattern phase %f pattern tempo %f \n",unit->m_patternphase,unit->m_patterntempo);
//
//		}

		
		
		//float tempowinner,phasewinner;
		//	
		//	tempoassess(unit,&tempowinner,&phasewinner);
		//	
		//	//1.0-phasewinner because the phasenow should be (1.0-pw) to get to a beat at pw
		//	updatetempophase(unit, tempowinner,phasewinner);
		//	
	}
	
	//updatephase
	
}	



//global
float scoretemp[ND]; 		

void do100thscoring(DrumTrack *unit,int j) {
	int i,k, baseframe, rounded, testframe;
	float tempo, Ti, starttime;
	float phase, beatpos;
	float beat, beatsum; 
	float prior;
	
	
	//weight by prior if one exists
	prior= 1.0;
	
	if (unit->m_prior) {prior= unit->m_prior[j];}
	
	//printf("prior %f %d\n",prior,j);
	
	baseframe=unit->m_storeloudnesscounter+LOUDNESSFRAMESSTORED;
	float * loudness= unit->m_loudness;
	
	float best, secondbest;
	int bestindex, secondbestindex;
	//find best two phases 
	best=-1000;
	bestindex=0;
	secondbest=-1000;
	secondbestindex=0;
	
	//float * scores=  unit->m_scores;
	
	//int groovetype=0;
	
	//for(int groove=0;groove<1;++groove) {
	
	tempo=TEMPOLOW+(j*TEMPOSTEP);
	
	Ti= 60/tempo;   //could have precalculated these- OPTIMISE 
	
	starttime= 0.0-(5*Ti);
	
	
	//different phase options
	for (k=0; k<ND; ++k) {
		
		phase=k*NDRECIP;    
		
		beatpos=starttime+(Ti*phase); 
		
		beatsum=0.0;
		
		//8 subbeats
		for (i=0;i<8;++i) {
			
			beat=((subbeats[i])*Ti)+beatpos;
			
			//beat=((subbeats[groove][i])*Ti)+beatpos;
			
			//get nearest frame position  
			
			rounded= (int)floor(beat*FRAMESR+ .5f);
			
			//printf("error in rounding? beat %f rounded %d correction %d \n",beat, rounded,  (int)(beat*FRAMESR -.5f));
			
			testframe=(baseframe+rounded)%LOUDNESSFRAMESSTORED;
			
			beatsum+=(sbtemplate[i])*loudness[testframe];    
			
			//could do a local sum +-2 frames
			
		//	float localsum=0.0;
//			
//			for (int hh=0; hh<3; ++hh) {
//			
//			int locale= (testframe-1+hh+LOUDNESSFRAMESSTORED)%LOUDNESSFRAMESSTORED;
//			
//			localsum+=loudness[locale];    
//			}
			
			//beatsum+=(sbtemplate[i])*localsum*0.333;    
			
		}
		
		beatsum=beatsum*prior;
		scoretemp[k]=beatsum; 
		//what is best so far?
		
		//for normalising
		if (beatsum>best) {
			secondbest=best;
			secondbestindex=bestindex;
			best=beatsum;
			bestindex=k;
			//groovetype=groove;
		} else if (beatsum>secondbest) {
			secondbest=beatsum;
			secondbestindex=k;
		} 
		
		//not sure this needs to be stored at all
		//scores[j*ND+k]=beatsum; 
		
	}
	
	
	
	
	//what is minimum you need to do to get into the best scoring table? 
	float leastbest= unit->m_bestscores[9];
	
	//update topten		
	if (best>leastbest) {
		
		float * bestscores= unit->m_bestscores;
		float * besttemposcores=unit->m_besttemposcores;
		float * bestphasescores=unit->m_bestphasescores;
		float * phasescores= unit->m_phasescores;
		
		//where does it rate
		for (i=0; i<10; ++i) {
			if(best>bestscores[i]) break;
		}
		
		//copy all below down one
		for (k=9; k>i; --k) {
			bestscores[k]=bestscores[k-1];
			besttemposcores[k]=besttemposcores[k-1];
			
			for (int g=0; g<4; ++g) {
				bestphasescores[k*4+g]=bestphasescores[(k-1)*4+g];
				phasescores[k*4+g]=phasescores[(k-1)*4+g];	
			}
		}
		
		//insertion at i
		bestscores[i]=best;
		besttemposcores[i]=tempo*0.01666667; //j;
		
		//actually stores phases
		bestphasescores[i*4]=bestindex*NDRECIP;
		bestphasescores[i*4+1]=secondbestindex*NDRECIP;
		bestphasescores[i*4+2]=((bestindex+NDOVER2)%ND)*NDRECIP; //pi out of phase option, may be doubled up safely I assume
		bestphasescores[i*4+3]=((secondbestindex+NDOVER2)%ND)*NDRECIP;
		
		//this stores scores
		phasescores[i*4]= scoretemp[bestindex];
		phasescores[i*4+1]= scoretemp[secondbestindex];
		phasescores[i*4+2]= scoretemp[((bestindex+NDOVER2)%ND)];
		phasescores[i*4+3]= scoretemp[((secondbestindex+NDOVER2)%ND)];
		
	}
	
	}





void do40thpath(DrumTrack *unit,int i) {
	
	int j;
	
	//will make them constants, variables?
	float dynleak=ZIN0(2); //0.7; //0.5;
	float tempowt=ZIN0(3); //1.0; //1.0; //dyn tempo cost
	float phasewt=ZIN0(4); //2.0; //dyn phase cost
						//	float gamma=0.0; //1.0; //1.0 //consistent tempo cost
						//	float epsilon=0.0; //2.0; //2.0; //consistent phase cost
	float basswt=ZIN0(5); //1.0;
	float patternwt=ZIN0(6); //1.0 used to be chordwt;
	
	
	//printf("do40th test1 %d %d \n",unit->m_amortisationstate,i);	
	
	float * lastscore= unit->m_lastscore;
	
	//maybe store float as cheaper on CPU?
	float * lastcandtempo= unit->m_lastcandidatetempo;
	float * lastcandphase= unit->m_lastcandidatephase;
	
	//float * bestscores= unit->m_bestscores;
	
	//or should have stored actual values?
	float tempocand= unit->m_besttemposcores[i/4];
	float phasecand=unit->m_bestphasescores[i];
	
	
	//test phasewinner against bass onsets
	float bassonsetson= phasematchesbassonsets(unit,phasecand,tempocand);
	//compare to antiphase
	
	//printf("phase winner %f antiphase %f\n",phasewinner,fmod(phasewinner+0.5,1.0));
	
	float antiphase= fmod(phasecand+0.5,1.0);
	
	float bassonsetsoff= phasematchesbassonsets(unit,antiphase,tempocand);
	
	if (bassonsetsoff<1) bassonsetsoff=1;
	
	float bassratio= bassonsetson/bassonsetsoff;
	
	//float chordratio= comparechordetect(unit,phasecand,tempocand);
	
	float bassmax= unit->m_bassmax;
	
	//gradual decay over time
	bassmax= 0.999*bassmax; 
	
	if (bassratio>bassmax) bassmax=bassratio;
	
	unit->m_bassmax=bassmax;
	
	//float chordmax= unit->m_chordmax;
	//
	////gradual decay over time
	//chordmax= 0.999*chordmax; 
	//
	//if (chordratio>chordmax) chordmax=chordratio;
	//
	//unit->m_chordmax=chordmax;
	
	//bigger the bass/chord ratio, less the cost
	float basscost= (basswt)*(1.0-(bassratio/bassmax));
	
	//maximal cost if no evidence
	float patterncost= 1.0; //(chordwt)*(1.0-(chordratio/chordmax));

	if (unit->m_patternscore>1.75) {
		//be more lenient because kick/snare detection is approximate
		float patttempodiff=fabs(tempocand-unit->m_patterntempo);
		if (patttempodiff>0.105) patttempodiff=1; else patttempodiff=0.0;
	
		//phase cost of this phase replacing current phase of oscillator
		float pattphaseerror= fabs(calcphasediff(unit->m_patternphase,phasecand,unit->m_patterntempo,tempocand));
	
		//printf("pattern phase cost %f pattern tempo cost %f \n",pattphaseerror,patttempodiff);
		
		//patterncost= patternwt*((0.0*patttempodiff) + (3*pattphaseerror)); 
		patterncost= patternwt*((0.5*patttempodiff) + (0.5*3*pattphaseerror)); 
	}
	
	
	//values now
	float tempo=unit->m_currtempo;
	float phase=unit->m_currphase; //captured at beginning of amortisation, can't used unit->m_phase without correction as some time has passed due to amortisation
	
	float prevtempo,prevphase;
	
	//find lowest score
	
	//score= previousscore*dynleak + score+ tempotransitioncost*alpha + epsilon*tempoconfrimationcost (from currtempo to this)  
	//beta*phasetransitioncost (path cost) + gamma*phaseconfirmationcost (from last phase to this) 
	
	float cost,mincost;
	
	mincost=1000000;
	
	//these two transitions are dyn prog independent so calculated outside this loop
	//tempo transition costs- fixed if tempo transition above some constant (assume JND for tempo = 8% so 0.105bps at 1.5bps 
	
	float tempodiff=fabs(tempocand-tempo);
	if (tempodiff>0.105) tempodiff=1; else tempodiff=0.0;
	
	float tempowincost= 0.0; //gamma*tempodiff;
	
	//phase cost of this phase replacing current phase of oscillator
	float phaseerror= fabs(calcphasediff(phase,phasecand,tempo,tempocand));
	
	float phasewincost = 0.0; //epsilon*phaseerror;
	
	float tempotransitioncost, phasetransitioncost, oldscorecost, newscorecost;
	
	//aim is to minimse cost, normed to 0-1 then inverted
	//could have *0.01);//fixed norm to avoid bias from calc to calc//
	newscorecost= 1.0-(unit->m_phasescores[i]*(unit->m_scorenorm)); //score normalised by best result //bestscores[j];
	
	//through 40 previous candidates to this one
	for(j=0;j<40;++j) {
		
		prevtempo=lastcandtempo[j/4];
		prevphase=lastcandphase[j];
		
		oldscorecost= dynleak*lastscore[j]; //could pre multiply to avoid redoing in each function call, one optimisation too much maybe
		
		//cost over changing hypotheses (no cost if consistent)
		tempodiff=fabs(prevtempo-tempocand);
		if(tempodiff>0.105) tempodiff=1; else tempodiff=0.0;
		
		tempotransitioncost=tempowt*tempodiff;
		
		//DrumTrack phase transition costs (my correction)
		phaseerror= fabs(calcphasediff(prevphase,phasecand,prevtempo,tempocand));
		
		phasetransitioncost=phasewt*3*phaseerror; //random 3!
		
		cost = oldscorecost+newscorecost+phasetransitioncost+tempotransitioncost+phasewincost+tempowincost+basscost+patterncost;
		
		//if(j==0)
		//printf(" oldscorecost %f newscorecost %f phasetransitioncost %f tempotransitioncost %f phasewincost %f tempowincost %f cost %f \n", oldscorecost,newscorecost,phasetransitioncost,tempotransitioncost,phasewincost,tempowincost,cost);
		
		//cost 
		if (cost<mincost)
			mincost=cost;
		
	}
 	
	//will store mincost in current score until finalisation
	//bestscores[j]=mincost;
	
	//will also be the min passed through as lastscore, works OK
	unit->m_phasescores[i]=mincost; 
	
	//save cost to unit->m_cost[j]=cost for unit->m_phasescores;
	
}

//find next beat times with respect to old phase, tempo and new phase, tempo at this point

//favours low tempi since phase representation is time independent! 
float calcphasediff(float prevphase, float phase, float prevtempo, float tempo) {
	
	float a,b,c,result;
	
	float phasediff= TIMEELAPSED*prevtempo- prevphase; //used to be timediff= TIMEELAPSED-(prevphase/prevtempo); phasediff=timediff*prevtempo
	
	//phase from old beat to next beat, see 12 OCT diagram
	float phasetobeat= floor(phasediff)+1.0-phasediff;
	
	//corrected for old tempo (currtempo) then rewritten in terms of the new tempowinner
	//float requiredphase= (phasetobeat*tempo)/prevtempo;
	
	float oldtimetobeat= phasetobeat/prevtempo; 
	float newtimetobeat= phase/tempo;	
	
	a=oldtimetobeat;
	b=newtimetobeat;
	
	//wlog
	if(b<a) {c=b; b=a; a=c;  c=1/prevtempo;} else {c= 1/tempo;}
	
	//closest between the two beat predictions
	result= sc_min(b-a, a-(b-c));
//	
//	printf("phase error %f \n",result);
//	
//if correct within 50mS
	//if (result<0.03) result=0.0;

	return result;//sc_min(b-a, a-(b-c));
}

/*
//favours low tempi since phase representation is time independent! 
float calcphasediff(float prevphase, float phase, float prevtempo, float tempo) {
	
	float a,b;
	
	float phasediff= TIMEELAPSED*prevtempo- prevphase; //used to be timediff= TIMEELAPSED-(prevphase/prevtempo); phasediff=timediff*prevtempo
	
	//phase from old beat to next beat, see 12 OCT diagram
	float phasetobeat= floor(phasediff)+1.0-phasediff;
	
	//corrected for old tempo (currtempo) then rewritten in terms of the new tempowinner
	float requiredphase= (phasetobeat*tempo)/prevtempo;
	
	a=requiredphase;
	b=phase;
	
	if(b<a) b=b+1.0;
	
	return sc_min(b-a, 1.0+a-b);
	
}
*/

//choose lowest score as winner-
//only swap if sufficiently unambiguous lead?
void finaldecision(DrumTrack *unit) {
	int i,j;
	
	float * lastscore= unit->m_lastscore;
	
	//maybe store float as cheaper on CPU?
	float * lastcandtempo= unit->m_lastcandidatetempo;
	float * lastcandphase= unit->m_lastcandidatephase;
	
	float * bestscores= unit->m_phasescores; //actually stored in this array, was bestscores before but bestscores used for holding 10 best tempi scores
	
	//or should have stored actual values?
	float * tempocand= unit->m_besttemposcores;
	float * phasecand=unit->m_bestphasescores;
	
	float score,min, secondmin;
	int minindex,secondminindex;
	
	minindex=0;
	min=10000000;
	secondmin=10000000;
	secondminindex=0;
	
	for (j=0;j<40;++j){
		
		score=bestscores[j];
		
		if(score<min) {
			min=score;
			minindex=j;
		} else if((score<secondmin) && (score>min)) {
			secondmin=score;
			secondminindex=j;
		}
		
		
	}
	
	//minindex is winner, update paths
	float phasewinner=phasecand[minindex];
	float tempowinner=tempocand[minindex/4];
	
	//int match= findarchetype(unit,tempowinner); //don't use tempowinner yet 
//	
//	if (match && (unit->m_patternscore>1.75)) {
//	
//	printf("phasewinner %f pattern phase %f tempowinner %f pattern tempo %f \n",phasewinner,unit->m_patternphase, tempowinner,unit->m_patterntempo);
//	
//	phasewinner= unit->m_patternphase; 
//	tempowinner= unit->m_patterntempo;
//	
//	}
	
	//perhaps only allow a tempochange if these two are sufficiently different
	//printf("best %f secondbest %f ratio %f \n",min,secondmin, min/secondmin);
	
	//check consistency of estimate
	
	//float tempodiff=fabs(tempocand-unit->m_prevtempo);
	//if (tempodiff<0.105) tempodiff=1; else tempodiff=0.0;
	//	
	
	//phase cost of last posited phase becoming new phase of oscillator
	float phaseerror= fabs(calcphasediff(unit->m_prevphase,phasewinner,unit->m_prevtempo,tempowinner));
	
	//phaseerror is in time, if less than 0.075 seconds? 
	//used to be 0.1
	//if ((phaseerror<0.1) && ((min/secondmin)<0.995))
	if (phaseerror<0.135) //0.14
		++unit->m_consistency;
	else
		unit->m_consistency=0.0;
	
	unit->m_prevphase=phasewinner;
	unit->m_prevtempo=tempowinner;
	
	
	//printf("phasewinner %f tempowinner %f \n",phasewinner, tempowinner);
	//printf("minimum score %f consistency %f phaseerror %f\n",min,unit->m_consistency,phaseerror);
	
	
	//could make the test value a parameter, or do -2 on score 
	//only swap when sufficient consistent, don't keep swapping if consistent! 
	//if( (unit->m_consistency>2.5) && (unit->m_consistency<3.5)) {
	if( (unit->m_consistency>1.5) && (unit->m_consistency<2.5)) {
	//if( (unit->m_consistency>0.5) && (unit->m_consistency<1.5)) {
	
		//phase has moved on by some time since the calculation frame
		////correction for recording happened during this! 
		//int framespassed= (unit->m_loudnesscounter+LOUDNESSFRAMESSTORED- unit->m_storeloudnesscounter)%LOUDNESSFRAMESSTORED;
		//float phasepassed= framespassed*FRAMEPERIOD*tempowinner;
		
		float timepassed= 0.2046259; //141*64/44100; //fixed by control period, amortisation and sampling rate
		float phasepassed= timepassed*tempowinner; 
		
		//don't update phase if sufficiently similar
		updatetempophase(unit, tempowinner,fmod(phasewinner+phasepassed,1.0)); //phasewinner
		
		if(unit->m_debugmode==1)
		printf("update phasewinner %f pattern phase %f tempowinner %f pattern tempo %f \n",phasewinner,unit->m_patternphase, tempowinner,unit->m_patterntempo);

	}
	
	int index;
	
	//update previous scores with these
	for(j=0;j<10;++j) {
		
		lastcandtempo[j]=tempocand[j]; //even spread
		
		for (i=0;i<4;++i) {
			index=4*j+i;
			lastcandphase[index]=phasecand[index]; //even spread
			lastscore[index]=bestscores[index];
		}
	}
	
}



//used twice, once for phase, once for antiphase?
float phasematchesbassonsets(DrumTrack *unit, float phase, float bps) {
	int i,j,baseframe, rounded, testframe;
	float Ti, starttime, beatpos, beat;
	//just testing at beat frame positions
	
	baseframe=unit->m_storeloudnesscounter+LOUDNESSFRAMESSTORED;
	
	Ti= 1/bps;   //could have precalculated these- OPTIMISE 
	
	////correction for recording happened during this! 
	//int framespassed= (unit->m_loudnesscounter+LOUDNESSFRAMESSTORED- unit->m_storeloudnesscounter)%LOUDNESSFRAMESSTORED;
	//		
	//printf("gone by %d \n",framespassed);		
	//		
	starttime= 0.0-(5*Ti);
	
	beatpos=starttime+(Ti*phase); 
	
	float beatsum=0;
	//int found;
	float avg;
	
	float * bassonsets= unit->m_bassonsets;
	
	//4 beats
	for (i=0;i<4;++i) {
		
		beat=(i*Ti)+beatpos;
		
		rounded= (int)floor(beat*FRAMESR+ .5f);
		
		//found=0;
		avg=0.0;
	
	//	//test within 3 either side
//		for(j=0;j<9;++j) {
//			testframe=(baseframe+rounded+j-4)%LOUDNESSFRAMESSTORED;
//			
//			//could do weighted sum, peak in middle...
//			avg=avg+bassonsets[testframe]; //if(bassonsets[testframe]==1) found=1;
//		}
//		
//		//testframe=(baseframe+rounded)%LOUDNESSFRAMESSTORED;
//		
//		beatsum+=avg*0.11111; //bassonsets[testframe];  
//		
			
		//test within 3 either side
		for(j=0;j<7;++j) {
			testframe=(baseframe+rounded+j-3)%LOUDNESSFRAMESSTORED;
			
			//could do weighted sum, peak in middle...
			avg=avg+bassonsets[testframe]; //if(bassonsets[testframe]==1) found=1;
		}
		
		//testframe=(baseframe+rounded)%LOUDNESSFRAMESSTORED;
		
		beatsum+=avg*0.1428571; //bassonsets[testframe];  
			
											                
	}
	
	return beatsum;
}




//only updates if sufficiently different
void updatetempophase(DrumTrack *unit, float newtempo,float newphase) {
	
	float phase,phasenow,a,b,ahead;
	
	phase=unit->m_phase; 
	
	//test phase difference of new estimate from current phase of oscillator
	
	a=1.0-newphase;
	b=phase;
	
	if(b<a) b=b+1.0;
	
	b=sc_min(b-a, 1.0+a-b); //value from 0.0 to 0.5
	
	//only update if greater than 50mS phase difference (adjusted for tempo)	
	
	//if (b>(0.05*newtempo)) {
	//if (b>(0.025*newtempo)) { //improved CDR by 5%
	if (b>(0.01*newtempo)) { //by 10% now, but 0.0 doesn't help
		//pass in [] if no update
		//if(newphase>=(-0.1))
		phasenow=1.0-newphase;
		//printf("phasechange %f\n",phasenow);
		//printf("check b %f\n",b);
		
	}
	else
		phasenow=phase;
	
	a=phase;
	b=phasenow;
	
	if(b<a) b=1.0+b;
	
	//phasenow up to 1.0 ahead, but by how much? 
	ahead= b-a;
	
	if(ahead>0.5) //phasenow behind phase- drop phase back, ahead will be negative
		ahead= ahead-1.0;
	
	
	//both cases now adjust by ahead
	//correction by up to a quarter cycle? No, correct by a maximum of x seconds, tempo dependent
	
	//actually, correct by full amount as long as there is consistent evidence
	//0.2 seconds in beats at current tempo
	//maxcorrection=1.0; //min(0.1*temponow,1.0); %correct by no more than a beat 
	//ahead=sign(ahead)*(min(abs(ahead),maxcorrection));
	
	//can't correct back earlier than current phase else may beat twice!
	//must temporarily drop the rate
	//phase=mod(max(phase+ahead,floor(phase)),1.0);
	
	//no, just store last beat time and require greater than...
	unit->m_phase=fmod(phase+ahead+1.0,1.0);
	
	unit->m_currtempo=newtempo;
	
	unit->m_phaseperblock= (unit->mWorld->mFullRate.mBufLength*newtempo)/(FS);
	
}




//also calc kick drum detector on bottom three bands
void calculateloudness(DrumTrack *unit) {
	
	int j,k;
	
	float * fftbuf= unit->m_FFTBuf;
	
	float dfsum=0.0; //,lsum=0.0;
	float bassonset=0;
	float basssum=0.0;
	
		//store powers for later chord detection step 
//	float * fftstore= unit->m_fftstore;
//	
//	unit->m_fftstorepos=(unit->m_fftstorepos+1)%FFTSTOREMEM;
//	
//	int basepos= 24*unit->m_fftstorepos;
//	for(j=0;j<24;++j) {
//		fftstore[basepos+j]=fftbuf[j+1];
//	}	
//	
	

	
	for (k=0; k<40; ++k){
		
		int bandstart=eqlbandbins[k];
		//int bandend=eqlbandbins[k+1];
		int bandsize= eqlbandsizes[k];
		
		float bsum=0.0;
		
		for (int h=0; h<bandsize;++h) {
			bsum= bsum+fftbuf[h+bandstart];
		}
		
		//store recips of bandsizes?
		bsum= bsum/bandsize;
		
		
			float diff=bsum; 
		dfsum=dfsum+diff; 
				if (k<5) basssum=basssum+diff; 
		
	}
	
		
	//increment first so this frame is unit->loudnesscounter
	unit->m_loudnesscounter=(unit->m_loudnesscounter+1)%LOUDNESSFRAMESSTORED;
	
		unit->m_loudness[unit->m_loudnesscounter]=dfsum; //dfsum*0.025; //divide by num of bands to get a dB answer
													 //printf("loudness %f %f \n",unit->loudness[unit->loudnesscounter], lsum);
		
bassonset= basssum; //90+(10*log10(basssum));

unit->m_bassonsets[unit->m_loudnesscounter]=bassonset; 

}


//snare limits= [34,177] (may try to reduce summands for CPU saving)
void snaredetection(DrumTrack *unit){
	
	float snaresig,highfreqave,lowfreqave,quantsum, power,temp;
	int count,num;
	
	//will also use these for indexing 
	int now=unit->m_powerbufcount;
	int last, lastlast;
	last=(2+now)%3; //avoid negative numbers in modulo- get back negatives! 
	lastlast=(1+now)%3;
	
	float * nowbuf,*lastbuf,*lastlastbuf;
	nowbuf=unit->m_powerbuf[now];
	lastbuf=unit->m_powerbuf[last];
	lastlastbuf=unit->m_powerbuf[lastlast];
	
    num=0;
    quantsum=0.0;
    count=0;
    
	snaresig=1.0;
	
    for(int f=33; f<177;++f) {
        
        power=lastbuf[f];
        
        highfreqave = 0.25*(lastbuf[f+2]+lastlastbuf[f+1]+lastbuf[f+1]+nowbuf[f+1]);  
        lowfreqave = 0.25*(lastbuf[f-2]+lastlastbuf[f-1]+lastbuf[f-1]+nowbuf[f-1]); 
        
		temp=sc_min(highfreqave,lowfreqave);
		
		//0.25
        if(temp > (0.5*power)) {
            quantsum=quantsum+power;
            num=num+1;
        }
        
		//printf("minave %f power %f quantsum %f \n",temp,power,quantsum);
		
        count=count+1;
        
        if(count==9) {
			//0.125*(quantsum*0.11111111)
			temp=0.0138889*quantsum;
			temp=sc_min(temp,0.54);
            snaresig*=(1+temp);
            quantsum=0.0;
            count=0;
		}
        
    }
    
    //snaresig(1,t)= prod(noisebands); %mean(noisebands); %num
	
	//printf("snaresig %f \n",snaresig);
	
	//can't allow max to get too high else detections suddenly cut out- 100 seems a fair value
	
	//if(snaresig>(unit->m_maxsnaresig)) {
//		//unit->m_maxsnaresig=snaresig;
//		
//		printf("max found! %f \n",snaresig); //unit->m_maxsnaresig);
//	}
	
	//max vals up to 1.54**16 ie about 1000
	
	snaresig=snaresig/(1000*unit->m_maxsnaresig);
	
	//printf("postsnaresig %f \n",snaresig);
	
	//should peak pick really!
	
	//snare detection- don't allow another for a while? m_lastsnaredetect
	if(snaresig>0.5) { //used to be 0.5, can add a user defined threshold
		
		//stop in a row detections
		if(unit->m_lastsnaredetect<(unit->m_frame-6)) {
			
			if(unit->m_debugmode==2)
			printf("snare found! %ld %ld \n", unit->m_lastsnaredetect, unit->m_frame);
			
			//occured one frame ago, but do -2 to avoid kick overwrite on next cycle!
			int onsetframe= (unit->m_loudnesscounter+LOUDNESSFRAMESSTORED-2)%LOUDNESSFRAMESSTORED;
			
			unit->m_onsets[onsetframe]=2; //(-1);  //snare
		}
		
		unit->m_lastsnaredetect=unit->m_frame;
		
	}
	
}



//uses Goto onset detection to with threshold to spot a kick
void kickdetection(DrumTrack *unit){
	
	float dsum;
	float kicksig;
	
	//will also use these for indexing 
	int now=unit->m_powerbufcount;
	int last, lastlast;
	last=(2+now)%3; //avoid negative numbers in modulo- get back negatives! 
	lastlast=(1+now)%3;
	
	float * nowbuf,*lastbuf,*lastlastbuf;
	nowbuf=unit->m_powerbuf[now];
	lastbuf=unit->m_powerbuf[last];
	lastlastbuf=unit->m_powerbuf[lastlast];
	
	dsum=0.0;
	
	for(int f=1; f<=3; ++f) {
		
		float updownmax= sc_max(lastlastbuf[f-1],lastlastbuf[f+1]);
		float prevpow= sc_max(lastlastbuf[f],updownmax);
		
		float tempmin=sc_min(lastbuf[f],nowbuf[f]);
		
		if(tempmin>prevpow) {
			
			float temp=sc_max(lastbuf[f],nowbuf[f]);
			dsum=dsum+temp;    
		}
	}			
	
	
	kicksig=dsum/3;
	//running max must be kept- just will do best so far for now...could become best within last x seconds
	//updatemaxforband
	
	
//	if(kicksig>(unit->m_maxkicksig)) {
//		unit->m_maxkicksig=kicksig;
//		
//		printf("max found! %f \n",unit->m_maxkicksig);
//	}
	
	kicksig=kicksig/(unit->m_maxkicksig);
	
	//kick detection- don't allow another for a while? m_lastsnaredetect
	if(kicksig>0.6) { //used to be 0.5, can add a user defined threshold
		
		//stop in a row detections
		if(unit->m_lastkickdetect<(unit->m_frame-6)) {
			
			if(unit->m_debugmode==2)
			printf("kick found! %ld %ld \n", unit->m_lastkickdetect, unit->m_frame);
			
			
			//occured one frame ago, but do -2 to avoid kick overwrite on next cycle!
			int onsetframe= (unit->m_loudnesscounter+LOUDNESSFRAMESSTORED-2)%LOUDNESSFRAMESSTORED;
			
			unit->m_onsets[onsetframe]=1;  //kick- might do 1 kick, -1 snare
		}
		
		unit->m_lastkickdetect=unit->m_frame;
		
	}
	
}



//one matching the tempowinner will score most (can over write even if an equal score)

//impose tempowinner t choose selection spread? o/w could be missing second beat but have third and fourth

//look for archetypal pattern in m_onsets
//phase not important, want to just look at a fixed area in the past
int findarchetype(DrumTrack *unit, float tempowinner) {
	int i,j,k,baseframe;
	float starttime, lasttime;
	int startframe, lastframe, nextonset; 
	//just testing at beat frame positions
	
	baseframe=unit->m_storeloudnesscounter+LOUDNESSFRAMESSTORED;
	int * onsets= unit->m_onsets;
	
	//don't know periods, just going to find from search
	starttime= 0.0-(5*MAXPERIOD); //0.0-(5*0.667); //one more eighth note than usual to calc pre beat
	
	startframe=((int)(starttime*FRAMESR+ .5f) + baseframe)%LOUDNESSFRAMESSTORED; 
	
	//must have at least room for a fast 0.333*4 bar else no point checking
	lasttime= -4*MINPERIOD; //-4*0.33; 
	
	//this is last start frame, last end frame is baseframe
	lastframe= ((int)(lasttime*FRAMESR+ .5f) + baseframe)%LOUDNESSFRAMESSTORED; 
	
	nextonset=(startframe+LOUDNESSFRAMESSTORED-1)%LOUDNESSFRAMESSTORED;
	
	//already made a list of onsets? 
	int numtolast=0;
	
	if (lastframe<startframe) 
		numtolast= lastframe+LOUDNESSFRAMESSTORED-startframe;
	else 
		numtolast= lastframe-startframe;
	
	int numtonow=0;
	
	if (baseframe<startframe) 
		numtonow= baseframe+LOUDNESSFRAMESSTORED-startframe;
	else 
		numtonow= baseframe-startframe;
	
	int pos;
	
	int numonsets=0;
	int numstarts=0;
	
	
	//store them in an array (preallocate 50, maximum size)
	//permanent array for this
	//int onsetposition[50];
	int * onsetposition= unit->m_onsetposition;
	
	for (i=0;i<numtonow;++i) {
		
		pos= (startframe+i+LOUDNESSFRAMESSTORED)%LOUDNESSFRAMESSTORED;
		
		if(onsets[pos]!=0) {
			if(i<numtolast) ++numstarts;
			
			onsetposition[numonsets]=pos;
			
			  ++numonsets;
			
			if (numonsets==MAXONSETS) {printf("disaster? %d \n",numonsets); break;}
		}
	}
	
	//if (numstarts>MAXONSETS) numstarts= MAXONSETS;
	//printf("numonsets %d numstarts %d \n", numonsets,numstarts);
		
	
	int bestindex=-1;
	float bestscore=0.0;
	int besttempo=0;
	
	//float score;
	//int tempo;
	

	
	for (i=0; i<numstarts; ++i) {
		
		int first=onsetposition[i];
		int firstval= onsets[first];
		
		for (j=i+1;j<numonsets; ++j) {
			int second=onsetposition[j];
			
			int diff, realdiff;
			
			diff= (second+LOUDNESSFRAMESSTORED- first)%LOUDNESSFRAMESSTORED; //works for both cases second<first and first<=second
			int test= (baseframe+LOUDNESSFRAMESSTORED- first)%LOUDNESSFRAMESSTORED; 
			
			//second IS second beat
			if(test>(3*diff)) {
			
			testpattern(unit,bestindex,bestscore,besttempo,first,diff,firstval);
	
			}
			
			////second is actually third beat
		
			if(test>((3/2)*diff)) {
			
			realdiff= (diff/2);
			
			testpattern(unit,bestindex,bestscore,besttempo,first,realdiff,firstval);
	
			}
			
			//second is fourth, already all beats will fit within
			realdiff= (diff/3);
			
			testpattern(unit,bestindex,bestscore,besttempo,first,realdiff,firstval);
			
		}
		
	}  
	
	//tempo= FRAMESR/(float)tempo;
	
	float actualtempo;
	
	if (besttempo>0) {
		actualtempo = FRAMESR/(float)besttempo;
		
		int dist= (baseframe-bestindex+LOUDNESSFRAMESSTORED)%LOUDNESSFRAMESSTORED;
		
		float phase= (dist%besttempo)/(float)besttempo;
		
		unit->m_patternphase= 1.0-phase;  
		unit->m_patternscore=bestscore;
		unit->m_patterntempo=actualtempo;
		
		if(unit->m_debugmode==1)
		printf("best match phase %f score %f tempo %f \n", unit->m_patternphase, unit->m_patternscore, unit->m_patterntempo);
		
		return 1;
	}
	
	return 0; //no matches found
}


void testpattern(DrumTrack * unit, int &bestindex, float &bestscore, int &besttempo, int first, int diff, int firstval) {
	
	int k;
	float score;
	
	//int * onsetposition= unit->m_onsetposition;	
	int * onsets= unit->m_onsets;
	
	int second= (first+diff)%LOUDNESSFRAMESSTORED;
	
	//first case- second is the second beat of the pattern
	//min one beat 0.33*FRAMESR max three beats 0.66*FRAMESR  86.1328
	int mindiff= MINDIFF; //28; //allowing for some error could be 25, usually 28
	int maxdiff= MAXDIFF; //58;
	
	//int test= (baseframe+LOUDNESSFRAMESSTORED- first)%LOUDNESSFRAMESSTORED; 
	
	//can exclude more cases knowing diff- 3*diff can't be greater than LOUDNESSFRAMESSTORED
	//if(3*diff<LOUDNESSFRAMESSTORED) {
	
	//spacing must be plausible
	if(diff>=mindiff && diff<=maxdiff) {
		
		int tempo= diff; //will convert later FRAMESR/(float)diff;
		int secondval= onsets[second];
		
		//spacing is diff for testing
		
		int third=(second+diff)%LOUDNESSFRAMESSTORED;
		int fourth=(third+diff)%LOUDNESSFRAMESSTORED;
		
		//two tests for each case, each case determines a unique position of bestindex
		
		int thirdval=0;
		int fourthval=0;
		
		int posindex,negindex;
		
		//allowing for an error of up to 5 either side, successively overwrites with best option if non zero
		for (k=4;k>=0;--k) {
			
			negindex=(third-k+LOUDNESSFRAMESSTORED)%LOUDNESSFRAMESSTORED;
			
			if(onsets[negindex])
				thirdval=onsets[negindex];
			
			posindex=(third+k)%LOUDNESSFRAMESSTORED;
			
			if(onsets[posindex])
				thirdval=onsets[posindex];
			
		}
		
		for (k=4;k>=0;--k) {
			
			negindex=(fourth-k+LOUDNESSFRAMESSTORED)%LOUDNESSFRAMESSTORED;
			
			if(onsets[negindex])
				fourthval=onsets[negindex];
			
			posindex=(fourth+k)%LOUDNESSFRAMESSTORED;
			
			if(onsets[posindex])
				fourthval=onsets[posindex];
			
		}
		
		
		if (firstval==2) //snare
		{
			
			//two possibilities
			
			score = 1.0;
			
			if(secondval==1) score+=0.5;
			if(thirdval==2) score+=1.0;
			if(fourthval==1) score+=1.0;
			
			if(score>bestscore) {
				
				bestindex=fourth; //first beat of bar! 
				bestscore=score;
				besttempo=tempo;
			}
			
			score = 1.0;
			
			if(secondval==1) score+=1.0;
			if(thirdval==2) score+=1.0;
			if(fourthval==1) score+=0.5;
			
			if(score>bestscore) {
				
				bestindex=second; //first beat of bar! 
				bestscore=score;
				besttempo=tempo;
			}
			
			
		} else { //kick
			
			score = 1.0;
			
			if(secondval==2) score+=1.0;
			if(thirdval==1) score+=0.5;
			if(fourthval==2) score+=1.0;
			
			if(score>bestscore) {
				
				bestindex=first; //first beat of bar! 
				bestscore=score;
				besttempo=tempo;
			}
			
			
			score = 0.5;
			
			if(secondval==2) score+=1.0;
			if(thirdval==1) score+=1.0;
			if(fourthval==2) score+=1.0;
			
			if(score>bestscore) {
				
				bestindex=third; //first beat of bar! 
				bestscore=score;
				besttempo=tempo;
			}
			
			
		}
		
		
		
		
		
	}
	
	
	
}











void prepareHanningWindow(){
	float ang;
	
	ang=(1.0/N)*TWOPI;
	
	for(int i=0;i<N;++i)
		hanning[i]=0.5 - 0.5*cos(ang*i);
	
}


PluginLoad(DrumTrack) {
	
	ft= inTable;
	
	prepareHanningWindow();
	
	DefineDtorCantAliasUnit(DrumTrack);
}



/*


//used twice, once for phase, once for antiphase?
float comparechordetect(DrumTrack *unit, float phase, float bps) {
	int i,j,k,baseframe, rounded, testframe;
	float Ti, starttime, beatpos, beat;
	//just testing at beat frame positions
	
	baseframe=unit->m_fftstoreposhold+FFTSTOREMEM;
	
	Ti= 1/bps;   //could have precalculated these- OPTIMISE 
	
	int framesize= (int)floor(0.5*Ti/FRAMEPERIOD); //eighth note area
	
	//%as goto ignore outer 1/5s, only take middle 3/5 
	int keep= (int)floor((0.6*framesize)+ .5f);
	int onefifth= (int)floor((0.2*framesize)+ .5f);
	
	starttime= 0.0-(5.5*Ti); //one more eighth note than usual to calc pre beat
	
	beatpos=starttime+(Ti*phase); 
	
	float * fftstore= unit->m_fftstore;
	
	//should allocate for each 
	float *chordhistogram= unit->m_chordhistogram; 
	float *chordhistogramprev= unit->m_chordhistogramprev;
	float onsum=0.0;
	float offsum=0.0;
	
	//8 beats
	for (i=0;i<9;++i) {
		
		beat=(i*(Ti*0.5))+beatpos;
		
		
		//ERROR if beat negative? Should be -.5f in rounding! No, floor goes moe negative
		rounded= (int)floor(beat*FRAMESR+ .5f);
		
		//printf("error in rounding? beat %f rounded %d correction %d \n",beat, rounded,  (int)(beat*FRAMESR -.5f));
		
		testframe=baseframe+rounded+onefifth;
		
		//calc new histogram
		for (j=0;j<24;++j) {
			
			float sum=0.0;
			
			for(k=0;k<keep;++k) { 
				
				sum+= fftstore[((testframe+k)%FFTSTOREMEM)*24+j];
			}
			
			
			chordhistogram[j]=sum;
		}
		
		//peak pick
		//for (j=1;j<23;++j) {
		//
		//if((chordhistogram[j-1]>chordhistogram[j]) || (chordhistogram[j]<chordhistogram[j+1]))
		//chordhistogram[j]=0;
		//
		//}
		
		//zero 0 and 23
		chordhistogram[0]=0;
		chordhistogram[23]=0;
		
		//do difference to prev, sum of positives only
		if(i>0) {
			
			for (j=0;j<24;++j) {	
				
				float diff; 
				
				diff=chordhistogram[j]-chordhistogramprev[j];
				
				//printf("%f \n",diff);
				
				if(diff<0) diff=0;	
				
				if(i%2==1)
					onsum+=diff; //bassonsets[testframe];                
				else
					offsum+=diff;
			}
			
		}
		
		//copy new into previous
		for (j=1;j<23;++j) 
			chordhistogramprev[j]=chordhistogram[j];
		
		
	}


if (offsum<1) offsum=1;

//printf("onsum %f offsum %f ratio %f\n",onsum, offsum, onsum/offsum);
//

return onsum/offsum;
}


*/