WavReader.h 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. #pragma once
  2. // Minimal WAV reader (PCM 8/16/24/32 and float 32, WavPack, FL's Ogg Vorbis: Ogg-in-WAV and .ogg, and MP3, decoded as
  3. // the app decodes them, core/OggVorbis.h and core/Mp3Decoder.h) for the tools and tests. The app uses JUCE's readers for the rest (FLAC…).
  4. #include <algorithm>
  5. #include <cstdint>
  6. #include <cstring>
  7. #include <fstream>
  8. #include <iterator>
  9. #include <memory>
  10. #include <string>
  11. #include <vector>
  12. #include "core/AudioEdit.h"
  13. #include "core/Instruments.h"
  14. #include "core/Mp3Decoder.h"
  15. #include "core/OggVorbis.h"
  16. #include "core/WavPack.h"
  17. namespace lynx::tools
  18. {
  19. inline std::shared_ptr<const SampleData> readWav (const std::string& path)
  20. {
  21. std::ifstream in (path, std::ios::binary);
  22. if (! in)
  23. return nullptr;
  24. std::vector<std::uint8_t> d ((std::istreambuf_iterator<char> (in)), std::istreambuf_iterator<char>());
  25. // WavPack (.wv from FL's packs)
  26. if (d.size() >= 4 && std::memcmp (d.data(), "wvpk", 4) == 0)
  27. {
  28. DecodedAudio audio;
  29. if (! decodeWavPack (d.data(), d.size(), audio))
  30. return nullptr;
  31. auto out = std::make_shared<SampleData>();
  32. out->sampleRate = audio.sampleRate;
  33. out->left = std::move (audio.channels[0]);
  34. if (audio.channels.size() > 1)
  35. out->right = std::move (audio.channels[1]);
  36. return out;
  37. }
  38. // FL's Ogg Vorbis samples (WAV with an Ogg "data" chunk, or plain .ogg), like the app's SampleLibrary
  39. if (oggvorbis::isOggWav (d.data(), d.size()) || oggvorbis::isOggStream (d.data(), d.size()))
  40. {
  41. oggvorbis::Decoded audio;
  42. if (! oggvorbis::decode (d.data(), d.size(), audio) || audio.sampleRate <= 0 || audio.data.empty() || audio.data[0].empty())
  43. return nullptr;
  44. auto out = std::make_shared<SampleData>();
  45. out->sampleRate = audio.sampleRate;
  46. out->left = std::move (audio.data[0]);
  47. if (audio.data.size() > 1)
  48. out->right = std::move (audio.data[1]);
  49. return out;
  50. }
  51. // MP3 (round 157b: the app's reader, with FL's timing)
  52. if (mp3::looksLikeMp3 (d.data(), std::min<std::size_t> (d.size(), 1 << 16)))
  53. {
  54. mp3::Decoded audio;
  55. if (! mp3::decode (d.data(), d.size(), audio))
  56. return nullptr;
  57. auto out = std::make_shared<SampleData>();
  58. out->sampleRate = audio.info.sampleRate;
  59. out->left = std::move (audio.left);
  60. out->right = std::move (audio.right);
  61. return out;
  62. }
  63. if (d.size() < 12 || std::memcmp (d.data(), "RIFF", 4) != 0 || std::memcmp (d.data() + 8, "WAVE", 4) != 0)
  64. return nullptr;
  65. auto u16 = [&] (std::size_t o) { return (std::uint16_t) (d[o] | (d[o + 1] << 8)); };
  66. auto u32 = [&] (std::size_t o) { return (std::uint32_t) (u16 (o) | ((std::uint32_t) u16 (o + 2) << 16)); };
  67. std::uint16_t format = 0, channels = 0, bits = 0;
  68. std::uint32_t rate = 0;
  69. const std::uint8_t* data = nullptr;
  70. std::size_t dataSize = 0;
  71. std::int64_t loopStart = -1, loopEnd = -1;
  72. for (std::size_t pos = 12; pos + 8 <= d.size();)
  73. {
  74. const auto size = u32 (pos + 4);
  75. const auto body = pos + 8;
  76. if (std::memcmp (&d[pos], "fmt ", 4) == 0 && body + 16 <= d.size())
  77. {
  78. format = u16 (body);
  79. channels = u16 (body + 2);
  80. rate = u32 (body + 4);
  81. bits = u16 (body + 14);
  82. if (format == 0xFFFE && size >= 26) // WAVE_FORMAT_EXTENSIBLE: subformat in the first 2 bytes of the GUID
  83. format = u16 (body + 24);
  84. }
  85. else if (std::memcmp (&d[pos], "smpl", 4) == 0 && size >= 60 && body + 60 <= d.size() && u32 (body + 28) > 0)
  86. {
  87. loopStart = u32 (body + 44); // first loop: start and end frames (end inclusive)
  88. loopEnd = (std::int64_t) u32 (body + 48) + 1;
  89. }
  90. else if (std::memcmp (&d[pos], "data", 4) == 0)
  91. {
  92. data = &d[body];
  93. dataSize = std::min<std::size_t> (size, d.size() - body);
  94. }
  95. pos = body + size + (size & 1);
  96. }
  97. if (data == nullptr || channels == 0 || rate == 0 || bits == 0 || (format != 1 && format != 3))
  98. return nullptr;
  99. const auto bytesPerSample = bits / 8u;
  100. const auto frames = dataSize / (bytesPerSample * channels);
  101. auto sample = std::make_shared<SampleData>();
  102. sample->sampleRate = rate;
  103. if (loopStart >= 0 && loopEnd > loopStart && loopEnd <= (std::int64_t) frames)
  104. sample->loopStart = loopStart, sample->loopEnd = loopEnd;
  105. sample->left.resize (frames);
  106. if (channels > 1)
  107. sample->right.resize (frames);
  108. auto read = [&] (const std::uint8_t* p) -> float {
  109. if (format == 3 && bits == 32)
  110. {
  111. float f;
  112. std::memcpy (&f, p, 4);
  113. return f;
  114. }
  115. switch (bits)
  116. {
  117. case 8: return (p[0] - 128) / 128.0f;
  118. case 16: return (std::int16_t) (p[0] | (p[1] << 8)) / 32768.0f;
  119. case 24: return (std::int32_t) ((p[0] << 8) | (p[1] << 16) | ((std::uint32_t) p[2] << 24)) / 2147483648.0f;
  120. case 32: return (std::int32_t) (p[0] | (p[1] << 8) | (p[2] << 16) | ((std::uint32_t) p[3] << 24)) / 2147483648.0f;
  121. default: return 0.0f;
  122. }
  123. };
  124. for (std::size_t i = 0; i < frames; ++i)
  125. {
  126. const auto* frame = data + i * bytesPerSample * channels;
  127. sample->left[i] = read (frame);
  128. if (channels > 1)
  129. sample->right[i] = read (frame + bytesPerSample);
  130. }
  131. // cue points / regions and ACID beats, as the app's SampleLibrary keeps them (Fruity Granulizer's slices and sync)
  132. const auto extras = audioedit::decodeWavExtras (d.data(), d.size(), (std::int64_t) frames);
  133. sample->markerStarts = extras.markerStarts;
  134. sample->acidBeats = extras.acidBeats;
  135. return sample;
  136. }
  137. } // namespace lynx::tools