Layer.cpp 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. #include "core/Layer.h"
  2. #include <algorithm>
  3. #include <cmath>
  4. namespace lynx::layer
  5. {
  6. namespace
  7. {
  8. bool isChild (const std::vector<ChannelDef>& channels, int layer, int c) noexcept
  9. {
  10. return c >= 0 && c < (int) channels.size() && c != layer && channels[(std::size_t) c].instrument != InstrumentKind::layer;
  11. }
  12. } // namespace
  13. std::vector<int> validChildren (const Song& song, int layer)
  14. {
  15. std::vector<int> out;
  16. if (layer < 0 || layer >= (int) song.channels.size())
  17. return out;
  18. for (const auto c : song.channels[(std::size_t) layer].layer.children)
  19. if (isChild (song.channels, layer, c) && std::find (out.begin(), out.end(), c) == out.end())
  20. out.push_back (c);
  21. return out;
  22. }
  23. float levelGain (const ChannelDef& layer) noexcept
  24. {
  25. const auto reference = channelGain (0.78f);
  26. return reference > 0.0f ? channelGain (layer.volume) / reference : 1.0f;
  27. }
  28. float crossfadeGain (int index, int count, float fade) noexcept
  29. {
  30. if (count <= 1)
  31. return 1.0f;
  32. const auto p = (double) std::clamp (fade, 0.0f, 1.0f) * (double) (count - 1);
  33. const auto d = std::abs (p - (double) index);
  34. if (d >= 1.0)
  35. return 0.0f;
  36. return (float) std::cos (d * 1.5707963267948966); // equal power between the two neighbours
  37. }
  38. int pick (const ChannelDef& layer, const std::vector<ChannelDef>& channels, int key, std::uint32_t& random, int& turn, Pick* out, int max) noexcept
  39. {
  40. const auto& kids = layer.layer.children;
  41. const auto count = (int) kids.size();
  42. if (count == 0 || max <= 0)
  43. return 0;
  44. const auto self = (int) (&layer - channels.data()); // (the Layer lives in `channels`)
  45. const auto takes = [&] (int k) {
  46. const auto c = kids[(std::size_t) k];
  47. return isChild (channels, self, c) && channels[(std::size_t) c].inKeyRegion (key);
  48. };
  49. const auto gainOf = [&] (int k) { return layer.layer.crossfade ? crossfadeGain (k, count, layer.layer.fade) : 1.0f; };
  50. if (layer.layer.random || layer.layer.sequential)
  51. {
  52. int candidates = 0;
  53. for (int k = 0; k < count; ++k)
  54. candidates += takes (k) ? 1 : 0;
  55. if (candidates == 0)
  56. return 0;
  57. int chosen = 0;
  58. if (layer.layer.random)
  59. {
  60. random = random * 1664525u + 1013904223u;
  61. chosen = (int) ((std::uint64_t) (random >> 8) * (std::uint64_t) candidates >> 24);
  62. }
  63. else
  64. {
  65. chosen = ((turn % candidates) + candidates) % candidates;
  66. turn = (chosen + 1) % candidates;
  67. }
  68. for (int k = 0; k < count; ++k)
  69. if (takes (k) && chosen-- == 0)
  70. {
  71. out[0] = { kids[(std::size_t) k], gainOf (k) };
  72. return out[0].gain > 0.0f ? 1 : 0;
  73. }
  74. return 0;
  75. }
  76. int n = 0;
  77. for (int k = 0; k < count && n < max; ++k)
  78. if (takes (k))
  79. if (const auto g = gainOf (k); g > 0.0f)
  80. out[n++] = { kids[(std::size_t) k], g };
  81. return n;
  82. }
  83. void setChildren (Song& song, int layer, std::vector<int> selected)
  84. {
  85. if (layer < 0 || layer >= (int) song.channels.size())
  86. return;
  87. std::sort (selected.begin(), selected.end());
  88. selected.erase (std::unique (selected.begin(), selected.end()), selected.end());
  89. selected.erase (std::remove_if (selected.begin(), selected.end(), [&] (int c) { return ! isChild (song.channels, layer, c); }), selected.end());
  90. song.channels[(std::size_t) layer].layer.children = std::move (selected);
  91. }
  92. void setRootNote (ChannelDef& d, int key)
  93. {
  94. key = std::clamp (key, 0, 127);
  95. if (d.instrument == InstrumentKind::sampler)
  96. {
  97. // the sample plays at its own pitch on the root: the pitch moves by the root's change (within ±24)
  98. const auto pitch = std::clamp (d.sampler.pitch + (float) (d.misc.rootNote - key), -24.0f, 24.0f);
  99. const auto moved = (int) std::lround (pitch - d.sampler.pitch);
  100. d.sampler.pitch = pitch;
  101. d.misc.rootNote = std::clamp (d.misc.rootNote - moved, 0, 127);
  102. }
  103. else
  104. d.misc.rootNote = key;
  105. }
  106. void splitChildren (Song& song, int layer)
  107. {
  108. if (layer < 0 || layer >= (int) song.channels.size())
  109. return;
  110. const auto root = song.channels[(std::size_t) layer].misc.rootNote;
  111. const auto kids = validChildren (song, layer);
  112. for (std::size_t k = 0; k < kids.size(); ++k)
  113. {
  114. auto& c = song.channels[(std::size_t) kids[k]];
  115. const auto key = std::clamp (root + (int) k, 0, 127);
  116. c.keyLow = c.keyHigh = key;
  117. setRootNote (c, key);
  118. // instruments that are not samplers sound their own pitch on their key through Add to key
  119. if (c.instrument != InstrumentKind::sampler && c.instrument != InstrumentKind::audioClip)
  120. c.misc.addToKey = true;
  121. }
  122. }
  123. void resetChildren (Song& song, int layer)
  124. {
  125. for (const auto k : validChildren (song, layer))
  126. {
  127. auto& c = song.channels[(std::size_t) k];
  128. c.keyLow = 0;
  129. c.keyHigh = 127;
  130. setRootNote (c, 60);
  131. if (c.instrument != InstrumentKind::sampler && c.instrument != InstrumentKind::audioClip)
  132. c.misc.addToKey = false;
  133. }
  134. }
  135. std::vector<int> parentsOf (const Song& song, int channel)
  136. {
  137. std::vector<int> out;
  138. for (int l = 0; l < (int) song.channels.size(); ++l)
  139. if (song.channels[(std::size_t) l].instrument == InstrumentKind::layer)
  140. {
  141. const auto& kids = song.channels[(std::size_t) l].layer.children;
  142. if (std::find (kids.begin(), kids.end(), channel) != kids.end())
  143. out.push_back (l);
  144. }
  145. return out;
  146. }
  147. } // namespace lynx::layer