Audio: Add .wav loader (using dr_wav)
This commit is contained in:
parent
28f1d8aa76
commit
7ced0e5f08
|
|
@ -7,6 +7,7 @@
|
|||
#include <Nazara/Audio/Enums.hpp>
|
||||
#include <Nazara/Audio/OpenAL.hpp>
|
||||
#include <Nazara/Audio/SoundBuffer.hpp>
|
||||
#include <Nazara/Audio/Formats/drwavLoader.hpp>
|
||||
#include <Nazara/Audio/Formats/minimp3Loader.hpp>
|
||||
#include <Nazara/Audio/Formats/sndfileLoader.hpp>
|
||||
#include <Nazara/Core/CallOnExit.hpp>
|
||||
|
|
@ -39,6 +40,8 @@ namespace Nz
|
|||
m_soundStreamLoader.RegisterLoader(Loaders::GetSoundStreamLoader_minimp3());
|
||||
m_soundBufferLoader.RegisterLoader(Loaders::GetSoundBufferLoader_sndfile());
|
||||
m_soundStreamLoader.RegisterLoader(Loaders::GetSoundStreamLoader_sndfile());
|
||||
m_soundBufferLoader.RegisterLoader(Loaders::GetSoundBufferLoader_drwav());
|
||||
m_soundStreamLoader.RegisterLoader(Loaders::GetSoundStreamLoader_drwav());
|
||||
}
|
||||
|
||||
Audio::~Audio()
|
||||
|
|
|
|||
|
|
@ -0,0 +1,353 @@
|
|||
// Copyright (C) 2020 Jérôme Leclercq
|
||||
// This file is part of the "Nazara Engine - Audio module"
|
||||
// For conditions of distribution and use, see copyright notice in Config.hpp
|
||||
|
||||
#include <Nazara/Audio/Formats/minimp3Loader.hpp>
|
||||
#include <Nazara/Audio/Algorithm.hpp>
|
||||
#include <Nazara/Audio/Audio.hpp>
|
||||
#include <Nazara/Audio/Config.hpp>
|
||||
#include <Nazara/Audio/SoundBuffer.hpp>
|
||||
#include <Nazara/Audio/SoundStream.hpp>
|
||||
#include <Nazara/Core/CallOnExit.hpp>
|
||||
#include <Nazara/Core/Endianness.hpp>
|
||||
#include <Nazara/Core/Error.hpp>
|
||||
#include <Nazara/Core/File.hpp>
|
||||
#include <Nazara/Core/MemoryView.hpp>
|
||||
#include <Nazara/Core/Stream.hpp>
|
||||
#include <optional>
|
||||
|
||||
#define DR_WAV_IMPLEMENTATION
|
||||
#define DR_WAV_NO_STDIO
|
||||
#include <dr_wav.h>
|
||||
|
||||
#include <Nazara/Audio/Debug.hpp>
|
||||
|
||||
namespace Nz
|
||||
{
|
||||
namespace
|
||||
{
|
||||
std::optional<AudioFormat> GuessFormat(UInt32 channelCount)
|
||||
{
|
||||
switch (channelCount)
|
||||
{
|
||||
case 1:
|
||||
return AudioFormat::U16_Mono;
|
||||
|
||||
case 2:
|
||||
return AudioFormat::U16_Stereo;
|
||||
|
||||
case 4:
|
||||
return AudioFormat::U16_Quad;
|
||||
|
||||
case 6:
|
||||
return AudioFormat::U16_5_1;
|
||||
|
||||
case 7:
|
||||
return AudioFormat::U16_6_1;
|
||||
|
||||
case 8:
|
||||
return AudioFormat::U16_7_1;
|
||||
|
||||
default:
|
||||
return std::nullopt;
|
||||
}
|
||||
}
|
||||
|
||||
std::string ErrToString(int errCode)
|
||||
{
|
||||
switch (errCode)
|
||||
{
|
||||
case DRWAV_SUCCESS: return "no error";
|
||||
case DRWAV_INVALID_ARGS: return "wrong parameters";
|
||||
case DRWAV_ERROR: return "generic error";
|
||||
case DRWAV_INVALID_OPERATION: return "invalid operation";
|
||||
case DRWAV_OUT_OF_MEMORY: return "out of memory";
|
||||
default: return "unknown error";
|
||||
}
|
||||
}
|
||||
|
||||
std::size_t ReadCallback(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)
|
||||
{
|
||||
Stream* stream = static_cast<Stream*>(pUserData);
|
||||
switch (origin)
|
||||
{
|
||||
case drwav_seek_origin_start:
|
||||
return stream->SetCursorPos(offset);
|
||||
|
||||
case drwav_seek_origin_current:
|
||||
return (stream->Read(nullptr, static_cast<std::size_t>(offset)) != 0);
|
||||
|
||||
default:
|
||||
NazaraInternalError("Seek mode not handled");
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
bool IsSupported(const std::string_view& extension)
|
||||
{
|
||||
return extension == "wav";
|
||||
}
|
||||
|
||||
Ternary CheckWav(Stream& stream)
|
||||
{
|
||||
drwav wav;
|
||||
if (!drwav_init(&wav, &ReadCallback, &SeekCallback, &stream, nullptr))
|
||||
return Ternary::False;
|
||||
|
||||
drwav_uninit(&wav);
|
||||
return Ternary::True;
|
||||
}
|
||||
|
||||
std::shared_ptr<SoundBuffer> LoadSoundBuffer(Stream& stream, const SoundBufferParams& parameters)
|
||||
{
|
||||
drwav wav;
|
||||
if (!drwav_init(&wav, &ReadCallback, &SeekCallback, &stream, nullptr))
|
||||
{
|
||||
NazaraError("failed to decode wav stream");
|
||||
return {};
|
||||
}
|
||||
|
||||
CallOnExit uninitOnExit([&] { drwav_uninit(&wav); });
|
||||
|
||||
std::optional<AudioFormat> formatOpt = GuessFormat(wav.channels);
|
||||
if (!formatOpt)
|
||||
{
|
||||
NazaraError("unexpected channel count: " + std::to_string(wav.channels));
|
||||
return {};
|
||||
}
|
||||
|
||||
AudioFormat format = *formatOpt;
|
||||
|
||||
UInt64 sampleCount = wav.totalPCMFrameCount * wav.channels;
|
||||
std::unique_ptr<Int16[]> samples = std::make_unique<Int16[]>(sampleCount); //< std::vector would default-init to zero
|
||||
|
||||
drwav_read_pcm_frames_s16(&wav, wav.totalPCMFrameCount, samples.get());
|
||||
|
||||
if (parameters.forceMono && format != AudioFormat::U16_Mono)
|
||||
{
|
||||
MixToMono(samples.get(), samples.get(), static_cast<UInt32>(wav.channels), wav.totalPCMFrameCount);
|
||||
|
||||
format = AudioFormat::U16_Mono;
|
||||
sampleCount = wav.totalPCMFrameCount;
|
||||
}
|
||||
|
||||
return std::make_shared<SoundBuffer>(format, sampleCount, wav.sampleRate, samples.get());
|
||||
}
|
||||
|
||||
class drwavStream : public SoundStream
|
||||
{
|
||||
public:
|
||||
drwavStream() :
|
||||
m_readSampleCount(0)
|
||||
{
|
||||
std::memset(&m_decoder, 0, sizeof(m_decoder));
|
||||
}
|
||||
|
||||
~drwavStream()
|
||||
{
|
||||
drwav_uninit(&m_decoder);
|
||||
}
|
||||
|
||||
UInt32 GetDuration() const override
|
||||
{
|
||||
return m_duration;
|
||||
}
|
||||
|
||||
AudioFormat GetFormat() const override
|
||||
{
|
||||
if (m_mixToMono)
|
||||
return AudioFormat::U16_Mono;
|
||||
else
|
||||
return m_format;
|
||||
}
|
||||
|
||||
std::mutex& GetMutex() override
|
||||
{
|
||||
return m_mutex;
|
||||
}
|
||||
|
||||
UInt64 GetSampleCount() const override
|
||||
{
|
||||
return m_sampleCount;
|
||||
}
|
||||
|
||||
UInt32 GetSampleRate() const override
|
||||
{
|
||||
return m_sampleRate;
|
||||
}
|
||||
|
||||
bool Open(const std::filesystem::path& filePath, bool forceMono)
|
||||
{
|
||||
std::unique_ptr<File> file = std::make_unique<File>();
|
||||
if (!file->Open(filePath, OpenMode::ReadOnly))
|
||||
{
|
||||
NazaraError("failed to open stream from file: " + Error::GetLastError());
|
||||
return false;
|
||||
}
|
||||
|
||||
m_ownedStream = std::move(file);
|
||||
return Open(*m_ownedStream, forceMono);
|
||||
}
|
||||
|
||||
bool Open(const void* data, std::size_t size, bool forceMono)
|
||||
{
|
||||
m_ownedStream = std::make_unique<MemoryView>(data, size);
|
||||
return Open(*m_ownedStream, forceMono);
|
||||
}
|
||||
|
||||
bool Open(Stream& stream, bool forceMono)
|
||||
{
|
||||
if (!drwav_init(&m_decoder, &ReadCallback, &SeekCallback, &stream, nullptr))
|
||||
{
|
||||
NazaraError("failed to decode wav stream");
|
||||
return {};
|
||||
}
|
||||
|
||||
CallOnExit resetOnError([this]
|
||||
{
|
||||
drwav_uninit(&m_decoder);
|
||||
std::memset(&m_decoder, 0, sizeof(m_decoder));
|
||||
});
|
||||
|
||||
std::optional<AudioFormat> formatOpt = GuessFormat(m_decoder.channels);
|
||||
if (!formatOpt)
|
||||
{
|
||||
NazaraError("unexpected channel count: " + std::to_string(m_decoder.channels));
|
||||
return false;
|
||||
}
|
||||
|
||||
m_format = *formatOpt;
|
||||
|
||||
m_duration = static_cast<UInt32>(1000ULL * m_decoder.totalPCMFrameCount / (m_decoder.sampleRate * m_decoder.channels));
|
||||
m_sampleCount = m_decoder.totalPCMFrameCount * m_decoder.channels;
|
||||
m_sampleRate = m_decoder.sampleRate;
|
||||
|
||||
// Mixing to mono will be done on the fly
|
||||
if (forceMono && m_format != AudioFormat::U16_Mono)
|
||||
{
|
||||
m_mixToMono = true;
|
||||
m_sampleCount = m_decoder.totalPCMFrameCount;
|
||||
}
|
||||
else
|
||||
m_mixToMono = false;
|
||||
|
||||
resetOnError.Reset();
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
UInt64 Read(void* buffer, UInt64 sampleCount) override
|
||||
{
|
||||
// Convert to mono in the fly if necessary
|
||||
if (m_mixToMono)
|
||||
{
|
||||
// Keep a buffer to the side to prevent allocation
|
||||
m_mixBuffer.resize(sampleCount * m_decoder.channels);
|
||||
std::size_t readSample = drwav_read_pcm_frames_s16(&m_decoder, sampleCount, static_cast<Int16*>(m_mixBuffer.data()));
|
||||
m_readSampleCount += readSample;
|
||||
|
||||
MixToMono(m_mixBuffer.data(), static_cast<Int16*>(buffer), m_decoder.channels, sampleCount);
|
||||
|
||||
return readSample;
|
||||
}
|
||||
else
|
||||
{
|
||||
UInt64 readSample = drwav_read_pcm_frames_s16(&m_decoder, sampleCount / m_decoder.channels, static_cast<Int16*>(buffer));
|
||||
m_readSampleCount += readSample;
|
||||
|
||||
return readSample * m_decoder.channels;
|
||||
}
|
||||
}
|
||||
|
||||
void Seek(UInt64 offset) override
|
||||
{
|
||||
drwav_seek_to_pcm_frame(&m_decoder, offset);
|
||||
m_readSampleCount = offset;
|
||||
}
|
||||
|
||||
UInt64 Tell() override
|
||||
{
|
||||
return m_readSampleCount;
|
||||
}
|
||||
|
||||
private:
|
||||
std::unique_ptr<Stream> m_ownedStream;
|
||||
std::vector<Int16> m_mixBuffer;
|
||||
AudioFormat m_format;
|
||||
drwav m_decoder;
|
||||
bool m_mixToMono;
|
||||
std::mutex m_mutex;
|
||||
UInt32 m_duration;
|
||||
UInt32 m_sampleRate;
|
||||
UInt64 m_readSampleCount;
|
||||
UInt64 m_sampleCount;
|
||||
};
|
||||
|
||||
std::shared_ptr<SoundStream> LoadSoundStreamFile(const std::filesystem::path& filePath, const SoundStreamParams& parameters)
|
||||
{
|
||||
std::shared_ptr<drwavStream> soundStream = std::make_shared<drwavStream>();
|
||||
if (!soundStream->Open(filePath, parameters.forceMono))
|
||||
{
|
||||
NazaraError("failed to open sound stream");
|
||||
return {};
|
||||
}
|
||||
|
||||
return soundStream;
|
||||
}
|
||||
|
||||
std::shared_ptr<SoundStream> LoadSoundStreamMemory(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))
|
||||
{
|
||||
NazaraError("failed to open music stream");
|
||||
return {};
|
||||
}
|
||||
|
||||
return soundStream;
|
||||
}
|
||||
|
||||
std::shared_ptr<SoundStream> LoadSoundStreamStream(Stream& stream, const SoundStreamParams& parameters)
|
||||
{
|
||||
std::shared_ptr<drwavStream> soundStream = std::make_shared<drwavStream>();
|
||||
if (!soundStream->Open(stream, parameters.forceMono))
|
||||
{
|
||||
NazaraError("failed to open music stream");
|
||||
return {};
|
||||
}
|
||||
|
||||
return soundStream;
|
||||
}
|
||||
}
|
||||
|
||||
namespace Loaders
|
||||
{
|
||||
SoundBufferLoader::Entry GetSoundBufferLoader_drwav()
|
||||
{
|
||||
SoundBufferLoader::Entry loaderEntry;
|
||||
loaderEntry.extensionSupport = IsSupported;
|
||||
loaderEntry.streamChecker = [](Stream& stream, const SoundBufferParams&) { return CheckWav(stream); };
|
||||
loaderEntry.streamLoader = LoadSoundBuffer;
|
||||
|
||||
return loaderEntry;
|
||||
}
|
||||
|
||||
SoundStreamLoader::Entry GetSoundStreamLoader_drwav()
|
||||
{
|
||||
SoundStreamLoader::Entry loaderEntry;
|
||||
loaderEntry.extensionSupport = IsSupported;
|
||||
loaderEntry.streamChecker = [](Stream& stream, const SoundStreamParams&) { return CheckWav(stream); };
|
||||
loaderEntry.fileLoader = LoadSoundStreamFile;
|
||||
loaderEntry.memoryLoader = LoadSoundStreamMemory;
|
||||
loaderEntry.streamLoader = LoadSoundStreamStream;
|
||||
|
||||
return loaderEntry;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,20 @@
|
|||
// Copyright (C) 2020 Jérôme Leclercq
|
||||
// This file is part of the "Nazara Engine - Utility module"
|
||||
// For conditions of distribution and use, see copyright notice in Config.hpp
|
||||
|
||||
#pragma once
|
||||
|
||||
#ifndef NAZARA_LOADERS_DRWAV_HPP
|
||||
#define NAZARA_LOADERS_DRWAV_HPP
|
||||
|
||||
#include <Nazara/Prerequisites.hpp>
|
||||
#include <Nazara/Audio/SoundBuffer.hpp>
|
||||
#include <Nazara/Audio/SoundStream.hpp>
|
||||
|
||||
namespace Nz::Loaders
|
||||
{
|
||||
SoundBufferLoader::Entry GetSoundBufferLoader_drwav();
|
||||
SoundStreamLoader::Entry GetSoundStreamLoader_drwav();
|
||||
}
|
||||
|
||||
#endif
|
||||
|
|
@ -1,7 +1,7 @@
|
|||
local modules = {
|
||||
Audio = {
|
||||
Deps = {"NazaraCore"},
|
||||
Packages = {"libsndfile", "minimp3"}
|
||||
Packages = {"dr_wav", "libsndfile", "minimp3"}
|
||||
},
|
||||
Core = {},
|
||||
Graphics = {
|
||||
|
|
@ -86,7 +86,7 @@ local modules = {
|
|||
|
||||
add_repositories("local-repo xmake-repo")
|
||||
|
||||
add_requires("chipmunk2d", "freetype", "libsndfile", "libsdl", "minimp3", "stb")
|
||||
add_requires("chipmunk2d", "dr_wav", "freetype", "libsndfile", "libsdl", "minimp3", "stb")
|
||||
add_requires("newtondynamics", { debug = is_plat("windows") and is_mode("debug") }) -- Newton doesn't like compiling in Debug on Linux
|
||||
|
||||
set_project("NazaraEngine")
|
||||
|
|
|
|||
Loading…
Reference in New Issue