You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

186 lines
5.7KB

  1. // Copyright 2012 Olivier Gillet.
  2. //
  3. // Author: Olivier Gillet (ol.gillet@gmail.com)
  4. //
  5. // Permission is hereby granted, free of charge, to any person obtaining a copy
  6. // of this software and associated documentation files (the "Software"), to deal
  7. // in the Software without restriction, including without limitation the rights
  8. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. // copies of the Software, and to permit persons to whom the Software is
  10. // furnished to do so, subject to the following conditions:
  11. //
  12. // The above copyright notice and this permission notice shall be included in
  13. // all copies or substantial portions of the Software.
  14. //
  15. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  20. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  21. // THE SOFTWARE.
  22. //
  23. // See http://creativecommons.org/licenses/MIT/ for more information.
  24. //
  25. // -----------------------------------------------------------------------------
  26. //
  27. // A set of basic operands, especially useful for fixed-point arithmetic, with
  28. // fast ASM implementations.
  29. #ifndef STMLIB_UTILS_DSP_H_
  30. #define STMLIB_UTILS_DSP_H_
  31. #include "stmlib/stmlib.h"
  32. #ifdef __GNUC__
  33. #define ALWAYS_INLINE __attribute__((always_inline))
  34. #else
  35. #define ALWAYS_INLINE
  36. #endif
  37. namespace stmlib {
  38. inline int16_t Interpolate824(const int16_t* table, uint32_t phase)
  39. ALWAYS_INLINE;
  40. inline uint16_t Interpolate824(const uint16_t* table, uint32_t phase)
  41. ALWAYS_INLINE;
  42. inline int16_t Interpolate824(const uint8_t* table, uint32_t phase)
  43. ALWAYS_INLINE;
  44. inline uint16_t Interpolate88(const uint16_t* table, uint16_t index)
  45. ALWAYS_INLINE;
  46. inline int16_t Interpolate88(const int16_t* table, uint16_t index)
  47. ALWAYS_INLINE;
  48. inline int16_t Interpolate1022(const int16_t* table, uint32_t phase)
  49. ALWAYS_INLINE;
  50. inline int16_t Interpolate115(const int16_t* table, uint32_t phase)
  51. ALWAYS_INLINE;
  52. inline int16_t Crossfade(
  53. const int16_t* table_a,
  54. const int16_t* table_b,
  55. uint32_t phase,
  56. uint16_t balance)
  57. ALWAYS_INLINE;
  58. inline int16_t Crossfade(
  59. const uint8_t* table_a,
  60. const uint8_t* table_b,
  61. uint32_t phase,
  62. uint16_t balance)
  63. ALWAYS_INLINE;
  64. inline int16_t Crossfade1022(
  65. const uint8_t* table_a,
  66. const uint8_t* table_b,
  67. uint32_t phase,
  68. uint16_t balance)
  69. ALWAYS_INLINE;
  70. inline int16_t Crossfade115(
  71. const uint8_t* table_a,
  72. const uint8_t* table_b,
  73. uint16_t phase,
  74. uint16_t balance)
  75. ALWAYS_INLINE;
  76. inline int16_t Mix(int16_t a, int16_t b, uint16_t balance) {
  77. return (a * (65535 - balance) + b * balance) >> 16;
  78. }
  79. inline uint16_t Mix(uint16_t a, uint16_t b, uint16_t balance) {
  80. return (a * (65535 - balance) + b * balance) >> 16;
  81. }
  82. inline int16_t Interpolate824(const int16_t* table, uint32_t phase) {
  83. int32_t a = table[phase >> 24];
  84. int32_t b = table[(phase >> 24) + 1];
  85. return a + ((b - a) * static_cast<int32_t>((phase >> 8) & 0xffff) >> 16);
  86. }
  87. inline uint16_t Interpolate824(const uint16_t* table, uint32_t phase) {
  88. uint32_t a = table[phase >> 24];
  89. uint32_t b = table[(phase >> 24) + 1];
  90. return a + ((b - a) * static_cast<uint32_t>((phase >> 8) & 0xffff) >> 16);
  91. }
  92. inline int16_t Interpolate824(const uint8_t* table, uint32_t phase) {
  93. int32_t a = table[phase >> 24];
  94. int32_t b = table[(phase >> 24) + 1];
  95. return (a << 8) + \
  96. ((b - a) * static_cast<int32_t>(phase & 0xffffff) >> 16) - 32768;
  97. }
  98. inline uint16_t Interpolate88(const uint16_t* table, uint16_t index) {
  99. int32_t a = table[index >> 8];
  100. int32_t b = table[(index >> 8) + 1];
  101. return a + ((b - a) * static_cast<int32_t>(index & 0xff) >> 8);
  102. }
  103. inline int16_t Interpolate88(const int16_t* table, uint16_t index) {
  104. int32_t a = table[index >> 8];
  105. int32_t b = table[(index >> 8) + 1];
  106. return a + ((b - a) * static_cast<int32_t>(index & 0xff) >> 8);
  107. }
  108. inline int16_t Interpolate1022(const int16_t* table, uint32_t phase) {
  109. int32_t a = table[phase >> 22];
  110. int32_t b = table[(phase >> 22) + 1];
  111. return a + ((b - a) * static_cast<int32_t>((phase >> 6) & 0xffff) >> 16);
  112. }
  113. inline int16_t Interpolate115(const int16_t* table, uint16_t phase) {
  114. int32_t a = table[phase >> 5];
  115. int32_t b = table[(phase >> 5) + 1];
  116. return a + ((b - a) * static_cast<int32_t>(phase & 0x1f) >> 5);
  117. }
  118. inline int16_t Crossfade(
  119. const int16_t* table_a,
  120. const int16_t* table_b,
  121. uint32_t phase,
  122. uint16_t balance) {
  123. int32_t a = Interpolate824(table_a, phase);
  124. int32_t b = Interpolate824(table_b, phase);
  125. return a + ((b - a) * static_cast<int32_t>(balance) >> 16);
  126. }
  127. inline int16_t Crossfade(
  128. const uint8_t* table_a,
  129. const uint8_t* table_b,
  130. uint32_t phase,
  131. uint16_t balance) {
  132. int32_t a = Interpolate824(table_a, phase);
  133. int32_t b = Interpolate824(table_b, phase);
  134. return a + ((b - a) * static_cast<int32_t>(balance) >> 16);
  135. }
  136. inline int16_t Crossfade1022(
  137. const int16_t* table_a,
  138. const int16_t* table_b,
  139. uint32_t phase,
  140. uint16_t balance) {
  141. int32_t a = Interpolate1022(table_a, phase);
  142. int32_t b = Interpolate1022(table_b, phase);
  143. return a + ((b - a) * static_cast<int32_t>(balance) >> 16);
  144. }
  145. inline int16_t Crossfade115(
  146. const int16_t* table_a,
  147. const int16_t* table_b,
  148. uint16_t phase,
  149. uint16_t balance) {
  150. int32_t a = Interpolate115(table_a, phase);
  151. int32_t b = Interpolate115(table_b, phase);
  152. return a + ((b - a) * static_cast<int32_t>(balance) >> 16);
  153. }
  154. } // namespace stmlib
  155. #endif // STMLIB_UTILS_DSP_H_