Android Open Source - OpenSynth Key Stack






From Project

Back to project page OpenSynth.

License

The source code is released under:

Apache License

If you think the Android project OpenSynth 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

/*
 * Copyright 2013 Daisuke Fuji <daisuke@indigo-lab.com>
 * /*  w  w  w .java 2 s.co m*/
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.thebends.synth;


/**
 * The key stack keeps track of notes pressed and released, determining when
 * the attack phase should be entered or when the note should just change
 * without starting a new note attack.
 */
public class KeyStack {
    private static final int MIDDLE_A_KEY = 49;
    private static final float NOTES_PER_OCTAVE = 12.0f;
    private static final float MIDDLE_A_FREQUENCY = 440.0f;

    public static final int MAX_SIZE = 64;
    private int mSize;
    private int[] mNotes = new int[MAX_SIZE];
    // Number of times the note at the position was pressed
    private int[] mCount = new int[MAX_SIZE];

    public KeyStack() {
        mSize = 0;
    }

    /**
     * Returns true if this was the first note pushed on to the key stack
     */
    public boolean noteOn(int note) {
        assert mSize < MAX_SIZE;
        for (int i = 0; i < mSize; ++i) {
            if (mNotes[i] == note) {
                mCount[i]++;
                return false;
            }
        }
        mNotes[mSize] = note;
        mCount[mSize] = 1;
        mSize++;
        return true;
    }

    /**
     * Returns true if this was the last note removed from the key stack
     */
    public boolean noteOff(int note) {
        for (int i = 0; i < mSize; ++i) {
            if (mNotes[i] == note) {
                mCount[i]--;
                if (mCount[i] == 0) {
                    // Remove this element from the stack -- copy all elements above
                    for (int j = i; j < mSize - 1; ++j) {
                        mNotes[j] = mNotes[j + 1];
                        mCount[j] = mCount[j + 1];
                    }
                    mSize--;
                }
                return true;
            }
        }
        // The note wasn't on the stack.  The multi-touch events on the iphone seem
        // to be flaky, so we don't worry if we were asked to remove something that
        // was not on the stack.  The controller also calls our clear() method when
        // no touch events are left as a fallback. 
        return false;
    }

    public boolean isNoteInStack(int note) {
        for (int i = 0; i < mSize; ++i) {
            if (mNotes[i] == note) {
                return true;
            }
        }
        return false;
    }

    public int size() {
        int count = 0;
        for (int i = 0; i < mSize; ++i) {
            count += mCount[i];
        }
        return count;
    }

    public void clear() {
        mSize = 0;
    }

    /**
     * Returns the current not, or 0 if no note is playing.
     */
    public int getCurrentNote() {
        if (mSize > 0) {
            return mNotes[mSize - 1];
        }
        return 0;
    }

    /**
     * Return the note at the specified position in the stack.  num must be less
     * than size. 
     */
    public int getNote(int num) {
        if (num >= mSize) {
            return 0;
        }
        return mNotes[num];
    }

    public static double toFrequency(int key) {
        return MIDDLE_A_FREQUENCY * Math.pow(2, (key - MIDDLE_A_KEY) / NOTES_PER_OCTAVE);
    }
}




Java Source Code List

com.google.synthesizer.android.widgets.piano.BlackPianoKey.java
com.google.synthesizer.android.widgets.piano.NotePianoKey.java
com.google.synthesizer.android.widgets.piano.OctavePianoKey.java
com.google.synthesizer.android.widgets.piano.PianoKey.java
com.google.synthesizer.android.widgets.piano.PianoViewListener.java
com.google.synthesizer.android.widgets.piano.PianoView.java
com.google.synthesizer.android.widgets.piano.WhitePianoKey.java
com.google.synthesizer.core.midi.MidiListener.java
com.google.synthesizer.core.music.Note.java
com.indigo_lab.android.opensynth.OpenSynthActivity.java
com.indigo_lab.android.opensynth.ViewPagerAdapter.java
com.indigo_lab.android.opensynth.view.ArpeggioView.java
com.indigo_lab.android.opensynth.view.ControllerView.java
com.indigo_lab.android.opensynth.view.EnvelopeView.java
com.indigo_lab.android.opensynth.view.FilterEnvelopeView.java
com.indigo_lab.android.opensynth.view.FilterView.java
com.indigo_lab.android.opensynth.view.ImageRadioButton.java
com.indigo_lab.android.opensynth.view.ModulationView.java
com.indigo_lab.android.opensynth.view.OscillatorDetailView.java
com.indigo_lab.android.opensynth.view.OscillatorView.java
com.indigo_lab.android.opensynth.view.RadioButton.java
com.indigo_lab.android.opensynth.view.VolumeEnvelopeView.java
org.thebends.synth.Arpeggio.java
org.thebends.synth.Controller.java
org.thebends.synth.Envelope.java
org.thebends.synth.FilterCutoff.java
org.thebends.synth.Filter.java
org.thebends.synth.FixedParameter.java
org.thebends.synth.KeyStack.java
org.thebends.synth.KeyboardOscillator.java
org.thebends.synth.LFO.java
org.thebends.synth.LagProcessor.java
org.thebends.synth.LowPassFilter.java
org.thebends.synth.MutableParameter.java
org.thebends.synth.Oscillator.java
org.thebends.synth.Parameter.java
org.thebends.synth.ResonantFilter.java
org.thebends.synth.SynthJni.java
org.thebends.synth.SynthTrack.java
org.thebends.synth.Volume.java