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 | |
| 17 | #ifndef RHYTHMGAME_MIXER_H |
| 18 | #define RHYTHMGAME_MIXER_H |
| 19 | |
| 20 | #include "RenderableAudio.h" |
| 21 | |
Don Turner | 495af0e | 2018-05-25 17:09:54 +0100 | [diff] [blame^] | 22 | constexpr int32_t kBufferSize = 192*10; // Temporary buffer is used for mixing |
Don Turner | c700fc8 | 2018-05-24 17:59:50 +0100 | [diff] [blame] | 23 | constexpr uint8_t kMaxTracks = 100; |
| 24 | |
| 25 | template <typename T> |
| 26 | class Mixer : public RenderableAudio<T> { |
| 27 | |
| 28 | public: |
Don Turner | c700fc8 | 2018-05-24 17:59:50 +0100 | [diff] [blame] | 29 | void renderAudio(T *audioData, int32_t numFrames) { |
| 30 | |
| 31 | // Zero out the incoming container array |
Don Turner | 495af0e | 2018-05-25 17:09:54 +0100 | [diff] [blame^] | 32 | memset(audioData, 0, sizeof(T) * numFrames); |
Don Turner | c700fc8 | 2018-05-24 17:59:50 +0100 | [diff] [blame] | 33 | |
| 34 | for (int i = 0; i < mNextFreeTrackIndex; ++i) { |
| 35 | mTracks[i]->renderAudio(mixingBuffer, numFrames); |
| 36 | |
| 37 | for (int j = 0; j < numFrames; ++j) { |
| 38 | audioData[j] += mixingBuffer[j]; |
| 39 | } |
| 40 | } |
| 41 | } |
| 42 | |
Don Turner | 495af0e | 2018-05-25 17:09:54 +0100 | [diff] [blame^] | 43 | void addTrack(std::shared_ptr<RenderableAudio<T>> renderer){ |
Don Turner | c700fc8 | 2018-05-24 17:59:50 +0100 | [diff] [blame] | 44 | mTracks[mNextFreeTrackIndex++] = renderer; |
| 45 | } |
| 46 | |
| 47 | private: |
Don Turner | 495af0e | 2018-05-25 17:09:54 +0100 | [diff] [blame^] | 48 | T mixingBuffer[kBufferSize]; |
| 49 | std::array<std::shared_ptr<RenderableAudio<T>>, kMaxTracks> mTracks; |
Don Turner | c700fc8 | 2018-05-24 17:59:50 +0100 | [diff] [blame] | 50 | uint8_t mNextFreeTrackIndex = 0; |
| 51 | }; |
| 52 | |
| 53 | |
| 54 | #endif //RHYTHMGAME_MIXER_H |