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
|
/*
LICENSE
-------
Copyright 2005 Nullsoft, Inc.
All rights reserved.
Redistribution and use in source and binary forms, with or without modification,
are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright notice,
this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.
* Neither the name of Nullsoft nor the names of its contributors may be used to
endorse or promote products derived from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR
IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include <windows.h>
#include <commctrl.h>
#include <math.h>
#include <stdio.h>
#include "draw.h"
#include "wnd.h"
#include "r_defs.h"
#include "render.h"
#include "vis.h"
#include "cfgwnd.h"
#include "resource.h"
#include "bpm.h"
#include "../Agave/Language/api_language.h"
int refineBeat(int isBeat);
BOOL TCHistStep(BeatType *t, DWORD _Avg, int *_halfDiscriminated, int *_hdPos, DWORD *_lastTC, DWORD TC, int Type);
void InsertHistStep(BeatType *t, DWORD TC, int Type, int i);
void CalcBPM(void);
BOOL ReadyToLearn(void);
BOOL ReadyToGuess(void);
void doubleBeat(void);
void halfBeat(void);
void ResetAdapt(void);
void SliderStep(int Ctl, int *slide);
void initBpm(void);
extern int g_fakeinit;
int cfg_smartbeat=0;
int cfg_smartbeatsticky=1;
int cfg_smartbeatresetnewsong=1;
int cfg_smartbeatonlysticky=0;
int sticked=0;
int arbVal, skipVal; // Values of arbitrary beat and beat skip
int Bpm, Confidence, Confidence1, Confidence2; // Calculated BPM (realtime), Confidence computation
DWORD lastTC; // Last beat Tick count
DWORD lastTC2; // Last beat tick count 2
BeatType TCHist[8]; // History of last 8 beats
BeatType TCHist2[8]; // History of last 8 beats
int Smoother[8]; // History of last 8 Bpm values, used to smooth changes
int halfDiscriminated[8]; // Discriminated beats table
int halfDiscriminated2[8]; // Discriminated beats table
int hdPos; // Position in discrimination table
int hdPos2; // Position in discrimination table
int smPtr, smSize; // Smoother pointer and size
int TCHistPtr; // Tick count history pointer
int TCHistSize; // Tick count history size
int offIMax; // Max divisor/multiplier used to guess/discriminate beats
int lastBPM; // Last calculated BPM, used by the smoother to detect new entry
int insertionCount; // Remembers how many beats were guessed
DWORD predictionLastTC; // Last tick count of guessed/accepted beat
DWORD Avg; // Average tick count interval between beats
DWORD Avg2; // Average tick count interval between beats
int skipCount; // Beat counter used by beat skipper
int inInc, outInc; // +1/-1, Used by the nifty beatsynced sliders
int inSlide, outSlide; // Positions of sliders
int oldInSlide, oldOutSlide; // Used by timer to detect changes in sliders
int oldsticked; // Used by timer to detect changes in sticked state
char txt[256]; // Contains txt about current BPM and confidence
int halfCount, doubleCount; // Counter used to autodetect if double/half beat needed
int TCUsed; // Remembers how many beats in the history were actually used for computation
int predictionBpm; // Contains BPM actually used to prediction (eliminates Bpm driftings)
int oldDisplayBpm, oldDisplayConfidence; // Detects stuff to redraw
int bestConfidence; // Best confidence we had so far
char lastSongName[256]; // Last song name, used to detect song changes in winamp
HWND winampWnd; // Winamp window
int forceNewBeat; // Force new bpm adoption
int betterConfidenceCount; // Used to decide when to adpot new beat
int topConfidenceCount; // Used to decide when to adpot new beat
int stickyConfidenceCount; // Used to decided when to go sticky
BOOL doResyncBpm=FALSE;
// configuration dialog stuff
BOOL CALLBACK DlgProc_Bpm(HWND hwndDlg, UINT uMsg, WPARAM wParam,LPARAM lParam)
{
switch (uMsg)
{
case WM_INITDIALOG:
inInc = 1;
outInc = 1;
inSlide = 0;
outSlide = 0;
oldDisplayBpm=-1;
oldDisplayConfidence=-1;
oldInSlide=-1;
oldOutSlide=-1;
if (cfg_smartbeat) CheckDlgButton(hwndDlg,IDC_BPMADV,BST_CHECKED); else CheckDlgButton(hwndDlg,IDC_BPMSTD,BST_CHECKED);
if (cfg_smartbeatsticky) CheckDlgButton(hwndDlg,IDC_STICKY,BST_CHECKED);
if (cfg_smartbeatresetnewsong) CheckDlgButton(hwndDlg,IDC_NEWRESET,BST_CHECKED); else CheckDlgButton(hwndDlg,IDC_NEWADAPT,BST_CHECKED);
if (cfg_smartbeatonlysticky) CheckDlgButton(hwndDlg,IDC_ONLYSTICKY,BST_CHECKED);
SendDlgItemMessage(hwndDlg, IDC_IN, TBM_SETTICFREQ, 1, 0);
SendDlgItemMessage(hwndDlg, IDC_IN, TBM_SETRANGE, TRUE, MAKELONG(0, 8));
SendDlgItemMessage(hwndDlg, IDC_OUT, TBM_SETTICFREQ, 1, 0);
SendDlgItemMessage(hwndDlg, IDC_OUT, TBM_SETRANGE, TRUE, MAKELONG(0, 8));
if (predictionBpm)
{
ShowWindow(GetDlgItem(hwndDlg, IDC_STICK), sticked ? SW_HIDE : SW_NORMAL);
ShowWindow(GetDlgItem(hwndDlg, IDC_UNSTICK), sticked ? SW_NORMAL : SW_HIDE);
}
else
{
ShowWindow(GetDlgItem(hwndDlg, IDC_STICK), SW_HIDE);
ShowWindow(GetDlgItem(hwndDlg, IDC_UNSTICK), SW_HIDE);
}
/* ShowWindow(GetDlgItem(hwndDlg, IDC_CURBPM), cfg_smartbeat ? SW_NORMAL : SW_HIDE);
ShowWindow(GetDlgItem(hwndDlg, IDC_CURCONF), cfg_smartbeat ? SW_NORMAL : SW_HIDE);
ShowWindow(GetDlgItem(hwndDlg, IDC_BPM), cfg_smartbeat ? SW_NORMAL : SW_HIDE);
ShowWindow(GetDlgItem(hwndDlg, IDC_CONFIDENCE), cfg_smartbeat ? SW_NORMAL : SW_HIDE);
ShowWindow(GetDlgItem(hwndDlg, IDC_RESET), cfg_smartbeat ? SW_NORMAL : SW_HIDE);*/
SetTimer(hwndDlg, 0, 50, NULL);
return 1;
case WM_TIMER:
{
if (oldInSlide != inSlide) {
SendDlgItemMessage(hwndDlg, IDC_IN, TBM_SETPOS, TRUE, inSlide); oldInSlide=inSlide; }
if (oldOutSlide != outSlide) {
SendDlgItemMessage(hwndDlg, IDC_OUT, TBM_SETPOS, TRUE, outSlide); oldOutSlide=outSlide; }
if (oldDisplayBpm != predictionBpm || oldsticked != sticked) {
char lBuf[16];
wsprintf(txt, predictionBpm ? "%d%s"/*/%d"*/ : WASABI_API_LNGSTRING_BUF(IDS_LEARNING,lBuf,16), predictionBpm, cfg_smartbeatsticky && sticked ? WASABI_API_LNGSTRING(IDS_GOT_IT) : ""/*, Bpm*/);
SetDlgItemText(hwndDlg, IDC_BPM, txt);
oldDisplayBpm=predictionBpm;
oldsticked=sticked;
if (predictionBpm)
{
ShowWindow(GetDlgItem(hwndDlg, IDC_STICK), sticked ? SW_HIDE : SW_NORMAL);
ShowWindow(GetDlgItem(hwndDlg, IDC_UNSTICK), sticked ? SW_NORMAL : SW_HIDE);
}
else
{
ShowWindow(GetDlgItem(hwndDlg, IDC_STICK), SW_HIDE);
ShowWindow(GetDlgItem(hwndDlg, IDC_UNSTICK), SW_HIDE);
}
}
if (oldDisplayConfidence != Confidence) {
wsprintf(txt, "%d%%"/* (%d%%/%d%% - %d)"*/, Confidence/*, Confidence1, Confidence2, TCUsed*/);
SetDlgItemText(hwndDlg, IDC_CONFIDENCE, txt);
oldDisplayConfidence=Confidence;
}
}
return 0;
case WM_COMMAND:
if ((LOWORD(wParam) == IDC_BPMSTD) ||
(LOWORD(wParam) == IDC_BPMADV) ||
(LOWORD(wParam) == IDC_NEWRESET) ||
(LOWORD(wParam) == IDC_NEWADAPT) ||
(LOWORD(wParam) == IDC_ONLYSTICKY) ||
(LOWORD(wParam) == IDC_STICKY))
{
cfg_smartbeat=IsDlgButtonChecked(hwndDlg,IDC_BPMADV)?1:0;
cfg_smartbeatsticky=IsDlgButtonChecked(hwndDlg,IDC_STICKY)?1:0;
cfg_smartbeatresetnewsong=IsDlgButtonChecked(hwndDlg,IDC_NEWRESET)?1:0;
cfg_smartbeatonlysticky=IsDlgButtonChecked(hwndDlg,IDC_ONLYSTICKY)?1:0;
oldsticked=-1;
/* ShowWindow(GetDlgItem(hwndDlg, IDC_CURBPM), cfg_smartbeat ? SW_NORMAL : SW_HIDE);
ShowWindow(GetDlgItem(hwndDlg, IDC_CURCONF), cfg_smartbeat ? SW_NORMAL : SW_HIDE);
ShowWindow(GetDlgItem(hwndDlg, IDC_BPM), cfg_smartbeat ? SW_NORMAL : SW_HIDE);
ShowWindow(GetDlgItem(hwndDlg, IDC_CONFIDENCE), cfg_smartbeat ? SW_NORMAL : SW_HIDE);
ShowWindow(GetDlgItem(hwndDlg, IDC_RESET), cfg_smartbeat ? SW_NORMAL : SW_HIDE);*/
}
if (LOWORD(wParam) == IDC_2X)
doubleBeat();
if (LOWORD(wParam) == IDC_DIV2)
halfBeat();
if (LOWORD(wParam) == IDC_RESET)
ResetAdapt();
if (LOWORD(wParam) == IDC_STICK)
{
sticked=1;
stickyConfidenceCount=0;
}
if (LOWORD(wParam) == IDC_UNSTICK)
{
sticked=0;
stickyConfidenceCount=0;
}
return 0;
case WM_DESTROY:
KillTimer(hwndDlg, 0);
return 0;
}
return 0;
}
void initBpm(void)
{
if (g_fakeinit) return;
TCUsed=0;
*txt=0;
betterConfidenceCount=0;
topConfidenceCount=0;
forceNewBeat=0;
hdPos=0;
hdPos2=0;
inSlide=0;
outSlide=0;
oldDisplayBpm=-1;
oldDisplayConfidence=-1;
oldInSlide=0;
oldOutSlide=0;
Bpm = 0;
Avg = 0;
Avg2 = 0;
smPtr = 0;
smSize = 8;
offIMax = 8;
insertionCount = 0;
predictionLastTC = 0;
Confidence = 0;
Confidence1 = 0;
Confidence2 = 0;
halfCount=0;
doubleCount=0;
TCHistSize = 8;
predictionBpm=0;
lastTC=GetTickCount();
stickyConfidenceCount=0;
memset(TCHist, 0, TCHistSize*sizeof(BeatType));
memset(Smoother, 0, smSize*sizeof(int));
memset(halfDiscriminated, 0, TCHistSize*sizeof(int));
memset(halfDiscriminated2, 0, TCHistSize*sizeof(int));
winampWnd = FindWindow("Winamp v1.x", NULL);
*lastSongName=0;
sticked=0;
oldsticked=-1;
}
BOOL songChanged(DWORD TCNow)
{
static DWORD lastCheck=0;
if (TCNow > lastCheck+1000)
{
char songName[256];
lastCheck=TCNow;
GetWindowText(winampWnd, songName, 255);
if (strcmp(songName, lastSongName))
{
strcpy(lastSongName, songName);
return TRUE;
}
}
return FALSE;
}
void ResetAdapt(void)
{
// Reset adaptive learning
*txt=0;
TCUsed=0;
hdPos=0;
Avg = 0;
Confidence=0;
Confidence1=0;
Confidence2=0;
betterConfidenceCount=0;
topConfidenceCount=0;
Bpm = 0;
smPtr = 0;
smSize = 8;
offIMax = 8;
insertionCount = 0;
predictionLastTC = 0;
halfCount=0;
doubleCount=0;
TCHistSize = 8;
predictionBpm=0;
bestConfidence=0;
lastTC=GetTickCount();
sticked=0;
oldsticked=-1;
stickyConfidenceCount=0;
memset(TCHist, 0, TCHistSize*sizeof(BeatType));
memset(TCHist2, 0, TCHistSize*sizeof(BeatType));
memset(Smoother, 0, smSize*sizeof(int));
memset(halfDiscriminated, 0, TCHistSize*sizeof(int));
}
// Insert a beat in history table. May be either real beat or guessed
void InsertHistStep(BeatType *t, DWORD TC, int Type, int i)
{
if (i >= TCHistSize) return;
if (t == TCHist && insertionCount < TCHistSize*2) insertionCount++;
memmove(t+i+1, t+i, sizeof(BeatType)*(TCHistSize-(i+1)));
t[0].TC = TC;
t[0].Type = Type;
}
// Doubles current beat
void doubleBeat(void)
{
int i;
int iv[8];
if (sticked && Bpm > MIN_BPM) return;
for (i=0;i<TCHistSize-1;i++)
iv[i] = TCHist[i].TC - TCHist[i+1].TC;
for (i=1;i<TCHistSize;i++)
TCHist[i].TC = TCHist[i-1].TC-iv[i-1]/2;
Avg /= 2;
Bpm *= 2;
doubleCount=0;
memset(Smoother, 0, smSize*sizeof(int));
memset(halfDiscriminated, 0, TCHistSize*sizeof(int));
//forceNewBeat=1;
}
// Halfs current beat
void halfBeat(void)
{
int i;
int iv[8];
if (sticked && Bpm < MIN_BPM) return;
for (i=0;i<TCHistSize-1;i++)
iv[i] = TCHist[i].TC - TCHist[i+1].TC;
for (i=1;i<TCHistSize;i++)
TCHist[i].TC = TCHist[i-1].TC-iv[i-1]*2;
Avg *= 2;
Bpm /= 2;
halfCount=0;
memset(Smoother, 0, smSize*sizeof(int));
memset(halfDiscriminated, 0, TCHistSize*sizeof(int));
}
// Called whenever isBeat was true in render
BOOL TCHistStep(BeatType *t, DWORD _Avg, int *_halfDiscriminated, int *_hdPos, DWORD *_lastTC, DWORD TC, int Type)
{
int i=0;
int offI;
DWORD thisLen;
BOOL learning = ReadyToLearn();
thisLen = TC-lastTC;
// If this beat is sooner than half the average - 20%, throw it away
if (thisLen < Avg/2 - Avg*0.2)
{
if (learning)
{
if (labs(Avg - (TC - t[1].TC)) < labs(Avg - (t[0].TC - t[1].TC)))
{
/* if (predictionLastTC && t[0].Type == BEAT_GUESSED && Type == BEAT_REAL)
predictionLastTC += (TC - t[0].TC)/2;*/
t[0].TC = TC;
t[0].Type = Type;
return TRUE;
}
}
return FALSE;
}
if (learning)
for (offI=2;offI<offIMax;offI++) // Try to see if this beat is in the middle of our current Bpm, or maybe 1/3, 1/4 etc... to offIMax
if ((float)labs((Avg/offI)-thisLen) < (float)(Avg/offI)*0.2)
{
_halfDiscriminated[(*_hdPos)++]=1; // Should test if offI==2 before doing that, but seems to have better results ? I'll have to investigate this
(*_hdPos)%=8;
return FALSE;
}
// This beat is accepted, so set this discrimination entry to false
_halfDiscriminated[hdPos++]=0;
(*_hdPos)%=8;
// Check if we missed some beats
/*if (learning && thisLen > 1000 || (float)abs(Avg-thisLen) > (float)Avg*0.3)
for (offI=2;offI<offIMax;offI++)
{
if ((float)abs((Avg*offI)-thisLen) < (float)Avg*0.2)
{
for (j=1;j<offI;j++) // Oh yeah we definitly did, add one!
InsertHistStep(TC - (Avg*j), BEAT_GUESSED, offI-1); // beat has been guessed so report it so confidence can be calculated
break;
}
}*/
// Remember this tick count
*_lastTC = TC;
// Insert this beat.
InsertHistStep(t, TC, Type, 0);
return TRUE;
}
// Am i ready to learn ?
BOOL ReadyToLearn(void)
{
int i;
for (i=0;i<TCHistSize;i++)
if (TCHist[i].TC==0) return FALSE;
return TRUE;
}
// Am i ready to guess ?
BOOL ReadyToGuess(void)
{
return insertionCount == TCHistSize*2;
}
void newBpm(int thisBpm)
{
Smoother[smPtr++] = thisBpm;
smPtr %= smSize;
}
int GetBpm(void)
{
int i;
int smN=0;
int smSum=0;
// Calculate smoothed Bpm
for (i=0;i<smSize;i++)
if (Smoother[i] > 0) {
smSum += Smoother[i];
smN++;
}
if (smN) return smSum / smN;
return 0;
}
// Calculate BPM according to beat history
void CalcBPM(void)
{
int i;
int hdCount=0;
int r=0;
int totalTC=0, totalN=0;
float rC, etC;
int v;
double sc=0;
int mx=0;
float et;
int smSum=0, smN=0;
if (!ReadyToLearn())
return;
// First calculate average beat
for (i=0;i<TCHistSize-1;i++)
totalTC += TCHist[i].TC - TCHist[i+1].TC;
Avg = totalTC/(TCHistSize-1);
// Count how many of then are real as opposed to guessed
for (i=0;i<TCHistSize;i++)
if (TCHist[i].Type == BEAT_REAL)
r++;
// Calculate part 1 of confidence
rC = (float)min((float)((float)r / (float)TCHistSize) * 2, 1);
// Calculate typical drift
for (i=0;i<TCHistSize-1;i++)
{
v = TCHist[i].TC - TCHist[i+1].TC;
mx = max(mx, v);
sc += v*v;
}
et = (float)sqrt(sc / (TCHistSize-1) - Avg*Avg);
// Calculate confidence based on typical drift and max derivation
etC = 1 - ((float)et / (float)mx);
// Calculate confidence
Confidence = max(0, (int)(((rC * etC) * 100.0) - 50) * 2);
Confidence1 = (int)(rC * 100);
Confidence2 = (int)(etC * 100);
// Now apply second layer, recalculate average using only beats within range of typical drift
// Also, count how many of them we are keeping
totalTC=0;
for (i=0;i<TCHistSize-1;i++)
{
v += TCHist[i].TC - TCHist[i+1].TC;
if (labs(Avg-v) < et)
{
totalTC += v;
totalN++;
v = 0;
}
else
if ((float)v > Avg)
v = 0;
}
TCUsed = totalN;
// If no beat was within typical drift (how would it be possible? well lets cover our ass) then keep the simple
// average calculated earlier, else recalculate average of beats within range
if (totalN)
Avg = totalTC/totalN;
if (ReadyToGuess())
{
if (Avg) // Avg = 0 ? Ahem..
Bpm = 60000 / Avg;
if (Bpm != lastBPM)
{
newBpm(Bpm); // If realtime Bpm has changed since last time, then insert it in the smoothing tab;e
lastBPM = Bpm;
if (cfg_smartbeatsticky && predictionBpm && Confidence >= ((predictionBpm < 90) ? STICKY_THRESHOLD_LOW : STICKY_THRESHOLD))
{
stickyConfidenceCount++;
if (stickyConfidenceCount >= MIN_STICKY)
sticked=1;
}
else
stickyConfidenceCount=0;
}
Bpm = GetBpm();
// Count how many beats we discriminated
for (i=0;i<TCHistSize;i++)
if (halfDiscriminated[i]) hdCount++;
if (hdCount >= TCHistSize/2) // If we removed at least half of our beats, then we are off course. We should double our bpm
{
if (Bpm * 2 < MAX_BPM) // Lets do so only if the doubled bpm is < MAX_BPM
{
doubleBeat();
memset(halfDiscriminated, 0, TCHistSize*sizeof(int)); // Reset discrimination table
}
}
if (Bpm > 500 || Bpm < 0)
{
ResetAdapt();
}
if (Bpm < MIN_BPM)
{
if (++doubleCount > 4) // We're going too slow, lets double our bpm
doubleBeat();
}
else
doubleCount=0;
if (Bpm > MAX_BPM) // We're going too fast, lets slow our bpm by a factor of 2
{
if (++halfCount > 4)
halfBeat();
}
else
halfCount=0;
}
}
void SliderStep(int Ctl, int *slide)
{
*slide += Ctl == IDC_IN ? inInc : outInc;
if (!*slide || *slide == 8) (Ctl == IDC_IN ? inInc : outInc) *= -1;
}
// render function
// render should return 0 if it only used framebuffer, or 1 if the new output data is in fbout. this is
// used when you want to do something that you'd otherwise need to make a copy of the framebuffer.
// w and h are the width and height of the screen, in pixels.
// isBeat is 1 if a beat has been detected.
// visdata is in the format of [spectrum:0,wave:1][channel][band].
int refineBeat(int isBeat)
{
BOOL accepted=FALSE;
BOOL predicted=FALSE;
BOOL resyncin=FALSE;
BOOL resyncout=FALSE;
if (isBeat) // Show the beat received from AVS
SliderStep(IDC_IN, &inSlide);
DWORD TCNow = GetTickCount();
if (songChanged(TCNow))
{
bestConfidence=(int)((float)bestConfidence*0.5);
sticked=0;
stickyConfidenceCount=0;
if (cfg_smartbeatresetnewsong)
ResetAdapt();
}
// Try to predict if this frame should be a beat
if (Bpm && TCNow > predictionLastTC + (60000 / Bpm))
predicted = TRUE;
if (isBeat) // If it is a real beat, do discrimination/guessing and computations, then see if it is accepted
accepted = TCHistStep(TCHist, Avg, halfDiscriminated, &hdPos, &lastTC, TCNow, BEAT_REAL);
// Calculate current Bpm
CalcBPM();
// If prediction Bpm has not yet been set
// or if prediction bpm is too high or too low
// or if 3/4 of our history buffer contains beats within the range of typical drift
// the accept the calculated Bpm as the new prediction Bpm
// This allows keeping the beat going on when the music fades out, and readapt to the new beat as soon as
// the music fades in again
if ((accepted || predicted) && !sticked && (!predictionBpm || predictionBpm > MAX_BPM || predictionBpm < MIN_BPM))
{
if (Confidence >= bestConfidence)
{
/* betterConfidenceCount++;
if (!predictionBpm || betterConfidenceCount == BETTER_CONF_ADOPT)
{*/
forceNewBeat=1;
/* betterConfidenceCount=0;
}*/
}
if (Confidence >= 50)
{
topConfidenceCount++;
if (topConfidenceCount == TOP_CONF_ADOPT)
{
forceNewBeat=1;
topConfidenceCount=0;
}
}
if (forceNewBeat)
{
forceNewBeat=0;
bestConfidence = Confidence;
predictionBpm=Bpm;
}
}
if (!sticked) predictionBpm = Bpm;
Bpm=predictionBpm;
/* resync = (predictionBpm &&
(predictionLastTC < TCNow - (30000/predictionBpm) - (60000/predictionBpm)*0.2) ||
(predictionLastTC < TCNow - (30000/predictionBpm) - (60000/predictionBpm)*0.2));*/
if (predictionBpm && accepted && !predicted)
{
int b=0;
if (TCNow > predictionLastTC + (60000 / predictionBpm)*0.7)
{
resyncin = TRUE;
b = (int)((float)predictionBpm * 1.01);
}
if (TCNow < predictionLastTC + (60000 / predictionBpm)*0.3)
{
resyncout = TRUE;
b = (int)((float)predictionBpm * 0.98);
}
if (!sticked && doResyncBpm && (resyncin || resyncout))
{
newBpm(b);
predictionBpm = GetBpm();
}
}
if (resyncin)
{
predictionLastTC = TCNow;
SliderStep(IDC_OUT, &outSlide);
doResyncBpm=TRUE;
return ((cfg_smartbeat && !cfg_smartbeatonlysticky) || (cfg_smartbeat && cfg_smartbeatonlysticky && sticked)) ? 1 : isBeat;
}
if (predicted)
{
predictionLastTC = TCNow;
if (Confidence > 25) TCHistStep(TCHist, Avg, halfDiscriminated, &hdPos, &lastTC, TCNow, BEAT_GUESSED);
SliderStep(IDC_OUT, &outSlide);
doResyncBpm=FALSE;
return ((cfg_smartbeat && !cfg_smartbeatonlysticky) || (cfg_smartbeat && cfg_smartbeatonlysticky && sticked)) ? 1 : isBeat;
}
if (resyncout)
{
predictionLastTC = TCNow;
doResyncBpm=TRUE;
return ((cfg_smartbeat && !cfg_smartbeatonlysticky) || (cfg_smartbeat && cfg_smartbeatonlysticky && sticked)) ? 0 : isBeat;
}
return ((cfg_smartbeat && !cfg_smartbeatonlysticky) || (cfg_smartbeat && cfg_smartbeatonlysticky && sticked)) ? (predictionBpm ? 0 : isBeat) : isBeat;
}
|