mirror of
https://github.com/Laupetin/OpenAssetTools.git
synced 2025-06-12 07:48:16 -05:00
add flac decoding to correctly add flac sounds to sound banks
This commit is contained in:
77
src/ObjCommon/Sound/FlacDecoder.cpp
Normal file
77
src/ObjCommon/Sound/FlacDecoder.cpp
Normal file
@ -0,0 +1,77 @@
|
||||
#include <foxen/flac.h>
|
||||
#include "FlacDecoder.h"
|
||||
|
||||
class fx_flac_raii
|
||||
{
|
||||
public:
|
||||
fx_flac_raii()
|
||||
{
|
||||
ptr = FX_FLAC_ALLOC_DEFAULT();
|
||||
}
|
||||
|
||||
~fx_flac_raii()
|
||||
{
|
||||
free(ptr);
|
||||
}
|
||||
|
||||
operator fx_flac_t* ()
|
||||
{
|
||||
return ptr;
|
||||
}
|
||||
|
||||
private:
|
||||
fx_flac_t* ptr;
|
||||
};
|
||||
|
||||
class FlacDecoderImpl : public FlacDecoder
|
||||
{
|
||||
private:
|
||||
void* m_data;
|
||||
size_t m_length;
|
||||
std::unique_ptr<fx_flac_raii> m_flac;
|
||||
|
||||
public:
|
||||
explicit FlacDecoderImpl(void* data, size_t length)
|
||||
: m_data(data),
|
||||
m_length(length)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
unsigned int GetFrameCount() override
|
||||
{
|
||||
return static_cast<unsigned int>(fx_flac_get_streaminfo(*m_flac.get(), FLAC_KEY_N_SAMPLES));
|
||||
}
|
||||
|
||||
unsigned int GetFrameRate() override
|
||||
{
|
||||
return static_cast<unsigned int>(fx_flac_get_streaminfo(*m_flac.get(), FLAC_KEY_SAMPLE_RATE));
|
||||
}
|
||||
|
||||
unsigned int GetNumChannels() override
|
||||
{
|
||||
return static_cast<unsigned int>(fx_flac_get_streaminfo(*m_flac.get(), FLAC_KEY_N_CHANNELS));
|
||||
}
|
||||
|
||||
bool Decode() override
|
||||
{
|
||||
m_flac = std::make_unique<fx_flac_raii>();
|
||||
|
||||
while (true)
|
||||
{
|
||||
auto res = fx_flac_process(*m_flac.get(), reinterpret_cast<uint8_t*>(m_data), &m_length, nullptr, nullptr);
|
||||
if (res == FLAC_ERR)
|
||||
return false;
|
||||
|
||||
if (res == FLAC_END_OF_METADATA)
|
||||
break;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
};
|
||||
|
||||
std::unique_ptr<FlacDecoder> FlacDecoder::Create(void* data, size_t length)
|
||||
{
|
||||
return std::make_unique<FlacDecoderImpl>(data, length);
|
||||
}
|
21
src/ObjCommon/Sound/FlacDecoder.h
Normal file
21
src/ObjCommon/Sound/FlacDecoder.h
Normal file
@ -0,0 +1,21 @@
|
||||
#pragma once
|
||||
#include <istream>
|
||||
|
||||
class FlacDecoder
|
||||
{
|
||||
public:
|
||||
FlacDecoder() = default;
|
||||
virtual ~FlacDecoder() = default;
|
||||
|
||||
FlacDecoder(const FlacDecoder& other) = default;
|
||||
FlacDecoder(FlacDecoder&& other) noexcept = default;
|
||||
FlacDecoder& operator=(const FlacDecoder& other) = default;
|
||||
FlacDecoder& operator=(FlacDecoder&& other) noexcept = default;
|
||||
|
||||
virtual bool Decode() = 0;
|
||||
virtual unsigned int GetFrameCount() = 0;
|
||||
virtual unsigned int GetFrameRate() = 0;
|
||||
virtual unsigned int GetNumChannels() = 0;
|
||||
|
||||
static std::unique_ptr<FlacDecoder> Create(void* data, size_t length);
|
||||
};
|
Reference in New Issue
Block a user