From e046ec27f17321576291267c085289b48741988e Mon Sep 17 00:00:00 2001 From: dimitri Date: Thu, 13 Dec 2018 20:40:24 -0800 Subject: [PATCH] further simplify --- examples/src/Main.cpp | 3 +- src/AudioDecoder.cpp | 129 ----------------------------------------- src/WavPackDecoder.cpp | 4 +- 3 files changed, 4 insertions(+), 132 deletions(-) delete mode 100644 src/AudioDecoder.cpp diff --git a/examples/src/Main.cpp b/examples/src/Main.cpp index fbc3d3b..73bdeba 100644 --- a/examples/src/Main.cpp +++ b/examples/src/Main.cpp @@ -36,7 +36,7 @@ int main(int argc, const char **argv) try else { // Circular libnyquist testing - //loader.Load(fileData.get(), "encoded.opus"); + //loader.Load(fileData.get(), "libnyquist_example_output.opus"); // 1-channel wave //loader.Load(fileData.get(), "test_data/1ch/44100/8/test.wav"); @@ -70,6 +70,7 @@ int main(int argc, const char **argv) try //loader.Load(fileData.get(), "test_data/ad_hoc/KittyPurr16_Stereo.flac"); //loader.Load(fileData.get(), "test_data/ad_hoc/KittyPurr16_Mono.flac"); //loader.Load(fileData.get(), "test_data/ad_hoc/KittyPurr24_Stereo.flac"); + //auto memory = ReadFile("test_data/ad_hoc/KittyPurr24_Stereo.flac"); // broken //loader.Load(fileData.get(), "flac", memory.buffer); // broken diff --git a/src/AudioDecoder.cpp b/src/AudioDecoder.cpp deleted file mode 100644 index 489d56b..0000000 --- a/src/AudioDecoder.cpp +++ /dev/null @@ -1,129 +0,0 @@ -/* -Copyright (c) 2019, Dimitri Diakopoulos All rights reserved. - -Redistribution and use in source and binary forms, with or without -modification, are permitted provided that the following conditions are met: - -* Redistributions of source code must retain the above copyright notice, this - list of conditions and the following disclaimer. - -* Redistributions in binary form must reproduce the above copyright notice, - this list of conditions and the following disclaimer in the documentation - and/or other materials provided with the distribution. - -THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" -AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE -IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE -DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE -FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL -DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR -SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER -CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, -OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE -OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. -*/ - -#include "Decoders.h" - -using namespace nqr; - -NyquistIO::NyquistIO() -{ - BuildDecoderTable(); -} - -NyquistIO::~NyquistIO() { } - -void NyquistIO::Load(AudioData * data, const std::string & path) -{ - if (IsFileSupported(path)) - { - if (decoderTable.size()) - { - auto fileExtension = ParsePathForExtension(path); - auto decoder = GetDecoderForExtension(fileExtension); - - try - { - decoder->LoadFromPath(data, path); - } - catch (const std::exception & e) - { - std::cerr << "Caught internal exception: " << e.what() << std::endl; - } - - } - else throw std::runtime_error("No available decoders."); - } - else - { - throw UnsupportedExtensionEx(); - } -} - -void NyquistIO::Load(AudioData * data, const std::string & extension, const std::vector & buffer) -{ - if (decoderTable.find(extension) == decoderTable.end()) - { - throw UnsupportedExtensionEx(); - } - - if (decoderTable.size()) - { - auto decoder = GetDecoderForExtension(extension); - try - { - decoder->LoadFromBuffer(data, buffer); - } - catch (const std::exception & e) - { - std::cerr << "Caught internal exception: " << e.what() << std::endl; - } - } - else - { - throw std::runtime_error("No available decoders."); - } -} - -bool NyquistIO::IsFileSupported(const std::string & path) const -{ - auto fileExtension = ParsePathForExtension(path); - if (decoderTable.find(fileExtension) == decoderTable.end()) return false; - else return true; -} - -std::string NyquistIO::ParsePathForExtension(const std::string & path) const -{ - if (path.find_last_of(".") != std::string::npos) return path.substr(path.find_last_of(".") + 1); - return std::string(""); -} - -std::shared_ptr NyquistIO::GetDecoderForExtension(const std::string & ext) -{ - if (decoderTable.size()) return decoderTable[ext]; - else throw std::runtime_error("No available decoders."); - return nullptr; -} - -void NyquistIO::AddDecoderToTable(std::shared_ptr decoder) -{ - auto supportedExtensions = decoder->GetSupportedFileExtensions(); - - for (const auto ext : supportedExtensions) - { - if (decoderTable.count(ext) >= 1) throw std::runtime_error("decoder already exists for extension."); - decoderTable.insert(DecoderPair(ext, decoder)); - } -} - -void NyquistIO::BuildDecoderTable() -{ - AddDecoderToTable(std::make_shared()); - AddDecoderToTable(std::make_shared()); - AddDecoderToTable(std::make_shared()); - AddDecoderToTable(std::make_shared()); - AddDecoderToTable(std::make_shared()); - AddDecoderToTable(std::make_shared()); - AddDecoderToTable(std::make_shared()); -} diff --git a/src/WavPackDecoder.cpp b/src/WavPackDecoder.cpp index 203bb7e..11fe82a 100644 --- a/src/WavPackDecoder.cpp +++ b/src/WavPackDecoder.cpp @@ -175,7 +175,7 @@ public: if (id != nullptr) { WavPackInternal *decoder = (WavPackInternal *)id; - int32_t readLength = std::min(byte_count, decoder->data.size() - decoder->dataPos); + auto readLength = std::min(byte_count, decoder->data.size() - decoder->dataPos); if (readLength > 0) { std::memcpy(data, decoder->data.data(), readLength); @@ -191,7 +191,7 @@ public: if (id != nullptr) { WavPackInternal *decoder = (WavPackInternal *)id; - int32_t writeLength = std::min(byte_count, decoder->data.size() - decoder->dataPos); + auto writeLength = std::min(byte_count, decoder->data.size() - decoder->dataPos); if (writeLength > 0) { std::memcpy(decoder->data.data(), data, writeLength);