Home | History | Annotate | Download | only in synth
      1 /*
      2  * Copyright (C) 2015 The Android Open Source Project
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  *      http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 
     17 package com.example.android.common.midi.synth;
     18 
     19 /**
     20  * Sawtooth oscillator with an ADSR.
     21  */
     22 public class SawVoice extends SynthVoice {
     23     private SawOscillator mOscillator;
     24     private EnvelopeADSR mEnvelope;
     25 
     26     public SawVoice() {
     27         mOscillator = createOscillator();
     28         mEnvelope = new EnvelopeADSR();
     29     }
     30 
     31     protected SawOscillator createOscillator() {
     32         return new SawOscillator();
     33     }
     34 
     35     @Override
     36     public void noteOn(int noteIndex, int velocity) {
     37         super.noteOn(noteIndex, velocity);
     38         mOscillator.setPitch(noteIndex);
     39         mOscillator.setAmplitude(getAmplitude());
     40         mEnvelope.on();
     41     }
     42 
     43     @Override
     44     public void noteOff() {
     45         super.noteOff();
     46         mEnvelope.off();
     47     }
     48 
     49     @Override
     50     public void setFrequencyScaler(float scaler) {
     51         mOscillator.setFrequencyScaler(scaler);
     52     }
     53 
     54     @Override
     55     public float render() {
     56         float output = mOscillator.render() * mEnvelope.render();
     57         return output;
     58     }
     59 
     60     @Override
     61     public boolean isDone() {
     62         return mEnvelope.isDone();
     63     }
     64 
     65 }
     66