Android Open Source - Music-Theory Quiz Util






From Project

Back to project page Music-Theory.

License

The source code is released under:

GNU General Public License

If you think the Android project Music-Theory listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package com.mt.utils;
/*w w w  . j a v a  2s  . c o m*/
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Random;

import android.content.SharedPreferences;

import com.mt.theory.Clef;
import com.mt.theory.KeySignature;
import com.mt.theory.Note;

public class QuizUtil {

  public static Clef getRandomClefFromPreferences(SharedPreferences prefs, String prefKeyPrefix) {
    Random rand = new Random();

    List<Clef> clefs = new ArrayList<Clef>();
    for (Clef clef : Clef.values()) {
      if (prefs.getBoolean(prefKeyPrefix + "Clef" + clef.getKeySuffix(), true)) {
        clefs.add(clef);
      }
    }

    Clef clef = null;
    if (clefs.size() == 1) {
      clef = clefs.get(0);
    } else {
      clef = clefs.get(rand.nextInt(clefs.size()));
    }

    return clef;
  }

  public static KeySignature getRandomKeySignatureFromPreferences(SharedPreferences prefs, String prefKeyPrefix) {
    Random rand = new Random();

    List<KeySignature> keys = new ArrayList<KeySignature>();
    if (prefs.getBoolean(prefKeyPrefix + "Flats", true)) {
      Collections.addAll(keys, KeySignature.getFlatKeys());
    }
    if (prefs.getBoolean(prefKeyPrefix + "Sharps", true)) {
      Collections.addAll(keys, KeySignature.getSharpKeys());
    }

    return keys.get(rand.nextInt(keys.size()));
  }

  public static Note getRandomNoteFromPreferenceRange(Clef clef, SharedPreferences prefs, String prefKeyPrefix) {
    Random rand = new Random();

    int upperLedgerBound = Integer.valueOf(prefs.getString(prefKeyPrefix + "UpperLedgers", "2"));
    int lowerLedgerBound = Integer.valueOf(prefs.getString(prefKeyPrefix + "LowerLedgers", "2"));

    // The first ledger line always begins at 6 steps above or below the
    // middle staff line for a given clef
    int upperLedgerStartValue = NoteUtil.getPositionValue(clef.getMiddleOctave(), clef.getMiddleTone()) + 6;
    int lowerLedgerStartValue = NoteUtil.getPositionValue(clef.getMiddleOctave(), clef.getMiddleTone()) - 6;

    // Numeric representations of the highest and lowest possible notes to
    // random
    int lowerNoteBound = lowerLedgerStartValue - (2 * lowerLedgerBound - 1);
    int upperNoteBound = upperLedgerStartValue + (2 * upperLedgerBound - 1);

    int randomTotalValue = lowerNoteBound + rand.nextInt(upperNoteBound - lowerNoteBound);

    return NoteUtil.getNoteFromPositionValue(randomTotalValue);
  }

}




Java Source Code List

com.mt.HomeActivity.java
com.mt.QuizActivity.java
com.mt.audio.MidiTrack.java
com.mt.keys.KeySignatureQuizActivity.java
com.mt.keys.KeySignatureQuizPreferenceActivity.java
com.mt.notes.NoteQuizActivity.java
com.mt.notes.NoteQuizPreferenceActivity.java
com.mt.staff.ScoreView.java
com.mt.theory.Accidental.java
com.mt.theory.Clef.java
com.mt.theory.Duration.java
com.mt.theory.Interval.java
com.mt.theory.KeySignature.java
com.mt.theory.NoteGroup.java
com.mt.theory.Note.java
com.mt.theory.Quality.java
com.mt.theory.Score.java
com.mt.theory.TimeSignature.java
com.mt.theory.Tone.java
com.mt.utils.NoteUtil.java
com.mt.utils.QuizUtil.java