Video174dTests.inc 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361
  1. // --- Video tracks and ADR (round 174d, docs/analise/REA_rodada174d.md)
  2. #include "core/VideoTime.h"
  3. TEST (video174d_rates)
  4. {
  5. using namespace lynx::vtime;
  6. CHECK (rateAt (0).num == 24000 && rateAt (0).den == 1001 && rateAt (0).nominal() == 24);
  7. CHECK (rateAt (3).drop && rateAt (3).nominal() == 30 && ! rateAt (4).drop);
  8. for (int i = 0; i < rateCount; ++i)
  9. {
  10. CHECK (indexOf (rateAt (i)) == i);
  11. CHECK (rateFromId (rateId (rateAt (i))) == rateAt (i));
  12. }
  13. CHECK (rateName (rateAt (3)) == "29.97 fps drop frame");
  14. CHECK (rateName (rateAt (0)) == "23.976 fps" && rateName (rateAt (2)) == "25 fps" && rateName (rateAt (9)) == "59.94 fps");
  15. // a file's measured rate: the non-drop listed rate
  16. CHECK (nearestRate (29.97002997) == rateAt (4));
  17. CHECK (nearestRate (23.976) == rateAt (0));
  18. CHECK (nearestRate (25.0) == rateAt (2));
  19. CHECK (nearestRate (12.5)->fps() == 12.5);
  20. CHECK (! nearestRate (0.0));
  21. }
  22. TEST (video174d_drop_frame_timecode)
  23. {
  24. using namespace lynx::vtime;
  25. const auto df = rateAt (3), ndf = rateAt (4), df60 = rateAt (8);
  26. // the minute boundaries: 00:00:59;29 → 00:01:00;02 (labels ;00 and ;01 skipped), but 00:10:00;00 exists
  27. CHECK (format (toTimecode (1799, df), df) == "00:00:59;29");
  28. CHECK (format (toTimecode (1800, df), df) == "00:01:00;02");
  29. CHECK (format (toTimecode (17982, df), df) == "00:10:00;00");
  30. CHECK (format (toTimecode (17981, df), df) == "00:09:59;29");
  31. // one hour of 29.97 DF = 107892 frames = 01:00:00;00 (drop frame keeps the clock)
  32. CHECK (format (toTimecode (107892, df), df) == "01:00:00;00");
  33. CHECK (format (toTimecode (107892, ndf), ndf) == "00:59:56:12");
  34. // 59.94 DF drops four labels
  35. CHECK (format (toTimecode (3600, df60), df60) == "00:01:00;04");
  36. // round trip over many frames
  37. bool ok = true;
  38. for (std::int64_t f = 0; f < 200000; f += 7)
  39. for (const auto& r : { df, ndf, df60, rateAt (2), rateAt (0) })
  40. ok = ok && toFrame (toTimecode (f, r), r) == f;
  41. CHECK (ok);
  42. // frame boundaries: k / fps is frame k
  43. CHECK (frameAt (frameStart (12345, ndf), ndf) == 12345);
  44. CHECK (frameAt (frameStart (12345, ndf) - 1e-4, ndf) == 12344);
  45. CHECK (format (-1.0, rateAt (2)) == "-00:00:01:00");
  46. }
  47. TEST (video174d_timecode_parse)
  48. {
  49. using namespace lynx::vtime;
  50. const auto r25 = rateAt (2), df = rateAt (3);
  51. CHECK (parse ("01:02:03:04", r25) == (Timecode { 1, 2, 3, 4, false }));
  52. CHECK (parse ("1:2:3:4", r25) == (Timecode { 1, 2, 3, 4, false }));
  53. CHECK (parse ("1020304", r25) == (Timecode { 1, 2, 3, 4, false }));
  54. CHECK (parse ("3:04", r25) == (Timecode { 0, 0, 3, 4, false }));
  55. CHECK (parse (" 10.00.00.00 ", r25) == (Timecode { 10, 0, 0, 0, false }));
  56. CHECK (! parse ("00:00:00:25", r25)); // 25 fps: frames 0…24
  57. CHECK (! parse ("00:61:00:00", r25));
  58. CHECK (! parse ("1:2:x", r25));
  59. CHECK (! parse ("", r25));
  60. // a dropped label goes to the minute's first frame
  61. CHECK (parse ("00:01:00;00", df) == (Timecode { 0, 1, 0, 2, false }));
  62. const auto s = parseSeconds ("00:00:01:00", r25);
  63. CHECK (s && std::abs (*s - 1.0) < 1e-9);
  64. const auto h = parseSeconds ("01:00:00;00", df);
  65. CHECK (h && std::abs (*h - 107892.0 * 1001.0 / 30000.0) < 1e-9);
  66. }
  67. #include "core/VideoTrack.h"
  68. TEST (video174d_track_showing_and_edits)
  69. {
  70. using namespace lynx;
  71. VideoTrack t;
  72. CHECK (videotrack::addClip (t, "a.mp4", 10.0, 30.0, 1) == 0); // 10…40
  73. CHECK (videotrack::addClip (t, "b.mp4", 50.0, 20.0, 2) == 1); // 50…70
  74. CHECK (! videotrack::showing (t, 5.0));
  75. const auto s = videotrack::showing (t, 15.0);
  76. CHECK (s && s->clip == 0 && std::abs (s->fileSeconds - 5.0) < 1e-9);
  77. CHECK (! videotrack::showing (t, 45.0));
  78. CHECK (videotrack::showing (t, 69.9)->clip == 1);
  79. t.muted = true;
  80. CHECK (! videotrack::showing (t, 15.0));
  81. t.muted = false;
  82. // trimming the head moves the file offset with it, never before the file's start
  83. auto c = t.clips[0];
  84. videotrack::trimStart (c, 12.0, 0.04);
  85. CHECK (std::abs (c.start - 12.0) < 1e-9 && std::abs (c.offset - 2.0) < 1e-9 && std::abs (c.end() - 40.0) < 1e-9);
  86. videotrack::trimStart (c, 0.0, 0.04);
  87. CHECK (std::abs (c.start - 10.0) < 1e-9 && std::abs (c.offset) < 1e-9);
  88. videotrack::trimEnd (c, 100.0, 0.04); // the file has 30 s
  89. CHECK (std::abs (c.end() - 40.0) < 1e-9);
  90. videotrack::trimEnd (c, 20.0, 0.04);
  91. CHECK (std::abs (c.length - 10.0) < 1e-9);
  92. // a clip moved over another cuts it (the part after stays, its offset follows)
  93. const auto moved = videotrack::moveClip (t, 1, 30.0); // b: 30…50 over a's 30…40
  94. CHECK (t.clips.size() == 2 && moved == 1);
  95. CHECK (std::abs (t.clips[0].end() - 30.0) < 1e-9 && std::abs (t.clips[1].start - 30.0) < 1e-9);
  96. // a clip dropped inside another splits it in three
  97. VideoTrack u;
  98. videotrack::addClip (u, "a.mp4", 0.0, 60.0, 1);
  99. videotrack::addClip (u, "b.mp4", 20.0, 10.0, 2);
  100. CHECK (u.clips.size() == 3 && u.clips[1].path == "b.mp4" && std::abs (u.clips[2].start - 30.0) < 1e-9 && std::abs (u.clips[2].offset - 30.0) < 1e-9);
  101. CHECK (u.clips[2].uid != 1 && u.clips[2].uid != 2);
  102. // split
  103. CHECK (videotrack::splitClip (u, 0, 10.0, 9));
  104. CHECK (u.clips.size() == 4 && std::abs (u.clips[1].offset - 10.0) < 1e-9 && u.clips[1].uid == 9);
  105. CHECK (! videotrack::splitClip (u, 0, 0.0, 10));
  106. CHECK (std::abs (videotrack::videoEnd ({ t, u }) - 60.0) < 1e-9);
  107. }
  108. TEST (video174d_timecode_clock_and_tempo)
  109. {
  110. using namespace lynx;
  111. VideoSettings v;
  112. v.rate = vtime::rateAt (2); // 25
  113. v.startTimecode = 3600.0;
  114. CHECK (vtime::format (videotrack::timecodeOf (v, 1.5), v.rate) == "01:00:01:12");
  115. CHECK (std::abs (videotrack::snapToFrame (v, 1.019) - 1.0) < 1e-9);
  116. CHECK (std::abs (videotrack::snapToFrame (v, 1.021) - 1.04) < 1e-9);
  117. // ticks ↔ seconds: the inverse of Song::secondsForTicks, with and without tempo automation
  118. auto song = Song::makeDefault();
  119. song.bpm = 120.0;
  120. CHECK (std::abs (videotrack::ticksForSeconds (song, 1.0) - 2.0 * ppq) < 1e-6);
  121. for (const auto ticks : { (std::int64_t) 0, (std::int64_t) ppq * 7 + 13, (std::int64_t) ppq * 333 })
  122. CHECK (std::abs (videotrack::ticksForSeconds (song, song.secondsForTicks (ticks)) - (double) ticks) < 1e-3);
  123. }
  124. TEST (video174d_tempo_map)
  125. {
  126. using namespace lynx;
  127. auto song = Song::makeDefault();
  128. song.bpm = 100.0;
  129. const videotrack::TempoMap flat (song, 60.0);
  130. CHECK (std::abs (flat.ticks (3.0) - 5.0 * ppq) < 1e-6 && std::abs (flat.seconds (5.0 * ppq) - 3.0) < 1e-9);
  131. // with tempo automation: the map agrees with Song::secondsForTicks and inverts itself
  132. song.mode = PlayMode::song;
  133. Automation a;
  134. a.target.kind = AutomationTargetKind::tempo;
  135. a.points = { { 0, 0.2f }, { (std::int64_t) ppq * 32, 0.8f } };
  136. song.automations.push_back (a);
  137. Clip c;
  138. c.kind = ClipKind::automation;
  139. c.automation = 0;
  140. c.start = 0;
  141. c.length = (std::int64_t) ppq * 32;
  142. song.playlist.push_back (c);
  143. const videotrack::TempoMap map (song, 120.0);
  144. bool ok = true;
  145. for (const auto ticks : { (std::int64_t) 0, (std::int64_t) ppq * 3, (std::int64_t) ppq * 17 + 5, (std::int64_t) ppq * 40 })
  146. {
  147. const auto s = song.secondsForTicks (ticks);
  148. ok = ok && std::abs (map.seconds ((double) ticks) - s) < 1e-6 && std::abs (map.ticks (s) - (double) ticks) < 1e-3;
  149. ok = ok && std::abs (videotrack::ticksForSeconds (song, s) - (double) ticks) < 1e-3;
  150. }
  151. CHECK (ok);
  152. }
  153. #include "core/Adr.h"
  154. #include "core/DialogueAlign.h"
  155. TEST (video174d_adr_cues_names_csv)
  156. {
  157. using namespace lynx;
  158. std::vector<AdrCue> cues;
  159. CHECK (adr::nextId (cues) == "1");
  160. AdrCue a;
  161. a.id = "SC1-09";
  162. a.in = 12.0;
  163. a.out = 14.52;
  164. a.character = "Dr. Silva";
  165. a.line = "Say \"hello\", then go";
  166. a.notes = "softer";
  167. cues.push_back (a);
  168. CHECK (adr::nextId (cues) == "SC1-10");
  169. CHECK (adr::takeName (a, 3) == "SC1-09_Dr.-Silva_T3");
  170. CHECK (adr::nextTakeNumber (a) == 1);
  171. a.takes.push_back ({ 4, "x", 0, 0, false, "", false });
  172. CHECK (adr::nextTakeNumber (a) == 5);
  173. AdrCue b;
  174. b.id = "101";
  175. b.in = 2.0;
  176. b.out = 3.0;
  177. b.status = AdrCue::Status::approved;
  178. cues.push_back (b);
  179. adr::sortByTime (cues);
  180. CHECK (cues[0].id == "101" && adr::cueAt (cues, 13.0) == 1 && adr::cueAt (cues, 5.0) == -1);
  181. // CSV round trip at 25 fps from 01:00:00:00
  182. const auto rate = vtime::rateAt (2);
  183. const auto csv = adr::toCsv (cues, rate, 3600.0);
  184. CHECK (csv.find ("101,01:00:02:00,01:00:03:00,,,,Approved") != std::string::npos);
  185. CHECK (csv.find ("\"Say \"\"hello\"\", then go\"") != std::string::npos);
  186. const auto back = adr::fromCsv (csv, rate, 3600.0);
  187. CHECK (back.error.empty() && back.cues.size() == 2 && back.skipped == 0);
  188. CHECK (back.cues[1].line == a.line && back.cues[1].character == "Dr. Silva" && std::abs (back.cues[1].in - 12.0) < 1e-6
  189. && std::abs (back.cues[1].out - 14.52) < 1e-6 && back.cues[0].status == AdrCue::Status::approved);
  190. // other spreadsheets: semicolons, a BOM, other column names and order, seconds, a row without In
  191. const auto other = adr::fromCsv ("\xEF\xBB\xBFRole;Dialogue;Start;End\r\nAna;Oi;5,5;7\r\nBia;Tchau;;\r\nCaio;Hum;9;8\r\n", rate, 0.0);
  192. CHECK (other.error.empty() && other.cues.size() == 2 && other.skipped == 1);
  193. CHECK (other.cues[0].character == "Ana" && std::abs (other.cues[0].in - 5.5) < 1e-9 && std::abs (other.cues[0].out - 7.0) < 1e-9
  194. && other.cues[0].id == "1" && other.cues[1].id == "2");
  195. CHECK (std::abs (other.cues[1].out - 11.0) < 1e-9); // (Out before In: In + 2 s)
  196. CHECK (! adr::fromCsv ("a,b\n1,2\n", rate, 0.0).error.empty());
  197. }
  198. TEST (video174d_adr_signals_and_comp)
  199. {
  200. using namespace lynx;
  201. AdrCue c;
  202. c.in = 10.0;
  203. c.out = 12.0;
  204. AdrSettings s;
  205. const auto beeps = adr::beepTimes (c.in);
  206. CHECK (beeps.size() == 3 && beeps[0] == 7.0 && beeps[2] == 9.0);
  207. const auto tone = adr::renderBeep (48000.0, 1000.0);
  208. CHECK (tone.size() == 1920 && std::abs (tone.front()) < 1e-6 && std::abs (tone[480 + 12]) > 0.2f);
  209. CHECK (! adr::signalsAt (c, s, 7.9, 0.04).streamer);
  210. CHECK (std::abs (*adr::signalsAt (c, s, 9.0, 0.04).streamer - 0.5) < 1e-9);
  211. CHECK (adr::signalsAt (c, s, 10.02, 0.04).punch && ! adr::signalsAt (c, s, 10.1, 0.04).punch);
  212. CHECK (adr::signalsAt (c, s, 11.0, 0.04).inLine && ! adr::signalsAt (c, s, 12.0, 0.04).inLine);
  213. // comp: take 1 plays, take 3 from 10.5 to 11, take 2 from 11.5 on
  214. c.selected = 1;
  215. adr::setCompRange (c, 10.5, 11.0, 3);
  216. adr::setCompRange (c, 11.5, 13.0, 2);
  217. auto spans = adr::compSpans (c);
  218. CHECK (spans.size() == 4 && spans[0].take == 1 && spans[1].take == 3 && spans[2].take == 1 && spans[3].take == 2 && spans[3].to == 12.0);
  219. // a range over the same take's neighbour merges; over another cuts it
  220. adr::setCompRange (c, 10.8, 11.6, 3);
  221. spans = adr::compSpans (c);
  222. CHECK (spans.size() == 3 && spans[1].take == 3 && std::abs (spans[1].from - 10.5) < 1e-9 && std::abs (spans[1].to - 11.6) < 1e-9
  223. && spans[2].take == 2 && std::abs (spans[2].from - 11.6) < 1e-9);
  224. }
  225. TEST (video174d_dialogue_alignment)
  226. {
  227. using namespace lynx;
  228. // a "line": syllables (harmonic bursts of different pitches and vowel colours) with pauses; the take says the same
  229. // syllables 0.15 s later, the middle ones slower and the pauses changed
  230. constexpr double sr = 16000.0;
  231. struct Syl { double start, length, f0; };
  232. const std::vector<Syl> guideSyl { { 0.20, 0.18, 140 }, { 0.45, 0.12, 220 }, { 0.62, 0.25, 170 }, { 1.05, 0.15, 260 }, { 1.30, 0.20, 120 } };
  233. const std::vector<Syl> takeSyl { { 0.35, 0.18, 140 }, { 0.62, 0.16, 220 }, { 0.86, 0.32, 170 }, { 1.30, 0.15, 260 }, { 1.52, 0.20, 120 } };
  234. auto render = [&] (const std::vector<Syl>& syl, double total) {
  235. std::vector<float> x ((std::size_t) (total * sr), 0.0f);
  236. std::uint32_t noise = 1;
  237. for (std::size_t i = 0; i < x.size(); ++i)
  238. {
  239. noise = noise * 1664525u + 1013904223u;
  240. x[i] = 0.002f * ((float) (noise >> 9) / 8388608.0f - 1.0f); // room noise
  241. }
  242. for (const auto& s : syl)
  243. for (auto i = (std::size_t) (s.start * sr); i < (std::size_t) ((s.start + s.length) * sr) && i < x.size(); ++i)
  244. {
  245. // (speech glides: the pitch rises 40 % over the syllable, so its inside has a shape to follow)
  246. const auto t = (double) i / sr - s.start;
  247. const auto env = std::sin (3.14159265 * t / s.length);
  248. const auto phase = 2 * 3.14159265 * s.f0 * (t + 0.2 * t * t / s.length);
  249. double v = 0.0;
  250. for (int h = 1; h <= 12; ++h)
  251. v += std::sin (phase * h) / h * (h == (int) (s.f0 / 50) % 7 + 2 ? 3.0 : 1.0);
  252. x[i] += (float) (0.15 * env * v);
  253. }
  254. return x;
  255. };
  256. const auto guide = render (guideSyl, 1.7), take = render (takeSyl, 1.9);
  257. const auto map = align::align (guide, take, sr);
  258. CHECK (! map.empty());
  259. double worst = 0.0;
  260. for (std::size_t k = 0; k < guideSyl.size(); ++k)
  261. for (const auto f : { 0.25, 0.5, 0.75 })
  262. {
  263. const auto g = guideSyl[k].start + f * guideSyl[k].length;
  264. const auto want = takeSyl[k].start + f * takeSyl[k].length;
  265. worst = std::max (worst, std::abs (map.takeAt (g) - want));
  266. if (std::getenv ("LYNX_ALIGN_DEBUG"))
  267. std::printf (" syllable %d at %.2f: %.1f ms\n", (int) k, f, (map.takeAt (g) - want) * 1000.0);
  268. }
  269. std::printf (" dialogue alignment: worst syllable error %.1f ms\n", worst * 1000.0);
  270. CHECK (worst < 0.025);
  271. // the warped take: its syllables sit where the guide's are (the energy envelope follows the guide's)
  272. const auto warped = align::warp (take, sr, map, guide.size());
  273. CHECK (warped.size() == guide.size());
  274. auto energy = [&] (const std::vector<float>& x, double from, double to) {
  275. double e = 0.0;
  276. for (auto i = (std::size_t) (from * sr); i < (std::size_t) (to * sr) && i < x.size(); ++i)
  277. e += (double) x[i] * x[i];
  278. return e;
  279. };
  280. bool onTime = true;
  281. for (const auto& s : guideSyl)
  282. onTime = onTime && energy (warped, s.start + 0.02, s.start + s.length - 0.02) > 20.0 * energy (warped, s.start - 0.06, s.start - 0.02) + 1e-9;
  283. CHECK (onTime);
  284. }
  285. TEST (video174d_player_clock_follows_the_tempo)
  286. {
  287. // FL's Video Player clock is the song's real time (round 174d A3): 120 BPM for 4 bars, then 60 BPM
  288. using namespace lynx;
  289. Song song;
  290. song.bpm = 120.0;
  291. song.mode = PlayMode::song;
  292. const auto bar = (std::int64_t) ppq * 4;
  293. Automation tempo;
  294. tempo.target = { AutomationTargetKind::tempo, 0, 0, 0 };
  295. tempo.points = { { 0, valueFromTempo (120.0), 0.0f }, { bar * 4 - 1, valueFromTempo (120.0), 0.0f },
  296. { bar * 4, valueFromTempo (60.0), 0.0f }, { bar * 8, valueFromTempo (60.0), 0.0f } };
  297. song.automations.push_back (tempo);
  298. Clip at;
  299. at.kind = ClipKind::automation;
  300. at.automation = 0;
  301. at.start = 0;
  302. at.length = bar * 8;
  303. song.playlist.push_back (at);
  304. video::publishTempo (song);
  305. auto inst = video::makeInstrument();
  306. auto* player = video::playerOf (inst.get());
  307. ChannelDef def;
  308. def.instrument = InstrumentKind::videoplayer;
  309. def.pluginState = video::write (video::Settings {});
  310. inst->prepare (48000.0);
  311. inst->configure (def, song.bpm);
  312. std::vector<float> l (64, 0.0f), r (64, 0.0f);
  313. const auto timeAt = [&] (std::int64_t ticks) {
  314. inst->setSongPosition ((double) ticks);
  315. inst->render (l.data(), r.data(), 64, 1.0f, 1.0f);
  316. return player->currentTime();
  317. };
  318. CHECK (std::abs (timeAt (bar * 2) - 4.0) < 1e-3); // 8 beats at 120
  319. CHECK (std::abs (timeAt (bar * 5) - song.secondsForTicks (bar * 5)) < 1e-3);
  320. CHECK (std::abs (timeAt (bar * 5) - 12.0) < 0.05); // 8 s + 4 beats at 60 (not 10 s)
  321. // no tempo automation: the song's tempo
  322. video::publishTempo (Song {});
  323. inst->configure (def, 120.0);
  324. CHECK (std::abs (timeAt (bar * 5) - 10.0) < 1e-3);
  325. }
  326. TEST (video174d_song_length_counts_the_picture)
  327. {
  328. // a project with only its picture so far plays (and its playhead runs) to the picture's end, not one bar
  329. using namespace lynx;
  330. Song song;
  331. song.bpm = 120.0;
  332. song.mode = PlayMode::song;
  333. song.patterns.emplace_back();
  334. const auto bar = (std::int64_t) ppq * 4;
  335. CHECK (song.songLength() == bar);
  336. VideoTrack vt;
  337. videotrack::addClip (vt, "picture.mp4", 1.0, 12.0, 1);
  338. song.videoTracks.push_back (vt);
  339. CHECK (song.songLength() == 26 * ppq); // 13 s at 120 BPM = 26 beats (the exact end, like FL's clips)
  340. Clip c;
  341. c.pattern = 0;
  342. c.start = 0;
  343. c.length = 40 * ppq;
  344. song.playlist.push_back (c);
  345. CHECK (song.songLength() == 40 * ppq);
  346. }