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 SHARED_MIXER_H 18 #define SHARED_MIXER_H 19 20 #include <array> 21 #include "IRenderableAudio.h" 22 23 constexpr int32_t kBufferSize = 192*10; // Temporary buffer is used for mixing 24 constexpr uint8_t kMaxTracks = 100; 25 26 /** 27 * A Mixer object which sums the output from multiple tracks into a single output. The number of 28 * input channels on each track must match the number of output channels (default 1=mono). This can 29 * be changed by calling `setChannelCount`. 30 * The inputs to the mixer are not owned by the mixer, they should not be deleted while rendering. 31 */ 32 class Mixer : public IRenderableAudio { 33 34 public: renderAudio(float * audioData,int32_t numFrames)35 void renderAudio(float *audioData, int32_t numFrames) { 36 37 // Zero out the incoming container array 38 memset(audioData, 0, sizeof(float) * numFrames * mChannelCount); 39 40 for (int i = 0; i < mNextFreeTrackIndex; ++i) { 41 mTracks[i]->renderAudio(mixingBuffer, numFrames); 42 43 for (int j = 0; j < numFrames * mChannelCount; ++j) { 44 audioData[j] += mixingBuffer[j]; 45 } 46 } 47 } 48 addTrack(IRenderableAudio * renderer)49 void addTrack(IRenderableAudio *renderer){ 50 mTracks[mNextFreeTrackIndex++] = renderer; 51 } 52 setChannelCount(int32_t channelCount)53 void setChannelCount(int32_t channelCount){ mChannelCount = channelCount; } 54 55 private: 56 float mixingBuffer[kBufferSize]; 57 std::array<IRenderableAudio*, kMaxTracks> mTracks; 58 uint8_t mNextFreeTrackIndex = 0; 59 int32_t mChannelCount = 1; // Default to mono 60 }; 61 62 63 #endif //SHARED_MIXER_H 64