FirstSongTour190dvTests.inc 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394
  1. // --- Round 190dv: the Simple workflow's first-song tour (core/FirstSongTour.h) and LYNX's demo songs (core/DemoSongs.h).
  2. // The tour's steps are read from the song (model events: a beat, a key, an instrument, notes, an audio clip, a one-knob
  3. // effect, a Master look), the state machine (advance on a step done, Next / Back / Skip, pause and resume, the saved
  4. // state), "Do it for me"'s song edits, and the two new demo songs rendered: no clipping, loudness about −14 LUFS.
  5. // LYNX_190DV_REPORT=1 prints the demos' measurements.
  6. #include <cstdlib>
  7. #include "core/DemoSongs.h"
  8. #include "core/FirstSongTour.h"
  9. #include "core/Loudness.h"
  10. #include "core/RackRenderer.h"
  11. namespace tour190dv
  12. {
  13. using namespace lynx;
  14. inline Song empty()
  15. {
  16. Song s;
  17. s.mixer = Song::defaultMixer();
  18. return s;
  19. }
  20. inline simpletracks::Result addDrums (Song& s, bool withNotes = true)
  21. {
  22. simpletracks::Request q;
  23. q.kind = simpletracks::Kind::drums;
  24. ChannelDef kick;
  25. kick.instrument = InstrumentKind::kick;
  26. q.kit = { kick };
  27. Pattern p;
  28. p.lengthTicks = 16 * ticksPerStep;
  29. if (withNotes)
  30. p.toggleStep (0, 0);
  31. q.pattern = p;
  32. return simpletracks::addTrack (s, q);
  33. }
  34. inline simpletracks::Result addInstrument (Song& s)
  35. {
  36. simpletracks::Request q;
  37. q.kind = simpletracks::Kind::instrument;
  38. ChannelDef c;
  39. c.instrument = InstrumentKind::synth;
  40. q.channel = c;
  41. q.emptyBars = 4;
  42. return simpletracks::addTrack (s, q);
  43. }
  44. inline simpletracks::Result addLoop (Song& s)
  45. {
  46. simpletracks::Request q;
  47. q.kind = simpletracks::Kind::audio;
  48. q.name = "Loop";
  49. q.audioFile = "/nowhere/Loop 120 Am.wav";
  50. q.audioLength = ppq * 8;
  51. return simpletracks::addTrack (s, q);
  52. }
  53. struct Measured
  54. {
  55. double lufs = 0.0, truePeak = 0.0, samplePeak = 0.0, seconds = 0.0;
  56. bool finite = true;
  57. };
  58. // the whole song through the real render path (the Playlist, the inserts' effects, the Master look), 48 kHz
  59. inline Measured render (const Song& source)
  60. {
  61. constexpr int rate = 48000, block = 256;
  62. std::int64_t ticks = 0;
  63. const auto song = makeExportSong (source, true, ppq * 4, ticks);
  64. const auto tps = ticksPerSample (song.bpm, rate);
  65. auto state = RackState::build (song, nullptr, rate);
  66. RackRenderer renderer;
  67. renderer.prepare (rate, block);
  68. loudness::Meter meter;
  69. meter.prepare (rate);
  70. Measured m;
  71. const auto total = (std::int64_t) ((double) ticks / tps);
  72. std::vector<float> l (block), r (block);
  73. double tick = 0.0;
  74. for (std::int64_t pos = 0; pos + block <= total; pos += block)
  75. {
  76. renderer.process (state.get(), true, tick, block, l.data(), r.data());
  77. tick += block * tps;
  78. for (int i = 0; i < block; ++i)
  79. m.finite = m.finite && std::isfinite (l[(std::size_t) i]) && std::isfinite (r[(std::size_t) i]);
  80. meter.process (l.data(), r.data(), block);
  81. }
  82. meter.flushTruePeak();
  83. m.lufs = meter.integratedLoudness();
  84. m.truePeak = meter.truePeakDb();
  85. m.samplePeak = meter.samplePeakDb();
  86. m.seconds = (double) total / rate;
  87. return m;
  88. }
  89. } // namespace tour190dv
  90. TEST (tour190dv_steps_are_read_from_the_song)
  91. {
  92. using namespace lynx;
  93. using namespace tour190dv;
  94. auto s = empty();
  95. for (int i = 0; i < tour::stepCount; ++i)
  96. CHECK (! tour::satisfied (s, i));
  97. CHECK (! tour::satisfied (s, tour::stepCount)); // (the end card is never "done")
  98. // a Drums track with an empty grid is not a beat yet; with a step it is
  99. auto d = addDrums (s, false);
  100. CHECK (d.ok());
  101. CHECK (! tour::satisfied (s, tour::beat));
  102. s.patterns[(std::size_t) d.pattern].toggleStep (0, 4);
  103. CHECK (tour::satisfied (s, tour::beat));
  104. projectkey::set (s, tour::defaultKey());
  105. CHECK (tour::satisfied (s, tour::key));
  106. CHECK (projectkey::name (*projectkey::get (s)) == "A Minor Natural (Aeolian)");
  107. CHECK (tour::firstInstrumentTrack (s) < 0);
  108. auto inst = addInstrument (s);
  109. CHECK (inst.ok());
  110. CHECK (tour::firstInstrumentTrack (s) == inst.track);
  111. CHECK (tour::satisfied (s, tour::instrument));
  112. CHECK (! tour::satisfied (s, tour::chords)); // (its 4-bar pattern is empty)
  113. // "Do it for me" on the chords: i – VI – III – VII of A minor (Am F C G), one triad per bar from C3 up
  114. CHECK (tour::writeChords (s, inst.track) == 12);
  115. CHECK (tour::satisfied (s, tour::chords));
  116. {
  117. const auto& p = s.patterns[(std::size_t) simpletracks::patternOf (s, inst.track)];
  118. std::vector<int> bar1, bar2;
  119. for (const auto& n : p.notes)
  120. {
  121. CHECK (n.channel == s.playlistTracks[(std::size_t) inst.track].channel);
  122. if (n.start == 0) bar1.push_back (n.key);
  123. if (n.start == 4 * ppq) bar2.push_back (n.key);
  124. }
  125. std::sort (bar1.begin(), bar1.end());
  126. std::sort (bar2.begin(), bar2.end());
  127. CHECK ((bar1 == std::vector<int> { 57, 60, 64 })); // A C E
  128. CHECK ((bar2 == std::vector<int> { 65, 69, 72 })); // F A C
  129. }
  130. CHECK (tour::writeChords (s, d.track) == 0); // (not an Instrument track)
  131. CHECK (! tour::satisfied (s, tour::loop));
  132. auto loop = addLoop (s);
  133. CHECK (loop.ok());
  134. CHECK (tour::satisfied (s, tour::loop));
  135. // an ordinary effect is not a one-knob effect; Room is; a Room edited by hand in its full editor is not any more
  136. CHECK (! tour::satisfied (s, tour::effect));
  137. s.mixer[(std::size_t) inst.insert].effects.push_back (makeEffectSlot (EffectKind::reverb));
  138. CHECK (! tour::satisfied (s, tour::effect));
  139. CHECK (tour::addOneKnob (s, inst.track));
  140. CHECK (tour::satisfied (s, tour::effect));
  141. CHECK (! tour::satisfied (s, tour::finish));
  142. MasterLookSettings look;
  143. look.id = looks::balanced;
  144. CHECK (looks::apply (s, look));
  145. CHECK (tour::satisfied (s, tour::finish));
  146. // every step names controls for its ring; only the loop step can't be done for the user
  147. for (int i = 0; i < tour::stepCount; ++i)
  148. {
  149. CHECK (! tour::targets (i).empty());
  150. CHECK (tour::canDoForUser (i) == (i != tour::loop));
  151. CHECK (std::string (tour::title (i)).size() > 4 && std::string (tour::text (i)).size() > 20);
  152. }
  153. }
  154. TEST (tour190dv_the_tour_moves_on_when_the_song_shows_a_step_done)
  155. {
  156. using namespace lynx;
  157. using namespace tour190dv;
  158. auto s = empty();
  159. tour::Machine m;
  160. CHECK (! m.running() && ! m.canContinue());
  161. m.start (s);
  162. CHECK (m.running() && m.canContinue() && m.step() == tour::beat);
  163. CHECK (! m.observe (s)); // (nothing changed)
  164. CHECK (m.step() == tour::beat);
  165. // the beat: on to the key
  166. addDrums (s);
  167. CHECK (m.observe (s));
  168. CHECK (m.step() == tour::key);
  169. CHECK ((m.state().completed & tour::Machine::bit (tour::beat)) != 0);
  170. // a key chosen AND an instrument added in one edit: both steps pass at once, the card shows the chords
  171. projectkey::set (s, tour::defaultKey());
  172. auto inst = addInstrument (s);
  173. CHECK (m.observe (s));
  174. CHECK (m.step() == tour::chords);
  175. // Next without doing it: passed, not completed
  176. m.next (s);
  177. CHECK (m.step() == tour::loop);
  178. CHECK ((m.state().passed & tour::Machine::bit (tour::chords)) != 0);
  179. CHECK ((m.state().completed & tour::Machine::bit (tour::chords)) == 0);
  180. // Back on a step done: it stays on the card with its tick until Next (no jump forward by itself)
  181. m.back();
  182. m.back();
  183. CHECK (m.step() == tour::instrument);
  184. CHECK (m.shownDone (s));
  185. CHECK (! m.observe (s));
  186. CHECK (m.step() == tour::instrument);
  187. m.next (s); // (done: not "passed")
  188. CHECK ((m.state().passed & tour::Machine::bit (tour::instrument)) == 0);
  189. CHECK (m.step() == tour::chords);
  190. CHECK (! m.shownDone (s));
  191. // the chords written: on to the loop
  192. tour::writeChords (s, inst.track);
  193. CHECK (m.observe (s));
  194. CHECK (m.step() == tour::loop);
  195. addLoop (s);
  196. CHECK (m.observe (s) && m.step() == tour::effect);
  197. tour::addOneKnob (s, inst.track);
  198. CHECK (m.observe (s) && m.step() == tour::finish);
  199. MasterLookSettings look;
  200. look.id = looks::warmTape;
  201. looks::apply (s, look);
  202. CHECK (m.observe (s));
  203. CHECK (m.atEnd() && m.step() == tour::stepCount);
  204. CHECK (! m.observe (s)); // (the end card stays)
  205. m.next (s);
  206. CHECK (m.step() == tour::stepCount);
  207. m.finishTour();
  208. CHECK (! m.running() && ! m.canContinue() && m.state().done);
  209. CHECK (m.state().completed == 0x7fu); // (all seven: the chords too, once written)
  210. // a step undone after Back is advanced again when it is done once more
  211. tour::Machine m2;
  212. auto s2 = empty();
  213. m2.start (s2);
  214. auto d = addDrums (s2);
  215. CHECK (m2.observe (s2) && m2.step() == tour::key);
  216. m2.back();
  217. CHECK (m2.step() == tour::beat && ! m2.observe (s2));
  218. s2.patterns[(std::size_t) d.pattern].notes.clear(); // (the steps cleared)
  219. CHECK (! m2.observe (s2));
  220. s2.patterns[(std::size_t) d.pattern].toggleStep (0, 8);
  221. CHECK (m2.observe (s2) && m2.step() == tour::key);
  222. }
  223. TEST (tour190dv_skip_pause_resume_and_the_saved_state)
  224. {
  225. using namespace lynx;
  226. using namespace tour190dv;
  227. // the saved state reads back as written, and odd text is ignored
  228. tour::State st;
  229. st.welcomeSeen = true;
  230. st.started = true;
  231. st.step = 4;
  232. st.passed = tour::Machine::bit (2);
  233. st.completed = 0x0b;
  234. CHECK (tour::parse (tour::serialize (st)) == st);
  235. CHECK (tour::parse ("") == tour::State {});
  236. {
  237. const auto odd = tour::parse ("step=99\nwelcome=x\nnothing\npassed=1023\ndone=1\n");
  238. CHECK (odd.step == tour::stepCount && ! odd.welcomeSeen && odd.passed == 0x7fu && odd.done);
  239. }
  240. // Welcome shown once
  241. tour::Machine fresh;
  242. CHECK (! fresh.state().welcomeSeen);
  243. fresh.welcomeShown();
  244. CHECK (tour::parse (tour::serialize (fresh.state())).welcomeSeen);
  245. // pause after the beat and the key, passing over the instrument
  246. auto s = empty();
  247. tour::Machine m;
  248. m.start (s);
  249. addDrums (s);
  250. projectkey::set (s, tour::defaultKey());
  251. m.observe (s);
  252. CHECK (m.step() == tour::instrument);
  253. m.next (s);
  254. CHECK (m.step() == tour::chords);
  255. m.pause();
  256. CHECK (! m.running() && m.canContinue());
  257. // a restart: the saved state continues on the same song where it was
  258. tour::Machine again (tour::parse (tour::serialize (m.state())));
  259. CHECK (! again.running() && again.canContinue() && again.step() == tour::chords);
  260. again.resume (s);
  261. CHECK (again.running() && again.step() == tour::chords);
  262. // ... and on another, empty song from the first step that song lacks and the user didn't pass over: the beat
  263. tour::Machine other (tour::parse (tour::serialize (m.state())));
  264. auto s2 = empty();
  265. other.resume (s2);
  266. CHECK (other.step() == tour::beat);
  267. // with only the beat there: the key (the instrument was passed with Next, so it is not asked again)
  268. addDrums (s2);
  269. tour::Machine third (tour::parse (tour::serialize (m.state())));
  270. third.resume (s2);
  271. CHECK (third.step() == tour::key);
  272. projectkey::set (s2, tour::defaultKey());
  273. // (going forward the tour still visits a step passed earlier that this song lacks: the instrument)
  274. CHECK (third.observe (s2) && third.step() == tour::instrument);
  275. // Skip tour: ended for good; it can't be continued, a new start begins again (Help)
  276. third.skip();
  277. CHECK (! third.running() && ! third.canContinue() && third.state().done);
  278. tour::Machine fromFile (tour::parse (tour::serialize (third.state())));
  279. CHECK (fromFile.state().done && ! fromFile.canContinue());
  280. fromFile.resume (s2); // (nothing to continue: a new tour, past what the song shows)
  281. CHECK (fromFile.running() && fromFile.step() == tour::instrument && ! fromFile.state().done);
  282. CHECK (fromFile.state().passed == 0);
  283. // a tour started on a finished song goes straight to the end card
  284. auto full = empty();
  285. addDrums (full);
  286. projectkey::set (full, tour::defaultKey());
  287. auto inst = addInstrument (full);
  288. tour::writeChords (full, inst.track);
  289. addLoop (full);
  290. tour::addOneKnob (full, inst.track);
  291. MasterLookSettings look;
  292. look.id = looks::balanced;
  293. looks::apply (full, look);
  294. tour::Machine done;
  295. done.start (full);
  296. CHECK (done.atEnd());
  297. }
  298. TEST (tour190dv_demo_songs_are_lynx_made_simple_tracks)
  299. {
  300. using namespace lynx;
  301. for (int d = 1; d < demos::count; ++d)
  302. {
  303. const auto song = demos::make (d);
  304. const auto again = demos::make (d);
  305. // the same song every time (Loop Starter's own grooves with a fixed seed, no sample library)
  306. CHECK (song.channels.size() == again.channels.size() && song.patterns.size() == again.patterns.size()
  307. && song.playlist.size() == again.playlist.size());
  308. bool same = true;
  309. for (std::size_t p = 0; p < song.patterns.size() && p < again.patterns.size(); ++p)
  310. same = same && song.patterns[p].notes == again.patterns[p].notes;
  311. CHECK (same);
  312. // only LYNX's own instruments: no samples, no plugins
  313. for (const auto& c : song.channels)
  314. {
  315. CHECK (c.samplePath.empty());
  316. CHECK (c.instrument != InstrumentKind::plugin && c.instrument != InstrumentKind::sampler);
  317. }
  318. // Simple tracks: drums and three instruments, each with notes; a key, one-knob effects, a Master look
  319. int drums = 0, instruments = 0;
  320. for (int t = 0; t < (int) song.playlistTracks.size(); ++t)
  321. if (const auto k = simpletracks::kindOf (song, t))
  322. {
  323. drums += *k == simpletracks::Kind::drums ? 1 : 0;
  324. instruments += *k == simpletracks::Kind::instrument ? 1 : 0;
  325. }
  326. CHECK (drums == 1 && instruments == 3);
  327. for (int step : { tour::beat, tour::key, tour::instrument, tour::chords, tour::effect, tour::finish })
  328. CHECK (tour::satisfied (song, step));
  329. CHECK (song.info.comments.find ("LYNX") != std::string::npos && song.info.author == "LYNX SHARE");
  330. // the bass in a bass register (keys C3 … B3 + a fifth: "Reese Bass" sounds an octave and two under the key)
  331. for (int t = 0; t < (int) song.playlistTracks.size(); ++t)
  332. if (song.playlistTracks[(std::size_t) t].name == "Bass")
  333. if (const auto p = simpletracks::patternOf (song, t); p >= 0)
  334. for (const auto& n : song.patterns[(std::size_t) p].notes)
  335. CHECK (n.key >= 48 && n.key <= 66);
  336. CHECK (song.bpm == (d == 1 ? 90.0 : 112.0));
  337. // 16 bars long
  338. std::int64_t end = 0;
  339. for (const auto& c : song.playlist)
  340. end = std::max (end, c.start + c.length);
  341. CHECK (end == 16 * 4 * (std::int64_t) ppq);
  342. }
  343. }
  344. TEST (tour190dv_demo_songs_render_clean_at_about_minus_14_lufs)
  345. {
  346. using namespace lynx;
  347. const auto report = std::getenv ("LYNX_190DV_REPORT") != nullptr;
  348. for (int d = 1; d < demos::count; ++d)
  349. {
  350. const auto m = tour190dv::render (demos::make (d));
  351. if (report)
  352. std::printf (" demo %d %s: %.1f s, %.2f LUFS, true peak %.2f dBTP, sample peak %.2f dBFS\n", d, demos::name (d), m.seconds, m.lufs,
  353. m.truePeak, m.samplePeak);
  354. CHECK (m.finite);
  355. CHECK (m.seconds > 30.0 && m.seconds < 50.0);
  356. CHECK (m.lufs > -15.0 && m.lufs < -13.0); // (about −14 LUFS: the streaming level)
  357. CHECK (m.samplePeak < -0.5); // no clipping
  358. CHECK (m.truePeak < -0.5); // (the Apex Limiter's true-peak ceiling: −1 dBTP)
  359. }
  360. }