blob: 52ccf3770ab134c0eb13bb447447d09bd8bad241 [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 Turner7cc61952019-01-03 21:34:35 +000017#ifndef SHARED_MIXER_H
18#define SHARED_MIXER_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/**
Don Turner7cc61952019-01-03 21:34:35 +000027 * A Mixer object which sums the output from multiple tracks into a single output. The number of
28 * input channels on each track must match the number of output channels (default 1=mono). This can
29 * be changed by calling `setChannelCount`.
Don Turner3a6de862019-01-03 17:06:38 +000030 */
Don Turner7cc61952019-01-03 21:34:35 +000031class Mixer : public IRenderableAudio {
Don Turnerc700fc82018-05-24 17:59:50 +010032
33public:
Don Turner8fb95a62019-01-02 22:52:15 +010034 void renderAudio(float *audioData, int32_t numFrames) {
Don Turnerc700fc82018-05-24 17:59:50 +010035
36 // Zero out the incoming container array
Don Turner7cc61952019-01-03 21:34:35 +000037 memset(audioData, 0, sizeof(float) * numFrames * mChannelCount);
Don Turnerc700fc82018-05-24 17:59:50 +010038
39 for (int i = 0; i < mNextFreeTrackIndex; ++i) {
40 mTracks[i]->renderAudio(mixingBuffer, numFrames);
41
Don Turner7cc61952019-01-03 21:34:35 +000042 for (int j = 0; j < numFrames * mChannelCount; ++j) {
Don Turnerc700fc82018-05-24 17:59:50 +010043 audioData[j] += mixingBuffer[j];
44 }
45 }
46 }
47
Don Turner8fb95a62019-01-02 22:52:15 +010048 void addTrack(std::shared_ptr<IRenderableAudio> renderer){
Don Turnerc700fc82018-05-24 17:59:50 +010049 mTracks[mNextFreeTrackIndex++] = renderer;
50 }
51
Don Turner7cc61952019-01-03 21:34:35 +000052 void setChannelCount(int32_t channelCount){ mChannelCount = channelCount; }
53
Don Turnerc700fc82018-05-24 17:59:50 +010054private:
Don Turner8fb95a62019-01-02 22:52:15 +010055 float mixingBuffer[kBufferSize];
56 std::array<std::shared_ptr<IRenderableAudio>, kMaxTracks> mTracks;
Don Turnerc700fc82018-05-24 17:59:50 +010057 uint8_t mNextFreeTrackIndex = 0;
Don Turner7cc61952019-01-03 21:34:35 +000058 int32_t mChannelCount = 1; // Default to mono
Don Turnerc700fc82018-05-24 17:59:50 +010059};
60
61
Don Turner7cc61952019-01-03 21:34:35 +000062#endif //SHARED_MIXER_H