Permalink
Browse files

Notes play support

  • Loading branch information...
1 parent b13f5dc commit 0db53d87a796c71ec0b348fd0c604e7a67ac22b8 @atd committed Mar 3, 2011
View
Binary file not shown.
View
Binary file not shown.
View
Binary file not shown.
View
Binary file not shown.
View
Binary file not shown.
View
Binary file not shown.
View
Binary file not shown.
View
Binary file not shown.
View
Binary file not shown.
View
Binary file not shown.
View
Binary file not shown.
View
Binary file not shown.
View
Binary file not shown.
View
Binary file not shown.
View
Binary file not shown.
View
Binary file not shown.
View
Binary file not shown.
View
Binary file not shown.
View
Binary file not shown.
View
Binary file not shown.
View
Binary file not shown.
View
Binary file not shown.
View
Binary file not shown.
View
Binary file not shown.
@@ -37,6 +37,7 @@
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
+import android.media.MediaPlayer;
import android.os.Bundle;
import android.os.SystemClock;
import android.view.*;
@@ -252,9 +253,12 @@ public void onClick(View v) {
}
else showTrainingEndDialog();
break;
- default:
+ default:
Button bt = (Button) v;
String bttext = String.valueOf(bt.getTag());
+
+ Player.play(getBaseContext(), bttext, notes[prevnotenum]);
+
if ((notes[prevnotenum].equals(bttext+"_0")) || (notes[prevnotenum].equals(bttext+"_1")) || (notes[prevnotenum].equals(bttext+"_2"))){
correct++;
showNextNote();
@@ -0,0 +1,64 @@
+package net.fercanet.LNM;
+
+import android.content.Context;
+import android.media.MediaPlayer;
+import android.os.Handler;
+
+public class Player {
+ private static MediaPlayer mp = new MediaPlayer();
+ static int noteFiles[] = { 0, R.raw.re_0, R.raw.mi_0, R.raw.fa_0, R.raw.sol_0, R.raw.la_0, R.raw.si_0,
+ R.raw.don_1, R.raw.re_1, R.raw.mi_1, R.raw.fa_1, R.raw.sol_1, R.raw.la_1, R.raw.si_1,
+ R.raw.don_2, R.raw.re_2, R.raw.mi_2, R.raw.fa_2, R.raw.sol_2, R.raw.la_2, R.raw.si_2,
+ R.raw.don_3, R.raw.re_3, R.raw.mi_3 };
+
+ static String[] notesList = { "don", "re", "mi", "fa", "sol", "la", "si" };
+
+ public static void play(Context context, String clickedNote, String correctNote) {
+ mp.release();
+
+ int noteFile;
+ try {
+ noteFile = noteFiles[fileIndex(clickedNote, correctNote)];
+ } catch (Exception e) {
+ // TODO Auto-generated catch block
+ e.printStackTrace();
+ return;
+ }
+
+ mp = MediaPlayer.create(context, noteFile);
+ mp.start();
+ }
+
+ private static int fileIndex(String clickedNote, String correctNoteWithPitch) throws Exception {
+ String[] parsedNote = correctNoteWithPitch.split("_");
+ String correctNote = parsedNote[0];
+ int pitch = Integer.parseInt(parsedNote[1]);
+
+ int clickedNoteIndex = indexOf(clickedNote);
+ int correctNoteIndex = indexOf(correctNote);
+ int diffNote = clickedNoteIndex - correctNoteIndex;
+
+ if (diffNote > 0) {
+ if (diffNote > 3) {
+ pitch--;
+ }
+ } else if (diffNote < 0) {
+ if (diffNote < -3) {
+ pitch++;
+ }
+ }
+
+ return clickedNoteIndex + pitch * 7;
+ }
+
+ // I cannot believe there is nothing like this in Java
+ // This is basic in Ruby!! Damn it!
+ private static int indexOf(String element) throws Exception {
+ for(int i = 0; i < notesList.length; i++) {
+ if (element.equals(notesList[i])) {
+ return i;
+ }
+ }
+ throw new Exception(element + ": Unknown note!");
+ }
+}

0 comments on commit 0db53d8

Please sign in to comment.