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 
SawVoice()26     public SawVoice() {
27         mOscillator = createOscillator();
28         mEnvelope = new EnvelopeADSR();
29     }
30 
createOscillator()31     protected SawOscillator createOscillator() {
32         return new SawOscillator();
33     }
34 
35     @Override
noteOn(int noteIndex, int velocity)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
noteOff()44     public void noteOff() {
45         super.noteOff();
46         mEnvelope.off();
47     }
48 
49     @Override
setFrequencyScaler(float scaler)50     public void setFrequencyScaler(float scaler) {
51         mOscillator.setFrequencyScaler(scaler);
52     }
53 
54     @Override
render()55     public float render() {
56         float output = mOscillator.render() * mEnvelope.render();
57         return output;
58     }
59 
60     @Override
isDone()61     public boolean isDone() {
62         return mEnvelope.isDone();
63     }
64 
65 }
66