further simplify

pull/38/head
dimitri 7 years ago
parent 56e6b387c1
commit e046ec27f1

@ -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

@ -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<uint8_t> & 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<BaseDecoder> 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<nqr::BaseDecoder> 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<WavDecoder>());
AddDecoderToTable(std::make_shared<WavPackDecoder>());
AddDecoderToTable(std::make_shared<FlacDecoder>());
AddDecoderToTable(std::make_shared<VorbisDecoder>());
AddDecoderToTable(std::make_shared<OpusDecoder>());
AddDecoderToTable(std::make_shared<MusepackDecoder>());
AddDecoderToTable(std::make_shared<Mp3Decoder>());
}

@ -175,7 +175,7 @@ public:
if (id != nullptr)
{
WavPackInternal *decoder = (WavPackInternal *)id;
int32_t readLength = std::min<size_t>(byte_count, decoder->data.size() - decoder->dataPos);
auto readLength = std::min<size_t>(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<size_t>(byte_count, decoder->data.size() - decoder->dataPos);
auto writeLength = std::min<size_t>(byte_count, decoder->data.size() - decoder->dataPos);
if (writeLength > 0)
{
std::memcpy(decoder->data.data(), data, writeLength);

Loading…
Cancel
Save