FlSamplers.cpp 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608
  1. #include "core/FlSamplers.h"
  2. #include <algorithm>
  3. #include <cctype>
  4. #include <cmath>
  5. #include <cstring>
  6. #include <functional>
  7. #include <map>
  8. #include <string>
  9. namespace lynx
  10. {
  11. namespace
  12. {
  13. // chunk = id:u32 size:u64 body (FPC and DirectWave); the body may have sub-chunks
  14. struct Chunk
  15. {
  16. std::uint32_t id = 0;
  17. const std::uint8_t* data = nullptr;
  18. std::size_t size = 0;
  19. };
  20. void forEachChunk (const std::uint8_t* p, std::size_t n, const std::function<void (const Chunk&)>& fn)
  21. {
  22. std::size_t pos = 0;
  23. while (pos + 12 <= n)
  24. {
  25. Chunk c;
  26. std::uint64_t size = 0;
  27. std::memcpy (&c.id, p + pos, 4);
  28. std::memcpy (&size, p + pos + 4, 8);
  29. pos += 12;
  30. if (size > n - pos)
  31. return; // truncated: stop here
  32. c.data = p + pos;
  33. c.size = (std::size_t) size;
  34. fn (c);
  35. pos += c.size;
  36. }
  37. }
  38. std::int32_t i32 (const Chunk& c, std::size_t index)
  39. {
  40. std::int32_t v = 0;
  41. if ((index + 1) * 4 <= c.size)
  42. std::memcpy (&v, c.data + index * 4, 4);
  43. return v;
  44. }
  45. float f32 (const std::uint8_t* p)
  46. {
  47. float v = 0.0f;
  48. std::memcpy (&v, p, 4);
  49. return std::isfinite (v) ? v : 0.0f;
  50. }
  51. std::string ascii (const Chunk& c)
  52. {
  53. std::string s (reinterpret_cast<const char*> (c.data), c.size);
  54. s.erase (std::find (s.begin(), s.end(), '\0'), s.end());
  55. return s;
  56. }
  57. std::string utf16 (const Chunk& c)
  58. {
  59. std::string out;
  60. for (std::size_t i = 0; i + 1 < c.size; i += 2)
  61. {
  62. const auto ch = (std::uint32_t) c.data[i] | ((std::uint32_t) c.data[i + 1] << 8);
  63. if (ch == 0)
  64. break;
  65. if (ch < 0x80)
  66. out += (char) ch;
  67. else if (ch < 0x800)
  68. {
  69. out += (char) (0xC0 | (ch >> 6));
  70. out += (char) (0x80 | (ch & 0x3F));
  71. }
  72. else
  73. {
  74. out += (char) (0xE0 | (ch >> 12));
  75. out += (char) (0x80 | ((ch >> 6) & 0x3F));
  76. out += (char) (0x80 | (ch & 0x3F));
  77. }
  78. }
  79. return out;
  80. }
  81. double l11 (double x) // (11^x − 1)/10: FL's volume curves
  82. {
  83. return (std::pow (11.0, std::clamp (x, 0.0, 2.0)) - 1.0) / 10.0;
  84. }
  85. } // namespace
  86. std::optional<std::vector<DrumPad>> fpcPads (const std::vector<std::uint8_t>& d)
  87. {
  88. if (d.size() < 16)
  89. return std::nullopt;
  90. std::uint32_t version = 0;
  91. std::memcpy (&version, d.data(), 4);
  92. if (version < 0xF424B || version > 0x1E84E403) // old formats without chunks: not handled
  93. return std::nullopt;
  94. std::vector<DrumPad> pads;
  95. forEachChunk (d.data() + 4, d.size() - 4, [&] (const Chunk& top) {
  96. if (top.id != 2) // pad
  97. return;
  98. DrumPad pad;
  99. bool muted = false;
  100. forEachChunk (top.data, top.size, [&] (const Chunk& c) {
  101. if (c.id == 0x64) // 16 × i32 parameters
  102. {
  103. pad.gain = (float) l11 (std::clamp (i32 (c, 0), 0, 128) / 127.0);
  104. pad.pan = std::clamp (i32 (c, 1), -128, 128) / 128.0f;
  105. muted = (i32 (c, 2) & 1) != 0;
  106. pad.key = std::clamp (i32 (c, 3), 0, 127);
  107. pad.mixerOffset = std::clamp (i32 (c, 4), -99, 103);
  108. pad.cutGroup = i32 (c, 5) >= 0 ? i32 (c, 5) + 1 : 0; // −1 = none; 0 becomes group 1
  109. pad.cutBy = i32 (c, 6) >= 0 ? i32 (c, 6) + 1 : 0;
  110. pad.tune = std::clamp (i32 (c, 7), -128, 128) * 9.375f / 100.0f;
  111. }
  112. else if (c.id == 0x65)
  113. pad.name = utf16 (c);
  114. else if (c.id == 0x69) // layer
  115. {
  116. SampleZone layer;
  117. bool hasPath = false;
  118. forEachChunk (c.data, c.size, [&] (const Chunk& l) {
  119. if (l.id == 0x12C)
  120. {
  121. layer.path = utf16 (l);
  122. hasPath = ! layer.path.empty();
  123. }
  124. else if (l.id == 0x12D)
  125. {
  126. layer.gain = (float) l11 (std::clamp (i32 (l, 0), 0, 127) / 127.0);
  127. layer.lowVelocity = std::clamp (i32 (l, 2), 0, 127);
  128. const auto high = std::clamp (i32 (l, 3), 0, 127); // exclusive, except 127
  129. layer.highVelocity = high >= 127 ? 127 : std::max (layer.lowVelocity, high - 1);
  130. layer.tune = std::clamp (i32 (l, 5), -128, 128) * 9.375f / 100.0f;
  131. }
  132. });
  133. if (hasPath)
  134. pad.layers.push_back (std::move (layer));
  135. }
  136. });
  137. if (! pad.layers.empty() && ! muted)
  138. {
  139. if (pad.name.empty())
  140. pad.name = "Pad " + std::to_string (pads.size() + 1);
  141. pads.push_back (std::move (pad));
  142. }
  143. });
  144. if (pads.empty())
  145. return std::nullopt;
  146. return pads;
  147. }
  148. std::optional<DirectWaveProgram> directWaveProgram (const std::vector<std::uint8_t>& d)
  149. {
  150. if (d.size() < 16)
  151. return std::nullopt;
  152. std::uint32_t version = 0;
  153. std::memcpy (&version, d.data(), 4);
  154. if (version < 36 || version > 38)
  155. return std::nullopt;
  156. std::optional<DirectWaveProgram> result;
  157. forEachChunk (d.data() + 4, d.size() - 4, [&] (const Chunk& top) {
  158. if (top.id != 1 || result) // the first program with zones
  159. return;
  160. DirectWaveProgram program;
  161. bool envelopeSet = false;
  162. forEachChunk (top.data, top.size, [&] (const Chunk& z) {
  163. if (z.id != 3) // zone
  164. return;
  165. SampleZone zone;
  166. float a = 0.0f, dcy = 0.0f, sus = 1.0f, rel = 0.0f;
  167. bool hasEnvelope = false;
  168. std::int64_t frames = 0;
  169. int channels = 0, bits = 0;
  170. float rate = 44100.0f;
  171. std::vector<std::uint8_t> embedded;
  172. forEachChunk (z.data, z.size, [&] (const Chunk& c) {
  173. if (c.id == 0x1F4 && c.size >= 13) // header: root, keys, velocities, gain
  174. {
  175. zone.rootKey = std::clamp ((int) c.data[0], 0, 127);
  176. zone.lowKey = std::clamp ((int) c.data[1], 0, 127);
  177. zone.highKey = std::clamp ((int) c.data[2], zone.lowKey, 127);
  178. zone.lowVelocity = std::clamp ((int) c.data[3], 0, 127);
  179. zone.highVelocity = std::clamp ((int) c.data[4], zone.lowVelocity, 127);
  180. const auto g = std::clamp (f32 (c.data + 9), 0.0f, 2.0f);
  181. zone.gain = g * g; // gain = v²
  182. }
  183. else if (c.id == 0x1F6)
  184. zone.path = ascii (c);
  185. else if (c.id == 0x1F7 && c.size >= 32) // frames, channels, ?, rate, loop mode, loop A, loop B
  186. {
  187. std::memcpy (&frames, c.data, 8);
  188. channels = i32 (c, 2);
  189. rate = f32 (c.data + 16);
  190. if (c.size >= 40)
  191. bits = i32 (c, 9);
  192. const auto mode = i32 (c, 5);
  193. const auto a = (std::int64_t) i32 (c, 6), b = (std::int64_t) i32 (c, 7);
  194. // 2 continuous, 3 while held, 4 ping-pong (here: continuous); 0/1 no loop
  195. if ((mode == 2 || mode == 3 || mode == 4) && b > a && a >= 0 && (frames <= 0 || b <= frames))
  196. {
  197. zone.loopStart = a;
  198. zone.loopEnd = b;
  199. zone.loopMode = mode == 3 ? 2 : 1;
  200. }
  201. }
  202. else if (c.id == 0x205 && frames > 0 && channels >= 1 && channels <= 2 && (bits == 16 || bits == 32))
  203. {
  204. // embedded audio (REA_rodada9 §2.5): channels in blocks of (frames + 512) samples, with 256
  205. // of padding before and after the audio [DATA: exactly 257 zeros at each end]
  206. const auto bytes = (std::size_t) bits / 8;
  207. const auto block = ((std::size_t) frames + 512) * bytes;
  208. if (block * (std::size_t) channels == c.size && std::isfinite (rate) && rate >= 1000.0f && rate <= 384000.0f)
  209. {
  210. std::vector<std::vector<float>> planar ((std::size_t) channels, std::vector<float> ((std::size_t) frames));
  211. for (int ch = 0; ch < channels; ++ch)
  212. for (std::int64_t i = 0; i < frames; ++i)
  213. {
  214. const auto* p = c.data + (std::size_t) ch * block + ((std::size_t) i + 256) * bytes;
  215. float v = 0.0f;
  216. if (bits == 32)
  217. std::memcpy (&v, p, 4);
  218. else
  219. {
  220. std::int16_t x = 0;
  221. std::memcpy (&x, p, 2);
  222. v = x / 32768.0f;
  223. }
  224. planar[(std::size_t) ch][(std::size_t) i] = std::isfinite (v) ? v : 0.0f;
  225. }
  226. embedded = makeWavFloat (planar, (int) std::lround (rate));
  227. }
  228. }
  229. else if (c.id == 0x1F8 && c.size >= 4)
  230. zone.tune = (std::clamp (f32 (c.data), 0.0f, 1.0f) - 0.5f) * 24.0f; // (v − 0.5)·2400 cents
  231. else if (c.id == 0x1FD && c.size >= 16)
  232. {
  233. auto time = [] (float v) { return 10.0f * std::pow (std::clamp (v, 0.0f, 1.0f), 4.0f); }; // 10·v⁴ s
  234. a = time (f32 (c.data));
  235. dcy = time (f32 (c.data + 4));
  236. sus = std::clamp (f32 (c.data + 8), 0.0f, 1.0f);
  237. rel = time (f32 (c.data + 12));
  238. hasEnvelope = true;
  239. }
  240. });
  241. if (zone.path.empty())
  242. return;
  243. if (hasEnvelope && ! envelopeSet)
  244. {
  245. program.attack = a;
  246. program.decay = dcy;
  247. program.sustain = sus;
  248. program.release = rel;
  249. envelopeSet = true;
  250. }
  251. program.zones.push_back (std::move (zone));
  252. program.embeddedWav.push_back (std::move (embedded));
  253. });
  254. if (! program.zones.empty())
  255. result = std::move (program);
  256. });
  257. return result;
  258. }
  259. namespace
  260. {
  261. // bounded sequential reader (the blob may be truncated)
  262. struct Stream
  263. {
  264. const std::vector<std::uint8_t>& d;
  265. std::size_t pos = 0;
  266. bool ok = true;
  267. template <typename T>
  268. T read()
  269. {
  270. T v {};
  271. if (pos + sizeof (T) > d.size())
  272. {
  273. ok = false;
  274. return v;
  275. }
  276. std::memcpy (&v, d.data() + pos, sizeof (T));
  277. pos += sizeof (T);
  278. return v;
  279. }
  280. std::string str8() // u8 length + ANSI bytes
  281. {
  282. const auto n = read<std::uint8_t>();
  283. if (! ok || pos + n > d.size())
  284. {
  285. ok = false;
  286. return {};
  287. }
  288. std::string s (reinterpret_cast<const char*> (d.data() + pos), n);
  289. pos += n;
  290. return s;
  291. }
  292. void skip (std::size_t n)
  293. {
  294. if (pos + n > d.size())
  295. ok = false;
  296. else
  297. pos += n;
  298. }
  299. };
  300. } // namespace
  301. std::optional<SlicedSample> fruitySlicer (const std::vector<std::uint8_t>& d)
  302. {
  303. Stream in { d };
  304. const auto version = in.read<std::uint32_t>();
  305. if (version < 13 || version > 16)
  306. return std::nullopt;
  307. SlicedSample result;
  308. in.skip (8); // length in beats, original BPM
  309. result.pitchCents = (float) std::clamp (in.read<std::int32_t>(), -1200, 1200);
  310. in.skip (16); // stretching, method, fades
  311. result.path = in.str8();
  312. const auto count = in.read<std::uint32_t>();
  313. if (! in.ok || count > 4096)
  314. return std::nullopt;
  315. for (std::uint32_t i = 0; i < count && in.ok; ++i)
  316. {
  317. in.str8(); // name
  318. const auto position = in.read<std::int32_t>();
  319. in.skip (4 + 4 + 1);
  320. if (in.ok && position >= 0)
  321. result.positions.push_back (position);
  322. }
  323. if (! in.ok || result.path.empty() || result.positions.empty())
  324. return std::nullopt;
  325. std::sort (result.positions.begin(), result.positions.end());
  326. return result;
  327. }
  328. std::optional<SlicedSample> slicex (const std::vector<std::uint8_t>& d)
  329. {
  330. Stream in { d };
  331. const auto version = in.read<std::uint32_t>();
  332. if (version != 0x00060003u && version != 6u)
  333. return std::nullopt;
  334. SlicedSample result;
  335. std::int32_t master[8] {};
  336. for (auto& m : master)
  337. m = in.read<std::int32_t>();
  338. result.pitchCents = (float) std::clamp (master[5], -1200, 1200);
  339. // master volume (DFM range 0…16000, 12800 = 100 %): FL's L11 law in single precision (Slicex FUN_0087efe0)
  340. {
  341. const auto x = (float) std::clamp (master[2], 0, 16000) * 7.8125e-05f;
  342. result.gain = x == 1.0f ? 1.0f : (float) ((std::exp ((double) (x * (float) std::log (11.0))) - 1.0) * (double) 0.1f);
  343. }
  344. in.skip (47 + 12); // options and UI state: deck A starts at 0x5F
  345. const auto referenceOnly = in.read<std::uint8_t>(); // 0 = embedded WAV follows
  346. result.path = in.str8();
  347. if (in.ok && referenceOnly == 0)
  348. {
  349. const auto size = in.read<std::uint32_t>();
  350. if (in.ok && size > 0 && in.pos + size <= d.size())
  351. result.embeddedWav.assign (d.begin() + (std::ptrdiff_t) in.pos, d.begin() + (std::ptrdiff_t) (in.pos + size));
  352. }
  353. if (! in.ok || (result.path.empty() && result.embeddedWav.empty()))
  354. return std::nullopt;
  355. return result;
  356. }
  357. std::vector<std::int64_t> wavCuePoints (const std::vector<std::uint8_t>& w, std::int64_t& frames)
  358. {
  359. std::vector<std::int64_t> cues;
  360. frames = 0;
  361. if (w.size() < 12 || std::memcmp (w.data(), "RIFF", 4) != 0 || std::memcmp (w.data() + 8, "WAVE", 4) != 0)
  362. return cues;
  363. std::uint16_t format = 0, blockAlign = 0;
  364. std::size_t pos = 12;
  365. while (pos + 8 <= w.size())
  366. {
  367. std::uint32_t size = 0;
  368. std::memcpy (&size, w.data() + pos + 4, 4);
  369. const auto* body = w.data() + pos + 8;
  370. const auto available = std::min<std::size_t> (size, w.size() - pos - 8);
  371. if (std::memcmp (w.data() + pos, "fmt ", 4) == 0 && available >= 16)
  372. {
  373. std::memcpy (&format, body, 2);
  374. std::memcpy (&blockAlign, body + 12, 2);
  375. }
  376. else if (std::memcmp (w.data() + pos, "data", 4) == 0 && blockAlign > 0 && (format == 1 || format == 3 || format == 0xFFFE))
  377. frames = (std::int64_t) (size / blockAlign);
  378. else if (std::memcmp (w.data() + pos, "cue ", 4) == 0 && available >= 4)
  379. {
  380. std::uint32_t count = 0;
  381. std::memcpy (&count, body, 4);
  382. for (std::uint32_t i = 0; i < count && 4 + (std::size_t) (i + 1) * 24 <= available; ++i)
  383. {
  384. std::uint32_t sampleOffset = 0;
  385. std::memcpy (&sampleOffset, body + 4 + (std::size_t) i * 24 + 20, 4);
  386. cues.push_back (sampleOffset);
  387. }
  388. }
  389. pos += 8 + size + (size & 1); // chunks aligned to 2
  390. }
  391. std::sort (cues.begin(), cues.end());
  392. cues.erase (std::unique (cues.begin(), cues.end()), cues.end());
  393. return cues;
  394. }
  395. std::optional<std::string> flKeysSet (const std::vector<std::uint8_t>& d)
  396. {
  397. Stream in { d };
  398. if (in.read<std::uint32_t>() != 1)
  399. return std::nullopt;
  400. auto name = in.str8();
  401. name.erase (std::find (name.begin(), name.end(), '\0'), name.end());
  402. if (! in.ok || name.empty() || name.find_first_of ("\\/:") != std::string::npos)
  403. return std::nullopt;
  404. return name;
  405. }
  406. std::vector<std::uint8_t> makeWav16Mono (const std::int16_t* samples, std::size_t count, int sampleRate)
  407. {
  408. std::vector<std::uint8_t> wav;
  409. auto u32 = [&] (std::uint32_t v) { for (int i = 0; i < 4; ++i) wav.push_back ((std::uint8_t) (v >> (8 * i))); };
  410. auto u16 = [&] (std::uint16_t v) { wav.push_back ((std::uint8_t) v); wav.push_back ((std::uint8_t) (v >> 8)); };
  411. auto tag = [&] (const char* t) { wav.insert (wav.end(), t, t + 4); };
  412. const auto bytes = (std::uint32_t) (count * 2);
  413. tag ("RIFF"); u32 (36 + bytes); tag ("WAVE");
  414. tag ("fmt "); u32 (16); u16 (1); u16 (1); u32 ((std::uint32_t) sampleRate); u32 ((std::uint32_t) sampleRate * 2); u16 (2); u16 (16);
  415. tag ("data"); u32 (bytes);
  416. const auto* raw = reinterpret_cast<const std::uint8_t*> (samples);
  417. wav.insert (wav.end(), raw, raw + bytes);
  418. return wav;
  419. }
  420. std::vector<std::uint8_t> makeWavFloat (const std::vector<std::vector<float>>& channels, int sampleRate)
  421. {
  422. std::vector<std::uint8_t> wav;
  423. if (channels.empty())
  424. return wav;
  425. const auto count = channels.front().size();
  426. const auto n = (std::uint16_t) channels.size();
  427. auto u32 = [&] (std::uint32_t v) { for (int i = 0; i < 4; ++i) wav.push_back ((std::uint8_t) (v >> (8 * i))); };
  428. auto u16 = [&] (std::uint16_t v) { wav.push_back ((std::uint8_t) v); wav.push_back ((std::uint8_t) (v >> 8)); };
  429. auto tag = [&] (const char* t) { wav.insert (wav.end(), t, t + 4); };
  430. const auto bytes = (std::uint32_t) (count * n * 4);
  431. tag ("RIFF"); u32 (36 + bytes); tag ("WAVE");
  432. tag ("fmt "); u32 (16); u16 (3); u16 (n); u32 ((std::uint32_t) sampleRate); u32 ((std::uint32_t) sampleRate * n * 4); u16 ((std::uint16_t) (n * 4)); u16 (32);
  433. tag ("data"); u32 (bytes);
  434. wav.reserve (wav.size() + bytes);
  435. for (std::size_t i = 0; i < count; ++i)
  436. for (const auto& ch : channels)
  437. {
  438. const auto v = i < ch.size() ? ch[i] : 0.0f;
  439. const auto* raw = reinterpret_cast<const std::uint8_t*> (&v);
  440. wav.insert (wav.end(), raw, raw + 4);
  441. }
  442. return wav;
  443. }
  444. std::vector<SampleZone> flKeysZones (const std::string& wti, const std::string& folder, const std::vector<std::uint8_t>& rawData,
  445. const std::function<std::string (const std::vector<std::uint8_t>&, const std::string&)>& saveWav)
  446. {
  447. // simple INI: [section] and key=value
  448. std::map<std::string, std::map<std::string, std::string>> ini;
  449. std::string section;
  450. std::size_t start = 0;
  451. while (start < wti.size())
  452. {
  453. auto end = wti.find ('\n', start);
  454. if (end == std::string::npos)
  455. end = wti.size();
  456. auto line = wti.substr (start, end - start);
  457. start = end + 1;
  458. line.erase (std::remove (line.begin(), line.end(), '\r'), line.end());
  459. if (line.size() >= 2 && line.front() == '[' && line.back() == ']')
  460. {
  461. section = line.substr (1, line.size() - 2);
  462. std::transform (section.begin(), section.end(), section.begin(), [] (unsigned char ch) { return (char) std::tolower (ch); });
  463. }
  464. else if (const auto eq = line.find ('='); eq != std::string::npos)
  465. ini[section][line.substr (0, eq)] = line.substr (eq + 1);
  466. }
  467. auto value = [&] (const std::string& sec, const std::string& key, int fallback) {
  468. const auto s = ini.find (sec);
  469. if (s == ini.end())
  470. return fallback;
  471. const auto k = s->second.find (key);
  472. if (k == s->second.end() || k->second.empty())
  473. return fallback;
  474. try { return std::stoi (k->second); } catch (...) { return fallback; }
  475. };
  476. auto text = [&] (const std::string& sec, const std::string& key) {
  477. const auto s = ini.find (sec);
  478. if (s == ini.end())
  479. return std::string();
  480. const auto k = s->second.find (key);
  481. return k == s->second.end() ? std::string() : k->second;
  482. };
  483. const auto count = std::clamp (value ("info", "samples", 0), 0, 512);
  484. const auto raw = text ("info", "filetype") == "raw";
  485. const auto rate = std::clamp (value ("info", "samplerate", 44100), 8000, 192000);
  486. const auto subfolder = text ("info", "path");
  487. const auto layers = std::clamp (value ("velocitylayers", "count", 1), 1, 16);
  488. std::vector<int> thresholds;
  489. for (int l = 0; l < layers; ++l)
  490. thresholds.push_back (std::clamp (value ("velocitylayers", std::to_string (l), 0), 0, 127));
  491. std::vector<SampleZone> zones;
  492. std::string lastName;
  493. for (int i = 0; i < count; ++i)
  494. {
  495. const auto sec = "sample" + std::to_string (i);
  496. SampleZone zone;
  497. zone.rootKey = zone.lowKey = std::clamp (value (sec, "root", 60), 0, 127); // the root is also the start of the range
  498. zone.highKey = std::clamp (value (sec, "high", 127), zone.lowKey, 127);
  499. const auto layer = i % layers;
  500. zone.lowVelocity = thresholds[(std::size_t) layer];
  501. zone.highVelocity = layer + 1 < layers ? std::max (zone.lowVelocity, thresholds[(std::size_t) layer + 1] - 1) : 127;
  502. if (raw)
  503. {
  504. const auto from = (std::size_t) std::max (0, value (sec, "pos", 0));
  505. const auto to = (std::size_t) std::max (0, value (sec, "end", 0));
  506. if (to <= from || to * 2 > rawData.size() || ! saveWav)
  507. continue;
  508. const auto wav = makeWav16Mono (reinterpret_cast<const std::int16_t*> (rawData.data()) + from, to - from, rate);
  509. zone.path = saveWav (wav, "FL Keys " + std::to_string (i));
  510. }
  511. else
  512. {
  513. if (const auto name = text (sec, "name"); ! name.empty())
  514. lastName = name;
  515. if (lastName.empty())
  516. continue;
  517. zone.path = folder + "\\" + (subfolder.empty() ? std::string() : subfolder + "\\") + lastName;
  518. }
  519. if (! zone.path.empty())
  520. zones.push_back (std::move (zone));
  521. }
  522. return zones;
  523. }
  524. } // namespace lynx