/** * Calculates average tempo of touches, sets it and starts metronome. Calculates tempo from 57 to * 200 bpm. Re-calculate after 1100 ms pause* */ private void tapTempo() { int hp = metronome.headPosition(); bpmf = bpm; playStatePrevious = metronome.getPlayState(); // If tapped while playing, just after the previous click, it doesn't click again, just delays // next click. if (playStatePrevious == 3) { if (hp < 8820) { metronome.setDelay(hp / 44100 * 1000); } } else { flasher(); metronome.play(); startDelayHandler.postDelayed(startDelay, 20000 / bpm); // } if (tapCount == 0) { // On the first tap it only records time timeFirstTap = System.currentTimeMillis(); timeLastTap = timeFirstTap; tapCount++; } else if ((System.currentTimeMillis() - timeLastTap) < 1050) { // Maximum time between taps is set to 1050 ms (57 bpm) if (hp > 8820) { // If tapped while playing, after less than 8820 samples from the previous // click, it doesn't stop, just delays next click. metronome.stop(); } timeLastTap = System.currentTimeMillis(); exactBpmMs = (double) (timeLastTap - timeFirstTap) / tapCount; exactBpm = 60000 / exactBpmMs; tapBpm = (int) Math.round(exactBpm); Log.i("exactBpm", "" + exactBpm); Log.i("tapBpm", "" + tapBpm); if (tapBpm > 200) tapBpm = 200; metronome.setTempo(tapBpm); bpm = tapBpm; bpmTextView.setText(String.valueOf(tapBpm)); tapCount++; if (hp > 7340) { metronome.play(); startDelayHandler.postDelayed(startDelay, (20000 / bpm)); } } else { tapCount = 1; timeFirstTap = System.currentTimeMillis(); timeLastTap = timeFirstTap; } }
@Override public void onLongPress(MotionEvent e) { metronome.stop(); }
@Override protected void onDestroy() { super.onDestroy(); metronome.stop(); // Prevents creating new AudioTrack over existing one }