🛠️🐜 Antkeeper superbuild with dependencies included https://antkeeper.com
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.

367 lines
12 KiB

  1. /*
  2. * Sinc interpolator coefficient and delta generator for the OpenAL Soft
  3. * cross platform audio library.
  4. *
  5. * Copyright (C) 2015 by Christopher Fitzgerald.
  6. *
  7. * This library is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU Library General Public
  9. * License as published by the Free Software Foundation; either
  10. * version 2 of the License, or (at your option) any later version.
  11. *
  12. * This library is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Library General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Library General Public
  18. * License along with this library; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
  20. * MA 02110-1301 USA
  21. *
  22. * Or visit: http://www.gnu.org/licenses/old-licenses/lgpl-2.0.html
  23. *
  24. * --------------------------------------------------------------------------
  25. *
  26. * This is a modified version of the bandlimited windowed sinc interpolator
  27. * algorithm presented here:
  28. *
  29. * Smith, J.O. "Windowed Sinc Interpolation", in
  30. * Physical Audio Signal Processing,
  31. * https://ccrma.stanford.edu/~jos/pasp/Windowed_Sinc_Interpolation.html,
  32. * online book,
  33. * accessed October 2012.
  34. */
  35. #define _UNICODE
  36. #include <stdio.h>
  37. #include <math.h>
  38. #include <string.h>
  39. #include <stdlib.h>
  40. #include "../common/win_main_utf8.h"
  41. #ifndef M_PI
  42. #define M_PI (3.14159265358979323846)
  43. #endif
  44. #if !(defined(_ISOC99_SOURCE) || (defined(_POSIX_C_SOURCE) && _POSIX_C_SOURCE >= 200112L))
  45. #define log2(x) (log(x) / log(2.0))
  46. #endif
  47. /* Same as in alu.h! */
  48. #define FRACTIONBITS (12)
  49. #define FRACTIONONE (1<<FRACTIONBITS)
  50. // The number of distinct scale and phase intervals within the filter table.
  51. // Must be the same as in alu.h!
  52. #define BSINC_SCALE_COUNT (16)
  53. #define BSINC_PHASE_COUNT (16)
  54. /* 48 points includes the doubling for downsampling, so the maximum number of
  55. * base sample points is 24, which is 23rd order.
  56. */
  57. #define BSINC_POINTS_MAX (48)
  58. static double MinDouble(double a, double b)
  59. { return (a <= b) ? a : b; }
  60. static double MaxDouble(double a, double b)
  61. { return (a >= b) ? a : b; }
  62. /* NOTE: This is the normalized (instead of just sin(x)/x) cardinal sine
  63. * function.
  64. * 2 f_t sinc(2 f_t x)
  65. * f_t -- normalized transition frequency (0.5 is nyquist)
  66. * x -- sample index (-N to N)
  67. */
  68. static double Sinc(const double x)
  69. {
  70. if(fabs(x) < 1e-15)
  71. return 1.0;
  72. return sin(M_PI * x) / (M_PI * x);
  73. }
  74. static double BesselI_0(const double x)
  75. {
  76. double term, sum, last_sum, x2, y;
  77. int i;
  78. term = 1.0;
  79. sum = 1.0;
  80. x2 = x / 2.0;
  81. i = 1;
  82. do {
  83. y = x2 / i;
  84. i++;
  85. last_sum = sum;
  86. term *= y * y;
  87. sum += term;
  88. } while(sum != last_sum);
  89. return sum;
  90. }
  91. /* NOTE: k is assumed normalized (-1 to 1)
  92. * beta is equivalent to 2 alpha
  93. */
  94. static double Kaiser(const double b, const double k)
  95. {
  96. if(!(k >= -1.0 && k <= 1.0))
  97. return 0.0;
  98. return BesselI_0(b * sqrt(1.0 - k*k)) / BesselI_0(b);
  99. }
  100. /* Calculates the (normalized frequency) transition width of the Kaiser window.
  101. * Rejection is in dB.
  102. */
  103. static double CalcKaiserWidth(const double rejection, const int order)
  104. {
  105. double w_t = 2.0 * M_PI;
  106. if(rejection > 21.0)
  107. return (rejection - 7.95) / (order * 2.285 * w_t);
  108. /* This enforces a minimum rejection of just above 21.18dB */
  109. return 5.79 / (order * w_t);
  110. }
  111. static double CalcKaiserBeta(const double rejection)
  112. {
  113. if(rejection > 50.0)
  114. return 0.1102 * (rejection - 8.7);
  115. else if(rejection >= 21.0)
  116. return (0.5842 * pow(rejection - 21.0, 0.4)) +
  117. (0.07886 * (rejection - 21.0));
  118. return 0.0;
  119. }
  120. /* Generates the coefficient, delta, and index tables required by the bsinc resampler */
  121. static void BsiGenerateTables(FILE *output, const char *tabname, const double rejection, const int order)
  122. {
  123. static double filter[BSINC_SCALE_COUNT][BSINC_PHASE_COUNT + 1][BSINC_POINTS_MAX];
  124. static double scDeltas[BSINC_SCALE_COUNT][BSINC_PHASE_COUNT ][BSINC_POINTS_MAX];
  125. static double phDeltas[BSINC_SCALE_COUNT][BSINC_PHASE_COUNT + 1][BSINC_POINTS_MAX];
  126. static double spDeltas[BSINC_SCALE_COUNT][BSINC_PHASE_COUNT ][BSINC_POINTS_MAX];
  127. static int mt[BSINC_SCALE_COUNT];
  128. static double at[BSINC_SCALE_COUNT];
  129. const int num_points_min = order + 1;
  130. double width, beta, scaleBase, scaleRange;
  131. int si, pi, i;
  132. memset(filter, 0, sizeof(filter));
  133. memset(scDeltas, 0, sizeof(scDeltas));
  134. memset(phDeltas, 0, sizeof(phDeltas));
  135. memset(spDeltas, 0, sizeof(spDeltas));
  136. /* Calculate windowing parameters. The width describes the transition
  137. band, but it may vary due to the linear interpolation between scales
  138. of the filter.
  139. */
  140. width = CalcKaiserWidth(rejection, order);
  141. beta = CalcKaiserBeta(rejection);
  142. scaleBase = width / 2.0;
  143. scaleRange = 1.0 - scaleBase;
  144. // Determine filter scaling.
  145. for(si = 0; si < BSINC_SCALE_COUNT; si++)
  146. {
  147. const double scale = scaleBase + (scaleRange * si / (BSINC_SCALE_COUNT - 1));
  148. const double a = MinDouble(floor(num_points_min / (2.0 * scale)), num_points_min);
  149. const int m = 2 * (int)a;
  150. mt[si] = m;
  151. at[si] = a;
  152. }
  153. /* Calculate the Kaiser-windowed Sinc filter coefficients for each scale
  154. and phase.
  155. */
  156. for(si = 0; si < BSINC_SCALE_COUNT; si++)
  157. {
  158. const int m = mt[si];
  159. const int o = num_points_min - (m / 2);
  160. const int l = (m / 2) - 1;
  161. const double a = at[si];
  162. const double scale = scaleBase + (scaleRange * si / (BSINC_SCALE_COUNT - 1));
  163. const double cutoff = (0.5 * scale) - (scaleBase * MaxDouble(0.5, scale));
  164. for(pi = 0; pi <= BSINC_PHASE_COUNT; pi++)
  165. {
  166. const double phase = l + ((double)pi / BSINC_PHASE_COUNT);
  167. for(i = 0; i < m; i++)
  168. {
  169. const double x = i - phase;
  170. filter[si][pi][o + i] = Kaiser(beta, x / a) * 2.0 * cutoff * Sinc(2.0 * cutoff * x);
  171. }
  172. }
  173. }
  174. /* Linear interpolation between scales is simplified by pre-calculating
  175. the delta (b - a) in: x = a + f (b - a)
  176. Given a difference in points between scales, the destination points
  177. will be 0, thus: x = a + f (-a)
  178. */
  179. for(si = 0; si < (BSINC_SCALE_COUNT - 1); si++)
  180. {
  181. const int m = mt[si];
  182. const int o = num_points_min - (m / 2);
  183. for(pi = 0; pi < BSINC_PHASE_COUNT; pi++)
  184. {
  185. for(i = 0; i < m; i++)
  186. scDeltas[si][pi][o + i] = filter[si + 1][pi][o + i] - filter[si][pi][o + i];
  187. }
  188. }
  189. // Linear interpolation between phases is also simplified.
  190. for(si = 0; si < BSINC_SCALE_COUNT; si++)
  191. {
  192. const int m = mt[si];
  193. const int o = num_points_min - (m / 2);
  194. for(pi = 0; pi < BSINC_PHASE_COUNT; pi++)
  195. {
  196. for(i = 0; i < m; i++)
  197. phDeltas[si][pi][o + i] = filter[si][pi + 1][o + i] - filter[si][pi][o + i];
  198. }
  199. }
  200. /* This last simplification is done to complete the bilinear equation for
  201. the combination of scale and phase.
  202. */
  203. for(si = 0; si < (BSINC_SCALE_COUNT - 1); si++)
  204. {
  205. const int m = mt[si];
  206. const int o = num_points_min - (m / 2);
  207. for(pi = 0; pi < BSINC_PHASE_COUNT; pi++)
  208. {
  209. for(i = 0; i < m; i++)
  210. spDeltas[si][pi][o + i] = phDeltas[si + 1][pi][o + i] - phDeltas[si][pi][o + i];
  211. }
  212. }
  213. // Make sure the number of points is a multiple of 4 (for SIMD).
  214. for(si = 0; si < BSINC_SCALE_COUNT; si++)
  215. mt[si] = (mt[si]+3) & ~3;
  216. // Calculate the table size.
  217. i = 0;
  218. for(si = 0; si < BSINC_SCALE_COUNT; si++)
  219. i += 4 * BSINC_PHASE_COUNT * mt[si];
  220. fprintf(output,
  221. "/* This %d%s order filter has a rejection of -%.0fdB, yielding a transition width\n"
  222. " * of ~%.3f (normalized frequency). Order increases when downsampling to a\n"
  223. " * limit of one octave, after which the quality of the filter (transition\n"
  224. " * width) suffers to reduce the CPU cost. The bandlimiting will cut all sound\n"
  225. " * after downsampling by ~%.2f octaves.\n"
  226. " */\n"
  227. "alignas(16) static constexpr float %s_tab[%d] = {\n",
  228. order, (((order%100)/10) == 1) ? "th" :
  229. ((order%10) == 1) ? "st" :
  230. ((order%10) == 2) ? "nd" :
  231. ((order%10) == 3) ? "rd" : "th",
  232. rejection, width, log2(1.0/scaleBase), tabname, i);
  233. for(si = 0; si < BSINC_SCALE_COUNT; si++)
  234. {
  235. const int m = mt[si];
  236. const int o = num_points_min - (m / 2);
  237. for(pi = 0; pi < BSINC_PHASE_COUNT; pi++)
  238. {
  239. fprintf(output, " /* %2d,%2d (%d) */", si, pi, m);
  240. fprintf(output, "\n ");
  241. for(i = 0; i < m; i++)
  242. fprintf(output, " %+14.9ef,", filter[si][pi][o + i]);
  243. fprintf(output, "\n ");
  244. for(i = 0; i < m; i++)
  245. fprintf(output, " %+14.9ef,", scDeltas[si][pi][o + i]);
  246. fprintf(output, "\n ");
  247. for(i = 0; i < m; i++)
  248. fprintf(output, " %+14.9ef,", phDeltas[si][pi][o + i]);
  249. fprintf(output, "\n ");
  250. for(i = 0; i < m; i++)
  251. fprintf(output, " %+14.9ef,", spDeltas[si][pi][o + i]);
  252. fprintf(output, "\n");
  253. }
  254. }
  255. fprintf(output, "};\nconst BSincTable %s = {\n", tabname);
  256. /* The scaleBase is calculated from the Kaiser window transition width.
  257. It represents the absolute limit to the filter before it fully cuts
  258. the signal. The limit in octaves can be calculated by taking the
  259. base-2 logarithm of its inverse: log_2(1 / scaleBase)
  260. */
  261. fprintf(output, " /* scaleBase */ %.9ef, /* scaleRange */ %.9ef,\n", scaleBase, 1.0 / scaleRange);
  262. fprintf(output, " /* m */ {");
  263. fprintf(output, " %d", mt[0]);
  264. for(si = 1; si < BSINC_SCALE_COUNT; si++)
  265. fprintf(output, ", %d", mt[si]);
  266. fprintf(output, " },\n");
  267. fprintf(output, " /* filterOffset */ {");
  268. fprintf(output, " %d", 0);
  269. i = mt[0]*4*BSINC_PHASE_COUNT;
  270. for(si = 1; si < BSINC_SCALE_COUNT; si++)
  271. {
  272. fprintf(output, ", %d", i);
  273. i += mt[si]*4*BSINC_PHASE_COUNT;
  274. }
  275. fprintf(output, " },\n");
  276. fprintf(output, " %s_tab\n", tabname);
  277. fprintf(output, "};\n\n");
  278. }
  279. int main(int argc, char *argv[])
  280. {
  281. FILE *output;
  282. GET_UNICODE_ARGS(&argc, &argv);
  283. if(argc > 2)
  284. {
  285. fprintf(stderr, "Usage: %s [output file]\n", argv[0]);
  286. return 1;
  287. }
  288. if(argc == 2)
  289. {
  290. output = fopen(argv[1], "wb");
  291. if(!output)
  292. {
  293. fprintf(stderr, "Failed to open %s for writing\n", argv[1]);
  294. return 1;
  295. }
  296. }
  297. else
  298. output = stdout;
  299. fprintf(output, "/* Generated by bsincgen, do not edit! */\n\n"
  300. "static_assert(BSINC_SCALE_COUNT == %d, \"Unexpected BSINC_SCALE_COUNT value!\");\n"
  301. "static_assert(BSINC_PHASE_COUNT == %d, \"Unexpected BSINC_PHASE_COUNT value!\");\n"
  302. "static_assert(FRACTIONONE == %d, \"Unexpected FRACTIONONE value!\");\n\n"
  303. "struct BSincTable {\n"
  304. " const float scaleBase, scaleRange;\n"
  305. " const int m[BSINC_SCALE_COUNT];\n"
  306. " const int filterOffset[BSINC_SCALE_COUNT];\n"
  307. " const float *Tab;\n"
  308. "};\n\n", BSINC_SCALE_COUNT, BSINC_PHASE_COUNT, FRACTIONONE);
  309. /* A 23rd order filter with a -60dB drop at nyquist. */
  310. BsiGenerateTables(output, "bsinc24", 60.0, 23);
  311. /* An 11th order filter with a -40dB drop at nyquist. */
  312. BsiGenerateTables(output, "bsinc12", 40.0, 11);
  313. if(output != stdout)
  314. fclose(output);
  315. output = NULL;
  316. return 0;
  317. }