Andy Hung | 8bfe6a4 | 2015-12-18 17:37:19 -0800 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright (C) 2015 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 | //#define LOG_NDEBUG 0 |
| 18 | #define LOG_TAG "audio_utils_conversion" |
| 19 | |
| 20 | #include <audio_utils/conversion.h> |
| 21 | #include <utils/Log.h> |
| 22 | |
| 23 | // TODO: Speed up for special case of 2 channels? |
| 24 | void mono_blend(void *buf, audio_format_t format, size_t channelCount, size_t frames) { |
| 25 | switch (format) { |
| 26 | case AUDIO_FORMAT_PCM_16_BIT: { |
| 27 | int16_t *out = (int16_t *)buf; |
| 28 | for (size_t i = 0; i < frames; ++i) { |
| 29 | const int16_t *in = out; |
| 30 | int accum = 0; |
| 31 | for (size_t j = 0; j < channelCount; ++j) { |
| 32 | accum += *in++; |
| 33 | } |
| 34 | accum /= channelCount; // round to 0 |
| 35 | for (size_t j = 0; j < channelCount; ++j) { |
| 36 | *out++ = accum; |
| 37 | } |
| 38 | } |
| 39 | } break; |
| 40 | case AUDIO_FORMAT_PCM_FLOAT: { |
| 41 | float *out = (float *)buf; |
| 42 | const float recipdiv = 1. / channelCount; |
| 43 | for (size_t i = 0; i < frames; ++i) { |
| 44 | const float *in = out; |
| 45 | float accum = 0; |
| 46 | for (size_t j = 0; j < channelCount; ++j) { |
| 47 | accum += *in++; |
| 48 | } |
| 49 | accum *= recipdiv; |
| 50 | for (size_t j = 0; j < channelCount; ++j) { |
| 51 | *out++ = accum; |
| 52 | } |
| 53 | } |
| 54 | } break; |
| 55 | default: |
| 56 | ALOGE("mono_blend: invalid format %d", format); |
| 57 | break; |
| 58 | } |
| 59 | } |