Add unity build support
This commit is contained in:
@@ -26,40 +26,13 @@ namespace Nz
|
||||
{
|
||||
namespace
|
||||
{
|
||||
std::optional<AudioFormat> GuessFormat(UInt32 channelCount)
|
||||
{
|
||||
switch (channelCount)
|
||||
{
|
||||
case 1:
|
||||
return AudioFormat::I16_Mono;
|
||||
|
||||
case 2:
|
||||
return AudioFormat::I16_Stereo;
|
||||
|
||||
case 4:
|
||||
return AudioFormat::I16_Quad;
|
||||
|
||||
case 6:
|
||||
return AudioFormat::I16_5_1;
|
||||
|
||||
case 7:
|
||||
return AudioFormat::I16_6_1;
|
||||
|
||||
case 8:
|
||||
return AudioFormat::I16_7_1;
|
||||
|
||||
default:
|
||||
return std::nullopt;
|
||||
}
|
||||
}
|
||||
|
||||
std::size_t ReadCallback(void* pUserData, void* pBufferOut, size_t bytesToRead)
|
||||
std::size_t ReadWavCallback(void* pUserData, void* pBufferOut, size_t bytesToRead)
|
||||
{
|
||||
Stream* stream = static_cast<Stream*>(pUserData);
|
||||
return static_cast<std::size_t>(stream->Read(pBufferOut, bytesToRead));
|
||||
}
|
||||
|
||||
drwav_bool32 SeekCallback(void* pUserData, int offset, drwav_seek_origin origin)
|
||||
drwav_bool32 SeekWavCallback(void* pUserData, int offset, drwav_seek_origin origin)
|
||||
{
|
||||
Stream* stream = static_cast<Stream*>(pUserData);
|
||||
switch (origin)
|
||||
@@ -76,7 +49,7 @@ namespace Nz
|
||||
}
|
||||
}
|
||||
|
||||
bool IsSupported(const std::string_view& extension)
|
||||
bool IsWavSupported(const std::string_view& extension)
|
||||
{
|
||||
return extension == "riff" || extension == "rf64" || extension == "wav" || extension == "w64";
|
||||
}
|
||||
@@ -88,17 +61,17 @@ namespace Nz
|
||||
return Ternary::False;
|
||||
|
||||
drwav wav;
|
||||
if (!drwav_init(&wav, &ReadCallback, &SeekCallback, &stream, nullptr))
|
||||
if (!drwav_init(&wav, &ReadWavCallback, &SeekWavCallback, &stream, nullptr))
|
||||
return Ternary::False;
|
||||
|
||||
drwav_uninit(&wav);
|
||||
return Ternary::True;
|
||||
}
|
||||
|
||||
std::shared_ptr<SoundBuffer> LoadSoundBuffer(Stream& stream, const SoundBufferParams& parameters)
|
||||
std::shared_ptr<SoundBuffer> LoadWavSoundBuffer(Stream& stream, const SoundBufferParams& parameters)
|
||||
{
|
||||
drwav wav;
|
||||
if (!drwav_init(&wav, &ReadCallback, &SeekCallback, &stream, nullptr))
|
||||
if (!drwav_init(&wav, &ReadWavCallback, &SeekWavCallback, &stream, nullptr))
|
||||
{
|
||||
NazaraError("failed to decode wav stream");
|
||||
return {};
|
||||
@@ -106,7 +79,7 @@ namespace Nz
|
||||
|
||||
CallOnExit uninitOnExit([&] { drwav_uninit(&wav); });
|
||||
|
||||
std::optional<AudioFormat> formatOpt = GuessFormat(wav.channels);
|
||||
std::optional<AudioFormat> formatOpt = GuessAudioFormat(wav.channels);
|
||||
if (!formatOpt)
|
||||
{
|
||||
NazaraError("unexpected channel count: " + std::to_string(wav.channels));
|
||||
@@ -198,7 +171,7 @@ namespace Nz
|
||||
|
||||
bool Open(Stream& stream, bool forceMono)
|
||||
{
|
||||
if (!drwav_init(&m_decoder, &ReadCallback, &SeekCallback, &stream, nullptr))
|
||||
if (!drwav_init(&m_decoder, &ReadWavCallback, &SeekWavCallback, &stream, nullptr))
|
||||
{
|
||||
NazaraError("failed to decode wav stream");
|
||||
return {};
|
||||
@@ -210,7 +183,7 @@ namespace Nz
|
||||
std::memset(&m_decoder, 0, sizeof(m_decoder));
|
||||
});
|
||||
|
||||
std::optional<AudioFormat> formatOpt = GuessFormat(m_decoder.channels);
|
||||
std::optional<AudioFormat> formatOpt = GuessAudioFormat(m_decoder.channels);
|
||||
if (!formatOpt)
|
||||
{
|
||||
NazaraError("unexpected channel count: " + std::to_string(m_decoder.channels));
|
||||
@@ -284,7 +257,7 @@ namespace Nz
|
||||
bool m_mixToMono;
|
||||
};
|
||||
|
||||
std::shared_ptr<SoundStream> LoadSoundStreamFile(const std::filesystem::path& filePath, const SoundStreamParams& parameters)
|
||||
std::shared_ptr<SoundStream> LoadWavSoundStreamFile(const std::filesystem::path& filePath, const SoundStreamParams& parameters)
|
||||
{
|
||||
std::shared_ptr<drwavStream> soundStream = std::make_shared<drwavStream>();
|
||||
if (!soundStream->Open(filePath, parameters.forceMono))
|
||||
@@ -296,7 +269,7 @@ namespace Nz
|
||||
return soundStream;
|
||||
}
|
||||
|
||||
std::shared_ptr<SoundStream> LoadSoundStreamMemory(const void* data, std::size_t size, const SoundStreamParams& parameters)
|
||||
std::shared_ptr<SoundStream> LoadWavSoundStreamMemory(const void* data, std::size_t size, const SoundStreamParams& parameters)
|
||||
{
|
||||
std::shared_ptr<drwavStream> soundStream = std::make_shared<drwavStream>();
|
||||
if (!soundStream->Open(data, size, parameters.forceMono))
|
||||
@@ -308,7 +281,7 @@ namespace Nz
|
||||
return soundStream;
|
||||
}
|
||||
|
||||
std::shared_ptr<SoundStream> LoadSoundStreamStream(Stream& stream, const SoundStreamParams& parameters)
|
||||
std::shared_ptr<SoundStream> LoadWavSoundStreamStream(Stream& stream, const SoundStreamParams& parameters)
|
||||
{
|
||||
std::shared_ptr<drwavStream> soundStream = std::make_shared<drwavStream>();
|
||||
if (!soundStream->Open(stream, parameters.forceMono))
|
||||
@@ -326,9 +299,9 @@ namespace Nz
|
||||
SoundBufferLoader::Entry GetSoundBufferLoader_drwav()
|
||||
{
|
||||
SoundBufferLoader::Entry loaderEntry;
|
||||
loaderEntry.extensionSupport = IsSupported;
|
||||
loaderEntry.extensionSupport = IsWavSupported;
|
||||
loaderEntry.streamChecker = [](Stream& stream, const SoundBufferParams& parameters) { return CheckWav(stream, parameters); };
|
||||
loaderEntry.streamLoader = LoadSoundBuffer;
|
||||
loaderEntry.streamLoader = LoadWavSoundBuffer;
|
||||
|
||||
return loaderEntry;
|
||||
}
|
||||
@@ -336,11 +309,11 @@ namespace Nz
|
||||
SoundStreamLoader::Entry GetSoundStreamLoader_drwav()
|
||||
{
|
||||
SoundStreamLoader::Entry loaderEntry;
|
||||
loaderEntry.extensionSupport = IsSupported;
|
||||
loaderEntry.extensionSupport = IsWavSupported;
|
||||
loaderEntry.streamChecker = [](Stream& stream, const SoundStreamParams& parameters) { return CheckWav(stream, parameters); };
|
||||
loaderEntry.fileLoader = LoadSoundStreamFile;
|
||||
loaderEntry.memoryLoader = LoadSoundStreamMemory;
|
||||
loaderEntry.streamLoader = LoadSoundStreamStream;
|
||||
loaderEntry.fileLoader = LoadWavSoundStreamFile;
|
||||
loaderEntry.memoryLoader = LoadWavSoundStreamMemory;
|
||||
loaderEntry.streamLoader = LoadWavSoundStreamStream;
|
||||
|
||||
return loaderEntry;
|
||||
}
|
||||
|
||||
@@ -23,35 +23,7 @@ namespace Nz
|
||||
{
|
||||
namespace
|
||||
{
|
||||
std::optional<AudioFormat> GuessFormat(UInt32 channelCount)
|
||||
{
|
||||
switch (channelCount)
|
||||
{
|
||||
case 1:
|
||||
return AudioFormat::I16_Mono;
|
||||
|
||||
case 2:
|
||||
return AudioFormat::I16_Stereo;
|
||||
|
||||
case 4:
|
||||
return AudioFormat::I16_Quad;
|
||||
|
||||
case 6:
|
||||
return AudioFormat::I16_5_1;
|
||||
|
||||
case 7:
|
||||
return AudioFormat::I16_6_1;
|
||||
|
||||
case 8:
|
||||
return AudioFormat::I16_7_1;
|
||||
|
||||
default:
|
||||
return std::nullopt;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
struct Userdata
|
||||
struct FlacUserdata
|
||||
{
|
||||
std::function<void(const FLAC__StreamDecoder* decoder, FLAC__StreamDecoderErrorStatus status)> errorCallback;
|
||||
std::function<void(const FLAC__StreamDecoder* decoder, const FLAC__StreamMetadata* metadata)> metadataCallback;
|
||||
@@ -59,22 +31,22 @@ namespace Nz
|
||||
Stream* stream;
|
||||
};
|
||||
|
||||
FLAC__bool EofCallback(const FLAC__StreamDecoder* /*decoder*/, void* client_data)
|
||||
FLAC__bool FlacEofCallback(const FLAC__StreamDecoder* /*decoder*/, void* client_data)
|
||||
{
|
||||
Userdata* ud = static_cast<Userdata*>(client_data);
|
||||
FlacUserdata* ud = static_cast<FlacUserdata*>(client_data);
|
||||
return ud->stream->EndOfStream();
|
||||
}
|
||||
|
||||
void ErrorCallback(const FLAC__StreamDecoder* decoder, FLAC__StreamDecoderErrorStatus status, void* client_data)
|
||||
{
|
||||
Userdata* ud = static_cast<Userdata*>(client_data);
|
||||
FlacUserdata* ud = static_cast<FlacUserdata*>(client_data);
|
||||
assert(ud->errorCallback);
|
||||
return ud->errorCallback(decoder, status);
|
||||
}
|
||||
|
||||
FLAC__StreamDecoderLengthStatus LengthCallback(const FLAC__StreamDecoder* /*decoder*/, FLAC__uint64* stream_length, void* client_data)
|
||||
FLAC__StreamDecoderLengthStatus FlacLengthCallback(const FLAC__StreamDecoder* /*decoder*/, FLAC__uint64* stream_length, void* client_data)
|
||||
{
|
||||
Userdata* ud = static_cast<Userdata*>(client_data);
|
||||
FlacUserdata* ud = static_cast<FlacUserdata*>(client_data);
|
||||
*stream_length = ud->stream->GetSize();
|
||||
|
||||
return FLAC__STREAM_DECODER_LENGTH_STATUS_OK;
|
||||
@@ -82,14 +54,14 @@ namespace Nz
|
||||
|
||||
void MetadataCallback(const FLAC__StreamDecoder* decoder, const FLAC__StreamMetadata* metadata, void* client_data)
|
||||
{
|
||||
Userdata* ud = static_cast<Userdata*>(client_data);
|
||||
FlacUserdata* ud = static_cast<FlacUserdata*>(client_data);
|
||||
if (ud->metadataCallback)
|
||||
ud->metadataCallback(decoder, metadata);
|
||||
}
|
||||
|
||||
FLAC__StreamDecoderReadStatus ReadCallback(const FLAC__StreamDecoder* /*decoder*/, FLAC__byte buffer[], size_t* bytes, void* client_data)
|
||||
FLAC__StreamDecoderReadStatus FlacReadCallback(const FLAC__StreamDecoder* /*decoder*/, FLAC__byte buffer[], size_t* bytes, void* client_data)
|
||||
{
|
||||
Userdata* ud = static_cast<Userdata*>(client_data);
|
||||
FlacUserdata* ud = static_cast<FlacUserdata*>(client_data);
|
||||
std::size_t readBytes = ud->stream->Read(buffer, *bytes);
|
||||
|
||||
*bytes = readBytes;
|
||||
@@ -99,25 +71,25 @@ namespace Nz
|
||||
return FLAC__STREAM_DECODER_READ_STATUS_CONTINUE;
|
||||
}
|
||||
|
||||
FLAC__StreamDecoderSeekStatus SeekCallback(const FLAC__StreamDecoder* /*decoder*/, FLAC__uint64 absolute_byte_offset, void* client_data)
|
||||
FLAC__StreamDecoderSeekStatus FlacSeekCallback(const FLAC__StreamDecoder* /*decoder*/, FLAC__uint64 absolute_byte_offset, void* client_data)
|
||||
{
|
||||
Userdata* ud = static_cast<Userdata*>(client_data);
|
||||
FlacUserdata* ud = static_cast<FlacUserdata*>(client_data);
|
||||
if (ud->stream->SetCursorPos(absolute_byte_offset))
|
||||
return FLAC__STREAM_DECODER_SEEK_STATUS_OK;
|
||||
else
|
||||
return FLAC__STREAM_DECODER_SEEK_STATUS_ERROR;
|
||||
}
|
||||
|
||||
FLAC__StreamDecoderTellStatus TellCallback(const FLAC__StreamDecoder* /*decoder*/, FLAC__uint64* absolute_byte_offset, void* client_data)
|
||||
FLAC__StreamDecoderTellStatus FlacTellCallback(const FLAC__StreamDecoder* /*decoder*/, FLAC__uint64* absolute_byte_offset, void* client_data)
|
||||
{
|
||||
Userdata* ud = static_cast<Userdata*>(client_data);
|
||||
FlacUserdata* ud = static_cast<FlacUserdata*>(client_data);
|
||||
*absolute_byte_offset = ud->stream->GetCursorPos();
|
||||
return FLAC__STREAM_DECODER_TELL_STATUS_OK;
|
||||
}
|
||||
|
||||
FLAC__StreamDecoderWriteStatus WriteCallback(const FLAC__StreamDecoder* decoder, const FLAC__Frame* frame, const FLAC__int32* const buffer[], void* client_data)
|
||||
{
|
||||
Userdata* ud = static_cast<Userdata*>(client_data);
|
||||
FlacUserdata* ud = static_cast<FlacUserdata*>(client_data);
|
||||
if (ud->writeCallback)
|
||||
return ud->writeCallback(decoder, frame, buffer);
|
||||
else
|
||||
@@ -125,7 +97,7 @@ namespace Nz
|
||||
}
|
||||
|
||||
template<UInt32 Bits, typename TargetType>
|
||||
bool DecodeFrameSamplesImpl(const FLAC__Frame* frame, const FLAC__int32* const buffer[], TargetType* samples, UInt32 frameIndex, UInt32 frameCount)
|
||||
bool DecodeFlacFrameSamplesImpl(const FLAC__Frame* frame, const FLAC__int32* const buffer[], TargetType* samples, UInt32 frameIndex, UInt32 frameCount)
|
||||
{
|
||||
constexpr UInt32 TargetBits = sizeof(TargetType) * CHAR_BIT;
|
||||
for (; frameIndex < frameCount; ++frameIndex)
|
||||
@@ -145,26 +117,26 @@ namespace Nz
|
||||
return true;
|
||||
}
|
||||
|
||||
bool DecodeFrameSamples(const FLAC__Frame* frame, const FLAC__int32* const buffer[], Int16* samples, UInt32 frameIndex, UInt32 frameCount)
|
||||
bool DecodeFlacFrameSamples(const FLAC__Frame* frame, const FLAC__int32* const buffer[], Int16* samples, UInt32 frameIndex, UInt32 frameCount)
|
||||
{
|
||||
switch (frame->header.bits_per_sample)
|
||||
{
|
||||
case 8: return DecodeFrameSamplesImpl<8>(frame, buffer, samples, frameIndex, frameCount);
|
||||
case 12: return DecodeFrameSamplesImpl<12>(frame, buffer, samples, frameIndex, frameCount);
|
||||
case 16: return DecodeFrameSamplesImpl<16>(frame, buffer, samples, frameIndex, frameCount);
|
||||
case 20: return DecodeFrameSamplesImpl<20>(frame, buffer, samples, frameIndex, frameCount);
|
||||
case 24: return DecodeFrameSamplesImpl<24>(frame, buffer, samples, frameIndex, frameCount);
|
||||
case 32: return DecodeFrameSamplesImpl<32>(frame, buffer, samples, frameIndex, frameCount);
|
||||
case 8: return DecodeFlacFrameSamplesImpl<8>(frame, buffer, samples, frameIndex, frameCount);
|
||||
case 12: return DecodeFlacFrameSamplesImpl<12>(frame, buffer, samples, frameIndex, frameCount);
|
||||
case 16: return DecodeFlacFrameSamplesImpl<16>(frame, buffer, samples, frameIndex, frameCount);
|
||||
case 20: return DecodeFlacFrameSamplesImpl<20>(frame, buffer, samples, frameIndex, frameCount);
|
||||
case 24: return DecodeFlacFrameSamplesImpl<24>(frame, buffer, samples, frameIndex, frameCount);
|
||||
case 32: return DecodeFlacFrameSamplesImpl<32>(frame, buffer, samples, frameIndex, frameCount);
|
||||
default: return false;
|
||||
}
|
||||
}
|
||||
|
||||
bool DecodeFrameSamples(const FLAC__Frame* frame, const FLAC__int32* const buffer[], Int16* samples)
|
||||
bool DecodeFlacFrameSamples(const FLAC__Frame* frame, const FLAC__int32* const buffer[], Int16* samples)
|
||||
{
|
||||
return DecodeFrameSamples(frame, buffer, samples, 0, frame->header.blocksize);
|
||||
return DecodeFlacFrameSamples(frame, buffer, samples, 0, frame->header.blocksize);
|
||||
}
|
||||
|
||||
bool IsSupported(const std::string_view& extension)
|
||||
bool IsFlacSupported(const std::string_view& extension)
|
||||
{
|
||||
return extension == "flac";
|
||||
}
|
||||
@@ -180,14 +152,14 @@ namespace Nz
|
||||
|
||||
bool hasError = false;
|
||||
|
||||
Userdata ud;
|
||||
FlacUserdata ud;
|
||||
ud.stream = &stream;
|
||||
ud.errorCallback = [&](const FLAC__StreamDecoder* /*decoder*/, FLAC__StreamDecoderErrorStatus /*status*/)
|
||||
{
|
||||
hasError = true;
|
||||
};
|
||||
|
||||
FLAC__StreamDecoderInitStatus status = FLAC__stream_decoder_init_stream(decoder, &ReadCallback, &SeekCallback, &TellCallback, &LengthCallback, &EofCallback, &WriteCallback, &MetadataCallback, &ErrorCallback, &ud);
|
||||
FLAC__StreamDecoderInitStatus status = FLAC__stream_decoder_init_stream(decoder, &FlacReadCallback, &FlacSeekCallback, &FlacTellCallback, &FlacLengthCallback, &FlacEofCallback, &WriteCallback, &MetadataCallback, &ErrorCallback, &ud);
|
||||
if (status != FLAC__STREAM_DECODER_INIT_STATUS_OK)
|
||||
{
|
||||
NazaraWarning(FLAC__StreamDecoderInitStatusString[status]); //< an error shouldn't happen at this state
|
||||
@@ -205,14 +177,14 @@ namespace Nz
|
||||
return Ternary::True;
|
||||
}
|
||||
|
||||
std::shared_ptr<SoundBuffer> LoadSoundBuffer(Stream& stream, const SoundBufferParams& parameters)
|
||||
std::shared_ptr<SoundBuffer> LoadFlacSoundBuffer(Stream& stream, const SoundBufferParams& parameters)
|
||||
{
|
||||
FLAC__StreamDecoder* decoder = FLAC__stream_decoder_new();
|
||||
CallOnExit freeDecoder([&] { FLAC__stream_decoder_delete(decoder); });
|
||||
|
||||
bool hasError = false;
|
||||
|
||||
Userdata ud;
|
||||
FlacUserdata ud;
|
||||
ud.stream = &stream;
|
||||
ud.errorCallback = [&](const FLAC__StreamDecoder* /*decoder*/, FLAC__StreamDecoderErrorStatus status)
|
||||
{
|
||||
@@ -249,7 +221,7 @@ namespace Nz
|
||||
return FLAC__STREAM_DECODER_WRITE_STATUS_ABORT;
|
||||
}
|
||||
|
||||
if (!DecodeFrameSamples(frame, buffer, samples.get() + sampleIndex))
|
||||
if (!DecodeFlacFrameSamples(frame, buffer, samples.get() + sampleIndex))
|
||||
{
|
||||
NazaraError("failed to decode samples");
|
||||
return FLAC__STREAM_DECODER_WRITE_STATUS_ABORT;
|
||||
@@ -260,7 +232,7 @@ namespace Nz
|
||||
return FLAC__STREAM_DECODER_WRITE_STATUS_CONTINUE;
|
||||
};
|
||||
|
||||
FLAC__StreamDecoderInitStatus status = FLAC__stream_decoder_init_stream(decoder, &ReadCallback, &SeekCallback, &TellCallback, &LengthCallback, &EofCallback, &WriteCallback, &MetadataCallback, &ErrorCallback, &ud);
|
||||
FLAC__StreamDecoderInitStatus status = FLAC__stream_decoder_init_stream(decoder, &FlacReadCallback, &FlacSeekCallback, &FlacTellCallback, &FlacLengthCallback, &FlacEofCallback, &WriteCallback, &MetadataCallback, &ErrorCallback, &ud);
|
||||
if (status != FLAC__STREAM_DECODER_INIT_STATUS_OK)
|
||||
{
|
||||
NazaraWarning(FLAC__StreamDecoderInitStatusString[status]); //< an error shouldn't happen at this state
|
||||
@@ -287,7 +259,7 @@ namespace Nz
|
||||
return {};
|
||||
}
|
||||
|
||||
std::optional<AudioFormat> formatOpt = GuessFormat(channelCount);
|
||||
std::optional<AudioFormat> formatOpt = GuessAudioFormat(channelCount);
|
||||
if (!formatOpt)
|
||||
{
|
||||
NazaraError("unexpected channel count: " + std::to_string(channelCount));
|
||||
@@ -397,7 +369,7 @@ namespace Nz
|
||||
m_duration = UInt32(1000ULL * frameCount / m_sampleRate);
|
||||
};
|
||||
|
||||
FLAC__StreamDecoderInitStatus status = FLAC__stream_decoder_init_stream(decoder, &ReadCallback, &SeekCallback, &TellCallback, &LengthCallback, &EofCallback, &WriteCallback, &MetadataCallback, &ErrorCallback, &m_userData);
|
||||
FLAC__StreamDecoderInitStatus status = FLAC__stream_decoder_init_stream(decoder, &FlacReadCallback, &FlacSeekCallback, &FlacTellCallback, &FlacLengthCallback, &FlacEofCallback, &WriteCallback, &MetadataCallback, &ErrorCallback, &m_userData);
|
||||
if (status != FLAC__STREAM_DECODER_INIT_STATUS_OK)
|
||||
{
|
||||
NazaraWarning(FLAC__StreamDecoderInitStatusString[status]); //< an error shouldn't happen at this state
|
||||
@@ -412,7 +384,7 @@ namespace Nz
|
||||
return false;
|
||||
}
|
||||
|
||||
std::optional<AudioFormat> formatOpt = GuessFormat(m_channelCount);
|
||||
std::optional<AudioFormat> formatOpt = GuessAudioFormat(m_channelCount);
|
||||
if (!formatOpt)
|
||||
{
|
||||
NazaraError("unexpected channel count: " + std::to_string(m_channelCount));
|
||||
@@ -480,20 +452,20 @@ namespace Nz
|
||||
if (sampleCount > 0)
|
||||
{
|
||||
assert(sampleCount % frame->header.channels == 0);
|
||||
if (!DecodeFrameSamples(frame, framebuffer, buffer + readSample, 0, static_cast<UInt32>(sampleCount / frame->header.channels)))
|
||||
if (!DecodeFlacFrameSamples(frame, framebuffer, buffer + readSample, 0, static_cast<UInt32>(sampleCount / frame->header.channels)))
|
||||
return FLAC__STREAM_DECODER_WRITE_STATUS_ABORT;
|
||||
|
||||
readSample += sampleCount;
|
||||
}
|
||||
|
||||
if (!DecodeFrameSamples(frame, framebuffer, &m_overflowBuffer[overflownOffset], static_cast<UInt32>(sampleCount / frame->header.channels), frameCount))
|
||||
if (!DecodeFlacFrameSamples(frame, framebuffer, &m_overflowBuffer[overflownOffset], static_cast<UInt32>(sampleCount / frame->header.channels), frameCount))
|
||||
return FLAC__STREAM_DECODER_WRITE_STATUS_ABORT;
|
||||
|
||||
sampleCount = 0;
|
||||
}
|
||||
else
|
||||
{
|
||||
if (!DecodeFrameSamples(frame, framebuffer, buffer + readSample))
|
||||
if (!DecodeFlacFrameSamples(frame, framebuffer, buffer + readSample))
|
||||
return FLAC__STREAM_DECODER_WRITE_STATUS_ABORT;
|
||||
|
||||
readSample += blockSampleCount;
|
||||
@@ -540,7 +512,7 @@ namespace Nz
|
||||
std::vector<Int16> m_overflowBuffer;
|
||||
FLAC__StreamDecoder* m_decoder;
|
||||
AudioFormat m_format;
|
||||
Userdata m_userData;
|
||||
FlacUserdata m_userData;
|
||||
UInt32 m_channelCount;
|
||||
UInt32 m_duration;
|
||||
UInt32 m_sampleRate;
|
||||
@@ -550,7 +522,7 @@ namespace Nz
|
||||
bool m_mixToMono;
|
||||
};
|
||||
|
||||
std::shared_ptr<SoundStream> LoadSoundStreamFile(const std::filesystem::path& filePath, const SoundStreamParams& parameters)
|
||||
std::shared_ptr<SoundStream> LoadFlacSoundStreamFile(const std::filesystem::path& filePath, const SoundStreamParams& parameters)
|
||||
{
|
||||
std::shared_ptr<libflacStream> soundStream = std::make_shared<libflacStream>();
|
||||
if (!soundStream->Open(filePath, parameters.forceMono))
|
||||
@@ -562,7 +534,7 @@ namespace Nz
|
||||
return soundStream;
|
||||
}
|
||||
|
||||
std::shared_ptr<SoundStream> LoadSoundStreamMemory(const void* data, std::size_t size, const SoundStreamParams& parameters)
|
||||
std::shared_ptr<SoundStream> LoadFlacSoundStreamMemory(const void* data, std::size_t size, const SoundStreamParams& parameters)
|
||||
{
|
||||
std::shared_ptr<libflacStream> soundStream = std::make_shared<libflacStream>();
|
||||
if (!soundStream->Open(data, size, parameters.forceMono))
|
||||
@@ -574,7 +546,7 @@ namespace Nz
|
||||
return soundStream;
|
||||
}
|
||||
|
||||
std::shared_ptr<SoundStream> LoadSoundStreamStream(Stream& stream, const SoundStreamParams& parameters)
|
||||
std::shared_ptr<SoundStream> LoadFlacSoundStreamStream(Stream& stream, const SoundStreamParams& parameters)
|
||||
{
|
||||
std::shared_ptr<libflacStream> soundStream = std::make_shared<libflacStream>();
|
||||
if (!soundStream->Open(stream, parameters.forceMono))
|
||||
@@ -592,9 +564,9 @@ namespace Nz
|
||||
SoundBufferLoader::Entry GetSoundBufferLoader_libflac()
|
||||
{
|
||||
SoundBufferLoader::Entry loaderEntry;
|
||||
loaderEntry.extensionSupport = IsSupported;
|
||||
loaderEntry.extensionSupport = IsFlacSupported;
|
||||
loaderEntry.streamChecker = [](Stream& stream, const SoundBufferParams& parameters) { return CheckFlac(stream, parameters); };
|
||||
loaderEntry.streamLoader = LoadSoundBuffer;
|
||||
loaderEntry.streamLoader = LoadFlacSoundBuffer;
|
||||
|
||||
return loaderEntry;
|
||||
}
|
||||
@@ -602,11 +574,11 @@ namespace Nz
|
||||
SoundStreamLoader::Entry GetSoundStreamLoader_libflac()
|
||||
{
|
||||
SoundStreamLoader::Entry loaderEntry;
|
||||
loaderEntry.extensionSupport = IsSupported;
|
||||
loaderEntry.extensionSupport = IsFlacSupported;
|
||||
loaderEntry.streamChecker = [](Stream& stream, const SoundStreamParams& parameters) { return CheckFlac(stream, parameters); };
|
||||
loaderEntry.fileLoader = LoadSoundStreamFile;
|
||||
loaderEntry.memoryLoader = LoadSoundStreamMemory;
|
||||
loaderEntry.streamLoader = LoadSoundStreamStream;
|
||||
loaderEntry.fileLoader = LoadFlacSoundStreamFile;
|
||||
loaderEntry.memoryLoader = LoadFlacSoundStreamMemory;
|
||||
loaderEntry.streamLoader = LoadFlacSoundStreamStream;
|
||||
|
||||
return loaderEntry;
|
||||
}
|
||||
|
||||
@@ -26,40 +26,13 @@ namespace Nz
|
||||
{
|
||||
namespace
|
||||
{
|
||||
std::optional<AudioFormat> GuessFormat(UInt32 channelCount)
|
||||
{
|
||||
switch (channelCount)
|
||||
{
|
||||
case 1:
|
||||
return AudioFormat::I16_Mono;
|
||||
|
||||
case 2:
|
||||
return AudioFormat::I16_Stereo;
|
||||
|
||||
case 4:
|
||||
return AudioFormat::I16_Quad;
|
||||
|
||||
case 6:
|
||||
return AudioFormat::I16_5_1;
|
||||
|
||||
case 7:
|
||||
return AudioFormat::I16_6_1;
|
||||
|
||||
case 8:
|
||||
return AudioFormat::I16_7_1;
|
||||
|
||||
default:
|
||||
return std::nullopt;
|
||||
}
|
||||
}
|
||||
|
||||
std::size_t ReadCallback(void* ptr, size_t size, size_t nmemb, void* datasource)
|
||||
std::size_t VorbisReadCallback(void* ptr, size_t size, size_t nmemb, void* datasource)
|
||||
{
|
||||
Stream* stream = static_cast<Stream*>(datasource);
|
||||
return static_cast<std::size_t>(stream->Read(ptr, size * nmemb));
|
||||
}
|
||||
|
||||
int SeekCallback(void* datasource, ogg_int64_t offset, int whence)
|
||||
int VorbisSeekCallback(void* datasource, ogg_int64_t offset, int whence)
|
||||
{
|
||||
Stream* stream = static_cast<Stream*>(datasource);
|
||||
switch (whence)
|
||||
@@ -84,21 +57,21 @@ namespace Nz
|
||||
return 0;
|
||||
}
|
||||
|
||||
long TellCallback(void* datasource)
|
||||
long VorbisTellCallback(void* datasource)
|
||||
{
|
||||
Stream* stream = static_cast<Stream*>(datasource);
|
||||
return static_cast<long>(stream->GetCursorPos());
|
||||
}
|
||||
|
||||
static ov_callbacks s_callbacks = {
|
||||
&ReadCallback,
|
||||
&SeekCallback,
|
||||
static ov_callbacks s_vorbisCallbacks = {
|
||||
&VorbisReadCallback,
|
||||
&VorbisSeekCallback,
|
||||
nullptr,
|
||||
&TellCallback
|
||||
&VorbisTellCallback
|
||||
};
|
||||
|
||||
|
||||
std::string ErrToString(int errCode)
|
||||
std::string VorbisErrToString(int errCode)
|
||||
{
|
||||
switch (errCode)
|
||||
{
|
||||
@@ -129,7 +102,7 @@ namespace Nz
|
||||
|
||||
if (readBytes < 0)
|
||||
{
|
||||
NazaraError("an error occurred while reading file: " + ErrToString(readBytes));
|
||||
NazaraError("an error occurred while reading file: " + VorbisErrToString(readBytes));
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -143,7 +116,7 @@ namespace Nz
|
||||
return sampleCount - remainingBytes / sizeof(Int16);
|
||||
}
|
||||
|
||||
bool IsSupported(const std::string_view& extension)
|
||||
bool IsVorbisSupported(const std::string_view& extension)
|
||||
{
|
||||
static std::set<std::string_view> supportedExtensions = {
|
||||
"oga", "ogg", "ogm", "ogv", "ogx", "opus", "spx"
|
||||
@@ -159,20 +132,20 @@ namespace Nz
|
||||
return Ternary::False;
|
||||
|
||||
OggVorbis_File file;
|
||||
if (ov_test_callbacks(&stream, &file, nullptr, 0, s_callbacks) != 0)
|
||||
if (ov_test_callbacks(&stream, &file, nullptr, 0, s_vorbisCallbacks) != 0)
|
||||
return Ternary::False;
|
||||
|
||||
ov_clear(&file);
|
||||
return Ternary::True;
|
||||
}
|
||||
|
||||
std::shared_ptr<SoundBuffer> LoadSoundBuffer(Stream& stream, const SoundBufferParams& parameters)
|
||||
std::shared_ptr<SoundBuffer> LoadVorbisSoundBuffer(Stream& stream, const SoundBufferParams& parameters)
|
||||
{
|
||||
OggVorbis_File file;
|
||||
int err = ov_open_callbacks(&stream, &file, nullptr, 0, s_callbacks);
|
||||
int err = ov_open_callbacks(&stream, &file, nullptr, 0, s_vorbisCallbacks);
|
||||
if (err != 0)
|
||||
{
|
||||
NazaraError(ErrToString(err));
|
||||
NazaraError(VorbisErrToString(err));
|
||||
return {};
|
||||
}
|
||||
|
||||
@@ -181,7 +154,7 @@ namespace Nz
|
||||
vorbis_info* info = ov_info(&file, -1);
|
||||
assert(info);
|
||||
|
||||
std::optional<AudioFormat> formatOpt = GuessFormat(info->channels);
|
||||
std::optional<AudioFormat> formatOpt = GuessAudioFormat(info->channels);
|
||||
if (!formatOpt)
|
||||
{
|
||||
NazaraError("unexpected channel count: " + std::to_string(info->channels));
|
||||
@@ -278,10 +251,10 @@ namespace Nz
|
||||
|
||||
bool Open(Stream& stream, bool forceMono)
|
||||
{
|
||||
int err = ov_open_callbacks(&stream, &m_decoder, nullptr, 0, s_callbacks);
|
||||
int err = ov_open_callbacks(&stream, &m_decoder, nullptr, 0, s_vorbisCallbacks);
|
||||
if (err != 0)
|
||||
{
|
||||
NazaraError(ErrToString(err));
|
||||
NazaraError(VorbisErrToString(err));
|
||||
return {};
|
||||
}
|
||||
|
||||
@@ -294,7 +267,7 @@ namespace Nz
|
||||
vorbis_info* info = ov_info(&m_decoder, -1);
|
||||
assert(info);
|
||||
|
||||
std::optional<AudioFormat> formatOpt = GuessFormat(info->channels);
|
||||
std::optional<AudioFormat> formatOpt = GuessAudioFormat(info->channels);
|
||||
if (!formatOpt)
|
||||
{
|
||||
NazaraError("unexpected channel count: " + std::to_string(info->channels));
|
||||
@@ -374,7 +347,7 @@ namespace Nz
|
||||
bool m_mixToMono;
|
||||
};
|
||||
|
||||
std::shared_ptr<SoundStream> LoadSoundStreamFile(const std::filesystem::path& filePath, const SoundStreamParams& parameters)
|
||||
std::shared_ptr<SoundStream> LoadVorbisSoundStreamFile(const std::filesystem::path& filePath, const SoundStreamParams& parameters)
|
||||
{
|
||||
std::shared_ptr<libvorbisStream> soundStream = std::make_shared<libvorbisStream>();
|
||||
if (!soundStream->Open(filePath, parameters.forceMono))
|
||||
@@ -386,7 +359,7 @@ namespace Nz
|
||||
return soundStream;
|
||||
}
|
||||
|
||||
std::shared_ptr<SoundStream> LoadSoundStreamMemory(const void* data, std::size_t size, const SoundStreamParams& parameters)
|
||||
std::shared_ptr<SoundStream> LoadVorbisSoundStreamMemory(const void* data, std::size_t size, const SoundStreamParams& parameters)
|
||||
{
|
||||
std::shared_ptr<libvorbisStream> soundStream = std::make_shared<libvorbisStream>();
|
||||
if (!soundStream->Open(data, size, parameters.forceMono))
|
||||
@@ -398,7 +371,7 @@ namespace Nz
|
||||
return soundStream;
|
||||
}
|
||||
|
||||
std::shared_ptr<SoundStream> LoadSoundStreamStream(Stream& stream, const SoundStreamParams& parameters)
|
||||
std::shared_ptr<SoundStream> LoadVorbisSoundStreamStream(Stream& stream, const SoundStreamParams& parameters)
|
||||
{
|
||||
std::shared_ptr<libvorbisStream> soundStream = std::make_shared<libvorbisStream>();
|
||||
if (!soundStream->Open(stream, parameters.forceMono))
|
||||
@@ -416,9 +389,9 @@ namespace Nz
|
||||
SoundBufferLoader::Entry GetSoundBufferLoader_libvorbis()
|
||||
{
|
||||
SoundBufferLoader::Entry loaderEntry;
|
||||
loaderEntry.extensionSupport = IsSupported;
|
||||
loaderEntry.extensionSupport = IsVorbisSupported;
|
||||
loaderEntry.streamChecker = [](Stream& stream, const SoundBufferParams& parameters) { return CheckOgg(stream, parameters); };
|
||||
loaderEntry.streamLoader = LoadSoundBuffer;
|
||||
loaderEntry.streamLoader = LoadVorbisSoundBuffer;
|
||||
|
||||
return loaderEntry;
|
||||
}
|
||||
@@ -426,11 +399,11 @@ namespace Nz
|
||||
SoundStreamLoader::Entry GetSoundStreamLoader_libvorbis()
|
||||
{
|
||||
SoundStreamLoader::Entry loaderEntry;
|
||||
loaderEntry.extensionSupport = IsSupported;
|
||||
loaderEntry.extensionSupport = IsVorbisSupported;
|
||||
loaderEntry.streamChecker = [](Stream& stream, const SoundStreamParams& parameters) { return CheckOgg(stream, parameters); };
|
||||
loaderEntry.fileLoader = LoadSoundStreamFile;
|
||||
loaderEntry.memoryLoader = LoadSoundStreamMemory;
|
||||
loaderEntry.streamLoader = LoadSoundStreamStream;
|
||||
loaderEntry.fileLoader = LoadVorbisSoundStreamFile;
|
||||
loaderEntry.memoryLoader = LoadVorbisSoundStreamMemory;
|
||||
loaderEntry.streamLoader = LoadVorbisSoundStreamStream;
|
||||
|
||||
return loaderEntry;
|
||||
}
|
||||
|
||||
@@ -25,34 +25,7 @@ namespace Nz
|
||||
{
|
||||
namespace
|
||||
{
|
||||
std::optional<AudioFormat> GuessFormat(UInt32 channelCount)
|
||||
{
|
||||
switch (channelCount)
|
||||
{
|
||||
case 1:
|
||||
return AudioFormat::I16_Mono;
|
||||
|
||||
case 2:
|
||||
return AudioFormat::I16_Stereo;
|
||||
|
||||
case 4:
|
||||
return AudioFormat::I16_Quad;
|
||||
|
||||
case 6:
|
||||
return AudioFormat::I16_5_1;
|
||||
|
||||
case 7:
|
||||
return AudioFormat::I16_6_1;
|
||||
|
||||
case 8:
|
||||
return AudioFormat::I16_7_1;
|
||||
|
||||
default:
|
||||
return std::nullopt;
|
||||
}
|
||||
}
|
||||
|
||||
std::string ErrToString(int errCode)
|
||||
std::string MP3ErrorToString(int errCode)
|
||||
{
|
||||
switch (errCode)
|
||||
{
|
||||
@@ -66,47 +39,47 @@ namespace Nz
|
||||
}
|
||||
}
|
||||
|
||||
size_t ReadCallback(void* buf, size_t size, void* user_data)
|
||||
size_t MP3ReadCallback(void* buf, size_t size, void* user_data)
|
||||
{
|
||||
Stream* stream = static_cast<Stream*>(user_data);
|
||||
return static_cast<size_t>(stream->Read(buf, size));
|
||||
}
|
||||
|
||||
int SeekCallback(uint64_t position, void* user_data)
|
||||
int MP3SeekCallback(uint64_t position, void* user_data)
|
||||
{
|
||||
Stream* stream = static_cast<Stream*>(user_data);
|
||||
return (stream->SetCursorPos(position)) ? 0 : MP3D_E_IOERROR;
|
||||
}
|
||||
|
||||
bool IsSupported(const std::string_view& extension)
|
||||
bool IsMP3Supported(const std::string_view& extension)
|
||||
{
|
||||
return extension == "mp3";
|
||||
}
|
||||
|
||||
Ternary CheckMp3(Stream& stream, const ResourceParameters& parameters)
|
||||
Ternary CheckMP3(Stream& stream, const ResourceParameters& parameters)
|
||||
{
|
||||
bool skip;
|
||||
if (parameters.custom.GetBooleanParameter("SkipNativeMP3Loader", &skip) && skip)
|
||||
return Ternary::False;
|
||||
|
||||
mp3dec_io_t io;
|
||||
io.read = &ReadCallback;
|
||||
io.read = &MP3ReadCallback;
|
||||
io.read_data = &stream;
|
||||
io.seek = &SeekCallback;
|
||||
io.seek = &MP3SeekCallback;
|
||||
io.seek_data = &stream;
|
||||
|
||||
std::vector<UInt8> buffer(MINIMP3_BUF_SIZE);
|
||||
return (mp3dec_detect_cb(&io, buffer.data(), buffer.size()) == 0) ? Ternary::True : Ternary::False;
|
||||
}
|
||||
|
||||
std::shared_ptr<SoundBuffer> LoadSoundBuffer(Stream& stream, const SoundBufferParams& parameters)
|
||||
std::shared_ptr<SoundBuffer> LoadMP3SoundBuffer(Stream& stream, const SoundBufferParams& parameters)
|
||||
{
|
||||
static_assert(std::is_same_v<mp3d_sample_t, Int16>);
|
||||
|
||||
mp3dec_io_t io;
|
||||
io.read = &ReadCallback;
|
||||
io.read = &MP3ReadCallback;
|
||||
io.read_data = &stream;
|
||||
io.seek = &SeekCallback;
|
||||
io.seek = &MP3SeekCallback;
|
||||
io.seek_data = &stream;
|
||||
|
||||
struct UserData
|
||||
@@ -122,13 +95,13 @@ namespace Nz
|
||||
int err = mp3dec_load_cb(&dec, &io, buffer.data(), buffer.size(), &info, nullptr, &userdata);
|
||||
if (err != 0)
|
||||
{
|
||||
NazaraError(ErrToString(err));
|
||||
NazaraError(MP3ErrorToString(err));
|
||||
return {};
|
||||
}
|
||||
|
||||
CallOnExit freeBuffer([&] { std::free(info.buffer); });
|
||||
|
||||
std::optional<AudioFormat> formatOpt = GuessFormat(info.channels);
|
||||
std::optional<AudioFormat> formatOpt = GuessAudioFormat(info.channels);
|
||||
if (!formatOpt)
|
||||
{
|
||||
NazaraError("unexpected channel count: " + std::to_string(info.channels));
|
||||
@@ -214,15 +187,15 @@ namespace Nz
|
||||
|
||||
bool Open(Stream& stream, bool forceMono)
|
||||
{
|
||||
m_io.read = &ReadCallback;
|
||||
m_io.read = &MP3ReadCallback;
|
||||
m_io.read_data = &stream;
|
||||
m_io.seek = &SeekCallback;
|
||||
m_io.seek = &MP3SeekCallback;
|
||||
m_io.seek_data = &stream;
|
||||
|
||||
int err = mp3dec_ex_open_cb(&m_decoder, &m_io, MP3D_SEEK_TO_SAMPLE);
|
||||
if (err != 0)
|
||||
{
|
||||
NazaraError(ErrToString(err));
|
||||
NazaraError(MP3ErrorToString(err));
|
||||
return {};
|
||||
}
|
||||
|
||||
@@ -232,7 +205,7 @@ namespace Nz
|
||||
std::memset(&m_decoder, 0, sizeof(m_decoder));
|
||||
});
|
||||
|
||||
std::optional<AudioFormat> formatOpt = GuessFormat(m_decoder.info.channels);
|
||||
std::optional<AudioFormat> formatOpt = GuessAudioFormat(m_decoder.info.channels);
|
||||
if (!formatOpt)
|
||||
{
|
||||
NazaraError("unexpected channel count: " + std::to_string(m_decoder.info.channels));
|
||||
@@ -309,7 +282,7 @@ namespace Nz
|
||||
bool m_mixToMono;
|
||||
};
|
||||
|
||||
std::shared_ptr<SoundStream> LoadSoundStreamFile(const std::filesystem::path& filePath, const SoundStreamParams& parameters)
|
||||
std::shared_ptr<SoundStream> MP3LoadSoundStreamFile(const std::filesystem::path& filePath, const SoundStreamParams& parameters)
|
||||
{
|
||||
std::shared_ptr<minimp3Stream> soundStream = std::make_shared<minimp3Stream>();
|
||||
if (!soundStream->Open(filePath, parameters.forceMono))
|
||||
@@ -321,7 +294,7 @@ namespace Nz
|
||||
return soundStream;
|
||||
}
|
||||
|
||||
std::shared_ptr<SoundStream> LoadSoundStreamMemory(const void* data, std::size_t size, const SoundStreamParams& parameters)
|
||||
std::shared_ptr<SoundStream> MP3LoadSoundStreamMemory(const void* data, std::size_t size, const SoundStreamParams& parameters)
|
||||
{
|
||||
std::shared_ptr<minimp3Stream> soundStream = std::make_shared<minimp3Stream>();
|
||||
if (!soundStream->Open(data, size, parameters.forceMono))
|
||||
@@ -333,7 +306,7 @@ namespace Nz
|
||||
return soundStream;
|
||||
}
|
||||
|
||||
std::shared_ptr<SoundStream> LoadSoundStreamStream(Stream& stream, const SoundStreamParams& parameters)
|
||||
std::shared_ptr<SoundStream> MP3LoadSoundStreamStream(Stream& stream, const SoundStreamParams& parameters)
|
||||
{
|
||||
std::shared_ptr<minimp3Stream> soundStream = std::make_shared<minimp3Stream>();
|
||||
if (!soundStream->Open(stream, parameters.forceMono))
|
||||
@@ -351,9 +324,9 @@ namespace Nz
|
||||
SoundBufferLoader::Entry GetSoundBufferLoader_minimp3()
|
||||
{
|
||||
SoundBufferLoader::Entry loaderEntry;
|
||||
loaderEntry.extensionSupport = IsSupported;
|
||||
loaderEntry.streamChecker = [](Stream& stream, const SoundBufferParams& parameters) { return CheckMp3(stream, parameters); };
|
||||
loaderEntry.streamLoader = LoadSoundBuffer;
|
||||
loaderEntry.extensionSupport = IsMP3Supported;
|
||||
loaderEntry.streamChecker = [](Stream& stream, const SoundBufferParams& parameters) { return CheckMP3(stream, parameters); };
|
||||
loaderEntry.streamLoader = LoadMP3SoundBuffer;
|
||||
|
||||
return loaderEntry;
|
||||
}
|
||||
@@ -361,11 +334,11 @@ namespace Nz
|
||||
SoundStreamLoader::Entry GetSoundStreamLoader_minimp3()
|
||||
{
|
||||
SoundStreamLoader::Entry loaderEntry;
|
||||
loaderEntry.extensionSupport = IsSupported;
|
||||
loaderEntry.streamChecker = [](Stream& stream, const SoundStreamParams& parameters) { return CheckMp3(stream, parameters); };
|
||||
loaderEntry.fileLoader = LoadSoundStreamFile;
|
||||
loaderEntry.memoryLoader = LoadSoundStreamMemory;
|
||||
loaderEntry.streamLoader = LoadSoundStreamStream;
|
||||
loaderEntry.extensionSupport = IsMP3Supported;
|
||||
loaderEntry.streamChecker = [](Stream& stream, const SoundStreamParams& parameters) { return CheckMP3(stream, parameters); };
|
||||
loaderEntry.fileLoader = MP3LoadSoundStreamFile;
|
||||
loaderEntry.memoryLoader = MP3LoadSoundStreamMemory;
|
||||
loaderEntry.streamLoader = MP3LoadSoundStreamStream;
|
||||
|
||||
return loaderEntry;
|
||||
}
|
||||
|
||||
@@ -18,23 +18,15 @@ namespace Nz
|
||||
{
|
||||
namespace
|
||||
{
|
||||
DynLib s_library;
|
||||
std::string s_deviceName;
|
||||
std::string s_rendererName;
|
||||
std::string s_vendorName;
|
||||
ALCdevice* s_device = nullptr;
|
||||
ALCcontext* s_context = nullptr;
|
||||
unsigned int s_version;
|
||||
DynLib s_openalLbrary;
|
||||
std::string s_openalDeviceName;
|
||||
std::string s_openalRndererName;
|
||||
std::string s_openalVendorName;
|
||||
ALCdevice* s_openalDevice = nullptr;
|
||||
ALCcontext* s_openalContext = nullptr;
|
||||
unsigned int s_openalVersion;
|
||||
|
||||
/*!
|
||||
* \brief Parses the devices
|
||||
* \return Number of devices
|
||||
*
|
||||
* \param deviceString String for the device (input / output)
|
||||
* \param devices List of names of the devices
|
||||
*/
|
||||
|
||||
std::size_t ParseDevices(const char* deviceString, std::vector<std::string>& devices)
|
||||
std::size_t ParseOpenALDevices(const char* deviceString, std::vector<std::string>& devices)
|
||||
{
|
||||
if (!deviceString)
|
||||
return 0;
|
||||
@@ -81,7 +73,7 @@ namespace Nz
|
||||
|
||||
std::string OpenAL::GetRendererName()
|
||||
{
|
||||
return s_rendererName;
|
||||
return s_openalRndererName;
|
||||
}
|
||||
|
||||
/*!
|
||||
@@ -91,7 +83,7 @@ namespace Nz
|
||||
|
||||
std::string OpenAL::GetVendorName()
|
||||
{
|
||||
return s_vendorName;
|
||||
return s_openalVendorName;
|
||||
}
|
||||
|
||||
/*!
|
||||
@@ -101,7 +93,7 @@ namespace Nz
|
||||
|
||||
unsigned int OpenAL::GetVersion()
|
||||
{
|
||||
return s_version;
|
||||
return s_openalVersion;
|
||||
}
|
||||
|
||||
/*!
|
||||
@@ -149,7 +141,7 @@ namespace Nz
|
||||
{
|
||||
ErrorFlags errFlags(ErrorMode::Silent);
|
||||
std::filesystem::path libPath(path);
|
||||
if (!s_library.Load(libPath))
|
||||
if (!s_openalLbrary.Load(libPath))
|
||||
continue;
|
||||
|
||||
errFlags.SetFlags(0);
|
||||
@@ -284,7 +276,7 @@ namespace Nz
|
||||
|
||||
bool OpenAL::IsInitialized()
|
||||
{
|
||||
return s_library.IsLoaded();
|
||||
return s_openalLbrary.IsLoaded();
|
||||
}
|
||||
|
||||
/*!
|
||||
@@ -300,7 +292,7 @@ namespace Nz
|
||||
if (!deviceString)
|
||||
return 0;
|
||||
|
||||
return ParseDevices(deviceString, devices);
|
||||
return ParseOpenALDevices(deviceString, devices);
|
||||
}
|
||||
|
||||
/*!
|
||||
@@ -316,7 +308,7 @@ namespace Nz
|
||||
if (!deviceString)
|
||||
return 0;
|
||||
|
||||
return ParseDevices(deviceString, devices);
|
||||
return ParseOpenALDevices(deviceString, devices);
|
||||
}
|
||||
|
||||
/*!
|
||||
@@ -328,7 +320,7 @@ namespace Nz
|
||||
|
||||
bool OpenAL::SetDevice(const std::string& deviceName)
|
||||
{
|
||||
s_deviceName = deviceName;
|
||||
s_openalDeviceName = deviceName;
|
||||
if (IsInitialized())
|
||||
{
|
||||
CloseDevice();
|
||||
@@ -347,9 +339,9 @@ namespace Nz
|
||||
{
|
||||
CloseDevice();
|
||||
|
||||
s_rendererName.clear();
|
||||
s_vendorName.clear();
|
||||
s_library.Unload();
|
||||
s_openalRndererName.clear();
|
||||
s_openalVendorName.clear();
|
||||
s_openalLbrary.Unload();
|
||||
}
|
||||
|
||||
ALenum OpenAL::AudioFormat[AudioFormatCount] = {0}; // Added values with loading of OpenAL
|
||||
@@ -362,20 +354,20 @@ namespace Nz
|
||||
|
||||
void OpenAL::CloseDevice()
|
||||
{
|
||||
if (s_device)
|
||||
if (s_openalDevice)
|
||||
{
|
||||
if (s_context)
|
||||
if (s_openalContext)
|
||||
{
|
||||
alcMakeContextCurrent(nullptr);
|
||||
alcDestroyContext(s_context);
|
||||
s_context = nullptr;
|
||||
alcDestroyContext(s_openalContext);
|
||||
s_openalContext = nullptr;
|
||||
}
|
||||
|
||||
if (!alcCloseDevice(s_device))
|
||||
if (!alcCloseDevice(s_openalDevice))
|
||||
// We could not close the close, this means that it's still in use
|
||||
NazaraWarning("Failed to close device");
|
||||
|
||||
s_device = nullptr;
|
||||
s_openalDevice = nullptr;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -389,29 +381,29 @@ namespace Nz
|
||||
bool OpenAL::OpenDevice()
|
||||
{
|
||||
// Initialisation of the module
|
||||
s_device = alcOpenDevice(s_deviceName.empty() ? nullptr : s_deviceName.data()); // We choose the default device
|
||||
if (!s_device)
|
||||
s_openalDevice = alcOpenDevice(s_openalDeviceName.empty() ? nullptr : s_openalDeviceName.data()); // We choose the default device
|
||||
if (!s_openalDevice)
|
||||
{
|
||||
NazaraError("Failed to open default device");
|
||||
return false;
|
||||
}
|
||||
|
||||
// One context is enough
|
||||
s_context = alcCreateContext(s_device, nullptr);
|
||||
if (!s_context)
|
||||
s_openalContext = alcCreateContext(s_openalDevice, nullptr);
|
||||
if (!s_openalContext)
|
||||
{
|
||||
NazaraError("Failed to create context");
|
||||
return false;
|
||||
}
|
||||
|
||||
if (!alcMakeContextCurrent(s_context))
|
||||
if (!alcMakeContextCurrent(s_openalContext))
|
||||
{
|
||||
NazaraError("Failed to activate context");
|
||||
return false;
|
||||
}
|
||||
|
||||
s_rendererName = reinterpret_cast<const char*>(alGetString(AL_RENDERER));
|
||||
s_vendorName = reinterpret_cast<const char*>(alGetString(AL_VENDOR));
|
||||
s_openalRndererName = reinterpret_cast<const char*>(alGetString(AL_RENDERER));
|
||||
s_openalVendorName = reinterpret_cast<const char*>(alGetString(AL_VENDOR));
|
||||
|
||||
const ALchar* version = alGetString(AL_VERSION);
|
||||
if (version)
|
||||
@@ -427,20 +419,20 @@ namespace Nz
|
||||
minor = 0;
|
||||
}
|
||||
|
||||
s_version = major*100 + minor*10;
|
||||
s_openalVersion = major*100 + minor*10;
|
||||
|
||||
NazaraDebug("OpenAL version: " + NumberToString(major) + '.' + NumberToString(minor));
|
||||
}
|
||||
else
|
||||
{
|
||||
NazaraDebug("Unable to retrieve OpenAL major version");
|
||||
s_version = 0;
|
||||
s_openalVersion = 0;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
NazaraDebug("Unable to retrieve OpenAL version");
|
||||
s_version = 0;
|
||||
s_openalVersion = 0;
|
||||
}
|
||||
|
||||
// We complete the formats table
|
||||
@@ -473,7 +465,7 @@ namespace Nz
|
||||
|
||||
OpenALFunc OpenAL::LoadEntry(const char* name, bool throwException)
|
||||
{
|
||||
OpenALFunc entry = reinterpret_cast<OpenALFunc>(s_library.GetSymbol(name));
|
||||
OpenALFunc entry = reinterpret_cast<OpenALFunc>(s_openalLbrary.GetSymbol(name));
|
||||
if (!entry && throwException)
|
||||
{
|
||||
std::ostringstream oss;
|
||||
|
||||
Reference in New Issue
Block a user