GUI.cpp
///////////////////////////////////////////////
//
// metronome main loop (TODO)
//
///////////////////////////////////////////////
metronome main loop (TODO)
metronome main loop ( TODO )
void GUI::runMetronome()
{
	powerButton.update();
	if(powerButton.tailActionRequest())
	{
		cout << "Power off - exiting app...\n";
		exitApp = true;
	}
	menuButton.update();
	if(menuButton.tailActionRequest())
	{
		cout << "Mode change request, going to Next Mode...\n";
		metronomePlaying = false;
		metronome.stop();
		currentMode = SETUP_MODE;
	}
	metronomeNBeatsButton.update();
	if(metronomeNBeatsButton.frontActionRequest())
	{
		metronomeNBeats++;
		if(metronomeNBeats > 7) metronomeNBeats = 0;
		metronome.setNBeats(metronomeNBeats);
		ss.str("");
		ss << metronomeNBeats;
		string strNBeats = ss.str();
		strNBeats += "/4";
		nBeatsText.setString(strNBeats);
		cout << "nBeats changed to " << metronomeNBeats << endl;
	}	
	metronomeTapButton.update();
	if(metronomeTapButton.frontActionRequest())
	{
		tap.tap();
		if(!tap.isBPMReady())
		{
			cout << "NOT READY YET - listening... -\n";
			BPMText.setString("...");
		}
		else
		{
			int newBPM = (int)(round(tap.getBPM()));
			cout << "current BPM = " << tap.getBPM() << " (History size = " << tap.getHistorySize() << ")\n";
			metronome.setBPM((double)newBPM);
			currentBPM = newBPM;
			refreshBPMText();
			if(metronomePlaying)
				metronome.start();
			tapFlashTimer.restart();
		}		
	}
	if(tapFlashTimer.getElapsedTime().asSeconds() < 0.05)
		tapFlashOn = true;
	else
		tapFlashOn = false;	
	metronomePlayButton.update();
	if(metronomePlayButton.frontActionRequest())
	{
		if(!metronomePlaying)
		{
			metronomePlaying = true;
			metronomePlayButton.showAlternateImage();
			cout << "metronome - play!\n";
			metronome.start();
			refreshBPMText();
		}
		else
		{
			metronomePlaying = false;
			metronomePlayButton.showDefaultImage();
			cout << "metronome - stop.\n";
			metronome.stop();
			refreshBPMText();
		}
	}
	metronomeTempoUpButton.update();
	if(metronomeTempoUpButton.frontActionRequest())
	{
		currentBPM++;
		if(currentBPM>600) currentBPM=600;
		metronome.setBPM((double)currentBPM);
		cout << "tempo++, new BPM = " << currentBPM << endl;
		refreshBPMText();
		tempoUpBtnFireTimer.restart();
		tempoUpFiring = true;
		tempoUpNextUpdateTime = 0.6;
	}
	double upBtnTimeNow = (double)(tempoUpBtnFireTimer.getElapsedTime().asSeconds());	
	if(upBtnTimeNow>0.6 && tempoUpFiring)
	{
		if(upBtnTimeNow > tempoUpNextUpdateTime)
		{
			currentBPM++;
			if(currentBPM>600) currentBPM=600;
			metronome.setBPM((double)currentBPM);
			cout << "tempo++, new BPM = " << currentBPM << endl;
			refreshBPMText();			
			tempoUpNextUpdateTime = upBtnTimeNow + 0.06;
		}
	}
	if(tempoUpFiring && !metronomeTempoUpButton.pressedDown())
		tempoUpFiring = false;
	metronomeTempoDownButton.update();
	if(metronomeTempoDownButton.frontActionRequest())
	{
		currentBPM--;
		if(currentBPM<30) currentBPM=30;
		metronome.setBPM((double)currentBPM);
		cout << "tempo--, new BPM = " << currentBPM << endl;
		refreshBPMText();
		tempoDownBtnFireTimer.restart();
		tempoDownFiring = true;
		tempoDownNextUpdateTime = 0.6;
	}
	double downBtnTimeNow = (double)(tempoDownBtnFireTimer.getElapsedTime().asSeconds());	
	if(downBtnTimeNow>0.6 && tempoDownFiring)
	{
		if(downBtnTimeNow > tempoDownNextUpdateTime)
		{
			currentBPM--;
			if(currentBPM<30) currentBPM=30;
			metronome.setBPM((double)currentBPM);
			cout << "tempo--, new BPM = " << currentBPM << endl;
			refreshBPMText();			
			tempoDownNextUpdateTime = downBtnTimeNow + 0.06;
		}
	}
	if(tempoDownFiring && !metronomeTempoDownButton.pressedDown())
		tempoDownFiring = false;
	metronomeVolSlider.update();
	if(metronomeVolSlider.stateChanged())
	{
		metronomeVolume = metronomeVolSlider.getValuePercent();
		float volAmountToPass = metronomeVolume / 100.0f;
		metronome.setVolume(volAmountToPass);
		cout << "Volume slider: " << metronomeVolume << endl;
	}
	if(metronome.blinkerFlashRequested())
	{
		if(metronome.getBlinkerFlashColor()==0) 
			blinker.turnGreen();
		else if(metronome.getBlinkerFlashColor()==1)
			blinker.turnRed();
		if(metronome.getNBeats()==0) 
			blinker.turnRed();
		blinkerTimer.restart();
	}
	if(blinkerTimer.getElapsedTime().asSeconds() > 0.07)
		blinker.turnOff();
	window.clear();
	window.draw(tunerBase); 
	menuButton.draw();
	powerButton.draw();
	window.draw(metronomeScreen);
	if(tapFlashOn)
		window.draw(metronomeScreenRed);
	window.draw(BPMText);
	window.draw(nBeatsText);
	metronomeNBeatsButton.draw();
	metronomeTapButton.draw();
	metronomePlayButton.draw();
	metronomeTempoDownButton.draw();
	metronomeTempoUpButton.draw();
	blinker.draw();
	metronomeVolSlider.draw();
	window.display();
}
void GUI :: runMetronome ( ) { powerButton . update ( ) ; if ( powerButton . tailActionRequest ( ) ) { cout << " \n " ; exitApp = true ; } menuButton . update ( ) ; if ( menuButton . tailActionRequest ( ) ) { cout << " \n " ; metronomePlaying = false ; metronome . stop ( ) ; currentMode = SETUP_MODE ; } metronomeNBeatsButton . update ( ) ; if ( metronomeNBeatsButton . frontActionRequest ( ) ) { metronomeNBeats ++ ; if ( metronomeNBeats > 7 ) metronomeNBeats = 0 ; metronome . setNBeats ( metronomeNBeats ) ; ss . str ( " " ) ; ss << metronomeNBeats ; string strNBeats = ss . str ( ) ; strNBeats += " " ; nBeatsText . setString ( strNBeats ) ; cout << " " << metronomeNBeats << endl ; } metronomeTapButton . update ( ) ; if ( metronomeTapButton . frontActionRequest ( ) ) { tap . tap ( ) ; if ( ! tap . isBPMReady ( ) ) { cout << " \n " ; BPMText . setString ( " " ) ; } else { int newBPM = ( int ) ( round ( tap . getBPM ( ) ) ) ; cout << " " << tap . getBPM ( ) << " " << tap . getHistorySize ( ) << " \n " ; metronome . setBPM ( ( double ) newBPM ) ; currentBPM = newBPM ; refreshBPMText ( ) ; if ( metronomePlaying ) metronome . start ( ) ; tapFlashTimer . restart ( ) ; } } if ( tapFlashTimer . getElapsedTime ( ) . asSeconds ( ) < 0.05 ) tapFlashOn = true ; else tapFlashOn = false ; metronomePlayButton . update ( ) ; if ( metronomePlayButton . frontActionRequest ( ) ) { if ( ! metronomePlaying ) { metronomePlaying = true ; metronomePlayButton . showAlternateImage ( ) ; cout << " \n " ; metronome . start ( ) ; refreshBPMText ( ) ; } else { metronomePlaying = false ; metronomePlayButton . showDefaultImage ( ) ; cout << " \n " ; metronome . stop ( ) ; refreshBPMText ( ) ; } } metronomeTempoUpButton . update ( ) ; if ( metronomeTempoUpButton . frontActionRequest ( ) ) { currentBPM ++ ; if ( currentBPM > 600 ) currentBPM = 600 ; metronome . setBPM ( ( double ) currentBPM ) ; cout << " " << currentBPM << endl ; refreshBPMText ( ) ; tempoUpBtnFireTimer . restart ( ) ; tempoUpFiring = true ; tempoUpNextUpdateTime = 0.6 ; } double upBtnTimeNow = ( double ) ( tempoUpBtnFireTimer . getElapsedTime ( ) . asSeconds ( ) ) ; if ( upBtnTimeNow > 0.6 && tempoUpFiring ) { if ( upBtnTimeNow > tempoUpNextUpdateTime ) { currentBPM ++ ; if ( currentBPM > 600 ) currentBPM = 600 ; metronome . setBPM ( ( double ) currentBPM ) ; cout << " " << currentBPM << endl ; refreshBPMText ( ) ; tempoUpNextUpdateTime = upBtnTimeNow + 0.06 ; } } if ( tempoUpFiring && ! metronomeTempoUpButton . pressedDown ( ) ) tempoUpFiring = false ; metronomeTempoDownButton . update ( ) ; if ( metronomeTempoDownButton . frontActionRequest ( ) ) { currentBPM -- ; if ( currentBPM < 30 ) currentBPM = 30 ; metronome . setBPM ( ( double ) currentBPM ) ; cout << " " << currentBPM << endl ; refreshBPMText ( ) ; tempoDownBtnFireTimer . restart ( ) ; tempoDownFiring = true ; tempoDownNextUpdateTime = 0.6 ; } double downBtnTimeNow = ( double ) ( tempoDownBtnFireTimer . getElapsedTime ( ) . asSeconds ( ) ) ; if ( downBtnTimeNow > 0.6 && tempoDownFiring ) { if ( downBtnTimeNow > tempoDownNextUpdateTime ) { currentBPM -- ; if ( currentBPM < 30 ) currentBPM = 30 ; metronome . setBPM ( ( double ) currentBPM ) ; cout << " " << currentBPM << endl ; refreshBPMText ( ) ; tempoDownNextUpdateTime = downBtnTimeNow + 0.06 ; } } if ( tempoDownFiring && ! metronomeTempoDownButton . pressedDown ( ) ) tempoDownFiring = false ; metronomeVolSlider . update ( ) ; if ( metronomeVolSlider . stateChanged ( ) ) { metronomeVolume = metronomeVolSlider . getValuePercent ( ) ; float volAmountToPass = metronomeVolume / 100.0f ; metronome . setVolume ( volAmountToPass ) ; cout << " " << metronomeVolume << endl ; } if ( metronome . blinkerFlashRequested ( ) ) { if ( metronome . getBlinkerFlashColor ( ) == 0 ) blinker . turnGreen ( ) ; else if ( metronome . getBlinkerFlashColor ( ) == 1 ) blinker . turnRed ( ) ; if ( metronome . getNBeats ( ) == 0 ) blinker . turnRed ( ) ; blinkerTimer . restart ( ) ; } if ( blinkerTimer . getElapsedTime ( ) . asSeconds ( ) > 0.07 ) blinker . turnOff ( ) ; window . clear ( ) ; window . draw ( tunerBase ) ; menuButton . draw ( ) ; powerButton . draw ( ) ; window . draw ( metronomeScreen ) ; if ( tapFlashOn ) window . draw ( metronomeScreenRed ) ; window . draw ( BPMText ) ; window . draw ( nBeatsText ) ; metronomeNBeatsButton . draw ( ) ; metronomeTapButton . draw ( ) ; metronomePlayButton . draw ( ) ; metronomeTempoDownButton . draw ( ) ; metronomeTempoUpButton . draw ( ) ; blinker . draw ( ) ; metronomeVolSlider . draw ( ) ; window . display ( ) ; }
metronome main loop (TODO)
metronome main loop ( TODO )
// process for power button // process for menu button // now change mode to next one // audio.refresh(); // audio.setRecordingMode(); // pitch.refresh(); // refresh audio buffer for gentle restarting // backgroundAlpha = 0; // process for nbeats button - n/4 time // send this new nbeats info to metronome // set display for n/4 time signature // process for tap button // Tap class - "tap" once now to calculate BPM // get the calculated bpm from Tap class // send this new bpm info to metronome // if metronome's already playing, restart at top of beat one // make screen flash right on each tap // check if screen should flash or not for TAPPing // process for menu button // DEBUG // metronome.report();			 // process for tempo up button // handle tempo up button "firing" // process for tempo down button // handle tempo down button "firing" // process volume slider // adjust to 0 to 0.6f scale // get the blinker flash request from Metronome // first beat is always green, EXCEPT in 0/4 time // green // red // force red for 0/4 time // draw screen... // 1 - draw base GUI