1ffe9c25ce85e1af55d58ec025adc6367d70db7e8Eric Laurent/*
2135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent**
3135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent** Copyright 2009, The Android Open Source Project
4135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent**
5135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent** Licensed under the Apache License, Version 2.0 (the "License");
6135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent** you may not use this file except in compliance with the License.
7135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent** You may obtain a copy of the License at
8135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent**
9135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent**     http://www.apache.org/licenses/LICENSE-2.0
10135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent**
11135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent** Unless required by applicable law or agreed to in writing, software
12135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent** distributed under the License is distributed on an "AS IS" BASIS,
13135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent** See the License for the specific language governing permissions and
15135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent** limitations under the License.
16135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent*/
17135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent
18135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent#ifndef ANDROID_AUDIO_COMMON_H
19135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent#define ANDROID_AUDIO_COMMON_H
20135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent
21135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent#include <stdint.h>
22135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent#include <stddef.h>
23f6b1678f8f508b447155a81b44e214475ab634a8Glenn Kasten#include <cutils/compiler.h>
24135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent
25135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurentnamespace android {
26135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent
27135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent// Audio coefficient type.
28135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurenttypedef int32_t audio_coef_t;
29135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent// Audio sample type.
30135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurenttypedef int32_t audio_sample_t;
31135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent// Accumulator type for coef x sample.
32135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurenttypedef int64_t audio_coef_sample_acc_t;
33135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent
34135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent// Number of fraction bits for audio coefficient.
35135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurentstatic const int AUDIO_COEF_PRECISION = 24;
36135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent// Audio coefficient with the value of 1.0
37135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurentstatic const audio_coef_t AUDIO_COEF_ONE = 1 << AUDIO_COEF_PRECISION;
38135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent// Audio coefficient with the value of 0.5
39135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurentstatic const audio_coef_t AUDIO_COEF_HALF = 1 << (AUDIO_COEF_PRECISION - 1);
40135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent// Number of fraction bits for audio sample.
41135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurentstatic const int AUDIO_SAMPLE_PRECISION = 24;
42135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent// Audio sample with the value of 1.0
43135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurentstatic const audio_sample_t AUDIO_SAMPLE_ONE = 1 << AUDIO_SAMPLE_PRECISION;
44135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent
45135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent// TODO: These are just temporary naive implementations of the necessary
46135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent// arithmetic operations needed for the filter. They should be moved to a more
47135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent// generic location and implemented more efficiently.
48135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent
49135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent// Multiply a sample by a coefficient to return an accumulator.
50135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurentinline audio_coef_sample_acc_t mul_coef_sample(audio_coef_t x, audio_sample_t y) {
51135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent    return ((audio_coef_sample_acc_t) (x)) * y;
52135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent}
53135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent
54135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent// Multiply and accumulate sample by a coefficient to return an accumulator.
55135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurentinline audio_coef_sample_acc_t mac_coef_sample(audio_coef_t x, audio_sample_t y, audio_coef_sample_acc_t acc) {
56135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent    return acc + ((audio_coef_sample_acc_t) (x)) * y;
57135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent}
58135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent
59135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent// Convert a sample-coefficient accumulator to a sample.
60135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurentinline audio_sample_t coef_sample_acc_to_sample(audio_coef_sample_acc_t acc) {
61135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent    if (acc < 0) {
62135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent        acc += AUDIO_COEF_ONE - 1;
63135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent    }
64135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent    return (audio_sample_t) (acc >> AUDIO_COEF_PRECISION);
65135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent}
66135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent
67135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent// Convert a S15 sample to audio_sample_t
68135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurentinline audio_sample_t s15_to_audio_sample_t(int16_t s15) {
69135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent    return audio_sample_t(s15) << 9;
70135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent}
71135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent
72135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent// Convert a audio_sample_t sample to S15 (no clipping)
73135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurentinline int16_t audio_sample_t_to_s15(audio_sample_t sample) {
74135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent    return int16_t((sample + (1 << 8)) >> 9);
75135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent}
76135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent
77135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent// Convert a audio_sample_t sample to S15 (with clipping)
78135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurentinline int16_t audio_sample_t_to_s15_clip(audio_sample_t sample) {
79135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent    // TODO: optimize for targets supporting this as an atomic operation.
80f6b1678f8f508b447155a81b44e214475ab634a8Glenn Kasten    if (CC_UNLIKELY(sample >= (0x7FFF << 9))) {
81135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent        return 0x7FFF;
82f6b1678f8f508b447155a81b44e214475ab634a8Glenn Kasten    } else if (CC_UNLIKELY(sample <= -(0x8000 << 9))) {
83135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent        return 0x8000;
84135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent    } else {
85135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent        return audio_sample_t_to_s15(sample);
86135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent    }
87135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent}
88135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent
89135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent////////////////////////////////////////////////////////////////////////////////
90135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent
91135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent}
92135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent
93135ad07e33d30e5202deb21061a0e3ecf0ffad35Eric Laurent#endif // ANDROID_AUDIO_COMMON_H
94