You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
140 lines
3.8 KiB
C++
140 lines
3.8 KiB
C++
/*
|
|
Copyright (c) 2015, 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 "AudioDecoder.h"
|
|
|
|
#include "WavDecoder.h"
|
|
#include "WavPackDecoder.h"
|
|
#include "FlacDecoder.h"
|
|
#include "VorbisDecoder.h"
|
|
#include "OpusDecoder.h"
|
|
#include "CafDecoder.h"
|
|
|
|
using namespace nqr;
|
|
|
|
NyquistIO::NyquistIO()
|
|
{
|
|
BuildDecoderTable();
|
|
}
|
|
|
|
NyquistIO::~NyquistIO()
|
|
{
|
|
|
|
}
|
|
|
|
int NyquistIO::Load(AudioData * data, const std::string & path)
|
|
{
|
|
if (IsFileSupported(path))
|
|
{
|
|
if (decoderTable.size() > 0)
|
|
{
|
|
auto fileExtension = ParsePathForExtension(path);
|
|
auto decoder = GetDecoderForExtension(fileExtension);
|
|
|
|
try
|
|
{
|
|
return decoder->LoadFromPath(data, path);
|
|
}
|
|
catch (std::exception e)
|
|
{
|
|
std::cerr << "Caught fatal exception: " << e.what() << std::endl;
|
|
}
|
|
|
|
}
|
|
return IOError::NoDecodersLoaded;
|
|
}
|
|
else
|
|
{
|
|
return IOError::ExtensionNotSupported;
|
|
}
|
|
|
|
// Should never be reached
|
|
return IOError::UnknownError;
|
|
}
|
|
|
|
int NyquistIO::Load(AudioData *data, std::string extension, std::vector<uint8_t> & buffer)
|
|
{
|
|
if (decoderTable.find(extension) == decoderTable.end())
|
|
{
|
|
return IOError::ExtensionNotSupported;
|
|
}
|
|
|
|
if (decoderTable.size() > 0)
|
|
{
|
|
auto decoder = GetDecoderForExtension(extension);
|
|
|
|
try
|
|
{
|
|
return decoder->LoadFromBuffer(data, buffer);
|
|
}
|
|
catch (std::exception e)
|
|
{
|
|
std::cerr << "Caught fatal exception: " << e.what() << std::endl;
|
|
}
|
|
}
|
|
else
|
|
{
|
|
return IOError::NoDecodersLoaded;
|
|
}
|
|
|
|
// Should never be reached
|
|
return IOError::UnknownError;
|
|
}
|
|
|
|
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)
|
|
{
|
|
return decoderTable[ext];
|
|
}
|
|
|
|
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<CAFDecoder>());
|
|
} |