1 /* 2 ** 3 ** Copyright 2009, The Android Open Source Project 4 ** 5 ** Licensed under the Apache License, Version 2.0 (the "License"); 6 ** you may not use this file except in compliance with the License. 7 ** You may obtain a copy of the License at 8 ** 9 ** http://www.apache.org/licenses/LICENSE-2.0 10 ** 11 ** Unless required by applicable law or agreed to in writing, software 12 ** distributed under the License is distributed on an "AS IS" BASIS, 13 ** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 ** See the License for the specific language governing permissions and 15 ** limitations under the License. 16 */ 17 18 #ifndef ANDROID_AUDIO_COMMON_H 19 #define ANDROID_AUDIO_COMMON_H 20 21 #include <stdint.h> 22 #include <stddef.h> 23 #include <cutils/compiler.h> 24 25 namespace android { 26 27 // Audio coefficient type. 28 typedef int32_t audio_coef_t; 29 // Audio sample type. 30 typedef int32_t audio_sample_t; 31 // Accumulator type for coef x sample. 32 typedef int64_t audio_coef_sample_acc_t; 33 34 // Number of fraction bits for audio coefficient. 35 static const int AUDIO_COEF_PRECISION = 24; 36 // Audio coefficient with the value of 1.0 37 static const audio_coef_t AUDIO_COEF_ONE = 1 << AUDIO_COEF_PRECISION; 38 // Audio coefficient with the value of 0.5 39 static const audio_coef_t AUDIO_COEF_HALF = 1 << (AUDIO_COEF_PRECISION - 1); 40 // Number of fraction bits for audio sample. 41 static const int AUDIO_SAMPLE_PRECISION = 24; 42 // Audio sample with the value of 1.0 43 static const audio_sample_t AUDIO_SAMPLE_ONE = 1 << AUDIO_SAMPLE_PRECISION; 44 45 // TODO: These are just temporary naive implementations of the necessary 46 // arithmetic operations needed for the filter. They should be moved to a more 47 // generic location and implemented more efficiently. 48 49 // Multiply a sample by a coefficient to return an accumulator. 50 inline audio_coef_sample_acc_t mul_coef_sample(audio_coef_t x, audio_sample_t y) { 51 return ((audio_coef_sample_acc_t) (x)) * y; 52 } 53 54 // Multiply and accumulate sample by a coefficient to return an accumulator. 55 inline audio_coef_sample_acc_t mac_coef_sample(audio_coef_t x, audio_sample_t y, audio_coef_sample_acc_t acc) { 56 return acc + ((audio_coef_sample_acc_t) (x)) * y; 57 } 58 59 // Convert a sample-coefficient accumulator to a sample. 60 inline audio_sample_t coef_sample_acc_to_sample(audio_coef_sample_acc_t acc) { 61 if (acc < 0) { 62 acc += AUDIO_COEF_ONE - 1; 63 } 64 return (audio_sample_t) (acc >> AUDIO_COEF_PRECISION); 65 } 66 67 // Convert a S15 sample to audio_sample_t 68 inline audio_sample_t s15_to_audio_sample_t(int16_t s15) { 69 return audio_sample_t(s15) << 9; 70 } 71 72 // Convert a audio_sample_t sample to S15 (no clipping) 73 inline int16_t audio_sample_t_to_s15(audio_sample_t sample) { 74 return int16_t((sample + (1 << 8)) >> 9); 75 } 76 77 // Convert a audio_sample_t sample to S15 (with clipping) 78 inline int16_t audio_sample_t_to_s15_clip(audio_sample_t sample) { 79 // TODO: optimize for targets supporting this as an atomic operation. 80 if (CC_UNLIKELY(sample >= (0x7FFF << 9))) { 81 return 0x7FFF; 82 } else if (CC_UNLIKELY(sample <= -(0x8000 << 9))) { 83 return 0x8000; 84 } else { 85 return audio_sample_t_to_s15(sample); 86 } 87 } 88 89 //////////////////////////////////////////////////////////////////////////////// 90 91 } 92 93 #endif // ANDROID_AUDIO_COMMON_H 94