Don Turner | c700fc8 | 2018-05-24 17:59:50 +0100 | [diff] [blame] | 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 | |
Don Turner | 3a6de86 | 2019-01-03 17:06:38 +0000 | [diff] [blame^] | 17 | #ifndef SHARED_MIXER_MONO_H |
| 18 | #define SHARED_MIXER_MONO_H |
Don Turner | c700fc8 | 2018-05-24 17:59:50 +0100 | [diff] [blame] | 19 | |
Don Turner | 8fb95a6 | 2019-01-02 22:52:15 +0100 | [diff] [blame] | 20 | #include <array> |
| 21 | #include "IRenderableAudio.h" |
Don Turner | c700fc8 | 2018-05-24 17:59:50 +0100 | [diff] [blame] | 22 | |
Don Turner | 495af0e | 2018-05-25 17:09:54 +0100 | [diff] [blame] | 23 | constexpr int32_t kBufferSize = 192*10; // Temporary buffer is used for mixing |
Don Turner | c700fc8 | 2018-05-24 17:59:50 +0100 | [diff] [blame] | 24 | constexpr uint8_t kMaxTracks = 100; |
| 25 | |
Don Turner | 3a6de86 | 2019-01-03 17:06:38 +0000 | [diff] [blame^] | 26 | /** |
| 27 | * A Mixer object which sums the output from multiple mono tracks into a single mono output |
| 28 | */ |
| 29 | class MixerMono : public IRenderableAudio { |
Don Turner | c700fc8 | 2018-05-24 17:59:50 +0100 | [diff] [blame] | 30 | |
| 31 | public: |
Don Turner | 8fb95a6 | 2019-01-02 22:52:15 +0100 | [diff] [blame] | 32 | void renderAudio(float *audioData, int32_t numFrames) { |
Don Turner | c700fc8 | 2018-05-24 17:59:50 +0100 | [diff] [blame] | 33 | |
| 34 | // Zero out the incoming container array |
Don Turner | 8fb95a6 | 2019-01-02 22:52:15 +0100 | [diff] [blame] | 35 | memset(audioData, 0, sizeof(float) * numFrames); |
Don Turner | c700fc8 | 2018-05-24 17:59:50 +0100 | [diff] [blame] | 36 | |
| 37 | for (int i = 0; i < mNextFreeTrackIndex; ++i) { |
| 38 | mTracks[i]->renderAudio(mixingBuffer, numFrames); |
| 39 | |
| 40 | for (int j = 0; j < numFrames; ++j) { |
| 41 | audioData[j] += mixingBuffer[j]; |
| 42 | } |
| 43 | } |
| 44 | } |
| 45 | |
Don Turner | 8fb95a6 | 2019-01-02 22:52:15 +0100 | [diff] [blame] | 46 | void addTrack(std::shared_ptr<IRenderableAudio> renderer){ |
Don Turner | c700fc8 | 2018-05-24 17:59:50 +0100 | [diff] [blame] | 47 | mTracks[mNextFreeTrackIndex++] = renderer; |
| 48 | } |
| 49 | |
| 50 | private: |
Don Turner | 8fb95a6 | 2019-01-02 22:52:15 +0100 | [diff] [blame] | 51 | float mixingBuffer[kBufferSize]; |
| 52 | std::array<std::shared_ptr<IRenderableAudio>, kMaxTracks> mTracks; |
Don Turner | c700fc8 | 2018-05-24 17:59:50 +0100 | [diff] [blame] | 53 | uint8_t mNextFreeTrackIndex = 0; |
| 54 | }; |
| 55 | |
| 56 | |
Don Turner | 3a6de86 | 2019-01-03 17:06:38 +0000 | [diff] [blame^] | 57 | #endif //SHARED_MIXER_MONO_H |