AudioCommon.h 3.2 KB

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