Home | History | Annotate | Download | only in testlibs
      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 
     24 namespace android {
     25 
     26 // Audio coefficient type.
     27 typedef int32_t audio_coef_t;
     28 // Audio sample type.
     29 typedef int32_t audio_sample_t;
     30 // Accumulator type for coef x sample.
     31 typedef int64_t audio_coef_sample_acc_t;
     32 
     33 // Number of fraction bits for audio coefficient.
     34 static const int AUDIO_COEF_PRECISION = 24;
     35 // Audio coefficient with the value of 1.0
     36 static const audio_coef_t AUDIO_COEF_ONE = 1 << AUDIO_COEF_PRECISION;
     37 // Audio coefficient with the value of 0.5
     38 static const audio_coef_t AUDIO_COEF_HALF = 1 << (AUDIO_COEF_PRECISION - 1);
     39 // Number of fraction bits for audio sample.
     40 static const int AUDIO_SAMPLE_PRECISION = 24;
     41 // Audio sample with the value of 1.0
     42 static const audio_sample_t AUDIO_SAMPLE_ONE = 1 << AUDIO_SAMPLE_PRECISION;
     43 
     44 // TODO: These are just temporary naive implementations of the necessary
     45 // arithmetic operations needed for the filter. They should be moved to a more
     46 // generic location and implemented more efficiently.
     47 
     48 // Multiply a sample by a coefficient to return an accumulator.
     49 inline audio_coef_sample_acc_t mul_coef_sample(audio_coef_t x, audio_sample_t y) {
     50     return ((audio_coef_sample_acc_t) (x)) * y;
     51 }
     52 
     53 // Multiply and accumulate sample by a coefficient to return an accumulator.
     54 inline audio_coef_sample_acc_t mac_coef_sample(audio_coef_t x, audio_sample_t y, audio_coef_sample_acc_t acc) {
     55     return acc + ((audio_coef_sample_acc_t) (x)) * y;
     56 }
     57 
     58 // Convert a sample-coefficient accumulator to a sample.
     59 inline audio_sample_t coef_sample_acc_to_sample(audio_coef_sample_acc_t acc) {
     60     if (acc < 0) {
     61         acc += AUDIO_COEF_ONE - 1;
     62     }
     63     return (audio_sample_t) (acc >> AUDIO_COEF_PRECISION);
     64 }
     65 
     66 // Convert a S15 sample to audio_sample_t
     67 inline audio_sample_t s15_to_audio_sample_t(int16_t s15) {
     68     return audio_sample_t(s15) << 9;
     69 }
     70 
     71 // Convert a audio_sample_t sample to S15 (no clipping)
     72 inline int16_t audio_sample_t_to_s15(audio_sample_t sample) {
     73     return int16_t((sample + (1 << 8)) >> 9);
     74 }
     75 
     76 // Convert a audio_sample_t sample to S15 (with clipping)
     77 inline int16_t audio_sample_t_to_s15_clip(audio_sample_t sample) {
     78     // TODO: optimize for targets supporting this as an atomic operation.
     79     if (__builtin_expect(sample >= (0x7FFF << 9), 0)) {
     80         return 0x7FFF;
     81     } else if (__builtin_expect(sample <= -(0x8000 << 9), 0)) {
     82         return 0x8000;
     83     } else {
     84         return audio_sample_t_to_s15(sample);
     85     }
     86 }
     87 
     88 ////////////////////////////////////////////////////////////////////////////////
     89 
     90 }
     91 
     92 #endif // ANDROID_AUDIO_COMMON_H
     93