blob: 2d45dbc1489c17efe7176a951a350632ce989485 [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
22constexpr int32_t kBufferSize = 192*10; // Temporary buffer is used for mixing
23constexpr uint8_t kMaxTracks = 100;
24
25template <typename T>
26class Mixer : public RenderableAudio<T> {
27
28public:
29
30 void renderAudio(T *audioData, int32_t numFrames) {
31
32 // Zero out the incoming container array
33 for (int j = 0; j < numFrames; ++j) {
34 audioData[j] = 0;
35 }
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
46 void addTrack(RenderableAudio<T> *renderer){
47 mTracks[mNextFreeTrackIndex++] = renderer;
48 }
49
50private:
51 T mixingBuffer[kBufferSize]; // TODO: smart pointer
52 RenderableAudio<T>* mTracks[kMaxTracks]; // TODO: this might be better as a linked list for easy track removal
53 uint8_t mNextFreeTrackIndex = 0;
54};
55
56
57#endif //RHYTHMGAME_MIXER_H