public void loadSong(int s) { TextView songName = (TextView) findViewById(R.id.textViewSong); if (s == 1) { songName.setText(songName1); bpm = bpm1; metronome.setTempo(bpm); } if (s == 2) { songName.setText(songName2); bpm = bpm2; metronome.setTempo(bpm); } if (s == 3) { songName.setText(songName3); bpm = bpm3; metronome.setTempo(bpm); } if (s == 4) { songName.setText(songName4); bpm = bpm4; metronome.setTempo(bpm); } if (s == 5) { songName.setText(songName5); bpm = bpm5; metronome.setTempo(bpm); } bpmTextView.setText(String.valueOf(bpm)); }
@Override public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) { Log.i("msg", "onScroll X=" + distanceX + " Y=" + distanceY); if (Math.abs(distanceY) > Math.abs(distanceX) * 1.5) { if (playStatePrevious != 3) // prevents starting metronome when changing tempo stop = true; flasherState = 0; bpmf = bpmf + distanceY / 10; if (bpmf > 49 || bpmf < 201) { bpm = ((int) bpmf); bpmTextView.setText(String.valueOf(bpm)); metronome.setTempo(bpm); } } return false; }
/** * 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 protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); getWindow() .addFlags( WindowManager.LayoutParams .FLAG_KEEP_SCREEN_ON); // Flasher should always be visible for a musician mSettings = getSharedPreferences(APP_PREFERENCES, Context.MODE_PRIVATE); detector = new GestureDetector(this, this); metronome = new Metronome(); InputStream in = getResources().openRawResource(R.raw.click_fine); mHandler = handler(); metronome.createPlayer(bpm, in, mHandler); // creating an AudioTrack with the click sound bpmTextView = (TextView) findViewById(R.id.textViewBPM); beatTextView = (TextView) findViewById(R.id.textViewBeat); flashSmall = (ImageView) findViewById(R.id.flashSmall); flashFadeout = AnimationUtils.loadAnimation(this, R.anim.flash_fadeout); loadSong(songNumber); bpmTextView.setText(String.valueOf(bpm)); AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this); builder .setTitle("Thank you for testing Live Metronome!") .setMessage( "This is an alpha version, tested only on API10 (Android 2.3.6). " + "It may fail to work on other API.") .setCancelable(false) .setNegativeButton( "OK", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int id) { dialog.cancel(); } }); AlertDialog alert = builder.create(); alert.show(); }
@Override public void run() { metronome.compensateDelay(); }
@Override public void onLongPress(MotionEvent e) { metronome.stop(); }
@Override protected void onDestroy() { super.onDestroy(); metronome.stop(); // Prevents creating new AudioTrack over existing one }