• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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  * Base class for a polyphonic synthesizer voice.
21  */
22 public abstract class SynthVoice {
23     private int mNoteIndex;
24     private float mAmplitude;
25     public static final int STATE_OFF = 0;
26     public static final int STATE_ON = 1;
27     private int mState = STATE_OFF;
28 
SynthVoice()29     public SynthVoice() {
30         mNoteIndex = -1;
31     }
32 
noteOn(int noteIndex, int velocity)33     public void noteOn(int noteIndex, int velocity) {
34         mState = STATE_ON;
35         this.mNoteIndex = noteIndex;
36         setAmplitude(velocity / 128.0f);
37     }
38 
noteOff()39     public void noteOff() {
40         mState = STATE_OFF;
41     }
42 
43     /**
44      * Add the output of this voice to an output buffer.
45      *
46      * @param outputBuffer
47      * @param samplesPerFrame
48      * @param level
49      */
mix(float[] outputBuffer, int samplesPerFrame, float level)50     public void mix(float[] outputBuffer, int samplesPerFrame, float level) {
51         int numFrames = outputBuffer.length / samplesPerFrame;
52         for (int i = 0; i < numFrames; i++) {
53             float output = render();
54             int offset = i * samplesPerFrame;
55             for (int jf = 0; jf < samplesPerFrame; jf++) {
56                 outputBuffer[offset + jf] += output * level;
57             }
58         }
59     }
60 
render()61     public abstract float render();
62 
isDone()63     public boolean isDone() {
64         return mState == STATE_OFF;
65     }
66 
getNoteIndex()67     public int getNoteIndex() {
68         return mNoteIndex;
69     }
70 
getAmplitude()71     public float getAmplitude() {
72         return mAmplitude;
73     }
74 
setAmplitude(float amplitude)75     public void setAmplitude(float amplitude) {
76         this.mAmplitude = amplitude;
77     }
78 
79     /**
80      * @param scaler
81      */
setFrequencyScaler(float scaler)82     public void setFrequencyScaler(float scaler) {
83     }
84 
85 }
86