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.

179 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. namespace stmlib {
  33. inline int16_t Interpolate824(const int16_t* table, uint32_t phase)
  34. __attribute__((always_inline));
  35. inline uint16_t Interpolate824(const uint16_t* table, uint32_t phase)
  36. __attribute__((always_inline));
  37. inline int16_t Interpolate824(const uint8_t* table, uint32_t phase)
  38. __attribute__((always_inline));
  39. inline uint16_t Interpolate88(const uint16_t* table, uint16_t index)
  40. __attribute__((always_inline));
  41. inline int16_t Interpolate88(const int16_t* table, uint16_t index)
  42. __attribute__((always_inline));
  43. inline int16_t Interpolate1022(const int16_t* table, uint32_t phase)
  44. __attribute__((always_inline));
  45. inline int16_t Interpolate115(const int16_t* table, uint32_t phase)
  46. __attribute__((always_inline));
  47. inline int16_t Crossfade(
  48. const int16_t* table_a,
  49. const int16_t* table_b,
  50. uint32_t phase,
  51. uint16_t balance)
  52. __attribute__((always_inline));
  53. inline int16_t Crossfade(
  54. const uint8_t* table_a,
  55. const uint8_t* table_b,
  56. uint32_t phase,
  57. uint16_t balance)
  58. __attribute__((always_inline));
  59. inline int16_t Crossfade1022(
  60. const uint8_t* table_a,
  61. const uint8_t* table_b,
  62. uint32_t phase,
  63. uint16_t balance)
  64. __attribute__((always_inline));
  65. inline int16_t Crossfade115(
  66. const uint8_t* table_a,
  67. const uint8_t* table_b,
  68. uint16_t phase,
  69. uint16_t balance)
  70. __attribute__((always_inline));
  71. inline int16_t Mix(int16_t a, int16_t b, uint16_t balance) {
  72. return (a * (65535 - balance) + b * balance) >> 16;
  73. }
  74. inline uint16_t Mix(uint16_t a, uint16_t b, uint16_t balance) {
  75. return (a * (65535 - balance) + b * balance) >> 16;
  76. }
  77. inline int16_t Interpolate824(const int16_t* table, uint32_t phase) {
  78. int32_t a = table[phase >> 24];
  79. int32_t b = table[(phase >> 24) + 1];
  80. return a + ((b - a) * static_cast<int32_t>((phase >> 8) & 0xffff) >> 16);
  81. }
  82. inline uint16_t Interpolate824(const uint16_t* table, uint32_t phase) {
  83. uint32_t a = table[phase >> 24];
  84. uint32_t b = table[(phase >> 24) + 1];
  85. return a + ((b - a) * static_cast<uint32_t>((phase >> 8) & 0xffff) >> 16);
  86. }
  87. inline int16_t Interpolate824(const uint8_t* table, uint32_t phase) {
  88. int32_t a = table[phase >> 24];
  89. int32_t b = table[(phase >> 24) + 1];
  90. return (a << 8) + \
  91. ((b - a) * static_cast<int32_t>(phase & 0xffffff) >> 16) - 32768;
  92. }
  93. inline uint16_t Interpolate88(const uint16_t* table, uint16_t index) {
  94. int32_t a = table[index >> 8];
  95. int32_t b = table[(index >> 8) + 1];
  96. return a + ((b - a) * static_cast<int32_t>(index & 0xff) >> 8);
  97. }
  98. inline int16_t Interpolate88(const int16_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 Interpolate1022(const int16_t* table, uint32_t phase) {
  104. int32_t a = table[phase >> 22];
  105. int32_t b = table[(phase >> 22) + 1];
  106. return a + ((b - a) * static_cast<int32_t>((phase >> 6) & 0xffff) >> 16);
  107. }
  108. inline int16_t Interpolate115(const int16_t* table, uint16_t phase) {
  109. int32_t a = table[phase >> 5];
  110. int32_t b = table[(phase >> 5) + 1];
  111. return a + ((b - a) * static_cast<int32_t>(phase & 0x1f) >> 5);
  112. }
  113. inline int16_t Crossfade(
  114. const int16_t* table_a,
  115. const int16_t* table_b,
  116. uint32_t phase,
  117. uint16_t balance) {
  118. int32_t a = Interpolate824(table_a, phase);
  119. int32_t b = Interpolate824(table_b, phase);
  120. return a + ((b - a) * static_cast<int32_t>(balance) >> 16);
  121. }
  122. inline int16_t Crossfade(
  123. const uint8_t* table_a,
  124. const uint8_t* table_b,
  125. uint32_t phase,
  126. uint16_t balance) {
  127. int32_t a = Interpolate824(table_a, phase);
  128. int32_t b = Interpolate824(table_b, phase);
  129. return a + ((b - a) * static_cast<int32_t>(balance) >> 16);
  130. }
  131. inline int16_t Crossfade1022(
  132. const int16_t* table_a,
  133. const int16_t* table_b,
  134. uint32_t phase,
  135. uint16_t balance) {
  136. int32_t a = Interpolate1022(table_a, phase);
  137. int32_t b = Interpolate1022(table_b, phase);
  138. return a + ((b - a) * static_cast<int32_t>(balance) >> 16);
  139. }
  140. inline int16_t Crossfade115(
  141. const int16_t* table_a,
  142. const int16_t* table_b,
  143. uint16_t phase,
  144. uint16_t balance) {
  145. int32_t a = Interpolate115(table_a, phase);
  146. int32_t b = Interpolate115(table_b, phase);
  147. return a + ((b - a) * static_cast<int32_t>(balance) >> 16);
  148. }
  149. } // namespace stmlib
  150. #endif // STMLIB_UTILS_DSP_H_