1 /* 2 * Copyright 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 #include <math.h> 18 #include <unistd.h> 19 20 #include "SineOscillator.h" 21 22 /* 23 * This calls sinf() so it is not very efficient. 24 * A more efficient implementation might use a wave-table or a polynomial. 25 */ SineOscillator()26SineOscillator::SineOscillator() 27 : OscillatorBase() { 28 } 29 onProcess(int32_t numFrames)30int32_t SineOscillator::onProcess(int32_t numFrames) { 31 const float *frequencies = frequency.getBuffer(); 32 const float *amplitudes = amplitude.getBuffer(); 33 float *buffer = output.getBuffer(); 34 35 // Generate sine wave. 36 for (int i = 0; i < numFrames; i++) { 37 float phase = incrementPhase(frequencies[i]); // phase ranges from -1 to +1 38 *buffer++ = sinf(phase * M_PI) * amplitudes[i]; 39 } 40 41 return numFrames; 42 } 43