153334cdb81bab4a4dfd0a41d2ef50709015a36c8Eric Laurent/* 25fe37c6838de9fbd959ad19ba44aa3d00d1b4e6fEric Laurent** 35fe37c6838de9fbd959ad19ba44aa3d00d1b4e6fEric Laurent** Copyright 2009, The Android Open Source Project 45fe37c6838de9fbd959ad19ba44aa3d00d1b4e6fEric Laurent** 55fe37c6838de9fbd959ad19ba44aa3d00d1b4e6fEric Laurent** Licensed under the Apache License, Version 2.0 (the "License"); 65fe37c6838de9fbd959ad19ba44aa3d00d1b4e6fEric Laurent** you may not use this file except in compliance with the License. 75fe37c6838de9fbd959ad19ba44aa3d00d1b4e6fEric Laurent** You may obtain a copy of the License at 85fe37c6838de9fbd959ad19ba44aa3d00d1b4e6fEric Laurent** 95fe37c6838de9fbd959ad19ba44aa3d00d1b4e6fEric Laurent** http://www.apache.org/licenses/LICENSE-2.0 105fe37c6838de9fbd959ad19ba44aa3d00d1b4e6fEric Laurent** 115fe37c6838de9fbd959ad19ba44aa3d00d1b4e6fEric Laurent** Unless required by applicable law or agreed to in writing, software 125fe37c6838de9fbd959ad19ba44aa3d00d1b4e6fEric Laurent** distributed under the License is distributed on an "AS IS" BASIS, 135fe37c6838de9fbd959ad19ba44aa3d00d1b4e6fEric Laurent** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 145fe37c6838de9fbd959ad19ba44aa3d00d1b4e6fEric Laurent** See the License for the specific language governing permissions and 155fe37c6838de9fbd959ad19ba44aa3d00d1b4e6fEric Laurent** limitations under the License. 165fe37c6838de9fbd959ad19ba44aa3d00d1b4e6fEric Laurent*/ 175fe37c6838de9fbd959ad19ba44aa3d00d1b4e6fEric Laurent 185fe37c6838de9fbd959ad19ba44aa3d00d1b4e6fEric Laurent#ifndef ANDROID_AUDIO_COMMON_H 195fe37c6838de9fbd959ad19ba44aa3d00d1b4e6fEric Laurent#define ANDROID_AUDIO_COMMON_H 205fe37c6838de9fbd959ad19ba44aa3d00d1b4e6fEric Laurent 215fe37c6838de9fbd959ad19ba44aa3d00d1b4e6fEric Laurent#include <stdint.h> 225fe37c6838de9fbd959ad19ba44aa3d00d1b4e6fEric Laurent#include <stddef.h> 235fe37c6838de9fbd959ad19ba44aa3d00d1b4e6fEric Laurent 245fe37c6838de9fbd959ad19ba44aa3d00d1b4e6fEric Laurentnamespace android { 255fe37c6838de9fbd959ad19ba44aa3d00d1b4e6fEric Laurent 265fe37c6838de9fbd959ad19ba44aa3d00d1b4e6fEric Laurent// Audio coefficient type. 275fe37c6838de9fbd959ad19ba44aa3d00d1b4e6fEric Laurenttypedef int32_t audio_coef_t; 285fe37c6838de9fbd959ad19ba44aa3d00d1b4e6fEric Laurent// Audio sample type. 295fe37c6838de9fbd959ad19ba44aa3d00d1b4e6fEric Laurenttypedef int32_t audio_sample_t; 305fe37c6838de9fbd959ad19ba44aa3d00d1b4e6fEric Laurent// Accumulator type for coef x sample. 315fe37c6838de9fbd959ad19ba44aa3d00d1b4e6fEric Laurenttypedef int64_t audio_coef_sample_acc_t; 325fe37c6838de9fbd959ad19ba44aa3d00d1b4e6fEric Laurent 335fe37c6838de9fbd959ad19ba44aa3d00d1b4e6fEric Laurent// Number of fraction bits for audio coefficient. 345fe37c6838de9fbd959ad19ba44aa3d00d1b4e6fEric Laurentstatic const int AUDIO_COEF_PRECISION = 24; 355fe37c6838de9fbd959ad19ba44aa3d00d1b4e6fEric Laurent// Audio coefficient with the value of 1.0 365fe37c6838de9fbd959ad19ba44aa3d00d1b4e6fEric Laurentstatic const audio_coef_t AUDIO_COEF_ONE = 1 << AUDIO_COEF_PRECISION; 375fe37c6838de9fbd959ad19ba44aa3d00d1b4e6fEric Laurent// Audio coefficient with the value of 0.5 385fe37c6838de9fbd959ad19ba44aa3d00d1b4e6fEric Laurentstatic const audio_coef_t AUDIO_COEF_HALF = 1 << (AUDIO_COEF_PRECISION - 1); 395fe37c6838de9fbd959ad19ba44aa3d00d1b4e6fEric Laurent// Number of fraction bits for audio sample. 405fe37c6838de9fbd959ad19ba44aa3d00d1b4e6fEric Laurentstatic const int AUDIO_SAMPLE_PRECISION = 24; 415fe37c6838de9fbd959ad19ba44aa3d00d1b4e6fEric Laurent// Audio sample with the value of 1.0 425fe37c6838de9fbd959ad19ba44aa3d00d1b4e6fEric Laurentstatic const audio_sample_t AUDIO_SAMPLE_ONE = 1 << AUDIO_SAMPLE_PRECISION; 435fe37c6838de9fbd959ad19ba44aa3d00d1b4e6fEric Laurent 445fe37c6838de9fbd959ad19ba44aa3d00d1b4e6fEric Laurent// TODO: These are just temporary naive implementations of the necessary 455fe37c6838de9fbd959ad19ba44aa3d00d1b4e6fEric Laurent// arithmetic operations needed for the filter. They should be moved to a more 465fe37c6838de9fbd959ad19ba44aa3d00d1b4e6fEric Laurent// generic location and implemented more efficiently. 475fe37c6838de9fbd959ad19ba44aa3d00d1b4e6fEric Laurent 485fe37c6838de9fbd959ad19ba44aa3d00d1b4e6fEric Laurent// Multiply a sample by a coefficient to return an accumulator. 495fe37c6838de9fbd959ad19ba44aa3d00d1b4e6fEric Laurentinline audio_coef_sample_acc_t mul_coef_sample(audio_coef_t x, audio_sample_t y) { 505fe37c6838de9fbd959ad19ba44aa3d00d1b4e6fEric Laurent return ((audio_coef_sample_acc_t) (x)) * y; 515fe37c6838de9fbd959ad19ba44aa3d00d1b4e6fEric Laurent} 525fe37c6838de9fbd959ad19ba44aa3d00d1b4e6fEric Laurent 535fe37c6838de9fbd959ad19ba44aa3d00d1b4e6fEric Laurent// Multiply and accumulate sample by a coefficient to return an accumulator. 545fe37c6838de9fbd959ad19ba44aa3d00d1b4e6fEric Laurentinline audio_coef_sample_acc_t mac_coef_sample(audio_coef_t x, audio_sample_t y, audio_coef_sample_acc_t acc) { 555fe37c6838de9fbd959ad19ba44aa3d00d1b4e6fEric Laurent return acc + ((audio_coef_sample_acc_t) (x)) * y; 565fe37c6838de9fbd959ad19ba44aa3d00d1b4e6fEric Laurent} 575fe37c6838de9fbd959ad19ba44aa3d00d1b4e6fEric Laurent 585fe37c6838de9fbd959ad19ba44aa3d00d1b4e6fEric Laurent// Convert a sample-coefficient accumulator to a sample. 595fe37c6838de9fbd959ad19ba44aa3d00d1b4e6fEric Laurentinline audio_sample_t coef_sample_acc_to_sample(audio_coef_sample_acc_t acc) { 605fe37c6838de9fbd959ad19ba44aa3d00d1b4e6fEric Laurent if (acc < 0) { 615fe37c6838de9fbd959ad19ba44aa3d00d1b4e6fEric Laurent acc += AUDIO_COEF_ONE - 1; 625fe37c6838de9fbd959ad19ba44aa3d00d1b4e6fEric Laurent } 635fe37c6838de9fbd959ad19ba44aa3d00d1b4e6fEric Laurent return (audio_sample_t) (acc >> AUDIO_COEF_PRECISION); 645fe37c6838de9fbd959ad19ba44aa3d00d1b4e6fEric Laurent} 655fe37c6838de9fbd959ad19ba44aa3d00d1b4e6fEric Laurent 665fe37c6838de9fbd959ad19ba44aa3d00d1b4e6fEric Laurent// Convert a S15 sample to audio_sample_t 675fe37c6838de9fbd959ad19ba44aa3d00d1b4e6fEric Laurentinline audio_sample_t s15_to_audio_sample_t(int16_t s15) { 685fe37c6838de9fbd959ad19ba44aa3d00d1b4e6fEric Laurent return audio_sample_t(s15) << 9; 695fe37c6838de9fbd959ad19ba44aa3d00d1b4e6fEric Laurent} 705fe37c6838de9fbd959ad19ba44aa3d00d1b4e6fEric Laurent 715fe37c6838de9fbd959ad19ba44aa3d00d1b4e6fEric Laurent// Convert a audio_sample_t sample to S15 (no clipping) 725fe37c6838de9fbd959ad19ba44aa3d00d1b4e6fEric Laurentinline int16_t audio_sample_t_to_s15(audio_sample_t sample) { 735fe37c6838de9fbd959ad19ba44aa3d00d1b4e6fEric Laurent return int16_t((sample + (1 << 8)) >> 9); 745fe37c6838de9fbd959ad19ba44aa3d00d1b4e6fEric Laurent} 755fe37c6838de9fbd959ad19ba44aa3d00d1b4e6fEric Laurent 765fe37c6838de9fbd959ad19ba44aa3d00d1b4e6fEric Laurent// Convert a audio_sample_t sample to S15 (with clipping) 775fe37c6838de9fbd959ad19ba44aa3d00d1b4e6fEric Laurentinline int16_t audio_sample_t_to_s15_clip(audio_sample_t sample) { 785fe37c6838de9fbd959ad19ba44aa3d00d1b4e6fEric Laurent // TODO: optimize for targets supporting this as an atomic operation. 795fe37c6838de9fbd959ad19ba44aa3d00d1b4e6fEric Laurent if (__builtin_expect(sample >= (0x7FFF << 9), 0)) { 805fe37c6838de9fbd959ad19ba44aa3d00d1b4e6fEric Laurent return 0x7FFF; 815fe37c6838de9fbd959ad19ba44aa3d00d1b4e6fEric Laurent } else if (__builtin_expect(sample <= -(0x8000 << 9), 0)) { 825fe37c6838de9fbd959ad19ba44aa3d00d1b4e6fEric Laurent return 0x8000; 835fe37c6838de9fbd959ad19ba44aa3d00d1b4e6fEric Laurent } else { 845fe37c6838de9fbd959ad19ba44aa3d00d1b4e6fEric Laurent return audio_sample_t_to_s15(sample); 855fe37c6838de9fbd959ad19ba44aa3d00d1b4e6fEric Laurent } 865fe37c6838de9fbd959ad19ba44aa3d00d1b4e6fEric Laurent} 875fe37c6838de9fbd959ad19ba44aa3d00d1b4e6fEric Laurent 885fe37c6838de9fbd959ad19ba44aa3d00d1b4e6fEric Laurent//////////////////////////////////////////////////////////////////////////////// 895fe37c6838de9fbd959ad19ba44aa3d00d1b4e6fEric Laurent 905fe37c6838de9fbd959ad19ba44aa3d00d1b4e6fEric Laurent} 915fe37c6838de9fbd959ad19ba44aa3d00d1b4e6fEric Laurent 925fe37c6838de9fbd959ad19ba44aa3d00d1b4e6fEric Laurent#endif // ANDROID_AUDIO_COMMON_H 93