39=/////////////////////////////////////////////////////////
40=/////////////////////// READ ME /////////////////////////
41=/////////////////////////////////////////////////////////
42=//  This algorithm is designed to detect beats at constant beatrates from around 110 to
43=//  180 beats per minute. It has only been tested with psy-trance and progressive trance/house
44=//  and seems to work well (for most tracks) Don't complain if it doesn't work with dnb,
45=//  breaks and stuff; i haven't tried it yet. (but it probably won't!)
46=//  In order to use it, you only need to paste the code labelled "beat detection code" into
47=//  your preset's main per frame code. Then you can use the variables listed below...
48=//  (The algorithm doesn't make use of any q variables)
49=//
50=//  My advice: It is natural that the algorithm will always miss some beats no matter what.
51=//  So instead of waiting for the beat variable to become 1 and do your OnBeat() stuff, use the
52=//  phase variable that the algorithm provides (more details below) to synchronize the entire
53=//  preset to the (predicted) beat flow. Even if one beat is missed, chances are you won't even
54=//  notice it. (in the demo you'll see that sometimes the character jumps to the beat without any beats detected. (no screen flashing!)
55=//
56=//  IMPORTANT: The algorithm is very sensitive to framerate changes within the preset once
57=//  it's started working. It shouldn't be a problem, but if some values suddenly start going
58=//  crazy, maybe that's the reason.  (i.e. bringing up the menu in MD)
59=//
60=//  If the algorithm runs long enough with any beats in the music (e.g. long intros), it may 
61=//  take a little longer to adjust when the music starts to kick in. Generally it shouldn't
62=//  have much trouble adjusting.
63=//
64=//  The variables:
65=//
66=//     beat      : 0 OR 1, whether a beat was detected. (in this preset it controls the white flashes)
67=//     phase     : 0 TO 1, normalized phase between 2 succesive beats. Upon a beat, its value
68=//                 becomes 0, then it gradually increases to 1 where the next beat is expected, and so on.
69=//                 If the next beat doesn't show up as expected the value remains 1.
70=//                 In this demo it is used to control the character's jumps. From 0 to .5 the character
71=//                 is going up, from .5 to 1 the character is going down.
72=//     bpm       : The number of beats per minute at that instant. To get the BPM for the entire
73=//                 song use the quantity (60*FPS/lockinterval) but i can't guarantee any accuracy. (at any random moment)
74=//     lastframe : The value of MD's 'frame' variable when the last beat was detected.
75=//     interval  : The number of frames between the last 2 beats.
76=//     avginterval:The average number of frames between 2 successive beats.
77=//          sure : Ranging from 0.5 to 1. 0.5->the algorithm has no clue of the beatrate, 1->almost
78=//                 sure it's got it right.
79=//
80=//  For any integer n of your choice, the expression: 
81=//         above(frame-lastbeat,(n+.1)*interval)*min((frame-lastbeat)/interval-n,1)
82=//    will become positive if #n beats should had been detected already but none was actually found.
83=//  This is used in the demo (q8 variable) to draw the character unhappy when no beats are detected!
84=//
85=//  I hope i'm not forgetting anything important, i think that's it. Of course you can do whatever
86=//  you want with the code, no credits due. So, thanks for reading this, i hope you find this useful
87=//  keep on milking!									
				yin