blob: 425095d6fbc3badde6b7dbac8fa84debcb4f573d [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
Don Turner3a6de862019-01-03 17:06:38 +000017#ifndef SHARED_MIXER_MONO_H
18#define SHARED_MIXER_MONO_H
Don Turnerc700fc82018-05-24 17:59:50 +010019
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 Turner3a6de862019-01-03 17:06:38 +000026/**
27 * A Mixer object which sums the output from multiple mono tracks into a single mono output
28 */
29class MixerMono : public IRenderableAudio {
Don Turnerc700fc82018-05-24 17:59:50 +010030
31public:
Don Turner8fb95a62019-01-02 22:52:15 +010032 void renderAudio(float *audioData, int32_t numFrames) {
Don Turnerc700fc82018-05-24 17:59:50 +010033
34 // Zero out the incoming container array
Don Turner8fb95a62019-01-02 22:52:15 +010035 memset(audioData, 0, sizeof(float) * numFrames);
Don Turnerc700fc82018-05-24 17:59:50 +010036
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 Turner8fb95a62019-01-02 22:52:15 +010046 void addTrack(std::shared_ptr<IRenderableAudio> renderer){
Don Turnerc700fc82018-05-24 17:59:50 +010047 mTracks[mNextFreeTrackIndex++] = renderer;
48 }
49
50private:
Don Turner8fb95a62019-01-02 22:52:15 +010051 float mixingBuffer[kBufferSize];
52 std::array<std::shared_ptr<IRenderableAudio>, kMaxTracks> mTracks;
Don Turnerc700fc82018-05-24 17:59:50 +010053 uint8_t mNextFreeTrackIndex = 0;
54};
55
56
Don Turner3a6de862019-01-03 17:06:38 +000057#endif //SHARED_MIXER_MONO_H