4
0

modrender.cpp 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. // modrender: renders a tracker module (.mod .xm .s3m .it) with libxmp-lite's own player (round 190nw), the reference the
  2. // module import's renders are compared with (scripts/flref_compare.py pair <this.wav> <LYNX's export.wav>).
  3. // Usage: modrender <module> <out.wav> [--rate 44100] [--mix 100] [--interp nearest|linear|spline]
  4. // 16-bit stereo PCM; the song once (stops where it would loop), as LYNX's export plays it.
  5. #include <cstdint>
  6. #include <cstdio>
  7. #include <cstdlib>
  8. #include <cstring>
  9. #include <string>
  10. #include <vector>
  11. #ifndef LIBXMP_STATIC
  12. #define LIBXMP_STATIC 1
  13. #endif
  14. #include "xmp.h"
  15. namespace
  16. {
  17. void put16 (std::FILE* f, int v) { std::fputc (v & 255, f), std::fputc ((v >> 8) & 255, f); }
  18. void put32 (std::FILE* f, std::uint32_t v) { for (int i = 0; i < 4; ++i) std::fputc ((int) ((v >> (8 * i)) & 255), f); }
  19. } // namespace
  20. int main (int argc, char** argv)
  21. {
  22. if (argc < 3)
  23. {
  24. std::fprintf (stderr, "usage: modrender <module> <out.wav> [--rate N] [--mix 0..100] [--interp nearest|linear|spline]\n");
  25. return 2;
  26. }
  27. int rate = 44100, mix = 100, interp = XMP_INTERP_SPLINE;
  28. for (int i = 3; i + 1 < argc; i += 2)
  29. {
  30. const std::string a = argv[i], v = argv[i + 1];
  31. if (a == "--rate")
  32. rate = std::atoi (v.c_str());
  33. else if (a == "--mix")
  34. mix = std::atoi (v.c_str());
  35. else if (a == "--interp")
  36. interp = v == "nearest" ? XMP_INTERP_NEAREST : v == "linear" ? XMP_INTERP_LINEAR : XMP_INTERP_SPLINE;
  37. }
  38. auto* ctx = xmp_create_context();
  39. if (xmp_load_module (ctx, argv[1]) != 0)
  40. {
  41. std::fprintf (stderr, "cannot load %s\n", argv[1]);
  42. return 1;
  43. }
  44. if (xmp_start_player (ctx, rate, 0) != 0)
  45. return 1;
  46. xmp_set_player (ctx, XMP_PLAYER_MIX, mix);
  47. xmp_set_player (ctx, XMP_PLAYER_INTERP, interp);
  48. std::vector<std::int16_t> pcm;
  49. xmp_frame_info fi {};
  50. while (xmp_play_frame (ctx) == 0)
  51. {
  52. xmp_get_frame_info (ctx, &fi);
  53. if (fi.loop_count > 0)
  54. break;
  55. const auto* b = static_cast<const std::int16_t*> (fi.buffer);
  56. pcm.insert (pcm.end(), b, b + fi.buffer_size / 2);
  57. if (pcm.size() > (std::size_t) rate * 2 * 3600)
  58. break;
  59. }
  60. xmp_end_player (ctx);
  61. xmp_release_module (ctx);
  62. xmp_free_context (ctx);
  63. auto* f = std::fopen (argv[2], "wb");
  64. if (f == nullptr)
  65. return 1;
  66. const auto bytes = (std::uint32_t) (pcm.size() * 2);
  67. std::fwrite ("RIFF", 1, 4, f);
  68. put32 (f, 36 + bytes);
  69. std::fwrite ("WAVEfmt ", 1, 8, f);
  70. put32 (f, 16);
  71. put16 (f, 1);
  72. put16 (f, 2);
  73. put32 (f, (std::uint32_t) rate);
  74. put32 (f, (std::uint32_t) rate * 4);
  75. put16 (f, 4);
  76. put16 (f, 16);
  77. std::fwrite ("data", 1, 4, f);
  78. put32 (f, bytes);
  79. std::fwrite (pcm.data(), 2, pcm.size(), f);
  80. std::fclose (f);
  81. std::printf ("%s: %.2f s\n", argv[2], (double) pcm.size() / 2.0 / rate);
  82. return 0;
  83. }