diff --git a/src/avtk/avtk_button.h b/src/avtk/avtk_button.h index 376f4b2..a77030a 100644 --- a/src/avtk/avtk_button.h +++ b/src/avtk/avtk_button.h @@ -49,8 +49,17 @@ class Button : public Fl_Button highlight = false; mouseOver = false; + + greyedOut = false; } + void setGreyOut( bool g ) + { + greyedOut = g; + } + + bool greyedOut; + bool mouseOver; bool highlight; int x, y, w, h; @@ -95,13 +104,19 @@ class Button : public Fl_Button cairo_save( cr ); cairo_rectangle( cr, x+1, y+1, w-2, h-2 ); - cairo_set_source_rgb( cr, _bgr, _bgg, _bgb ); + if ( !greyedOut ) + cairo_set_source_rgb( cr, _bgr, _bgg, _bgb ); + else + { + float grey = (_bgr + _bgg + _bgb) / 3; + cairo_set_source_rgb( cr, grey, grey, grey ); + } cairo_fill_preserve(cr); cairo_set_line_width(cr, 1.3); cairo_rectangle( cr, x+1, y+1, w-2, h-2 ); - if ( highlight ) + if ( highlight && !greyedOut ) { cairo_set_source_rgba(cr, _r, _g, _b, 0.4); cairo_fill_preserve(cr); @@ -110,8 +125,16 @@ class Button : public Fl_Button float alpha = 0.6; if (mouseOver) alpha = 1; - cairo_set_source_rgba(cr, _r, _g, _b, alpha); - if ( highlight ) + + if ( !greyedOut ) + cairo_set_source_rgba(cr, _r, _g, _b, alpha); + else + { + float grey = (_r + _g + _b) / 3; + cairo_set_source_rgb( cr, grey, grey, grey ); + } + + if ( highlight && !greyedOut ) cairo_set_line_width(cr, 2.2); cairo_stroke(cr); diff --git a/src/gaudioeditor.cxx b/src/gaudioeditor.cxx index 325f2b1..6a33a88 100644 --- a/src/gaudioeditor.cxx +++ b/src/gaudioeditor.cxx @@ -22,6 +22,8 @@ #include #include "config.hxx" +#include "gui.hxx" +extern Gui* gui; #include "audiobuffer.hxx" @@ -84,11 +86,86 @@ void AudioEditor::show( AudioBuffer* buf, bool modal ) else { std::vector& tmp = ab->getData(); - waveform->setData( &tmp[0], tmp.size() ); + int size = tmp.size(); + waveform->setData( &tmp[0], size ); + + const int beats[]={1,2,4,8,16,32,64}; + + int iBeatOne = -1; + int iBeatTwo = -1; + + // figure out BPM values from size + for( int i = 0; i < 7; i++ ) + { + int beat = beats[i]; + + int fpb = size / beat; + + int bpm = (gui->samplerate / fpb) * 60; + + + if ( bpm < 60 || bpm > 220 ) + { + // disable option: not valid + beatButtons[i]->setGreyOut( true ); + beatButtons[i]->setColor( 0.4, 0.4, 0.4 ); + } + else + { + printf("%i, fpb = %i, bpm= = %i\n", beat, fpb, bpm ); + // enable option ( may be disabled previously! ) + beatButtons[i]->setGreyOut( false ); + + // remember this beat was a valid one, to check for best match + if ( iBeatOne == -1 ) + iBeatOne = i; + else + iBeatTwo = i; + } + } + + // both valid: compare, and adjust color + if ( iBeatOne != -1 && iBeatTwo != -1 ) + { + int masterFpb = (gui->samplerate * 60) / gui->getMasterTrack()->getBpm(); + int oneFpb = size / beats[iBeatOne]; + int twoFpb = size / beats[iBeatTwo]; + + int oneDelta = masterFpb - oneFpb; + int twoDelta = masterFpb - twoFpb; + + if ( oneDelta < 0 ) oneDelta = -oneDelta; + if ( twoDelta < 0 ) twoDelta = -twoDelta; + + if ( oneDelta == twoDelta ) + { + beatButtons[iBeatOne]->setColor( 0.0, 1.0, 0.0 ); + beatButtons[iBeatTwo]->setColor( 0.0, 1.0, 0.0 ); + } + else if ( oneDelta <= twoDelta ) + { + // one is the better match + beatButtons[iBeatOne]->setColor( 0.0, 1.0, 0.0 ); + beatButtons[iBeatTwo]->setColor( 1.0, 0.0, 0.0 ); + } + else + { + beatButtons[iBeatTwo]->setColor( 0.0, 1.0, 0.0 ); + beatButtons[iBeatOne]->setColor( 1.0, 0.0, 0.0 ); + } + } + else if( iBeatOne != -1 && iBeatTwo == -1) // only one valid + { + beatButtons[iBeatOne]->setColor( 0.0, 1.0, 0.0 ); + } + else + { + // no valid BPM range..? + } + } window->set_modal(); - window->show(); }