alsrender.cpp 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. // alsrender: imports a Live set (.als) and renders each MIDI track alone, like Live's "All Individual Tracks" export
  2. // (round 190hs: the LYNX side of the Operator / Tetra quality gate). 48 kHz float WAVs, one per track, named
  3. // "<prefix> <track name>.wav", the song from its start for `seconds`.
  4. // Usage: alsrender set.als out-folder [prefix] [seconds] [track-name-filter]
  5. // LYNX_BLOCK=n: block size (default 256). Prints each channel's instrument kind and the render time per track.
  6. #include <chrono>
  7. #include <cmath>
  8. #include <cstdio>
  9. #include <cstdlib>
  10. #include <cstring>
  11. #include <filesystem>
  12. #include <fstream>
  13. #include <iterator>
  14. #include <string>
  15. #include <vector>
  16. #include "core/AlsImport.h"
  17. #include "core/RackRenderer.h"
  18. #include "WavReader.h"
  19. namespace
  20. {
  21. void writeWav (const std::string& path, const std::vector<float>& l, const std::vector<float>& r, int rate)
  22. {
  23. std::ofstream f (path, std::ios::binary);
  24. const std::uint32_t frames = (std::uint32_t) l.size(), bytes = frames * 8;
  25. auto u32 = [&] (std::uint32_t v) { f.write ((const char*) &v, 4); };
  26. auto u16 = [&] (std::uint16_t v) { f.write ((const char*) &v, 2); };
  27. f.write ("RIFF", 4), u32 (36 + bytes), f.write ("WAVEfmt ", 8), u32 (16), u16 (3), u16 (2), u32 ((std::uint32_t) rate),
  28. u32 ((std::uint32_t) rate * 8), u16 (8), u16 (32), f.write ("data", 4), u32 (bytes);
  29. for (std::uint32_t i = 0; i < frames; ++i)
  30. {
  31. f.write ((const char*) &l[i], 4);
  32. f.write ((const char*) &r[i], 4);
  33. }
  34. }
  35. } // namespace
  36. int main (int argc, char** argv)
  37. {
  38. if (argc < 3)
  39. {
  40. std::fprintf (stderr, "usage: alsrender set.als out-folder [prefix] [seconds] [filter]\n");
  41. return 2;
  42. }
  43. std::ifstream in (argv[1], std::ios::binary);
  44. std::vector<std::uint8_t> bytes ((std::istreambuf_iterator<char> (in)), std::istreambuf_iterator<char>());
  45. const std::string out = argv[2], prefix = argc > 3 ? argv[3] : "t";
  46. const double seconds = argc > 4 ? std::atof (argv[4]) : 12.0;
  47. const std::string filter = argc > 5 ? argv[5] : "";
  48. lynx::AlsImportOptions options;
  49. lynx::FlpImportReport report;
  50. options.common.report = &report;
  51. options.common.fileExists = [] (const std::string& p) { return std::filesystem::exists (p); };
  52. std::string error;
  53. auto song = lynx::importAls (bytes, options, &error);
  54. if (! song)
  55. {
  56. std::fprintf (stderr, "error: %s\n", error.c_str());
  57. return 1;
  58. }
  59. for (const auto& n : report.unsupported)
  60. std::printf ("unsupported: %s (%d)\n", n.first.c_str(), n.second);
  61. constexpr int rate = 48000;
  62. const int block = std::getenv ("LYNX_BLOCK") ? std::max (16, std::atoi (std::getenv ("LYNX_BLOCK"))) : 256;
  63. const auto total = (std::size_t) (seconds * rate);
  64. std::filesystem::create_directories (out);
  65. for (std::size_t c = 0; c < song->channels.size(); ++c)
  66. {
  67. const auto name = song->channels[c].name;
  68. if (! filter.empty() && name.find (filter) == std::string::npos)
  69. continue;
  70. auto solo = *song;
  71. for (std::size_t j = 0; j < solo.channels.size(); ++j)
  72. solo.channels[j].muted = j != c;
  73. std::int64_t songTicks = 0;
  74. solo = lynx::makeExportSong (solo, true, (std::int64_t) std::ceil (lynx::ticksPerSample (solo.bpm, rate) * rate), songTicks);
  75. lynx::RackState::Factories factories;
  76. factories.samples = [] (const std::string& p) { return lynx::tools::readWav (p); };
  77. auto state = lynx::RackState::build (solo, nullptr, rate, factories);
  78. lynx::RackRenderer renderer;
  79. renderer.prepare (rate, std::max (2048, block));
  80. std::vector<float> l (total), r (total);
  81. const auto tps = lynx::ticksPerSample (solo.bpm, rate);
  82. double tick = 0.0;
  83. const auto t0 = std::chrono::steady_clock::now();
  84. for (std::size_t pos = 0; pos < total; pos += (std::size_t) block)
  85. {
  86. const auto n = (int) std::min<std::size_t> ((std::size_t) block, total - pos);
  87. renderer.process (state.get(), tick < (double) songTicks, tick, n, l.data() + pos, r.data() + pos);
  88. tick += n * tps;
  89. }
  90. const auto ms = std::chrono::duration<double, std::milli> (std::chrono::steady_clock::now() - t0).count();
  91. std::printf ("%-44s kind %3d %.1f ms (%.2f %% of real time)\n", name.c_str(), (int) song->channels[c].instrument, ms,
  92. 100.0 * ms / (seconds * 1000.0));
  93. writeWav (out + "/" + prefix + " " + name + ".wav", l, r, rate);
  94. }
  95. return 0;
  96. }