Rec170dTests.inc 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357
  1. // Round 170d (the Busy Works video, 02:07–02:52: "the first chord on the downbeat after the pre-count is not recorded",
  2. // FL drops it too): notes played during the count-in are kept, the ones just before the start go onto it (Cubase's
  3. // catch range, Logic keeps them), MIDI notes are timed by the device's stamp against the audio clock and moved back by
  4. // the output latency (Pro Tools / Cubase / REAPER), audio takes by the measured loopback round trip, and Record ›
  5. // Capture MIDI (Ableton / Logic / Cubase retrospective record). coord/recording-research-0.1.24.md.
  6. namespace rec170d
  7. {
  8. constexpr double bar = 4.0 * lynx::ppq; // 4/4
  9. // a take the way AudioEngine::updateRecording runs it: each event through the count-in catch, then the NoteRecorder,
  10. // every finished batch committed by commitNoteBatch inside one edit
  11. struct Take
  12. {
  13. lynx::Song& song;
  14. lynx::CountInCatch catcher;
  15. lynx::NoteRecorder recorder;
  16. std::vector<int> cleared;
  17. bool first = true;
  18. lynx::TakeQuantize q {};
  19. Take (lynx::Song& s, std::int64_t start, std::int64_t window, std::int64_t countIn) : song (s)
  20. {
  21. q.grid = 0;
  22. const auto loop = song.patterns[(std::size_t) song.currentPattern].effectiveLength (song.meter());
  23. catcher.start = start;
  24. catcher.window = window;
  25. catcher.songMode = song.mode == lynx::PlayMode::song;
  26. catcher.wrap = lynx::countInWrap (countIn, loop);
  27. recorder.begin (loop);
  28. if (song.mode == lynx::PlayMode::song && ! song.hasLoop())
  29. recorder.setLinear (true);
  30. }
  31. void flush()
  32. {
  33. auto notes = recorder.takeFinished();
  34. if (notes.empty())
  35. return;
  36. const auto before = lynx::patternLengths (song);
  37. lynx::commitNoteBatch (song, recorder, std::move (notes), lynx::RecordMode::overdub, q, cleared, true, first);
  38. lynx::followPatternLengths (before, song);
  39. first = false;
  40. }
  41. void event (double tick, int key, bool on)
  42. {
  43. lynx::RecordedEvent e { tick, 0, key, on ? 0.8f : 0.0f, on };
  44. if (lynx::catchEarlyNote (e, catcher))
  45. recorder.add (song, e);
  46. flush();
  47. }
  48. void stop (double tick)
  49. {
  50. recorder.end (song, tick);
  51. flush();
  52. }
  53. };
  54. lynx::Song emptySong (lynx::PlayMode mode)
  55. {
  56. auto s = lynx::Song::makeDefault();
  57. s.bpm = 120.0;
  58. s.mode = mode;
  59. for (auto& p : s.patterns)
  60. p.notes.clear();
  61. s.playlist.clear();
  62. s.currentPattern = 0;
  63. return s;
  64. }
  65. const lynx::Note* find (const lynx::Pattern& p, int key)
  66. {
  67. for (const auto& n : p.notes)
  68. if (n.key == key)
  69. return &n;
  70. return nullptr;
  71. }
  72. const lynx::Clip* clipOf (const lynx::Song& s, int pattern)
  73. {
  74. for (const auto& c : s.playlist)
  75. if (c.kind == lynx::ClipKind::pattern && c.pattern == pattern)
  76. return &c;
  77. return nullptr;
  78. }
  79. } // namespace rec170d
  80. TEST (rec170d_live_clock_uses_the_event_time_and_the_output_latency)
  81. {
  82. // 120 bpm at 48 kHz: 1.92 ticks a millisecond
  83. lynx::LiveClock c;
  84. c.tick = 7680.0;
  85. c.timeMs = 10000.0;
  86. c.ticksPerMs = 960.0 * 2.0 / 1000.0;
  87. c.running = true;
  88. // an event stamped 5 ms after the block, 16 ms of output latency: heard 11 ms before the clock's position
  89. CHECK (std::abs (lynx::liveTickAt (c, 10005.0, 16.0) - (7680.0 - 11.0 * 1.92)) < 1e-9);
  90. // without compensation: sample-accurate against the block, not "now"
  91. CHECK (std::abs (lynx::liveTickAt (c, 10005.0, 0.0) - (7680.0 + 5.0 * 1.92)) < 1e-9);
  92. // a stamp before the block (the device stamped it while the callback ran)
  93. CHECK (std::abs (lynx::liveTickAt (c, 9998.0, 0.0) - (7680.0 - 2.0 * 1.92)) < 1e-9);
  94. // a stamp from another clock is held to a quarter second
  95. CHECK (std::abs (lynx::liveTickAt (c, 99999.0, 0.0) - (7680.0 + 250.0 * 1.92)) < 1e-9);
  96. // a stopped clock: its position, whatever the time
  97. c.running = false;
  98. CHECK (lynx::liveTickAt (c, 10500.0, 16.0) == 7680.0);
  99. }
  100. TEST (rec170d_count_in_catch_rules)
  101. {
  102. using lynx::RecordedEvent;
  103. lynx::CountInCatch c;
  104. c.start = 7680;
  105. c.window = lynx::ppq / 4; // a 1/16 note
  106. c.songMode = true;
  107. RecordedEvent on { 7600.0, 0, 60, 0.8f, true };
  108. CHECK (lynx::catchEarlyNote (on, c) && on.tick == 7680.0); // a hair early: onto the start
  109. RecordedEvent off { 7650.0, 0, 60, 0.0f, false };
  110. CHECK (lynx::catchEarlyNote (off, c) && off.tick == 7730.0); // released before the start: its played length (50)
  111. RecordedEvent held { 7590.0, 0, 64, 0.8f, true }, heldOff { 8000.0, 0, 64, 0.0f, false };
  112. CHECK (lynx::catchEarlyNote (held, c) && held.tick == 7680.0);
  113. CHECK (lynx::catchEarlyNote (heldOff, c) && heldOff.tick == 8000.0); // released after the start: kept
  114. RecordedEvent early { 4800.0, 0, 62, 0.8f, true };
  115. CHECK (lynx::catchEarlyNote (early, c) && early.tick == 4800.0); // earlier in the count-in: its real song position
  116. RecordedEvent late { 9000.0, 0, 65, 0.8f, true };
  117. CHECK (lynx::catchEarlyNote (late, c) && late.tick == 9000.0); // after the start: untouched
  118. // the song start: nothing before it
  119. lynx::CountInCatch z;
  120. z.start = 0;
  121. z.window = lynx::ppq / 4;
  122. RecordedEvent before { -1000.0, 0, 60, 0.8f, true };
  123. CHECK (! lynx::catchEarlyNote (before, z));
  124. RecordedEvent hair { -100.0, 0, 60, 0.8f, true };
  125. CHECK (lynx::catchEarlyNote (hair, z) && hair.tick == 0.0);
  126. // pattern mode: the count-in is the loop's previous pass
  127. lynx::CountInCatch p;
  128. p.start = 0;
  129. p.window = lynx::ppq / 4;
  130. p.songMode = false;
  131. p.wrap = lynx::countInWrap (3840, 3840);
  132. CHECK (p.wrap == 3840 && lynx::countInWrap (7680, 3840) == 7680 && lynx::countInWrap (3840, 7680) == 7680);
  133. RecordedEvent beat4 { -960.0, 0, 67, 0.8f, true }, beat4Off { -500.0, 0, 67, 0.0f, false };
  134. CHECK (lynx::catchEarlyNote (beat4, p) && beat4.tick == 2880.0);
  135. CHECK (lynx::catchEarlyNote (beat4Off, p) && beat4Off.tick == 3340.0);
  136. RecordedEvent over { -960.0, 0, 69, 0.8f, true }, overOff { 500.0, 0, 69, 0.0f, false };
  137. CHECK (lynx::catchEarlyNote (over, p) && lynx::catchEarlyNote (overOff, p) && overOff.tick == 3840.0); // to the loop's end
  138. // no count-in: nothing changes
  139. lynx::CountInCatch none;
  140. RecordedEvent any { -5.0, 0, 60, 0.8f, true };
  141. CHECK (lynx::catchEarlyNote (any, none) && any.tick == -5.0);
  142. }
  143. TEST (rec170d_the_video_chord_on_the_downbeat_after_the_count_in_is_recorded)
  144. {
  145. // Song mode, a 1-bar count-in before bar 3: the chord played 15 ms early (29 ticks), held a beat; then a note a bar
  146. // for 6 bars ("it stopped after two bars", round 168k): everything is kept, the chord on bar 3
  147. auto s = rec170d::emptySong (lynx::PlayMode::song);
  148. const auto start = (std::int64_t) (2 * rec170d::bar);
  149. rec170d::Take take (s, start, lynx::ppq / 4, (std::int64_t) rec170d::bar);
  150. for (int key : { 60, 64, 67 })
  151. take.event ((double) start - 29.0, key, true);
  152. for (int key : { 60, 64, 67 })
  153. take.event ((double) start + lynx::ppq, key, false);
  154. for (int b = 1; b <= 6; ++b)
  155. {
  156. take.event ((double) start + b * rec170d::bar, 70 + b, true);
  157. take.event ((double) start + b * rec170d::bar + lynx::ppq / 2, 70 + b, false);
  158. }
  159. take.stop ((double) start + 7 * rec170d::bar);
  160. const auto& p = s.patterns[0];
  161. CHECK (p.notes.size() == 9);
  162. for (int key : { 60, 64, 67 })
  163. {
  164. const auto* n = rec170d::find (p, key);
  165. CHECK (n != nullptr && n->start == 0 && n->length == lynx::ppq); // on the take's first beat, its clip at bar 3
  166. }
  167. for (int b = 1; b <= 6; ++b)
  168. {
  169. const auto* n = rec170d::find (p, 70 + b);
  170. CHECK (n != nullptr && n->start == (std::int64_t) (b * rec170d::bar));
  171. }
  172. const auto* clip = rec170d::clipOf (s, 0);
  173. CHECK (clip != nullptr && clip->start == start && clip->length == (std::int64_t) (7 * rec170d::bar));
  174. }
  175. TEST (rec170d_count_in_notes_keep_their_real_time_and_the_window_can_be_off)
  176. {
  177. // a note in the count-in bar (beat 2 of bar 2) and the chord 15 ms early, catch window off: both where played
  178. auto s = rec170d::emptySong (lynx::PlayMode::song);
  179. const auto start = (std::int64_t) (2 * rec170d::bar);
  180. rec170d::Take take (s, start, 0, (std::int64_t) rec170d::bar);
  181. take.event ((double) rec170d::bar + lynx::ppq, 62, true);
  182. take.event ((double) rec170d::bar + 1.5 * lynx::ppq, 62, false);
  183. take.event ((double) start - 29.0, 60, true);
  184. take.event ((double) start + lynx::ppq, 60, false);
  185. take.stop ((double) start + rec170d::bar);
  186. const auto& p = s.patterns[0];
  187. const auto* pickup = rec170d::find (p, 62);
  188. const auto* chord = rec170d::find (p, 60);
  189. CHECK (pickup != nullptr && pickup->start == lynx::ppq); // the take's origin is bar 2 now
  190. CHECK (chord != nullptr && chord->start == (std::int64_t) rec170d::bar - 29);
  191. const auto* clip = rec170d::clipOf (s, 0);
  192. CHECK (clip != nullptr && clip->start == (std::int64_t) rec170d::bar);
  193. }
  194. TEST (rec170d_pattern_mode_count_in_is_the_previous_loop_pass)
  195. {
  196. auto s = rec170d::emptySong (lynx::PlayMode::pattern);
  197. s.patterns[0].lengthTicks = (std::int64_t) rec170d::bar; // a 1-bar pattern
  198. rec170d::Take take (s, 0, lynx::ppq / 4, (std::int64_t) rec170d::bar);
  199. take.event (-20.0, 60, true); // a hair early: beat 1
  200. take.event (400.0, 60, false);
  201. take.event (-960.0, 67, true); // beat 4 of the count-in: beat 4 of the pattern
  202. take.event (-480.0, 67, false);
  203. take.event (960.0, 62, true);
  204. take.event (1200.0, 62, false);
  205. take.stop (2000.0);
  206. const auto& p = s.patterns[0];
  207. CHECK (p.notes.size() == 3);
  208. const auto* one = rec170d::find (p, 60);
  209. const auto* four = rec170d::find (p, 67);
  210. const auto* two = rec170d::find (p, 62);
  211. CHECK (one != nullptr && one->start == 0 && one->length == 400);
  212. CHECK (four != nullptr && four->start == 2880 && four->length == 480);
  213. CHECK (two != nullptr && two->start == 960);
  214. CHECK (s.patterns[0].effectiveLength (s.meter()) == (std::int64_t) rec170d::bar);
  215. }
  216. TEST (rec170d_capture_midi_played_while_playing_lands_where_played)
  217. {
  218. auto s = rec170d::emptySong (lynx::PlayMode::song);
  219. lynx::scorelog::Log log;
  220. // an old phrase (stopped), then a phrase played while the song played (bars 5–6), not recorded
  221. log.noteOn (1000.0, 0, 50, 0.7f);
  222. log.noteOff (1200.0, 0, 50);
  223. const double barMs = 2000.0; // 120 bpm
  224. for (int i = 0; i < 4; ++i)
  225. {
  226. const auto ms = 60000.0 + i * barMs / 2;
  227. log.noteOn (ms, 0, 60 + i, 0.8f, 4 * rec170d::bar + i * 2 * lynx::ppq);
  228. log.noteOff (ms + 250.0, 0, 60 + i);
  229. }
  230. lynx::CaptureOptions o;
  231. o.nowMs = 70000.0;
  232. o.gapMs = 4000.0;
  233. const auto r = lynx::captureMidi (s, log.snapshot(), o);
  234. CHECK (r.notes == 4 && r.played && r.newestMs == 60000.0 + 3 * barMs / 2);
  235. const auto& p = s.patterns[0];
  236. CHECK (p.notes.size() == 4 && rec170d::find (p, 50) == nullptr);
  237. const auto* clip = rec170d::clipOf (s, 0);
  238. CHECK (clip != nullptr && clip->start == (std::int64_t) (4 * rec170d::bar)); // a clip where they were played
  239. for (int i = 0; i < 4; ++i)
  240. {
  241. const auto* n = rec170d::find (p, 60 + i);
  242. CHECK (n != nullptr && n->start == i * 2 * lynx::ppq && n->length == lynx::ppq / 2); // 250 ms = half a beat
  243. }
  244. // captured once: the next capture finds nothing new
  245. o.afterMs = r.newestMs;
  246. CHECK (lynx::capturePhrase (s, log.snapshot(), o).empty());
  247. }
  248. TEST (rec170d_capture_midi_while_stopped_keeps_the_timing_from_the_start)
  249. {
  250. // Pattern mode, stopped: the last phrase (after a 10 s pause) from the pattern start, at the project tempo
  251. auto s = rec170d::emptySong (lynx::PlayMode::pattern);
  252. lynx::scorelog::Log log;
  253. log.noteOn (1000.0, 0, 40, 0.5f);
  254. log.noteOff (1100.0, 0, 40);
  255. log.noteOn (20000.0, 0, 60, 0.8f);
  256. log.noteOff (20250.0, 0, 60);
  257. log.noteOn (20500.0, 0, 64, 0.8f); // a beat later
  258. log.noteOff (20750.0, 0, 64);
  259. log.noteOn (22000.0, 0, 67, 0.8f); // a bar after the first
  260. // (67 still held: it ends now)
  261. log.noteOn (22100.0, 0, 72, 0.8f, -1.0, true); // went into a take: never captured
  262. lynx::CaptureOptions o;
  263. o.nowMs = 22500.0;
  264. o.gapMs = 4000.0;
  265. const auto r = lynx::captureMidi (s, log.snapshot(), o);
  266. CHECK (r.notes == 3 && ! r.played);
  267. const auto& p = s.patterns[0];
  268. CHECK (p.notes.size() == 3);
  269. CHECK (rec170d::find (p, 60) != nullptr && rec170d::find (p, 60)->start == 0 && rec170d::find (p, 60)->length == lynx::ppq / 2);
  270. CHECK (rec170d::find (p, 64) != nullptr && rec170d::find (p, 64)->start == lynx::ppq);
  271. CHECK (rec170d::find (p, 67) != nullptr && rec170d::find (p, 67)->start == (std::int64_t) rec170d::bar
  272. && rec170d::find (p, 67)->length == lynx::ppq); // held: to now (500 ms)
  273. CHECK (rec170d::find (p, 40) == nullptr && rec170d::find (p, 72) == nullptr);
  274. // Song mode, stopped with the playhead in bar 3: from bar 3, a clip placed there
  275. auto song = rec170d::emptySong (lynx::PlayMode::song);
  276. o.playhead = (std::int64_t) (2 * rec170d::bar + 500);
  277. const auto r2 = lynx::captureMidi (song, log.snapshot(), o);
  278. CHECK (r2.notes == 3);
  279. const auto* clip = rec170d::clipOf (song, 0);
  280. CHECK (clip != nullptr && clip->start == (std::int64_t) (2 * rec170d::bar));
  281. CHECK (rec170d::find (song.patterns[0], 64) != nullptr && rec170d::find (song.patterns[0], 64)->start == lynx::ppq);
  282. // nothing played: nothing captured, nothing changed
  283. auto quiet = rec170d::emptySong (lynx::PlayMode::pattern);
  284. CHECK (lynx::captureMidi (quiet, {}, o).notes == 0 && quiet.patterns[0].notes.empty());
  285. }
  286. TEST (rec170d_score_log_dump_ignores_the_capture_fields)
  287. {
  288. // FL's "Dump score log" (round 165f) is unchanged by the new fields
  289. lynx::scorelog::Log log;
  290. log.noteOn (0.0, 0, 60, 0.8f, 1234.0, true);
  291. log.noteOff (500.0, 0, 60);
  292. lynx::scorelog::DumpOptions d;
  293. d.bpm = 120.0;
  294. const auto notes = lynx::scorelog::dump (log.snapshot(), d);
  295. CHECK (notes.size() == 1 && notes[0].start == 0 && notes[0].length == lynx::ppq);
  296. }
  297. TEST (rec170d_loopback_audio_take_lands_on_the_played_click)
  298. {
  299. // (c) audio: a click played at song tick 7680 comes back through a loopback `delay` samples later; the ping measures
  300. // the loopback, the take is placed with the measured round trip (+ a manual offset): the recorded click sits on 7680
  301. using namespace lynx::takes;
  302. const auto delay = 731; // e.g. 512 buffer + converters
  303. LatencyPing ping;
  304. ping.start (8, 4800, 0.5f);
  305. std::vector<float> line (200000, 0.0f), out (128), in (128);
  306. std::int64_t pos = 0;
  307. for (int guard = 0; ping.isRunning() && guard < 4000; ++guard)
  308. {
  309. for (int i = 0; i < 128; ++i)
  310. in[(std::size_t) i] = pos + i - delay >= 0 ? 0.8f * line[(std::size_t) (pos + i - delay)] : 0.0f;
  311. std::fill (out.begin(), out.end(), 0.0f);
  312. float* outs[] = { out.data(), nullptr };
  313. ping.process (in.data(), outs, 2, 128);
  314. std::copy (out.begin(), out.end(), line.begin() + pos);
  315. pos += 128;
  316. }
  317. const auto measured = ping.result();
  318. CHECK (measured.ok && measured.samples == delay);
  319. Latency l;
  320. l.driverInput = 64; // what the driver reports is replaced by the measurement
  321. l.driverOutput = 64;
  322. l.measured = measured.samples;
  323. const auto tps = 960.0 / 24000.0; // 120 bpm, 48 kHz
  324. // the take's first sample arrived when the transport was at 7000 ticks; the click is in it at sample s
  325. const auto firstTick = 7000.0;
  326. const auto clickSample = (7680.0 - 7000.0) / tps + delay; // played at 7680, recorded `delay` later
  327. const auto p = place (firstTick, 48000.0 * tps, tps, l.total());
  328. const auto clickTick = p.exactStart + clickSample * tps;
  329. CHECK (std::abs (clickTick - 7680.0) < 1e-6);
  330. // a manual offset of +24 samples moves it 24 samples earlier (a converter the loopback did not include)
  331. l.manual = 24;
  332. const auto q = place (firstTick, 48000.0 * tps, tps, l.total());
  333. CHECK (std::abs ((q.exactStart + clickSample * tps) - (7680.0 - 24 * tps)) < 1e-6);
  334. }