diff --git a/include/libnyquist/AudioDecoder.h b/include/libnyquist/AudioDecoder.h index 69336f7..c082c2d 100644 --- a/include/libnyquist/AudioDecoder.h +++ b/include/libnyquist/AudioDecoder.h @@ -72,6 +72,7 @@ public: NyquistIO(); ~NyquistIO(); void Load(AudioData * data, const std::string & path); + void Load(AudioData * data, const std::vector & buffer); void Load(AudioData * data, const std::string & extension, const std::vector & buffer); bool IsFileSupported(const std::string & path) const; }; diff --git a/src/AudioDecoder.cpp b/src/AudioDecoder.cpp index 4082efe..5fae520 100644 --- a/src/AudioDecoder.cpp +++ b/src/AudioDecoder.cpp @@ -33,6 +33,8 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. #include "MusepackDecoder.h" #include "ModplugDecoder.h" +#include + using namespace nqr; NyquistIO::NyquistIO() @@ -69,6 +71,44 @@ void NyquistIO::Load(AudioData * data, const std::string & path) } } + +void NyquistIO::Load(AudioData * data, const std::vector & buffer) +{ + const std::vector wv = { 'w', 'v', 'p', 'k' }; + const std::vector wav = { 0x52, 0x49, 0x46, 0x46, -0x1, -0x1, -0x11, -0x1, 0x57, 0x41, 0x56, 0x45 }; + const std::vector flac = { 0x66, 0x4C, 0x61, 0x43 }; + const std::vector pat = { 0x47, 0x50, 0x41, 0x54 }; + const std::vector opus = { 'O', 'p', 'u', 's', 'H', 'e', 'a', 'd' }; + const std::vector ogg = { 0x4F, 0x67, 0x67, 0x53, 0x00, 0x02, 0x00, 0x00 }; + const std::vector mid = { 0x4D, 0x54, 0x68, 0x64 }; + + auto match_magic = [](const uint8_t * data, const int16_t * magic) + { + for (int i = 0; magic[i] != -2; i++) + { + if (magic[i] >= 0 && data[i] != magic[i]) return false; + } + return true; + }; + + + static const int16_t * numbers[] = { wv, wav, flac, pat, mid, opus, ogg, nullptr }; + static const char* extensions[] = { "wv", "wav", "flac", "pat", "mid", "opus", "ogg" }; + + for (int i = 0; numbers[i] != nullptr; i++) + { + + if (match_magic(data, numbers[i])) { + return extensions[i]; + } + + } + + + +} + +// With a known file extension void NyquistIO::Load(AudioData * data, const std::string & extension, const std::vector & buffer) { if (decoderTable.find(extension) == decoderTable.end())