Compare commits

...

8 Commits

12 changed files with 138 additions and 46 deletions

View File

@ -25,6 +25,8 @@ namespace Nz
class NAZARA_AUDIO_API Music final : public Resource, public SoundEmitter
{
public:
using Params = SoundStreamParams;
Music();
Music(AudioDevice& device);
Music(const Music&) = delete;
@ -46,9 +48,9 @@ namespace Nz
bool IsLooping() const override;
bool OpenFromFile(const std::filesystem::path& filePath, const SoundStreamParams& params = SoundStreamParams());
bool OpenFromMemory(const void* data, std::size_t size, const SoundStreamParams& params = SoundStreamParams());
bool OpenFromStream(Stream& stream, const SoundStreamParams& params = SoundStreamParams());
static std::shared_ptr<Music> OpenFromFile(const std::filesystem::path& filePath, const Params& params = Params());
static std::shared_ptr<Music> OpenFromMemory(const void* data, std::size_t size, const Params& params = Params());
static std::shared_ptr<Music> OpenFromStream(Stream& stream, const Params& params = Params());
void Pause() override;
void Play() override;

View File

@ -17,6 +17,8 @@ namespace Nz
class NAZARA_AUDIO_API Sound final : public SoundEmitter
{
public:
using Params = SoundBufferParams;
using SoundEmitter::SoundEmitter;
Sound();
Sound(AudioDevice& audioDevice, std::shared_ptr<SoundBuffer> soundBuffer);
@ -36,9 +38,9 @@ namespace Nz
bool IsLooping() const override;
bool IsPlayable() const;
bool LoadFromFile(const std::filesystem::path& filePath, const SoundBufferParams& params = SoundBufferParams());
bool LoadFromMemory(const void* data, std::size_t size, const SoundBufferParams& params = SoundBufferParams());
bool LoadFromStream(Stream& stream, const SoundBufferParams& params = SoundBufferParams());
static std::shared_ptr<Sound> LoadFromFile(const std::filesystem::path& filePath, const Params& params = {});
static std::shared_ptr<Sound> LoadFromMemory(const void* data, std::size_t size, const Params& params = {});
static std::shared_ptr<Sound> LoadFromStream(Stream& stream, const Params& params = {});
void Pause() override;
void Play() override;

View File

@ -29,6 +29,9 @@ namespace Nz
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 AudioDevice;
class Sound;

View File

@ -25,8 +25,8 @@ namespace Nz
bool IsValid() const;
};
NAZARA_CORE_API bool Serialize(SerializationContext& context, SoundStreamParams& params, TypeTag<SoundStreamParams>);
NAZARA_CORE_API bool Unserialize(SerializationContext& context, SoundStreamParams* params, TypeTag<SoundStreamParams>);
NAZARA_AUDIO_API bool Serialize(SerializationContext& context, SoundStreamParams& params, TypeTag<SoundStreamParams>);
NAZARA_AUDIO_API bool Unserialize(SerializationContext& context, SoundStreamParams* params, TypeTag<SoundStreamParams>);
class Mutex;
class SoundStream;

View File

@ -11,6 +11,11 @@ namespace Nz
const std::shared_ptr<TResource>& Get() const { 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:
AssetDescriptor<TResource> m_descriptor;
std::shared_ptr<TResource> m_resource;

View File

@ -4,27 +4,55 @@
namespace Nz
{
template <typename TResource>
concept IsStreamingResource = requires(TResource)
{
{ TResource::OpenFromFile({}) } -> std::same_as<std::shared_ptr<TResource>>;
};
template <typename TResource>
Asset<TResource> AssetCatalog::LoadFromFile(const std::filesystem::path& path)
{
Asset<TResource> asset;
JsonSerializationContext json;
if (!json.Load(path))
return {};
if (!Unserialize(json, "", &asset.m_descriptor))
return {};
FilesystemAppComponent* fs = ApplicationBase::Instance()->TryGetComponent<FilesystemAppComponent>();
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();
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
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;
}

View File

@ -10,10 +10,13 @@ namespace Nz
: public SerializationContext
{
public:
struct Params {};
JsonSerializationContext();
~JsonSerializationContext();
bool Load(const std::filesystem::path& path);
bool Load(Stream& stream);
EnumSerializationMode GetEnumSerializationMode() const override { return EnumSerializationMode::String; }
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, std::string* value) override;
static std::shared_ptr<JsonSerializationContext> LoadFromStream(Stream& stream, const Params& params = {});
private:
std::unique_ptr<struct JsonImpl> m_impl;
};

View File

@ -240,12 +240,17 @@ namespace Nz
* \param filePath Path to the file
* \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))
return Create(std::move(soundStream));
else
return false;
{
std::shared_ptr<Music> music = std::make_shared<Music>();
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
*/
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))
return Create(std::move(soundStream));
else
return false;
{
std::shared_ptr<Music> music = std::make_shared<Music>();
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
*/
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))
return Create(std::move(soundStream));
else
return false;
{
std::shared_ptr<Music> music = std::make_shared<Music>();
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
}
}
catch (const std::exception&)
catch (const std::exception& e)
{
err = std::current_exception();

View File

@ -141,17 +141,18 @@ namespace Nz
*
* \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);
if (!buffer)
{
NazaraErrorFmt("failed to load buffer from file ({0})", filePath);
return false;
return {};
}
SetBuffer(std::move(buffer));
return true;
std::shared_ptr<Sound> sound = std::make_shared<Sound>();
sound->SetBuffer(std::move(buffer));
return sound;
}
/*!
@ -164,17 +165,18 @@ namespace Nz
*
* \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);
if (!buffer)
{
NazaraErrorFmt("failed to load buffer from memory ({0})", PointerToString(data));
return false;
return {};
}
SetBuffer(std::move(buffer));
return true;
std::shared_ptr<Sound> sound = std::make_shared<Sound>();
sound->SetBuffer(std::move(buffer));
return sound;
}
/*!
@ -186,17 +188,18 @@ namespace Nz
*
* \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);
if (!buffer)
{
NazaraError("failed to load buffer from stream");
return false;
return {};
}
SetBuffer(std::move(buffer));
return true;
std::shared_ptr<Sound> sound = std::make_shared<Sound>();
sound->SetBuffer(std::move(buffer));
return sound;
}
/*!

View File

@ -32,6 +32,20 @@ namespace Nz
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", &params->forceMono);
return params->IsValid();
}
/*!
* \brief Constructs a SoundBuffer object
*

View File

@ -18,7 +18,7 @@ namespace Nz
{
Unserialize(context, params, TypeTag<ResourceParameters>());
Unserialize(context, "forceMono", &params->forceMono);
return true;
return params->IsValid();
}
bool SoundStreamParams::IsValid() const

View File

@ -36,11 +36,20 @@ namespace Nz
bool JsonSerializationContext::Load(const std::filesystem::path& path)
{
std::ifstream file(path);
if (!file || !file.is_open())
Nz::File file(path, Nz::OpenMode::Read);
return Load(file);
}
bool JsonSerializationContext::Load(Stream& stream)
{
if (!stream.IsReadable())
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);
return true;
}
@ -129,4 +138,12 @@ namespace Nz
*value = m_impl->Top()[name].get<std::string>();
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 {};
}
}