Unhardcoded ticks per beat. Fixed triplet bugs.
[epichord.git] / src / metronome.cpp
blob16c0a0d0770163d557a78df87f6e950bee2d1ead
1 #include <stdio.h>
2 #include <math.h>
4 #include <fltk/Widget.h>
5 #include <fltk/events.h>
6 #include <fltk/draw.h>
8 #include "seq.h"
9 #include "backend.h"
10 #include "metronome.h"
14 using namespace fltk;
16 Metronome::Metronome(int x, int y, int w, int h, const char* label = 0) : fltk::Widget(x, y, w, h, label) {
17 N = 4;
18 n = 2;
20 last_beat = 0;
21 plus = 0;
23 r=255; g=0; b=0;
25 update(0);
28 int Metronome::handle(int event){
29 return 0;
32 void Metronome::draw(){
33 draw_box();
35 //setcolor(fltk::color(r,g,b));
36 //fillrect(2,2,w()-4,h()-4);
38 setcolor(fltk::WHITE);
39 int X = last_beat%N*(w()-4)/N+2;
40 int W = (w()-4)/N;
41 int H = h()-4;
42 fillrect(X,2,W,h()-4);
44 //int W2 = W/2;
45 //int H2 = H/2;
46 //setcolor(fltk::color(r,g,b));
47 //fillrect(X+W/2-W2/2,h()/2-H2/2,W2,H2);
49 //setcolor(fltk::BLACK);
50 //int C = (getascent()-getdescent())/2;
51 //drawtext(label(),X+W/2-getwidth(label())/2,h()/2+C);
55 //div N is the greatest divisor of N less than or equal to sqrt(N)
56 int div(int N){
57 int d = 0;
58 for(int i=1; i<=sqrt(N); i++){
59 if(N%i==0){
60 d=i;
63 if(d==1){
64 //prime
66 return d;
69 void Metronome::update(int tick){
70 int now_beat = tick/TICKS_PER_BEAT;
72 if(now_beat != last_beat){
73 int step = last_beat - now_beat;
74 last_beat = now_beat;
76 char buf[16];
77 snprintf(buf,16,"%d",now_beat%N + plus);
78 copy_label(buf);
80 if(now_beat%N == 0){
81 r=255; g=0; b=0;
83 else if(now_beat%n == 0){
84 r=128; g=0; b=0;
86 else{
87 r=0; g=0; b=0;
90 redraw();
94 void Metronome::set_N(int zN){
95 N = zN;
96 n = div(zN);