Android Open Source - OpenSynth Keyboard Oscillator






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  ww .java2  s  .c o  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;

/**
 * Groups logic related to running the oscillators from keyboard input.
 */
public class KeyboardOscillator implements Parameter {
    private final float OCTAVE_CENTS = 1200;

    private Parameter mBaseFrequency;

    private double mOsc1Octave;
    private double mOsc2Octave;
    private double mOsc1Level;
    private double mOsc2Level;
    private double mOsc2Shift;

    private MutableParameter mOsc1Freq;
    private MutableParameter mOsc2Freq;
    private Parameter mFrequencyModulation;

    private boolean mSync;

    private Oscillator mOsc1;
    private Oscillator mOsc2;

    public KeyboardOscillator(Oscillator osc1,
            Oscillator osc2,
            Parameter frequency) {
        mBaseFrequency = frequency;
        mOsc1Octave = 1;
        mOsc2Octave = 1;
        mOsc1Level = 0;
        mOsc2Level = 0;
        mOsc2Shift = 1.0f;
        mOsc1Freq = new MutableParameter();
        mOsc2Freq = new MutableParameter();
        mFrequencyModulation = null;
        mSync = false;
        mOsc1 = osc1;
        mOsc2 = osc2;

        mOsc1.setFrequency(mOsc1Freq);
        mOsc2.setFrequency(mOsc2Freq);
    }

    /**
     * Multiple to the specified octave
     */
    public void setOsc1Octave(double multiply) {
        mOsc1Octave = multiply;
    }

    public void setOsc2Octave(double multiply) {
        mOsc2Octave = multiply;
    }

    public void setOsc1Level(double level) {
        mOsc1Level = level;
    }

    public void setOsc2Level(double level) {
        mOsc2Level = level;
    }

    /**
     * Number of cents to shift osc2
     */
    public void setOsc2Shift(int cents) {
        if (cents == 0) {
            mOsc2Shift = 1.0f;
        } else {
            mOsc2Shift = Math.pow(2.0f, cents / OCTAVE_CENTS);
        }
    }

    /**
     * Sync osc2 to osc1 (master)
     */
    public void setOscSync(boolean sync) {
        mSync = sync;
    }

    /**
     * Can be NUL to disable frequency modulation, otherwise a multiplier of the
     * current frequency intended to change over time.
     */
    public void setFrequencyModulation(Parameter parameter) {
        mFrequencyModulation = parameter;
    }

    /**
     * Return the value of the combine oscillators
     */
    public double getValue() {
        // osc2 is a slave to osc1 when sync is enabled.
        if (mSync && mOsc1.isStart()) {
            mOsc2.reset();
        }
  
        double root_note = mBaseFrequency.getValue(); 
        if (mFrequencyModulation != null) {
            root_note *= mFrequencyModulation.getValue();
        }
        mOsc1Freq.setValue(root_note * mOsc1Octave);
        double mOsc2freq = root_note * mOsc2Octave;
        mOsc2freq *= mOsc2Shift;
        mOsc2Freq.setValue(mOsc2freq);

        double value = mOsc1Level * mOsc1.getValue() +
            mOsc2Level * mOsc2.getValue();
        // Clip
        value = Math.min(value, 1.0f);
        return Math.max(value, -1.0f);
    }
}




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