2 * Copyright (C) 2015 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 //#define LOG_NDEBUG 0
18 #define LOG_TAG "audio_utils_mono_blend"
21 #include <audio_utils/mono_blend.h>
22 #include <utils/Log.h>
23 #include <audio_utils/limiter.h>
25 // TODO: Speed up for special case of 2 channels?
26 void mono_blend(void *buf, audio_format_t format, size_t channelCount, size_t frames, bool limit) {
27 if (channelCount < 2) {
31 case AUDIO_FORMAT_PCM_16_BIT: {
32 int16_t *out = (int16_t *)buf;
33 for (size_t i = 0; i < frames; ++i) {
34 const int16_t *in = out;
36 for (size_t j = 0; j < channelCount; ++j) {
39 accum /= channelCount; // round to 0
40 for (size_t j = 0; j < channelCount; ++j) {
45 case AUDIO_FORMAT_PCM_FLOAT: {
46 float *out = (float *)buf;
47 const float recipdiv = 1. / channelCount;
48 for (size_t i = 0; i < frames; ++i) {
49 const float *in = out;
51 for (size_t j = 0; j < channelCount; ++j) {
54 if (limit && channelCount == 2) {
55 accum = limiter(accum * M_SQRT1_2);
59 for (size_t j = 0; j < channelCount; ++j) {
65 ALOGE("mono_blend: invalid format %d", format);