blob: d58d192f7bf93fd10aeaab39c43321a900d378d1 [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
Don Turner8fb95a62019-01-02 22:52:15 +010020#include <array>
21#include "IRenderableAudio.h"
Don Turnerc700fc82018-05-24 17:59:50 +010022
Don Turner495af0e2018-05-25 17:09:54 +010023constexpr int32_t kBufferSize = 192*10; // Temporary buffer is used for mixing
Don Turnerc700fc82018-05-24 17:59:50 +010024constexpr uint8_t kMaxTracks = 100;
25
Don Turner8fb95a62019-01-02 22:52:15 +010026class Mixer : public IRenderableAudio {
Don Turnerc700fc82018-05-24 17:59:50 +010027
28public:
Don Turner8fb95a62019-01-02 22:52:15 +010029 void renderAudio(float *audioData, int32_t numFrames) {
Don Turnerc700fc82018-05-24 17:59:50 +010030
31 // Zero out the incoming container array
Don Turner8fb95a62019-01-02 22:52:15 +010032 memset(audioData, 0, sizeof(float) * 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 Turner8fb95a62019-01-02 22:52:15 +010043 void addTrack(std::shared_ptr<IRenderableAudio> renderer){
Don Turnerc700fc82018-05-24 17:59:50 +010044 mTracks[mNextFreeTrackIndex++] = renderer;
45 }
46
47private:
Don Turner8fb95a62019-01-02 22:52:15 +010048 float mixingBuffer[kBufferSize];
49 std::array<std::shared_ptr<IRenderableAudio>, kMaxTracks> mTracks;
Don Turnerc700fc82018-05-24 17:59:50 +010050 uint8_t mNextFreeTrackIndex = 0;
51};
52
53
54#endif //RHYTHMGAME_MIXER_H