1 /* 2 * Copyright 2018 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 #ifndef SAMPLES_FULLDUPLEXPASS_H 18 #define SAMPLES_FULLDUPLEXPASS_H 19 20 #include "FullDuplexStream.h" 21 22 class FullDuplexPass : public FullDuplexStream { 23 public: 24 virtual oboe::DataCallbackResult onBothStreamsReady(std::shared_ptr<oboe::AudioStream> inputStream,const void * inputData,int numInputFrames,std::shared_ptr<oboe::AudioStream> outputStream,void * outputData,int numOutputFrames)25 onBothStreamsReady( 26 std::shared_ptr<oboe::AudioStream> inputStream, 27 const void *inputData, 28 int numInputFrames, 29 std::shared_ptr<oboe::AudioStream> outputStream, 30 void *outputData, 31 int numOutputFrames) { 32 // Copy the input samples to the output with a little arbitrary gain change. 33 34 // This code assumes the data format for both streams is Float. 35 const float *inputFloats = static_cast<const float *>(inputData); 36 float *outputFloats = static_cast<float *>(outputData); 37 38 // It also assumes the channel count for each stream is the same. 39 int32_t samplesPerFrame = outputStream->getChannelCount(); 40 int32_t numInputSamples = numInputFrames * samplesPerFrame; 41 int32_t numOutputSamples = numOutputFrames * samplesPerFrame; 42 43 // It is possible that there may be fewer input than output samples. 44 int32_t samplesToProcess = std::min(numInputSamples, numOutputSamples); 45 for (int32_t i = 0; i < samplesToProcess; i++) { 46 *outputFloats++ = *inputFloats++ * 0.95; // do some arbitrary processing 47 } 48 49 // If there are fewer input samples then clear the rest of the buffer. 50 int32_t samplesLeft = numOutputSamples - numInputSamples; 51 for (int32_t i = 0; i < samplesLeft; i++) { 52 *outputFloats++ = 0.0; // silence 53 } 54 55 return oboe::DataCallbackResult::Continue; 56 } 57 }; 58 #endif //SAMPLES_FULLDUPLEXPASS_H 59