4
0

flpdump.cpp 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. // flpdump: shows the contents of an .flp project.
  2. // Usage: flpdump [--events] file.flp
  3. #include <cstdio>
  4. #include <fstream>
  5. #include <iterator>
  6. #include <map>
  7. #include <string>
  8. #include <vector>
  9. #include "flp/Flp.h"
  10. namespace
  11. {
  12. const char* typeName (flp::ChannelType t)
  13. {
  14. switch (t)
  15. {
  16. case flp::ChannelType::sampler: return "sampler";
  17. case flp::ChannelType::native: return "native";
  18. case flp::ChannelType::instrument: return "instrument";
  19. case flp::ChannelType::layer: return "layer";
  20. case flp::ChannelType::audioClip: return "audio clip";
  21. case flp::ChannelType::automationClip: return "automation";
  22. case flp::ChannelType::unknown: break;
  23. }
  24. return "?";
  25. }
  26. void dumpEvents (const std::vector<std::uint8_t>& bytes)
  27. {
  28. flp::EventReader reader (bytes);
  29. std::map<std::uint32_t, std::pair<int, std::size_t>> stats; // id -> (count, bytes)
  30. flp::Event e;
  31. while (reader.next (e))
  32. {
  33. auto& s = stats[e.id];
  34. ++s.first;
  35. s.second += e.data.size();
  36. }
  37. std::printf ("%-12s %8s %10s\n", "event", "count", "bytes");
  38. for (auto& [id, s] : stats)
  39. std::printf ("0x%-10X %8d %10zu\n", id, s.first, s.second);
  40. }
  41. void dumpProject (const std::vector<std::uint8_t>& bytes)
  42. {
  43. const auto p = flp::parseProject (bytes);
  44. std::printf ("Version : %s\n", p.version.c_str());
  45. std::printf ("PPQ : %u\n", p.header.ppq);
  46. std::printf ("Tempo : %.3f BPM\n", p.tempo);
  47. if (! p.title.empty()) std::printf ("Title : %s\n", p.title.c_str());
  48. if (! p.artist.empty()) std::printf ("Artist : %s\n", p.artist.c_str());
  49. if (! p.genre.empty()) std::printf ("Genre : %s\n", p.genre.c_str());
  50. std::printf ("\nChannels (%zu)\n", p.channels.size());
  51. for (auto& c : p.channels)
  52. std::printf (" %3d %-11s %-24s plugin=%-16s mixer=%d%s%s\n", c.index, typeName (c.type),
  53. c.name.c_str(), c.pluginName.c_str(), c.mixerTrack,
  54. c.samplePath.empty() ? "" : " sample=", c.samplePath.c_str());
  55. std::printf ("\nPatterns (%zu)\n", p.patterns.size());
  56. for (auto& pat : p.patterns)
  57. {
  58. std::uint32_t end = 0;
  59. for (auto& n : pat.notes)
  60. end = std::max (end, n.position + n.length);
  61. std::printf (" %3d %-24s %5zu notes up to tick %u\n", pat.index, pat.name.c_str(), pat.notes.size(), end);
  62. }
  63. for (auto& a : p.arrangements)
  64. {
  65. std::printf ("\nArrangement %d \"%s\": %zu playlist items\n", a.index, a.name.c_str(), a.items.size());
  66. int shown = 0;
  67. for (auto& item : a.items)
  68. {
  69. if (shown++ == 15)
  70. {
  71. std::printf (" ...\n");
  72. break;
  73. }
  74. const bool isPattern = item.kind == flp::PlaylistItem::Kind::pattern;
  75. std::string what;
  76. if (isPattern)
  77. {
  78. const auto* pat = p.findPattern (item.index);
  79. what = "pattern " + std::to_string (item.index) + (pat && ! pat->name.empty() ? " (" + pat->name + ")" : "");
  80. }
  81. else
  82. {
  83. const auto* ch = p.findChannel (item.index);
  84. what = "clip of channel " + std::to_string (item.index) + (ch ? " (" + ch->name + ")" : "");
  85. }
  86. std::printf (" track %3d tick %7u dur %6u %s\n", item.track, item.position, item.length, what.c_str());
  87. }
  88. }
  89. std::printf ("\nMixer: %zu inserts\n", p.mixer.size());
  90. for (auto& m : p.mixer)
  91. if (! m.name.empty())
  92. std::printf (" %3d %s\n", m.index, m.name.c_str());
  93. for (auto& w : p.warnings)
  94. std::printf ("WARNING: %s\n", w.c_str());
  95. }
  96. } // namespace
  97. int main (int argc, char** argv)
  98. {
  99. bool events = false;
  100. const char* path = nullptr;
  101. for (int i = 1; i < argc; ++i)
  102. {
  103. if (std::string (argv[i]) == "--events")
  104. events = true;
  105. else
  106. path = argv[i];
  107. }
  108. if (path == nullptr)
  109. {
  110. std::fprintf (stderr, "usage: flpdump [--events] file.flp\n");
  111. return 2;
  112. }
  113. std::ifstream in (path, std::ios::binary);
  114. if (! in)
  115. {
  116. std::fprintf (stderr, "could not open %s\n", path);
  117. return 1;
  118. }
  119. std::vector<std::uint8_t> bytes ((std::istreambuf_iterator<char> (in)), std::istreambuf_iterator<char>());
  120. try
  121. {
  122. events ? dumpEvents (bytes) : dumpProject (bytes);
  123. }
  124. catch (const flp::Error& e)
  125. {
  126. std::fprintf (stderr, "error: %s\n", e.what());
  127. return 1;
  128. }
  129. return 0;
  130. }