MicroDexed is a compatible 6-operator-FM-synth based on the Teensy(-3.6/-4.0) Microcontroller. https://www.parasitstudio.de
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.

284 lines
9.1KB

  1. /*
  2. * Copyright 2012 Google Inc.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #include <math.h>
  17. #include <cstdlib>
  18. #ifdef HAVE_NEON
  19. #include <cpu-features.h>
  20. #endif
  21. #include "synth.h"
  22. #include "sin.h"
  23. #include "fm_op_kernel.h"
  24. #ifdef HAVE_NEONx
  25. static bool hasNeon() {
  26. return true;
  27. return (android_getCpuFeatures() & ANDROID_CPU_ARM_FEATURE_NEON) != 0;
  28. }
  29. extern "C"
  30. void neon_fm_kernel(const int *in, const int *busin, int *out, int count,
  31. int32_t phase0, int32_t freq, int32_t gain1, int32_t dgain);
  32. const int32_t __attribute__ ((aligned(16))) zeros[N] = {0};
  33. #else
  34. static bool hasNeon() {
  35. return false;
  36. }
  37. #endif
  38. void FmOpKernel::compute(int32_t *output, const int32_t *input,
  39. int32_t phase0, int32_t freq,
  40. int32_t gain1, int32_t gain2, bool add) {
  41. int32_t dgain = (gain2 - gain1 + (N >> 1)) >> LG_N;
  42. int32_t gain = gain1;
  43. int32_t phase = phase0;
  44. if (hasNeon()) {
  45. #ifdef HAVE_NEON
  46. neon_fm_kernel(input, add ? output : zeros, output, _N_,
  47. phase0, freq, gain, dgain);
  48. #endif
  49. } else {
  50. if (add) {
  51. for (int i = 0; i < _N_; i++) {
  52. gain += dgain;
  53. int32_t y = Sin::lookup(phase + input[i]);
  54. int32_t y1 = ((int64_t)y * (int64_t)gain) >> 24;
  55. output[i] += y1;
  56. phase += freq;
  57. }
  58. } else {
  59. for (int i = 0; i < _N_; i++) {
  60. gain += dgain;
  61. int32_t y = Sin::lookup(phase + input[i]);
  62. int32_t y1 = ((int64_t)y * (int64_t)gain) >> 24;
  63. output[i] = y1;
  64. phase += freq;
  65. }
  66. }
  67. }
  68. }
  69. void FmOpKernel::compute_pure(int32_t *output, int32_t phase0, int32_t freq,
  70. int32_t gain1, int32_t gain2, bool add) {
  71. int32_t dgain = (gain2 - gain1 + (N >> 1)) >> LG_N;
  72. int32_t gain = gain1;
  73. int32_t phase = phase0;
  74. if (hasNeon()) {
  75. #ifdef HAVE_NEON
  76. neon_fm_kernel(zeros, add ? output : zeros, output, _N_,
  77. phase0, freq, gain, dgain);
  78. #endif
  79. } else {
  80. if (add) {
  81. for (int i = 0; i < _N_; i++) {
  82. gain += dgain;
  83. int32_t y = Sin::lookup(phase);
  84. int32_t y1 = ((int64_t)y * (int64_t)gain) >> 24;
  85. output[i] += y1;
  86. phase += freq;
  87. }
  88. } else {
  89. for (int i = 0; i < _N_; i++) {
  90. gain += dgain;
  91. int32_t y = Sin::lookup(phase);
  92. int32_t y1 = ((int64_t)y * (int64_t)gain) >> 24;
  93. output[i] = y1;
  94. phase += freq;
  95. }
  96. }
  97. }
  98. }
  99. #define noDOUBLE_ACCURACY
  100. #define HIGH_ACCURACY
  101. void FmOpKernel::compute_fb(int32_t *output, int32_t phase0, int32_t freq,
  102. int32_t gain1, int32_t gain2,
  103. int32_t *fb_buf, int fb_shift, bool add) {
  104. int32_t dgain = (gain2 - gain1 + (N >> 1)) >> LG_N;
  105. int32_t gain = gain1;
  106. int32_t phase = phase0;
  107. int32_t y0 = fb_buf[0];
  108. int32_t y = fb_buf[1];
  109. if (add) {
  110. for (int i = 0; i < _N_; i++) {
  111. gain += dgain;
  112. int32_t scaled_fb = (y0 + y) >> (fb_shift + 1);
  113. y0 = y;
  114. y = Sin::lookup(phase + scaled_fb);
  115. y = ((int64_t)y * (int64_t)gain) >> 24;
  116. output[i] += y;
  117. phase += freq;
  118. }
  119. } else {
  120. for (int i = 0; i < _N_; i++) {
  121. gain += dgain;
  122. int32_t scaled_fb = (y0 + y) >> (fb_shift + 1);
  123. y0 = y;
  124. y = Sin::lookup(phase + scaled_fb);
  125. y = ((int64_t)y * (int64_t)gain) >> 24;
  126. output[i] = y;
  127. phase += freq;
  128. }
  129. }
  130. fb_buf[0] = y0;
  131. fb_buf[1] = y;
  132. }
  133. ////////////////////////////////////////////////////////////////////////////////////
  134. ////////////////////////////////////////////////////////////////////////////////////
  135. ////////////////////////////////////////////////////////////////////////////////////
  136. ////////////////////////////////////////////////////////////////////////////////////
  137. // Experimental sine wave generators below
  138. #if 0
  139. // Results: accuracy 64.3 mean, 170 worst case
  140. // high accuracy: 5.0 mean, 49 worst case
  141. void FmOpKernel::compute_pure(int32_t *output, int32_t phase0, int32_t freq,
  142. int32_t gain1, int32_t gain2, bool add) {
  143. int32_t dgain = (gain2 - gain1 + (N >> 1)) >> LG_N;
  144. int32_t gain = gain1;
  145. int32_t phase = phase0;
  146. #ifdef HIGH_ACCURACY
  147. int32_t u = Sin::compute10(phase << 6);
  148. u = ((int64_t)u * gain) >> 30;
  149. int32_t v = Sin::compute10((phase << 6) + (1 << 28)); // quarter cycle
  150. v = ((int64_t)v * gain) >> 30;
  151. int32_t s = Sin::compute10(freq << 6);
  152. int32_t c = Sin::compute10((freq << 6) + (1 << 28));
  153. #else
  154. int32_t u = Sin::compute(phase);
  155. u = ((int64_t)u * gain) >> 24;
  156. int32_t v = Sin::compute(phase + (1 << 22)); // quarter cycle
  157. v = ((int64_t)v * gain) >> 24;
  158. int32_t s = Sin::compute(freq) << 6;
  159. int32_t c = Sin::compute(freq + (1 << 22)) << 6;
  160. #endif
  161. for (int i = 0; i < _N_; i++) {
  162. output[i] = u;
  163. int32_t t = ((int64_t)v * (int64_t)c - (int64_t)u * (int64_t)s) >> 30;
  164. u = ((int64_t)u * (int64_t)c + (int64_t)v * (int64_t)s) >> 30;
  165. v = t;
  166. }
  167. }
  168. #endif
  169. #if 0
  170. // Results: accuracy 392.3 mean, 15190 worst case (near freq = 0.5)
  171. // for freq < 0.25, 275.2 mean, 716 worst
  172. // high accuracy: 57.4 mean, 7559 worst
  173. // freq < 0.25: 17.9 mean, 78 worst
  174. void FmOpKernel::compute_pure(int32_t *output, int32_t phase0, int32_t freq,
  175. int32_t gain1, int32_t gain2, bool add) {
  176. int32_t dgain = (gain2 - gain1 + (N >> 1)) >> LG_N;
  177. int32_t gain = gain1;
  178. int32_t phase = phase0;
  179. #ifdef HIGH_ACCURACY
  180. int32_t u = floor(gain * sin(phase * (M_PI / (1 << 23))) + 0.5);
  181. int32_t v = floor(gain * cos((phase - freq * 0.5) * (M_PI / (1 << 23))) + 0.5);
  182. int32_t a = floor((1 << 25) * sin(freq * (M_PI / (1 << 24))) + 0.5);
  183. #else
  184. int32_t u = Sin::compute(phase);
  185. u = ((int64_t)u * gain) >> 24;
  186. int32_t v = Sin::compute(phase + (1 << 22) - (freq >> 1));
  187. v = ((int64_t)v * gain) >> 24;
  188. int32_t a = Sin::compute(freq >> 1) << 1;
  189. #endif
  190. for (int i = 0; i < _N_; i++) {
  191. output[i] = u;
  192. v -= ((int64_t)a * (int64_t)u) >> 24;
  193. u += ((int64_t)a * (int64_t)v) >> 24;
  194. }
  195. }
  196. #endif
  197. #if 0
  198. // Results: accuracy 370.0 mean, 15480 worst case (near freq = 0.5)
  199. // with FRAC_NUM accuracy initialization: mean 1.55, worst 58 (near freq = 0)
  200. // with high accuracy: mean 4.2, worst 292 (near freq = 0.5)
  201. void FmOpKernel::compute_pure(int32_t *output, int32_t phase0, int32_t freq,
  202. int32_t gain1, int32_t gain2, bool add) {
  203. int32_t dgain = (gain2 - gain1 + (N >> 1)) >> LG_N;
  204. int32_t gain = gain1;
  205. int32_t phase = phase0;
  206. #ifdef DOUBLE_ACCURACY
  207. int32_t u = floor((1 << 30) * sin(phase * (M_PI / (1 << 23))) + 0.5);
  208. FRAC_NUM a_d = sin(freq * (M_PI / (1 << 24)));
  209. int32_t v = floor((1LL << 31) * a_d * cos((phase - freq * 0.5) *
  210. (M_PI / (1 << 23))) + 0.5);
  211. int32_t aa = floor((1LL << 31) * a_d * a_d + 0.5);
  212. #else
  213. #ifdef HIGH_ACCURACY
  214. int32_t u = Sin::compute10(phase << 6);
  215. int32_t v = Sin::compute10((phase << 6) + (1 << 28) - (freq << 5));
  216. int32_t a = Sin::compute10(freq << 5);
  217. v = ((int64_t)v * (int64_t)a) >> 29;
  218. int32_t aa = ((int64_t)a * (int64_t)a) >> 29;
  219. #else
  220. int32_t u = Sin::compute(phase) << 6;
  221. int32_t v = Sin::compute(phase + (1 << 22) - (freq >> 1));
  222. int32_t a = Sin::compute(freq >> 1);
  223. v = ((int64_t)v * (int64_t)a) >> 17;
  224. int32_t aa = ((int64_t)a * (int64_t)a) >> 17;
  225. #endif
  226. #endif
  227. if (aa < 0) aa = (1 << 31) - 1;
  228. for (int i = 0; i < _N_; i++) {
  229. gain += dgain;
  230. output[i] = ((int64_t)u * (int64_t)gain) >> 30;
  231. v -= ((int64_t)aa * (int64_t)u) >> 29;
  232. u += v;
  233. }
  234. }
  235. #endif
  236. #if 0
  237. // Results:: accuracy 112.3 mean, 4262 worst (near freq = 0.5)
  238. // high accuracy 2.9 mean, 143 worst
  239. void FmOpKernel::compute_pure(int32_t *output, int32_t phase0, int32_t freq,
  240. int32_t gain1, int32_t gain2, bool add) {
  241. int32_t dgain = (gain2 - gain1 + (N >> 1)) >> LG_N;
  242. int32_t gain = gain1;
  243. int32_t phase = phase0;
  244. #ifdef HIGH_ACCURACY
  245. int32_t u = Sin::compute10(phase << 6);
  246. int32_t lastu = Sin::compute10((phase - freq) << 6);
  247. int32_t a = Sin::compute10((freq << 6) + (1 << 28)) << 1;
  248. #else
  249. int32_t u = Sin::compute(phase) << 6;
  250. int32_t lastu = Sin::compute(phase - freq) << 6;
  251. int32_t a = Sin::compute(freq + (1 << 22)) << 7;
  252. #endif
  253. if (a < 0 && freq < 256) a = (1 << 31) - 1;
  254. if (a > 0 && freq > 0x7fff00) a = -(1 << 31);
  255. for (int i = 0; i < _N_; i++) {
  256. gain += dgain;
  257. output[i] = ((int64_t)u * (int64_t)gain) >> 30;
  258. //output[i] = u;
  259. int32_t newu = (((int64_t)u * (int64_t)a) >> 30) - lastu;
  260. lastu = u;
  261. u = newu;
  262. }
  263. }
  264. #endif