Compare commits
8 Commits
467f471390
...
e306a66956
| Author | SHA1 | Date |
|---|---|---|
|
|
e306a66956 | |
|
|
4199582a52 | |
|
|
2784d62ec9 | |
|
|
d304c56898 | |
|
|
8cc1719d1b | |
|
|
cc2c36c75d | |
|
|
cc91b8d3b9 | |
|
|
841778fe2f |
|
|
@ -25,6 +25,8 @@ namespace Nz
|
||||||
class NAZARA_AUDIO_API Music final : public Resource, public SoundEmitter
|
class NAZARA_AUDIO_API Music final : public Resource, public SoundEmitter
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
|
using Params = SoundStreamParams;
|
||||||
|
|
||||||
Music();
|
Music();
|
||||||
Music(AudioDevice& device);
|
Music(AudioDevice& device);
|
||||||
Music(const Music&) = delete;
|
Music(const Music&) = delete;
|
||||||
|
|
@ -46,9 +48,9 @@ namespace Nz
|
||||||
|
|
||||||
bool IsLooping() const override;
|
bool IsLooping() const override;
|
||||||
|
|
||||||
bool OpenFromFile(const std::filesystem::path& filePath, const SoundStreamParams& params = SoundStreamParams());
|
static std::shared_ptr<Music> OpenFromFile(const std::filesystem::path& filePath, const Params& params = Params());
|
||||||
bool OpenFromMemory(const void* data, std::size_t size, const SoundStreamParams& params = SoundStreamParams());
|
static std::shared_ptr<Music> OpenFromMemory(const void* data, std::size_t size, const Params& params = Params());
|
||||||
bool OpenFromStream(Stream& stream, const SoundStreamParams& params = SoundStreamParams());
|
static std::shared_ptr<Music> OpenFromStream(Stream& stream, const Params& params = Params());
|
||||||
|
|
||||||
void Pause() override;
|
void Pause() override;
|
||||||
void Play() override;
|
void Play() override;
|
||||||
|
|
|
||||||
|
|
@ -17,6 +17,8 @@ namespace Nz
|
||||||
class NAZARA_AUDIO_API Sound final : public SoundEmitter
|
class NAZARA_AUDIO_API Sound final : public SoundEmitter
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
|
using Params = SoundBufferParams;
|
||||||
|
|
||||||
using SoundEmitter::SoundEmitter;
|
using SoundEmitter::SoundEmitter;
|
||||||
Sound();
|
Sound();
|
||||||
Sound(AudioDevice& audioDevice, std::shared_ptr<SoundBuffer> soundBuffer);
|
Sound(AudioDevice& audioDevice, std::shared_ptr<SoundBuffer> soundBuffer);
|
||||||
|
|
@ -36,9 +38,9 @@ namespace Nz
|
||||||
bool IsLooping() const override;
|
bool IsLooping() const override;
|
||||||
bool IsPlayable() const;
|
bool IsPlayable() const;
|
||||||
|
|
||||||
bool LoadFromFile(const std::filesystem::path& filePath, const SoundBufferParams& params = SoundBufferParams());
|
static std::shared_ptr<Sound> LoadFromFile(const std::filesystem::path& filePath, const Params& params = {});
|
||||||
bool LoadFromMemory(const void* data, std::size_t size, const SoundBufferParams& params = SoundBufferParams());
|
static std::shared_ptr<Sound> LoadFromMemory(const void* data, std::size_t size, const Params& params = {});
|
||||||
bool LoadFromStream(Stream& stream, const SoundBufferParams& params = SoundBufferParams());
|
static std::shared_ptr<Sound> LoadFromStream(Stream& stream, const Params& params = {});
|
||||||
|
|
||||||
void Pause() override;
|
void Pause() override;
|
||||||
void Play() override;
|
void Play() override;
|
||||||
|
|
|
||||||
|
|
@ -29,6 +29,9 @@ namespace Nz
|
||||||
bool IsValid() const;
|
bool IsValid() const;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
NAZARA_AUDIO_API bool Serialize(SerializationContext& context, SoundBufferParams& params, TypeTag<SoundBufferParams>);
|
||||||
|
NAZARA_AUDIO_API bool Unserialize(SerializationContext& context, SoundBufferParams* params, TypeTag<SoundBufferParams>);
|
||||||
|
|
||||||
class AudioBuffer;
|
class AudioBuffer;
|
||||||
class AudioDevice;
|
class AudioDevice;
|
||||||
class Sound;
|
class Sound;
|
||||||
|
|
|
||||||
|
|
@ -25,8 +25,8 @@ namespace Nz
|
||||||
bool IsValid() const;
|
bool IsValid() const;
|
||||||
};
|
};
|
||||||
|
|
||||||
NAZARA_CORE_API bool Serialize(SerializationContext& context, SoundStreamParams& params, TypeTag<SoundStreamParams>);
|
NAZARA_AUDIO_API bool Serialize(SerializationContext& context, SoundStreamParams& params, TypeTag<SoundStreamParams>);
|
||||||
NAZARA_CORE_API bool Unserialize(SerializationContext& context, SoundStreamParams* params, TypeTag<SoundStreamParams>);
|
NAZARA_AUDIO_API bool Unserialize(SerializationContext& context, SoundStreamParams* params, TypeTag<SoundStreamParams>);
|
||||||
|
|
||||||
class Mutex;
|
class Mutex;
|
||||||
class SoundStream;
|
class SoundStream;
|
||||||
|
|
|
||||||
|
|
@ -11,6 +11,11 @@ namespace Nz
|
||||||
const std::shared_ptr<TResource>& Get() const { return m_resource; }
|
const std::shared_ptr<TResource>& Get() const { return m_resource; }
|
||||||
explicit operator bool() const noexcept { return !!m_resource; }
|
explicit operator bool() const noexcept { return !!m_resource; }
|
||||||
|
|
||||||
|
TResource& operator*() { return *m_resource.get(); }
|
||||||
|
const TResource& operator*() const { return *m_resource.get(); }
|
||||||
|
TResource* operator->() { return m_resource.get(); }
|
||||||
|
const TResource* operator->() const { return m_resource.get(); }
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
AssetDescriptor<TResource> m_descriptor;
|
AssetDescriptor<TResource> m_descriptor;
|
||||||
std::shared_ptr<TResource> m_resource;
|
std::shared_ptr<TResource> m_resource;
|
||||||
|
|
|
||||||
|
|
@ -4,27 +4,55 @@
|
||||||
|
|
||||||
namespace Nz
|
namespace Nz
|
||||||
{
|
{
|
||||||
|
template <typename TResource>
|
||||||
|
concept IsStreamingResource = requires(TResource)
|
||||||
|
{
|
||||||
|
{ TResource::OpenFromFile({}) } -> std::same_as<std::shared_ptr<TResource>>;
|
||||||
|
};
|
||||||
|
|
||||||
template <typename TResource>
|
template <typename TResource>
|
||||||
Asset<TResource> AssetCatalog::LoadFromFile(const std::filesystem::path& path)
|
Asset<TResource> AssetCatalog::LoadFromFile(const std::filesystem::path& path)
|
||||||
{
|
{
|
||||||
Asset<TResource> asset;
|
Asset<TResource> asset;
|
||||||
|
|
||||||
JsonSerializationContext json;
|
|
||||||
if (!json.Load(path))
|
|
||||||
return {};
|
|
||||||
|
|
||||||
if (!Unserialize(json, "", &asset.m_descriptor))
|
|
||||||
return {};
|
|
||||||
|
|
||||||
FilesystemAppComponent* fs = ApplicationBase::Instance()->TryGetComponent<FilesystemAppComponent>();
|
FilesystemAppComponent* fs = ApplicationBase::Instance()->TryGetComponent<FilesystemAppComponent>();
|
||||||
if (fs) // first try using a FileSystem component to load the asset
|
if (fs) // first try using a FileSystem component to load the asset
|
||||||
{
|
{
|
||||||
|
auto json = fs->Load<JsonSerializationContext>(std::string_view(path.string()));
|
||||||
|
|
||||||
|
if (!Unserialize(*json, "", &asset.m_descriptor))
|
||||||
|
return {};
|
||||||
|
|
||||||
|
|
||||||
std::string filepath = asset.m_descriptor.path.string();
|
std::string filepath = asset.m_descriptor.path.string();
|
||||||
asset.m_resource = fs->Load<TResource>(std::string_view(filepath), asset.m_descriptor.parameters);
|
if constexpr (IsStreamingResource<TResource>)
|
||||||
|
{
|
||||||
|
asset.m_resource = fs->Open<TResource>(std::string_view(filepath), asset.m_descriptor.parameters);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
asset.m_resource = fs->Load<TResource>(std::string_view(filepath), asset.m_descriptor.parameters);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!asset) // if it fails, use the default loader
|
if (!asset) // if it fails, use the default loader
|
||||||
asset.m_resource = TResource::LoadFromFile(asset.m_descriptor.path, asset.m_descriptor.parameters);
|
{
|
||||||
|
JsonSerializationContext json;
|
||||||
|
if (!json.Load(path))
|
||||||
|
return {};
|
||||||
|
|
||||||
|
if (!Unserialize(json, "", &asset.m_descriptor))
|
||||||
|
return {};
|
||||||
|
|
||||||
|
if constexpr (IsStreamingResource<TResource>)
|
||||||
|
{
|
||||||
|
asset.m_resource = TResource::OpenFromFile(asset.m_descriptor.path, asset.m_descriptor.parameters);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
asset.m_resource = TResource::LoadFromFile(asset.m_descriptor.path, asset.m_descriptor.parameters);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
return asset;
|
return asset;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -10,10 +10,13 @@ namespace Nz
|
||||||
: public SerializationContext
|
: public SerializationContext
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
|
struct Params {};
|
||||||
|
|
||||||
JsonSerializationContext();
|
JsonSerializationContext();
|
||||||
~JsonSerializationContext();
|
~JsonSerializationContext();
|
||||||
|
|
||||||
bool Load(const std::filesystem::path& path);
|
bool Load(const std::filesystem::path& path);
|
||||||
|
bool Load(Stream& stream);
|
||||||
|
|
||||||
EnumSerializationMode GetEnumSerializationMode() const override { return EnumSerializationMode::String; }
|
EnumSerializationMode GetEnumSerializationMode() const override { return EnumSerializationMode::String; }
|
||||||
bool PushObject(std::string_view name) override;
|
bool PushObject(std::string_view name) override;
|
||||||
|
|
@ -47,6 +50,8 @@ namespace Nz
|
||||||
bool Read(std::string_view name, double* value) override;
|
bool Read(std::string_view name, double* value) override;
|
||||||
bool Read(std::string_view name, std::string* value) override;
|
bool Read(std::string_view name, std::string* value) override;
|
||||||
|
|
||||||
|
static std::shared_ptr<JsonSerializationContext> LoadFromStream(Stream& stream, const Params& params = {});
|
||||||
|
|
||||||
private:
|
private:
|
||||||
std::unique_ptr<struct JsonImpl> m_impl;
|
std::unique_ptr<struct JsonImpl> m_impl;
|
||||||
};
|
};
|
||||||
|
|
|
||||||
|
|
@ -240,12 +240,17 @@ namespace Nz
|
||||||
* \param filePath Path to the file
|
* \param filePath Path to the file
|
||||||
* \param params Parameters for the music
|
* \param params Parameters for the music
|
||||||
*/
|
*/
|
||||||
bool Music::OpenFromFile(const std::filesystem::path& filePath, const SoundStreamParams& params)
|
std::shared_ptr<Music> Music::OpenFromFile(const std::filesystem::path& filePath, const Params& params)
|
||||||
{
|
{
|
||||||
|
std::shared_ptr<Music> music = std::make_shared<Music>();
|
||||||
if (std::shared_ptr<SoundStream> soundStream = SoundStream::OpenFromFile(filePath, params))
|
if (std::shared_ptr<SoundStream> soundStream = SoundStream::OpenFromFile(filePath, params))
|
||||||
return Create(std::move(soundStream));
|
{
|
||||||
else
|
std::shared_ptr<Music> music = std::make_shared<Music>();
|
||||||
return false;
|
if (music->Create(std::move(soundStream)))
|
||||||
|
return music;
|
||||||
|
}
|
||||||
|
|
||||||
|
return {};
|
||||||
}
|
}
|
||||||
|
|
||||||
/*!
|
/*!
|
||||||
|
|
@ -258,12 +263,16 @@ namespace Nz
|
||||||
*
|
*
|
||||||
* \remark The memory pointer must stay valid (accessible) as long as the music is playing
|
* \remark The memory pointer must stay valid (accessible) as long as the music is playing
|
||||||
*/
|
*/
|
||||||
bool Music::OpenFromMemory(const void* data, std::size_t size, const SoundStreamParams& params)
|
std::shared_ptr<Music> Music::OpenFromMemory(const void* data, std::size_t size, const Params& params)
|
||||||
{
|
{
|
||||||
if (std::shared_ptr<SoundStream> soundStream = SoundStream::OpenFromMemory(data, size, params))
|
if (std::shared_ptr<SoundStream> soundStream = SoundStream::OpenFromMemory(data, size, params))
|
||||||
return Create(std::move(soundStream));
|
{
|
||||||
else
|
std::shared_ptr<Music> music = std::make_shared<Music>();
|
||||||
return false;
|
if (music->Create(std::move(soundStream)))
|
||||||
|
return music;
|
||||||
|
}
|
||||||
|
|
||||||
|
return {};
|
||||||
}
|
}
|
||||||
|
|
||||||
/*!
|
/*!
|
||||||
|
|
@ -275,12 +284,16 @@ namespace Nz
|
||||||
*
|
*
|
||||||
* \remark The stream must stay valid as long as the music is playing
|
* \remark The stream must stay valid as long as the music is playing
|
||||||
*/
|
*/
|
||||||
bool Music::OpenFromStream(Stream& stream, const SoundStreamParams& params)
|
std::shared_ptr<Music> Music::OpenFromStream(Stream& stream, const Params& params)
|
||||||
{
|
{
|
||||||
if (std::shared_ptr<SoundStream> soundStream = SoundStream::OpenFromStream(stream, params))
|
if (std::shared_ptr<SoundStream> soundStream = SoundStream::OpenFromStream(stream, params))
|
||||||
return Create(std::move(soundStream));
|
{
|
||||||
else
|
std::shared_ptr<Music> music = std::make_shared<Music>();
|
||||||
return false;
|
if (music->Create(std::move(soundStream)))
|
||||||
|
return music;
|
||||||
|
}
|
||||||
|
|
||||||
|
return {};
|
||||||
}
|
}
|
||||||
|
|
||||||
/*!
|
/*!
|
||||||
|
|
@ -439,7 +452,7 @@ namespace Nz
|
||||||
break; // We have reached the end of the stream, there is no use to add new buffers
|
break; // We have reached the end of the stream, there is no use to add new buffers
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
catch (const std::exception&)
|
catch (const std::exception& e)
|
||||||
{
|
{
|
||||||
err = std::current_exception();
|
err = std::current_exception();
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -141,17 +141,18 @@ namespace Nz
|
||||||
*
|
*
|
||||||
* \remark Produces a NazaraError if loading failed
|
* \remark Produces a NazaraError if loading failed
|
||||||
*/
|
*/
|
||||||
bool Sound::LoadFromFile(const std::filesystem::path& filePath, const SoundBufferParams& params)
|
std::shared_ptr<Sound> Sound::LoadFromFile(const std::filesystem::path& filePath, const Params& params)
|
||||||
{
|
{
|
||||||
std::shared_ptr<SoundBuffer> buffer = SoundBuffer::LoadFromFile(filePath, params);
|
std::shared_ptr<SoundBuffer> buffer = SoundBuffer::LoadFromFile(filePath, params);
|
||||||
if (!buffer)
|
if (!buffer)
|
||||||
{
|
{
|
||||||
NazaraErrorFmt("failed to load buffer from file ({0})", filePath);
|
NazaraErrorFmt("failed to load buffer from file ({0})", filePath);
|
||||||
return false;
|
return {};
|
||||||
}
|
}
|
||||||
|
|
||||||
SetBuffer(std::move(buffer));
|
std::shared_ptr<Sound> sound = std::make_shared<Sound>();
|
||||||
return true;
|
sound->SetBuffer(std::move(buffer));
|
||||||
|
return sound;
|
||||||
}
|
}
|
||||||
|
|
||||||
/*!
|
/*!
|
||||||
|
|
@ -164,17 +165,18 @@ namespace Nz
|
||||||
*
|
*
|
||||||
* \remark Produces a NazaraError if loading failed
|
* \remark Produces a NazaraError if loading failed
|
||||||
*/
|
*/
|
||||||
bool Sound::LoadFromMemory(const void* data, std::size_t size, const SoundBufferParams& params)
|
std::shared_ptr<Sound> Sound::LoadFromMemory(const void* data, std::size_t size, const Params& params)
|
||||||
{
|
{
|
||||||
std::shared_ptr<SoundBuffer> buffer = SoundBuffer::LoadFromMemory(data, size, params);
|
std::shared_ptr<SoundBuffer> buffer = SoundBuffer::LoadFromMemory(data, size, params);
|
||||||
if (!buffer)
|
if (!buffer)
|
||||||
{
|
{
|
||||||
NazaraErrorFmt("failed to load buffer from memory ({0})", PointerToString(data));
|
NazaraErrorFmt("failed to load buffer from memory ({0})", PointerToString(data));
|
||||||
return false;
|
return {};
|
||||||
}
|
}
|
||||||
|
|
||||||
SetBuffer(std::move(buffer));
|
std::shared_ptr<Sound> sound = std::make_shared<Sound>();
|
||||||
return true;
|
sound->SetBuffer(std::move(buffer));
|
||||||
|
return sound;
|
||||||
}
|
}
|
||||||
|
|
||||||
/*!
|
/*!
|
||||||
|
|
@ -186,17 +188,18 @@ namespace Nz
|
||||||
*
|
*
|
||||||
* \remark Produces a NazaraError if loading failed
|
* \remark Produces a NazaraError if loading failed
|
||||||
*/
|
*/
|
||||||
bool Sound::LoadFromStream(Stream& stream, const SoundBufferParams& params)
|
std::shared_ptr<Sound> Sound::LoadFromStream(Stream& stream, const Params& params)
|
||||||
{
|
{
|
||||||
std::shared_ptr<SoundBuffer> buffer = SoundBuffer::LoadFromStream(stream, params);
|
std::shared_ptr<SoundBuffer> buffer = SoundBuffer::LoadFromStream(stream, params);
|
||||||
if (!buffer)
|
if (!buffer)
|
||||||
{
|
{
|
||||||
NazaraError("failed to load buffer from stream");
|
NazaraError("failed to load buffer from stream");
|
||||||
return false;
|
return {};
|
||||||
}
|
}
|
||||||
|
|
||||||
SetBuffer(std::move(buffer));
|
std::shared_ptr<Sound> sound = std::make_shared<Sound>();
|
||||||
return true;
|
sound->SetBuffer(std::move(buffer));
|
||||||
|
return sound;
|
||||||
}
|
}
|
||||||
|
|
||||||
/*!
|
/*!
|
||||||
|
|
|
||||||
|
|
@ -32,6 +32,20 @@ namespace Nz
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
bool Serialize(SerializationContext& context, SoundBufferParams& params, TypeTag<SoundBufferParams>)
|
||||||
|
{
|
||||||
|
Serialize(context, params, TypeTag<ResourceParameters>());
|
||||||
|
Serialize(context, "forceMono", params.forceMono);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool Unserialize(SerializationContext& context, SoundBufferParams* params, TypeTag<SoundBufferParams>)
|
||||||
|
{
|
||||||
|
Unserialize(context, params, TypeTag<ResourceParameters>());
|
||||||
|
Unserialize(context, "forceMono", ¶ms->forceMono);
|
||||||
|
return params->IsValid();
|
||||||
|
}
|
||||||
|
|
||||||
/*!
|
/*!
|
||||||
* \brief Constructs a SoundBuffer object
|
* \brief Constructs a SoundBuffer object
|
||||||
*
|
*
|
||||||
|
|
|
||||||
|
|
@ -18,7 +18,7 @@ namespace Nz
|
||||||
{
|
{
|
||||||
Unserialize(context, params, TypeTag<ResourceParameters>());
|
Unserialize(context, params, TypeTag<ResourceParameters>());
|
||||||
Unserialize(context, "forceMono", ¶ms->forceMono);
|
Unserialize(context, "forceMono", ¶ms->forceMono);
|
||||||
return true;
|
return params->IsValid();
|
||||||
}
|
}
|
||||||
|
|
||||||
bool SoundStreamParams::IsValid() const
|
bool SoundStreamParams::IsValid() const
|
||||||
|
|
|
||||||
|
|
@ -36,11 +36,20 @@ namespace Nz
|
||||||
|
|
||||||
bool JsonSerializationContext::Load(const std::filesystem::path& path)
|
bool JsonSerializationContext::Load(const std::filesystem::path& path)
|
||||||
{
|
{
|
||||||
std::ifstream file(path);
|
Nz::File file(path, Nz::OpenMode::Read);
|
||||||
if (!file || !file.is_open())
|
return Load(file);
|
||||||
|
}
|
||||||
|
|
||||||
|
bool JsonSerializationContext::Load(Stream& stream)
|
||||||
|
{
|
||||||
|
if (!stream.IsReadable())
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
m_impl->json = nlohmann::json::parse(file);
|
auto size = stream.GetSize();
|
||||||
|
std::vector<char> content(size);
|
||||||
|
stream.Read(content.data(), content.size());
|
||||||
|
|
||||||
|
m_impl->json = nlohmann::json::parse(content.begin(), content.end());
|
||||||
m_impl->stack.push(&m_impl->json);
|
m_impl->stack.push(&m_impl->json);
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
@ -129,4 +138,12 @@ namespace Nz
|
||||||
*value = m_impl->Top()[name].get<std::string>();
|
*value = m_impl->Top()[name].get<std::string>();
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
std::shared_ptr<JsonSerializationContext> JsonSerializationContext::LoadFromStream(Stream& stream, const JsonSerializationContext::Params&)
|
||||||
|
{
|
||||||
|
std::shared_ptr<JsonSerializationContext> context = std::make_shared<JsonSerializationContext>();
|
||||||
|
if (context->Load(stream))
|
||||||
|
return context;
|
||||||
|
return {};
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue