Dvs.cpp 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  1. #include "core/Dvs.h"
  2. #include <algorithm>
  3. #include <chrono>
  4. #include <cmath>
  5. #include <thread>
  6. namespace lynx::dvs
  7. {
  8. namespace
  9. {
  10. // [DECOMP engine_x64 0x1800037c0] the five formats of FL's sine-and-amplitude decoder, by the state's timecode index
  11. const Format seratoA { 20, 0xa5411u, 0xa786cu, 710000u, 1000, 25.7, 0x00 };
  12. const Format seratoB { 20, 0xd76c6u, 0x9b0f2u, 940000u, 1000, 8.5, 0x00 };
  13. const Format seratoCd { 20, 0x30aefu, 0x34d54u, 982000u, 1000, 0.0, 0x00 };
  14. const Format traktorA { 23, 0x759c41u, 0x410410u, 1500001u, 2000, 0.0, 0x70 };
  15. const Format traktorB { 23, 0x53e4f7u, 0x410410u, 2109993u, 2000, 0.0, 0x70 };
  16. int parity (std::uint32_t x) noexcept
  17. {
  18. int p = 0;
  19. for (; x != 0; x >>= 1)
  20. p ^= (int) (x & 1u);
  21. return p;
  22. }
  23. // The position table: (register << 32 | cycle) sorted by register
  24. struct Table
  25. {
  26. std::vector<std::uint64_t> entries;
  27. bool find (std::uint32_t state, std::uint32_t& cycle) const noexcept
  28. {
  29. const auto key = (std::uint64_t) state << 32;
  30. const auto it = std::lower_bound (entries.begin(), entries.end(), key);
  31. if (it == entries.end() || (*it >> 32) != state)
  32. return false;
  33. cycle = (std::uint32_t) *it;
  34. return true;
  35. }
  36. };
  37. constexpr int slots = 8;
  38. std::atomic<const Table*> tables[slots] {};
  39. std::atomic<bool> building[slots] {};
  40. const Table* tableOf (int state) noexcept
  41. {
  42. return state >= 0 && state < slots ? tables[state].load (std::memory_order_acquire) : nullptr;
  43. }
  44. void build (int state)
  45. {
  46. const auto* f = format (state);
  47. auto* t = new Table();
  48. t->entries.reserve (f->length);
  49. auto s = f->seed;
  50. for (std::uint32_t i = 0; i < f->length; ++i)
  51. {
  52. t->entries.push_back ((std::uint64_t) s << 32 | i);
  53. s = nextState (s, *f);
  54. }
  55. std::sort (t->entries.begin(), t->entries.end());
  56. tables[state].store (t, std::memory_order_release); // kept for the program's life (shared by every decoder)
  57. }
  58. } // namespace
  59. int stateOfCombo (int combo) noexcept
  60. {
  61. // [DECOMP Fruity Scratcher 0x733fe0] Off → −1, Timecode CD (the last entry) → 0, the others keep their index
  62. if (combo <= 0 || combo >= comboCount)
  63. return -1;
  64. return combo == comboCount - 1 ? 0 : combo;
  65. }
  66. int comboOfState (int state) noexcept
  67. {
  68. if (state == 0)
  69. return comboCount - 1;
  70. return state >= 1 && state < comboCount - 1 ? state : 0;
  71. }
  72. const char* comboName (int combo) noexcept
  73. {
  74. static const char* const names[comboCount] = { "Off",
  75. "Serato Mk2, Side A",
  76. "Serato Mk2, Side B",
  77. "Serato CD",
  78. "Traktor Skratch Mk1, Side A",
  79. "Traktor Skratch Mk1, Side B",
  80. "Traktor Skratch Mk2, Side A",
  81. "Traktor Skratch Mk2, Side B",
  82. "Timecode CD" };
  83. return combo >= 0 && combo < comboCount ? names[combo] : names[0];
  84. }
  85. const Format* format (int state) noexcept
  86. {
  87. // [DECOMP engine_x64 createTimecodeDecoder 0x180001080]: 1 → Serato A (id 1), 2 → Serato B (id 0), 3 → Serato
  88. // CD (id 2), 4 / 5 → Traktor Mk1 A / B (ids 3 / 4); 6 / 7 = the Traktor Mk2 decoder, 0 = the CD decoder
  89. switch (state)
  90. {
  91. case 1: return &seratoA;
  92. case 2: return &seratoB;
  93. case 3: return &seratoCd;
  94. case 4: return &traktorA;
  95. case 5: return &traktorB;
  96. default: return nullptr;
  97. }
  98. }
  99. bool supported (int state) noexcept
  100. {
  101. return format (state) != nullptr;
  102. }
  103. std::uint32_t nextState (std::uint32_t state, const Format& f) noexcept
  104. {
  105. const auto mask = (1u << f.bits) - 1u;
  106. return ((state << 1) & mask) | (std::uint32_t) parity (state & f.taps);
  107. }
  108. void warm (int state, bool wait)
  109. {
  110. if (format (state) == nullptr)
  111. return;
  112. if (tableOf (state) == nullptr && ! building[state].exchange (true))
  113. {
  114. if (wait)
  115. build (state);
  116. else
  117. std::thread ([state] { build (state); }).detach();
  118. }
  119. if (wait)
  120. while (tableOf (state) == nullptr)
  121. std::this_thread::sleep_for (std::chrono::milliseconds (2));
  122. }
  123. // ------------------------------------------------------------------------------------------------------- decoder
  124. void Decoder::Crossing::feed (float x, float k) noexcept
  125. {
  126. // [DECOMP 0x180003c10] a zero crossing with 0.004 of hysteresis around a fast-moving zero (the signal's own
  127. // low frequencies: rumble and DC follow the record)
  128. crossed = false;
  129. if (! positive)
  130. {
  131. if (x > zero + 0.004f)
  132. positive = crossed = true;
  133. }
  134. else if (x < zero - 0.004f)
  135. {
  136. positive = false;
  137. crossed = true;
  138. }
  139. zero += (x - zero) * k;
  140. }
  141. void Decoder::configure (int state, double sampleRate) noexcept
  142. {
  143. fmt = format (state);
  144. stateIndex = state;
  145. rate = sampleRate > 0.0 ? sampleRate : 44100.0;
  146. dt = 1.0 / rate;
  147. // [DECOMP 0x1800037c0] the zero's time constant: 1/900 s
  148. zeroK = (float) (1.0 - std::exp (-1.0 / (rate / 900.0)));
  149. reset();
  150. }
  151. void Decoder::reset() noexcept
  152. {
  153. primary = {};
  154. secondary = {};
  155. dirForward = true;
  156. reg = 0;
  157. valid = 0;
  158. refLevel = 1.0f;
  159. filtX = velocity = 0.0;
  160. pos = 0.0;
  161. lockCount = 0;
  162. }
  163. void Decoder::shiftIn (bool bit) noexcept
  164. {
  165. // [DECOMP 0x180003aa0] the bit enters the register at the end the needle moves towards; 25 bits in a row that
  166. // follow the register's rule name a place on the record
  167. const auto& f = *fmt;
  168. const auto mask = (1u << f.bits) - 1u;
  169. std::uint32_t predicted, next;
  170. if (dirForward)
  171. {
  172. predicted = ((reg << 1) & mask) | (std::uint32_t) parity (reg & f.taps);
  173. next = ((reg << 1) & mask) | (bit ? 1u : 0u);
  174. }
  175. else
  176. {
  177. predicted = (std::uint32_t) parity ((((f.taps << 1) & mask) | 1u) & reg) << (f.bits - 1) | reg >> 1;
  178. next = (bit ? 1u : 0u) << (f.bits - 1) | reg >> 1;
  179. }
  180. reg = next;
  181. if (predicted != next)
  182. {
  183. valid = 0;
  184. return;
  185. }
  186. if (++valid <= 24)
  187. return;
  188. const auto* table = tableOf (stateIndex);
  189. std::uint32_t cycle = 0;
  190. if (table == nullptr || ! table->find (next, cycle))
  191. return;
  192. // [DECOMP 0x180003e80] the place the register names; the running position jumps there when it is 0.1 s away
  193. const auto named = (double) cycle / (double) f.resolution - f.offset;
  194. if (std::abs (named - pos) > 0.1)
  195. pos = named;
  196. ++lockCount;
  197. }
  198. void Decoder::process (float left, float right) noexcept
  199. {
  200. if (fmt == nullptr)
  201. return;
  202. const auto& f = *fmt;
  203. // [DECOMP 0x180003df0] the primary channel is the right one unless the format says left
  204. const bool leftPrimary = (f.flags & 0x40) != 0;
  205. primary.feed (leftPrimary ? left : right, zeroK);
  206. secondary.feed (leftPrimary ? right : left, zeroK);
  207. double step = 0.0;
  208. if (primary.crossed || secondary.crossed)
  209. {
  210. // the order of the two channels' crossings gives the direction
  211. bool fwd = primary.crossed ? primary.positive != secondary.positive : primary.positive == secondary.positive;
  212. if ((f.flags & 0x10) != 0)
  213. fwd = ! fwd;
  214. if (fwd != dirForward)
  215. {
  216. dirForward = fwd;
  217. valid = 0;
  218. }
  219. step = 0.25 / (double) f.resolution; // a quarter of a cycle per crossing
  220. }
  221. if (! dirForward)
  222. step = -step;
  223. // the speed tracker (alpha 1/512, beta 1/131072 per sample)
  224. const auto predicted = dt * velocity + filtX;
  225. const auto err = step - predicted;
  226. filtX = (err * 0.001953121185 + predicted) - step;
  227. velocity += err * 7.62937963e-06 / dt;
  228. // a bit on each peak of the primary channel (the secondary's crossing): loud = 1, quiet = 0
  229. const bool wantPositive = (f.flags & 0x20) == 0;
  230. if (secondary.crossed && primary.positive == wantPositive)
  231. {
  232. const auto level = std::abs ((leftPrimary ? left : right) - primary.zero);
  233. shiftIn (level > refLevel);
  234. refLevel += (level - refLevel) * (1.0f / 48.0f);
  235. }
  236. // [DECOMP 0x180003f10] the running position follows the speed
  237. pos += velocity / rate;
  238. }
  239. } // namespace lynx::dvs