tests.cpp 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625
  1. // Testes do núcleo (sem JUCE). Uso: daw_tests [pasta-com-arquivos-flp]
  2. #include <cmath>
  3. #include <cstdio>
  4. #include <cstring>
  5. #include <filesystem>
  6. #include <fstream>
  7. #include <functional>
  8. #include <iterator>
  9. #include <string>
  10. #include <vector>
  11. #include "core/FlpImport.h"
  12. #include "core/LiveEventQueue.h"
  13. #include "core/RackRenderer.h"
  14. #include "core/Scheduler.h"
  15. #include "core/SnapshotExchange.h"
  16. #include "core/SoftClip.h"
  17. #include "core/Song.h"
  18. #include "flp/Flp.h"
  19. namespace
  20. {
  21. int failures = 0;
  22. int checks = 0;
  23. std::vector<std::pair<const char*, std::function<void()>>>& registry()
  24. {
  25. static std::vector<std::pair<const char*, std::function<void()>>> r;
  26. return r;
  27. }
  28. struct Register
  29. {
  30. Register (const char* name, std::function<void()> fn) { registry().emplace_back (name, std::move (fn)); }
  31. };
  32. #define TEST(name) \
  33. static void name(); \
  34. static Register reg_##name (#name, name); \
  35. static void name()
  36. #define CHECK(cond) \
  37. do \
  38. { \
  39. ++checks; \
  40. if (! (cond)) \
  41. { \
  42. ++failures; \
  43. std::printf (" FALHOU %s:%d: %s\n", __FILE__, __LINE__, #cond); \
  44. } \
  45. } while (false)
  46. std::string realFlpDir;
  47. // --- utilitários para montar .flp sintéticos -------------------------------
  48. struct FlpBuilder
  49. {
  50. std::vector<std::uint8_t> bytes;
  51. FlpBuilder (std::uint16_t ppq = 96)
  52. {
  53. put ("FLhd");
  54. u32 (6);
  55. u16 (0);
  56. u16 (1);
  57. u16 (ppq);
  58. put ("FLdt");
  59. u32 (0); // tamanho não é usado pelo leitor
  60. }
  61. void put (const char* s) { bytes.insert (bytes.end(), s, s + std::strlen (s)); }
  62. void u8 (std::uint8_t v) { bytes.push_back (v); }
  63. void u16 (std::uint16_t v) { u8 ((std::uint8_t) v); u8 ((std::uint8_t) (v >> 8)); }
  64. void u32 (std::uint32_t v) { u16 ((std::uint16_t) v); u16 ((std::uint16_t) (v >> 16)); }
  65. void varint (std::size_t n)
  66. {
  67. do
  68. {
  69. auto b = (std::uint8_t) (n & 0x7F);
  70. n >>= 7;
  71. u8 (n ? (std::uint8_t) (b | 0x80) : b);
  72. } while (n);
  73. }
  74. void data (std::uint8_t id, const std::vector<std::uint8_t>& payload)
  75. {
  76. u8 (id);
  77. varint (payload.size());
  78. bytes.insert (bytes.end(), payload.begin(), payload.end());
  79. }
  80. void ascii (std::uint8_t id, const std::string& s)
  81. {
  82. std::vector<std::uint8_t> p (s.begin(), s.end());
  83. p.push_back (0);
  84. data (id, p);
  85. }
  86. void utf16 (std::uint8_t id, const std::u16string& s)
  87. {
  88. std::vector<std::uint8_t> p;
  89. for (auto c : s)
  90. {
  91. p.push_back ((std::uint8_t) c);
  92. p.push_back ((std::uint8_t) (c >> 8));
  93. }
  94. p.push_back (0);
  95. p.push_back (0);
  96. data (id, p);
  97. }
  98. };
  99. std::vector<std::uint8_t> noteRecord (std::uint32_t pos, std::uint16_t channel, std::uint32_t len, std::uint16_t key,
  100. std::uint8_t velocity)
  101. {
  102. std::vector<std::uint8_t> r (24, 0);
  103. std::memcpy (&r[0], &pos, 4);
  104. std::memcpy (&r[6], &channel, 2);
  105. std::memcpy (&r[8], &len, 4);
  106. std::memcpy (&r[12], &key, 2);
  107. r[16] = 120;
  108. r[20] = 64;
  109. r[21] = velocity;
  110. return r;
  111. }
  112. } // namespace
  113. // ---------------------------------------------------------------------------
  114. // FLP
  115. TEST (flp_rejects_garbage)
  116. {
  117. std::vector<std::uint8_t> junk (64, 0x41);
  118. bool threw = false;
  119. try { flp::EventReader r (junk); } catch (const flp::Error&) { threw = true; }
  120. CHECK (threw);
  121. }
  122. TEST (flp_reads_size_classes_varint_and_extended_ids)
  123. {
  124. FlpBuilder b;
  125. b.u8 (0x15); b.u8 (2); // u8
  126. b.u8 (0x40); b.u16 (513); // u16
  127. b.u8 (0x9C); b.u32 (128500); // u32
  128. b.data (0xD5, std::vector<std::uint8_t> (300, 7)); // varint de 2 bytes
  129. b.u8 (0xAC); b.u32 (0xC0000100); b.varint (3); b.u8 (1); b.u8 (2); b.u8 (3); // ID estendido
  130. flp::EventReader r (b.bytes);
  131. flp::Event e;
  132. CHECK (r.header().ppq == 96);
  133. CHECK (r.next (e) && e.id == 0x15 && e.value == 2);
  134. CHECK (r.next (e) && e.id == 0x40 && e.value == 513);
  135. CHECK (r.next (e) && e.id == 0x9C && e.value == 128500);
  136. CHECK (r.next (e) && e.id == 0xD5 && e.data.size() == 300 && e.data[299] == 7);
  137. CHECK (r.next (e) && e.id == 0xC0000100 && e.isData() && e.data.size() == 3 && e.data[2] == 3);
  138. CHECK (! r.next (e));
  139. }
  140. TEST (flp_truncated_file_throws)
  141. {
  142. FlpBuilder b;
  143. b.u8 (0x9C); b.u8 (1); // u32 com só 1 byte
  144. flp::EventReader r (b.bytes);
  145. flp::Event e;
  146. bool threw = false;
  147. try { r.next (e); } catch (const flp::Error&) { threw = true; }
  148. CHECK (threw);
  149. }
  150. TEST (flp_version_numbers)
  151. {
  152. CHECK (flp::parseVersionNumber ("25.2.5.5319") == 25002005u);
  153. CHECK (flp::parseVersionNumber ("20.99.3000.3031") == 20102000u);
  154. CHECK (flp::parseVersionNumber ("11.0.3") == 11000003u);
  155. }
  156. TEST (flp_playlist_record_size_by_version)
  157. {
  158. CHECK (flp::playlistRecordSize (12009003, 64) == 32);
  159. CHECK (flp::playlistRecordSize (21000000, 120) == 60);
  160. CHECK (flp::playlistRecordSize (25002005, 160) == 80);
  161. CHECK (flp::playlistRecordSize (25002005, 60) == 60); // tamanho não bate com a versão → tenta os outros
  162. CHECK (flp::playlistRecordSize (25002005, 7) == 0);
  163. }
  164. TEST (flp_parses_project_model)
  165. {
  166. FlpBuilder b (96);
  167. b.ascii (0xC7, "25.2.5.5319");
  168. b.u8 (0x9C); b.u32 (174500);
  169. b.utf16 (0xC2, u"Título");
  170. b.u8 (0x41); b.u16 (3);
  171. auto notes = noteRecord (96, 1, 48, 64, 100);
  172. auto n2 = noteRecord (0, 0, 0, 60, 128);
  173. notes.insert (notes.end(), n2.begin(), n2.end());
  174. b.data (0xE0, notes);
  175. b.u8 (0x40); b.u16 (0);
  176. b.u8 (0x15); b.u8 (0);
  177. b.utf16 (0xCB, u"Kick");
  178. b.u8 (0x68); b.u16 (5);
  179. b.u8 (0x40); b.u16 (1);
  180. b.u8 (0x15); b.u8 (2);
  181. b.utf16 (0xC9, u"FLEX");
  182. b.utf16 (0xCB, u"Lead");
  183. b.u8 (0x63); b.u16 (0);
  184. std::vector<std::uint8_t> item (80, 0);
  185. std::uint32_t pos = 384, len = 768;
  186. std::uint16_t base = 20480, idx = 20480 + 3, rv = 499 - 2;
  187. std::memcpy (&item[0], &pos, 4);
  188. std::memcpy (&item[4], &base, 2);
  189. std::memcpy (&item[6], &idx, 2);
  190. std::memcpy (&item[8], &len, 4);
  191. std::memcpy (&item[12], &rv, 2);
  192. b.data (0xE9, item);
  193. b.utf16 (0xCC, u"Drums");
  194. b.data (0xEC, std::vector<std::uint8_t> (12, 0));
  195. b.data (0xEC, std::vector<std::uint8_t> (12, 0));
  196. const auto p = flp::parseProject (b.bytes);
  197. CHECK (p.version == "25.2.5.5319");
  198. CHECK (std::abs (p.tempo - 174.5) < 1e-9);
  199. CHECK (p.title == "Título");
  200. CHECK (p.channels.size() == 2);
  201. CHECK (p.channels[0].name == "Kick" && p.channels[0].mixerTrack == 5);
  202. CHECK (p.channels[1].type == flp::ChannelType::instrument && p.channels[1].pluginName == "FLEX");
  203. CHECK (p.patterns.size() == 1 && p.patterns[0].index == 3);
  204. CHECK (p.patterns[0].notes.size() == 2 && p.patterns[0].notes[0].position == 0); // ordenadas
  205. CHECK (p.patterns[0].notes[1].key == 64 && p.patterns[0].notes[1].length == 48);
  206. CHECK (p.arrangements.size() == 1 && p.arrangements[0].items.size() == 1);
  207. const auto& it = p.arrangements[0].items[0];
  208. CHECK (it.kind == flp::PlaylistItem::Kind::pattern && it.index == 3 && it.track == 2 && it.position == 384);
  209. CHECK (p.mixer.size() == 2 && p.mixer[0].name == "Drums" && p.mixer[1].name.empty());
  210. CHECK (p.warnings.empty());
  211. }
  212. TEST (flp_real_files)
  213. {
  214. if (realFlpDir.empty())
  215. {
  216. std::printf (" (pulado: passe uma pasta com .flp como argumento)\n");
  217. return;
  218. }
  219. int files = 0, withNotes = 0;
  220. for (auto& entry : std::filesystem::recursive_directory_iterator (realFlpDir))
  221. {
  222. if (entry.path().extension() != ".flp")
  223. continue;
  224. std::ifstream in (entry.path(), std::ios::binary);
  225. std::vector<std::uint8_t> bytes ((std::istreambuf_iterator<char> (in)), std::istreambuf_iterator<char>());
  226. ++files;
  227. try
  228. {
  229. const auto p = flp::parseProject (bytes);
  230. CHECK (! p.version.empty());
  231. CHECK (p.tempo > 10.0 && p.tempo < 1000.0);
  232. const auto song = daw::importFlp (p);
  233. CHECK (! song.patterns.empty());
  234. for (auto& pat : p.patterns)
  235. if (! pat.notes.empty())
  236. {
  237. ++withNotes;
  238. break;
  239. }
  240. for (auto& a : p.arrangements)
  241. for (auto& item : a.items)
  242. CHECK (item.track >= 0 && item.track < 500);
  243. }
  244. catch (const flp::Error& e)
  245. {
  246. ++failures;
  247. std::printf (" FALHOU %s: %s\n", entry.path().string().c_str(), e.what());
  248. }
  249. }
  250. std::printf (" %d arquivos .flp lidos, %d com notas\n", files, withNotes);
  251. CHECK (files > 0);
  252. }
  253. // ---------------------------------------------------------------------------
  254. // Sequenciador
  255. TEST (pattern_toggle_step_and_length)
  256. {
  257. daw::Pattern p;
  258. CHECK (! p.hasStep (0, 4));
  259. p.toggleStep (0, 4);
  260. CHECK (p.hasStep (0, 4));
  261. CHECK (p.notes.size() == 1 && p.notes[0].start == 4 * daw::ticksPerStep);
  262. CHECK (p.effectiveLength (4) == 4 * daw::ppq); // um compasso
  263. p.toggleStep (1, 20); // segundo compasso
  264. CHECK (p.effectiveLength (4) == 8 * daw::ppq);
  265. p.toggleStep (0, 4);
  266. CHECK (! p.hasStep (0, 4) && p.notes.size() == 1);
  267. }
  268. TEST (scheduler_places_events_at_exact_samples)
  269. {
  270. daw::Pattern p;
  271. p.toggleStep (0, 0);
  272. p.toggleStep (0, 4); // tick 960 = 1 tempo
  273. const double sr = 48000.0, bpm = 120.0; // 1 tempo = 0,5 s = 24000 samples
  274. const auto tps = daw::ticksPerSample (bpm, sr);
  275. daw::EventBuffer ev;
  276. daw::schedulePatternLoop (p, p.effectiveLength (4), 23900.0 * tps, 512, tps, ev);
  277. // a nota do tempo 2 começa no sample 24000 → offset 100 neste bloco
  278. int ons = 0;
  279. for (auto& e : ev)
  280. if (e.type == daw::NoteEvent::Type::on)
  281. {
  282. ++ons;
  283. CHECK (e.sampleOffset == 100);
  284. }
  285. CHECK (ons == 1);
  286. }
  287. TEST (scheduler_wraps_the_loop)
  288. {
  289. daw::Pattern p;
  290. p.toggleStep (2, 0);
  291. const auto len = p.effectiveLength (4); // 3840 ticks
  292. const double sr = 48000.0;
  293. const auto tps = daw::ticksPerSample (120.0, sr);
  294. daw::EventBuffer ev;
  295. // bloco que atravessa o fim do loop: começa 10 samples antes
  296. const auto start = (double) len - 10.0 * tps;
  297. daw::schedulePatternLoop (p, len, start, 64, tps, ev);
  298. bool found = false;
  299. for (auto& e : ev)
  300. if (e.type == daw::NoteEvent::Type::on && e.channel == 2)
  301. {
  302. found = true;
  303. CHECK (e.sampleOffset == 10);
  304. }
  305. CHECK (found);
  306. }
  307. TEST (scheduler_off_before_on_at_same_sample)
  308. {
  309. daw::Pattern p;
  310. daw::Note a;
  311. a.start = 0; a.length = 240; a.channel = 0;
  312. daw::Note b = a;
  313. b.start = 240;
  314. p.notes = { a, b };
  315. const auto tps = daw::ticksPerSample (120.0, 48000.0);
  316. daw::EventBuffer ev;
  317. daw::schedulePatternLoop (p, p.effectiveLength (4), 200.0, 4000, tps, ev);
  318. CHECK (ev.size() >= 2);
  319. CHECK (ev[0].type == daw::NoteEvent::Type::off);
  320. CHECK (ev[1].type == daw::NoteEvent::Type::on);
  321. CHECK (ev[0].sampleOffset == ev[1].sampleOffset);
  322. }
  323. TEST (snapshot_exchange_hands_over_and_recycles)
  324. {
  325. struct Counted
  326. {
  327. int value;
  328. int* alive;
  329. Counted (int v, int* a) : value (v), alive (a) { ++*alive; }
  330. ~Counted() { --*alive; }
  331. };
  332. int alive = 0;
  333. {
  334. daw::SnapshotExchange<Counted> x;
  335. CHECK (x.acquire() == nullptr);
  336. x.publish (std::make_unique<Counted> (1, &alive));
  337. CHECK (x.acquire()->value == 1);
  338. x.publish (std::make_unique<Counted> (2, &alive));
  339. x.publish (std::make_unique<Counted> (3, &alive)); // 2 nunca foi lido: liberado na hora
  340. CHECK (alive == 2);
  341. CHECK (x.acquire()->value == 3);
  342. CHECK (alive == 2); // o 1 foi devolvido, mas só a UI libera
  343. x.collectGarbage();
  344. CHECK (alive == 1);
  345. // sem coleta, as vagas de devolução enchem e o áudio segura o atual
  346. for (int i = 0; i < 20; ++i)
  347. {
  348. x.publish (std::make_unique<Counted> (10 + i, &alive));
  349. CHECK (x.acquire() != nullptr);
  350. }
  351. }
  352. CHECK (alive == 0);
  353. }
  354. TEST (rack_renderer_makes_sound_and_reuses_instruments)
  355. {
  356. auto song = daw::Song::makeDefault();
  357. song.bpm = 120.0;
  358. const double sr = 48000.0;
  359. auto state = daw::RackState::build (song, nullptr, sr);
  360. daw::RackRenderer r;
  361. r.prepare (sr);
  362. std::vector<float> l (512), rr (512);
  363. double tick = 0.0;
  364. float peak = 0.0f;
  365. int kicks = 0;
  366. for (int block = 0; block < 93; ++block) // 47616 samples: logo antes de 1 s
  367. {
  368. r.process (state.get(), true, tick, 512, l.data(), rr.data());
  369. for (auto& e : r.lastEvents())
  370. if (e.type == daw::NoteEvent::Type::on && e.channel == 0)
  371. ++kicks;
  372. for (int i = 0; i < 512; ++i)
  373. {
  374. CHECK (std::isfinite (l[(std::size_t) i]));
  375. peak = std::max (peak, std::abs (l[(std::size_t) i]));
  376. }
  377. tick += 512 * daw::ticksPerSample (song.bpm, sr);
  378. }
  379. CHECK (peak > 0.1f && peak < 4.0f);
  380. CHECK (kicks == 2); // 120 BPM: tempos em 0 e 0,5 s
  381. // editar a música preserva os instrumentos de canais que não mudaram de tipo
  382. song.channels[4].instrument = daw::InstrumentKind::kick;
  383. auto next = daw::RackState::build (song, state.get(), sr);
  384. CHECK (next->instruments[0] == state->instruments[0]);
  385. CHECK (next->instruments[4] != state->instruments[4]);
  386. // parar silencia o synth e não dispara notas novas
  387. r.process (next.get(), false, tick, 512, l.data(), rr.data());
  388. CHECK (r.lastEvents().size() == 0);
  389. }
  390. TEST (flp_import_maps_channels_and_rescales)
  391. {
  392. flp::Project p;
  393. p.header.ppq = 96;
  394. p.tempo = 150.0;
  395. p.channels = { { 0, flp::ChannelType::sampler, "Kick 808", "", "", 1 },
  396. { 1, flp::ChannelType::audioClip, "Vocal", "", "", 2 },
  397. { 2, flp::ChannelType::instrument, "Lead", "FLEX", "", 3 } };
  398. flp::Pattern pat;
  399. pat.index = 1;
  400. flp::Note n;
  401. n.position = 96;
  402. n.length = 0;
  403. n.channel = 2;
  404. n.key = 72;
  405. n.velocity = 64;
  406. pat.notes.push_back (n);
  407. p.patterns.push_back (pat);
  408. const auto s = daw::importFlp (p);
  409. CHECK (s.bpm == 150.0);
  410. CHECK (s.channels.size() == 2); // o clip de áudio fica de fora
  411. CHECK (s.channels[0].instrument == daw::InstrumentKind::kick);
  412. CHECK (s.channels[1].instrument == daw::InstrumentKind::synth);
  413. CHECK (s.patterns.size() == 1);
  414. CHECK (s.patterns[0].notes[0].start == daw::ppq); // 96 @ PPQ 96 = 1 tempo
  415. CHECK (s.patterns[0].notes[0].channel == 1); // índice denso
  416. CHECK (s.patterns[0].notes[0].length == daw::ticksPerStep); // nota de step
  417. CHECK (std::abs (s.patterns[0].notes[0].velocity - 0.5f) < 1e-6f);
  418. }
  419. TEST (live_queue_is_fifo_and_bounded)
  420. {
  421. daw::LiveEventQueue q;
  422. daw::NoteEvent e;
  423. CHECK (! q.pop (e));
  424. for (int i = 0; i < 10; ++i)
  425. {
  426. daw::NoteEvent in;
  427. in.key = i;
  428. CHECK (q.push (in));
  429. }
  430. for (int i = 0; i < 10; ++i)
  431. CHECK (q.pop (e) && e.key == i);
  432. int pushed = 0;
  433. while (q.push (e))
  434. ++pushed;
  435. CHECK (pushed == (int) daw::LiveEventQueue::capacity - 1); // uma vaga fica livre para distinguir cheio de vazio
  436. }
  437. TEST (sampler_plays_pitched_one_shot)
  438. {
  439. // sample de 1000 frames com rampa: dá para medir a posição pela amplitude
  440. auto data = std::make_shared<daw::SampleData>();
  441. data->sampleRate = 48000.0;
  442. for (int i = 0; i < 1000; ++i)
  443. data->left.push_back (1.0f);
  444. auto sampler = daw::makeInstrument (daw::InstrumentKind::sampler, data);
  445. sampler->prepare (48000.0);
  446. std::vector<float> l (2048, 0.0f), r (2048, 0.0f);
  447. sampler->noteOn (60, 1.0f);
  448. sampler->noteOff (60); // one-shot: noteOff não corta
  449. sampler->render (l.data(), r.data(), 2048, 1.0f, 1.0f);
  450. int sounding = 0;
  451. for (auto s : l)
  452. sounding += s > 0.5f;
  453. CHECK (sounding >= 995 && sounding <= 1000); // toca o sample inteiro na altura original
  454. std::fill (l.begin(), l.end(), 0.0f);
  455. sampler->noteOn (72, 1.0f); // uma oitava acima: metade da duração
  456. sampler->render (l.data(), r.data(), 2048, 1.0f, 1.0f);
  457. sounding = 0;
  458. for (auto s : l)
  459. sounding += s > 0.5f;
  460. CHECK (sounding >= 495 && sounding <= 500);
  461. CHECK (r[10] > 0.5f); // mono vai para os dois lados
  462. auto silent = daw::makeInstrument (daw::InstrumentKind::sampler, nullptr);
  463. silent->prepare (48000.0);
  464. silent->noteOn (60, 1.0f);
  465. std::fill (l.begin(), l.end(), 0.0f);
  466. silent->render (l.data(), r.data(), 64, 1.0f, 1.0f);
  467. CHECK (l[0] == 0.0f);
  468. }
  469. TEST (rack_live_notes_and_sample_provider)
  470. {
  471. daw::Song song;
  472. daw::ChannelDef c;
  473. c.name = "Smp";
  474. c.instrument = daw::InstrumentKind::sampler;
  475. c.samplePath = "a.wav";
  476. song.channels.push_back (c);
  477. song.patterns.emplace_back();
  478. int loads = 0;
  479. auto provider = [&] (const std::string& path) -> std::shared_ptr<const daw::SampleData> {
  480. ++loads;
  481. CHECK (path == "a.wav");
  482. auto d = std::make_shared<daw::SampleData>();
  483. d->left.assign (4800, 0.5f);
  484. return d;
  485. };
  486. auto state = daw::RackState::build (song, nullptr, 48000.0, provider);
  487. CHECK (loads == 1);
  488. daw::RackRenderer renderer;
  489. renderer.prepare (48000.0);
  490. daw::LiveEventQueue live;
  491. daw::NoteEvent on;
  492. on.channel = 0;
  493. on.velocity = 1.0f;
  494. live.push (on);
  495. std::vector<float> l (256), r (256);
  496. renderer.process (state.get(), false, 0.0, 256, l.data(), r.data(), &live); // parado, mas a nota ao vivo toca
  497. CHECK (l[0] > 0.1f);
  498. // mesmo sample: reaproveita; sample diferente: recarrega
  499. auto same = daw::RackState::build (song, state.get(), 48000.0, provider);
  500. CHECK (loads == 1 && same->instruments[0] == state->instruments[0]);
  501. song.channels[0].samplePath = "b.wav";
  502. auto other = daw::RackState::build (song, state.get(), 48000.0, [&] (const std::string&) { ++loads; return nullptr; });
  503. CHECK (loads == 2 && other->instruments[0] != state->instruments[0]);
  504. }
  505. TEST (flp_paths_resolve_and_samplers_import)
  506. {
  507. daw::FlPathRoots roots { "C:\\FL", "C:\\FL\\Data", "D:\\User" };
  508. CHECK (daw::resolveFlPath ("%FLStudioFactoryData%\\Data\\x.wav", roots) == "C:\\FL\\Data\\x.wav");
  509. CHECK (daw::resolveFlPath ("%FLStudioData%\\Patches\\y.wav", roots) == "C:\\FL\\Data\\Patches\\y.wav");
  510. CHECK (daw::resolveFlPath ("%FLStudioUserData%\\z.wav", roots) == "D:\\User\\z.wav");
  511. CHECK (daw::resolveFlPath ("E:\\abs.wav", roots) == "E:\\abs.wav");
  512. CHECK (daw::resolveFlPath ("%FLStudioUserData%\\z.wav", {}) == "");
  513. flp::Project p;
  514. p.channels = { { 0, flp::ChannelType::sampler, "Kick", "", "%FLStudioData%\\k.wav", 1 },
  515. { 1, flp::ChannelType::sampler, "Hat", "", "%FLStudioData%\\missing.wav", 1 } };
  516. daw::FlpImportOptions options;
  517. options.roots = roots;
  518. options.fileExists = [] (const std::string& path) { return path == "C:\\FL\\Data\\k.wav"; };
  519. const auto s = daw::importFlp (p, options);
  520. CHECK (s.channels[0].instrument == daw::InstrumentKind::sampler);
  521. CHECK (s.channels[0].samplePath == "C:\\FL\\Data\\k.wav");
  522. CHECK (s.channels[1].instrument == daw::InstrumentKind::hat); // sem arquivo: instrumento interno
  523. }
  524. TEST (soft_clip_is_transparent_below_knee_and_bounded)
  525. {
  526. float s[] = { 0.5f, -0.89f, 1.5f, -3.0f, 0.95f };
  527. daw::softClip (s, 5);
  528. CHECK (s[0] == 0.5f && s[1] == -0.89f);
  529. CHECK (s[2] > 0.9f && s[2] <= 1.0f);
  530. CHECK (s[3] < -0.9f && s[3] >= -1.0f);
  531. CHECK (s[4] > 0.9f && s[4] < 0.95f);
  532. }
  533. int main (int argc, char** argv)
  534. {
  535. if (argc > 1)
  536. realFlpDir = argv[1];
  537. for (auto& [name, fn] : registry())
  538. {
  539. std::printf ("%s\n", name);
  540. fn();
  541. }
  542. std::printf ("\n%d verificações, %d falhas\n", checks, failures);
  543. return failures == 0 ? 0 : 1;
  544. }