blob: 696983282be7615bbc078332d0e3b9573701c120 [file] [log] [blame]
Don Turnerc700fc82018-05-24 17:59:50 +01001/*
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 Turner495af0e2018-05-25 17:09:54 +010022constexpr int32_t kBufferSize = 192*10; // Temporary buffer is used for mixing
Don Turnerc700fc82018-05-24 17:59:50 +010023constexpr uint8_t kMaxTracks = 100;
24
25template <typename T>
26class Mixer : public RenderableAudio<T> {
27
28public:
Don Turnerc700fc82018-05-24 17:59:50 +010029 void renderAudio(T *audioData, int32_t numFrames) {
30
31 // Zero out the incoming container array
Don Turner495af0e2018-05-25 17:09:54 +010032 memset(audioData, 0, sizeof(T) * numFrames);
Don Turnerc700fc82018-05-24 17:59:50 +010033
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 Turner495af0e2018-05-25 17:09:54 +010043 void addTrack(std::shared_ptr<RenderableAudio<T>> renderer){
Don Turnerc700fc82018-05-24 17:59:50 +010044 mTracks[mNextFreeTrackIndex++] = renderer;
45 }
46
47private:
Don Turner495af0e2018-05-25 17:09:54 +010048 T mixingBuffer[kBufferSize];
49 std::array<std::shared_ptr<RenderableAudio<T>>, kMaxTracks> mTracks;
Don Turnerc700fc82018-05-24 17:59:50 +010050 uint8_t mNextFreeTrackIndex = 0;
51};
52
53
54#endif //RHYTHMGAME_MIXER_H