DvsSignal.h 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. #pragma once
  2. // A generated control record for the DVS tests (core/Dvs.h): what a timecode vinyl / CD plays, written from the
  3. // format's description (REA_rodada167c §1.2) — a sine carrier on both channels a quarter cycle apart, each cycle's
  4. // peak loud (bit 1) or quiet (bit 0) to spell the register's bit sequence.
  5. #include <cmath>
  6. #include <cstdint>
  7. #include <functional>
  8. #include <vector>
  9. #include "core/Dvs.h"
  10. namespace dvstest
  11. {
  12. struct Signal
  13. {
  14. std::vector<float> left, right;
  15. std::vector<double> place; // the record's place under the needle at each frame (seconds, as the decoder names it)
  16. };
  17. // `speed(t)`: the record's speed at time t (1 = normal, negative = backwards), from `start` seconds on the record
  18. inline Signal make (const lynx::dvs::Format& f, double start, const std::function<double (double)>& speed, int frames,
  19. double rate, float amp = 0.5f, float quiet = 0.7f)
  20. {
  21. // the cycles the signal can reach, and their bits (the register's newest bit at each cycle)
  22. double c = (start + f.offset) * f.resolution;
  23. double lo = c, hi = c;
  24. {
  25. double x = c;
  26. for (int i = 0; i < frames; ++i)
  27. {
  28. x += speed (i / rate) * f.resolution / rate;
  29. lo = std::min (lo, x);
  30. hi = std::max (hi, x);
  31. }
  32. }
  33. const auto first = (std::int64_t) std::max (0.0, std::floor (lo) - 2.0);
  34. const auto last = (std::int64_t) std::ceil (hi) + 2;
  35. std::vector<std::uint8_t> bits ((std::size_t) (last - first + 1));
  36. auto s = f.seed;
  37. for (std::int64_t i = 0; i <= last; ++i)
  38. {
  39. if (i >= first)
  40. bits[(std::size_t) (i - first)] = (std::uint8_t) (s & 1u);
  41. s = lynx::dvs::nextState (s, f);
  42. }
  43. Signal out;
  44. out.left.resize ((std::size_t) frames);
  45. out.right.resize ((std::size_t) frames);
  46. out.place.resize ((std::size_t) frames);
  47. const bool flip = (f.flags & 0x10) != 0, leftPrimary = (f.flags & 0x40) != 0;
  48. for (int i = 0; i < frames; ++i)
  49. {
  50. const auto cycle = (std::int64_t) std::floor (c);
  51. const auto phase = 2.0 * 3.141592653589793 * (c - (double) cycle);
  52. const auto bit = cycle >= first && cycle <= last ? bits[(std::size_t) (cycle - first)] : 0;
  53. const auto a = (double) amp * (bit != 0 ? 1.0 : (double) quiet);
  54. const auto primary = (float) (a * std::sin (phase));
  55. const auto secondary = (float) (a * (flip ? std::cos (phase) : -std::cos (phase)));
  56. out.left[(std::size_t) i] = leftPrimary ? primary : secondary;
  57. out.right[(std::size_t) i] = leftPrimary ? secondary : primary;
  58. out.place[(std::size_t) i] = c / f.resolution - f.offset;
  59. c += speed (i / rate) * f.resolution / rate;
  60. }
  61. return out;
  62. }
  63. } // namespace dvstest