4
0

jsfx_ref.cpp 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. // Reference render of a JSFX script by the upstream ysfx library (pristine JoepVanlier/ysfx 5c3452fe), for LYNX's
  2. // --selftest (round 190pj). The input and the slider schedule match lynx::jsfx::selfTest's reference part exactly.
  3. // Build it against a pristine ysfx checkout (not LYNX's copy, so the reference is the library's own output):
  4. // cmake -S ysfx -B b -DYSFX_PLUGIN=OFF -DYSFX_GFX=OFF && cmake --build b
  5. // g++ -std=c++17 -O2 -Iysfx/include tools/jsfx_ref.cpp b/libysfx.a -lpthread -ldl -o jsfx_ref
  6. // ./jsfx_ref resources/jsfx/chokehold/Clipper/soft_clipper.jsfx src/engine/JsfxReference.h
  7. // usage: jsfx_ref <script.jsfx> <out.h>
  8. #include <ysfx.h>
  9. #include <cmath>
  10. #include <cstdint>
  11. #include <cstdio>
  12. #include <cstring>
  13. #include <vector>
  14. static void input (std::vector<float>& l, std::vector<float>& r, int frames, std::uint32_t& lcg, long start)
  15. {
  16. for (int i = 0; i < frames; ++i)
  17. {
  18. const double n = (double) (start + i);
  19. lcg = lcg * 1664525u + 1013904223u;
  20. const double noise = (double) (lcg >> 8) / 16777216.0 * 0.2 - 0.1;
  21. l[i] = (float) (0.6 * std::sin (2.0 * M_PI * 220.0 * n / 48000.0) + 0.3 * std::sin (2.0 * M_PI * 3317.0 * n / 48000.0));
  22. r[i] = (float) (0.5 * std::sin (2.0 * M_PI * 440.0 * n / 48000.0 + 0.3) + noise);
  23. }
  24. }
  25. int main (int argc, char** argv)
  26. {
  27. if (argc < 3) return 2;
  28. ysfx_config_t* config = ysfx_config_new();
  29. ysfx_t* fx = ysfx_new (config);
  30. ysfx_config_free (config);
  31. if (! ysfx_load_file (fx, argv[1], 0) || ! ysfx_compile (fx, ysfx_compile_no_gfx)) { std::fprintf (stderr, "compile failed\n"); return 1; }
  32. ysfx_set_sample_rate (fx, 48000.0);
  33. ysfx_set_block_size (fx, 4096);
  34. ysfx_init (fx);
  35. ysfx_time_info_t info {};
  36. info.tempo = 120.0; info.time_signature[0] = 4; info.time_signature[1] = 4; info.playback_state = ysfx_playback_stopped;
  37. const int block = 512, blocks = 64;
  38. std::vector<float> l (block), r (block);
  39. std::uint32_t lcg = 12345u;
  40. std::uint64_t hash = 14695981039346656037ull;
  41. FILE* out = std::fopen (argv[2], "w");
  42. std::fprintf (out, "#pragma once\n\n// Generated by the round 190pj reference tool from upstream ysfx (JoepVanlier/ysfx 5c3452fe): %s, 48 kHz,\n"
  43. "// 64 blocks of 512 frames, slider2 = 12 from block 32; every 61st frame (left, right) and an FNV-1a hash of all of it.\n"
  44. "// The input is lynx::jsfx's test signal (engine/JsfxFormat.cpp). Do not edit by hand.\n\nnamespace lynx::jsfxref\n{\n"
  45. "inline constexpr float softClipper[][2] = {\n", "chokehold/Clipper/soft_clipper.jsfx");
  46. long frame = 0;
  47. for (int b = 0; b < blocks; ++b)
  48. {
  49. if (b == 32) ysfx_slider_set_value (fx, 1, 12.0, true);
  50. input (l, r, block, lcg, frame);
  51. ysfx_set_time_info (fx, &info);
  52. const float* ins[2] = { l.data(), r.data() };
  53. float* outs[2] = { l.data(), r.data() };
  54. ysfx_process_float (fx, ins, outs, 2, 2, (uint32_t) block);
  55. for (int i = 0; i < block; ++i, ++frame)
  56. {
  57. for (const float v : { l[i], r[i] })
  58. {
  59. std::uint32_t bits; std::memcpy (&bits, &v, 4);
  60. for (int k = 0; k < 4; ++k) { hash ^= (bits >> (8 * k)) & 0xff; hash *= 1099511628211ull; }
  61. }
  62. if (frame % 61 == 0) std::fprintf (out, " { %a, %a },\n", (double) l[i], (double) r[i]);
  63. }
  64. }
  65. std::fprintf (out, "};\ninline constexpr unsigned long long softClipperHash = 0x%016llxull;\n} // namespace lynx::jsfxref\n", (unsigned long long) hash);
  66. std::fclose (out);
  67. ysfx_free (fx);
  68. std::printf ("hash %016llx frames %ld\n", (unsigned long long) hash, frame);
  69. return 0;
  70. }