Issue160Tests.inc 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. // Issue #160: Fruity Slicer — Piano roll notes all played the same part of the sample. The rack keeps a channel's
  2. // instrument across edits and configures it with the channel again; the Slicer's slices live in its state, so that
  3. // configure emptied the slice table and every key played the sample from its start. Also FL's key table
  4. // (FUN_0076c200): a slice's own key plays it, a later slice wins a shared key, keys without a slice are silent.
  5. #include "core/FruitySlicer.h"
  6. #include "core/RackRenderer.h"
  7. namespace issue160
  8. {
  9. // four slices of 11025 frames, slice i a sine at amplitude 0.1 · (i + 1)
  10. std::shared_ptr<const lynx::SampleData> steps()
  11. {
  12. auto d = std::make_shared<lynx::SampleData>();
  13. d->sampleRate = 44100.0;
  14. for (int i = 0; i < 4 * 11025; ++i)
  15. d->left.push_back (0.1f * (float) (i / 11025 + 1) * (float) std::sin (i * 0.05));
  16. d->right = d->left;
  17. return d;
  18. }
  19. lynx::fslicer::State fourSlices()
  20. {
  21. auto state = lynx::fslicer::fresh ("C:\\loops\\steps.wav", 44100);
  22. state.slices.clear();
  23. for (int i = 0; i < 4; ++i)
  24. {
  25. lynx::fslicer::Slice s;
  26. s.name = "s" + std::to_string (i + 1);
  27. s.position = i * 11025;
  28. state.slices.push_back (s);
  29. }
  30. return state;
  31. }
  32. lynx::Song song (const lynx::fslicer::State& state, float volume)
  33. {
  34. lynx::Song s;
  35. lynx::ChannelDef c;
  36. c.name = "Slicer";
  37. c.instrument = lynx::InstrumentKind::fruityslicer;
  38. c.samplePath = "C:\\loops\\steps.wav";
  39. c.pluginState = lynx::fslicer::write (state);
  40. c.volume = volume;
  41. s.channels.push_back (c);
  42. return s;
  43. }
  44. lynx::RackState::Factories factories()
  45. {
  46. lynx::RackState::Factories f;
  47. f.samples = [] (const std::string&) { return steps(); };
  48. return f;
  49. }
  50. // the peak of the first 2000 frames a key plays (0 = silent), the note then released and rung out
  51. float keyPeak (lynx::Instrument& instrument, int key)
  52. {
  53. std::vector<float> left (2000, 0.0f), right (2000, 0.0f);
  54. instrument.noteOn (key, 0.78125f);
  55. instrument.render (left.data(), right.data(), 2000, 1.0f, 1.0f);
  56. instrument.noteOff (key);
  57. std::vector<float> tail (44100, 0.0f), tail2 (44100, 0.0f);
  58. for (int at = 0; at < 44100; at += 512)
  59. instrument.render (tail.data() + at, tail2.data() + at, std::min (512, 44100 - at), 1.0f, 1.0f);
  60. float peak = 0.0f;
  61. for (auto v : left)
  62. peak = std::max (peak, std::fabs (v));
  63. return peak;
  64. }
  65. } // namespace issue160
  66. TEST (issue160_slicer_key_table_is_fls)
  67. {
  68. using namespace lynx;
  69. auto state = issue160::fourSlices();
  70. auto table = fslicer::keyTable (state);
  71. CHECK (table.size() == 125);
  72. CHECK (table[60] == 0 && table[61] == 1 && table[62] == 2 && table[63] == 3); // C5 = slice 1, upwards
  73. CHECK (table[59] == -1 && table[64] == -1 && table[0] == -1 && table[124] == -1); // nothing below or past them
  74. state.slices[2].key = 72; // the SliceKeyMenu: slice 3 on C6
  75. state.slices[3].key = 60; // slice 4 on C5: the later slice takes the key
  76. table = fslicer::keyTable (state);
  77. CHECK (table[72] == 2 && table[62] == -1 && table[60] == 3 && table[61] == 1);
  78. // what the sampler voices are given
  79. ChannelDef def;
  80. fslicer::configureSampler (def, state, 4 * 11025);
  81. CHECK (def.sampler.slices.size() == 4 && def.sampler.sliceKeys == table);
  82. }
  83. TEST (issue160_slicer_keys_play_their_slices_after_an_edit)
  84. {
  85. using namespace lynx;
  86. const auto state = issue160::fourSlices();
  87. const auto f = issue160::factories();
  88. auto first = RackState::build (issue160::song (state, 0.78f), nullptr, 44100.0, f);
  89. CHECK (first != nullptr && first->instruments.size() == 1);
  90. auto check = [] (Instrument& instrument) {
  91. // C5, D5, D#5: slices 1, 3, 4 (0.1, 0.3, 0.4); B4 and E5 have no slice
  92. const auto c5 = issue160::keyPeak (instrument, 60), d5 = issue160::keyPeak (instrument, 62), ds5 = issue160::keyPeak (instrument, 63);
  93. CHECK (c5 > 0.01f);
  94. CHECK (std::fabs (d5 / c5 - 3.0f) < 0.15f);
  95. CHECK (std::fabs (ds5 / c5 - 4.0f) < 0.2f);
  96. CHECK (issue160::keyPeak (instrument, 59) == 0.0f);
  97. CHECK (issue160::keyPeak (instrument, 64) == 0.0f);
  98. };
  99. check (*first->instruments[0]);
  100. // an edit (the channel's volume): the rack keeps the Slicer, and its keys still play their own slices
  101. auto second = RackState::build (issue160::song (state, 0.5f), first.get(), 44100.0, f);
  102. CHECK (second != nullptr && second->instruments.size() == 1 && second->instruments[0] == first->instruments[0]);
  103. check (*second->instruments[0]);
  104. // a slice's own key (FL's SliceKeyMenu) moves it: slice 3 on C6, and D5 is silent
  105. auto moved = state;
  106. moved.slices[2].key = 72;
  107. auto third = RackState::build (issue160::song (moved, 0.5f), second.get(), 44100.0, f);
  108. auto& slicer = *third->instruments[0];
  109. const auto c5 = issue160::keyPeak (slicer, 60);
  110. CHECK (issue160::keyPeak (slicer, 62) == 0.0f);
  111. CHECK (std::fabs (issue160::keyPeak (slicer, 72) / c5 - 3.0f) < 0.15f);
  112. }