This commit is contained in:
Arron David Nelson 2024-01-26 09:00:31 -08:00
parent b863f59d14
commit 4a4116da45
5 changed files with 161 additions and 50 deletions

View File

@ -107,6 +107,7 @@ set(EHS_SOURCES
src/json/JsonArray.cpp include/ehs/json/JsonArray.h
src/json/JsonVar.cpp include/ehs/json/JsonVar.h
src/io/Resource.cpp include/ehs/io/Resource.h
src/io/Console.cpp include/ehs/io/Console.h
src/io/RIFF_Chunk.cpp include/ehs/io/RIFF_Chunk.h
src/io/RIFF.cpp include/ehs/io/RIFF.h

45
include/ehs/io/Resource.h Normal file
View File

@ -0,0 +1,45 @@
#pragma once
#include "ehs/Types.h"
#include "ehs/Str.h"
#include "ehs/Vec2.h"
#include "ehs/BaseObj.h"
namespace ehs
{
class Resource : public BaseObj
{
private:
ehs::UInt_64 hashId;
ehs::Str_8 id;
public:
Resource();
Resource(ehs::Str_8 id);
Resource(Resource&& rsrc) noexcept;
Resource(const Resource& rsrc);
Resource& operator=(Resource&& rsrc) noexcept;
Resource& operator=(const Resource& rsrc);
bool operator==(ehs::UInt_64 otherHashId) const;
bool operator!=(ehs::UInt_64 otherHashId) const;
virtual void Release();
void SetId(ehs::Str_8 newId);
ehs::UInt_64 GetHashId() const;
ehs::Str_8 GetId() const;
virtual bool IsValid() const;
Resource* Clone() const override;
};
}

View File

@ -1,22 +1,20 @@
#pragma once
#include "ehs/Types.h"
#include "ehs/BaseObj.h"
#include "ehs/DataType.h"
#include "ehs/Str.h"
#include "ehs/Serializer.h"
#include "ehs/Vector.h"
#include "ehs/Array.h"
#include "ehs/io/Resource.h"
#include "AudioCodec.h"
namespace ehs
{
class Audio : public BaseObj
class Audio : public Resource
{
private:
static Array<AudioCodec> codecs;
UInt_64 hashId;
Str_8 id;
UInt_64 sampleRate;
DataType dataType;
UInt_8 byteDepth;
@ -37,7 +35,7 @@ namespace ehs
static const AudioCodec* GetCodec(const Str_8& ext);
~Audio();
~Audio() override;
Audio();
@ -65,13 +63,7 @@ namespace ehs
operator Byte*();
void Release();
UInt_64 GetHashId() const;
void SetId(Str_8 newId);
Str_8 GetId() const;
void Release() override;
UInt_64 GetSampleRate() const;

95
src/io/Resource.cpp Normal file
View File

@ -0,0 +1,95 @@
#include "ehs/io/Resource.h"
namespace ehs
{
Resource::Resource()
: hashId(0)
{
AddType("Resource");
}
Resource::Resource(ehs::Str_8 id)
: hashId(id.Hash_64()), id(std::move(id))
{
AddType("Resource");
}
Resource::Resource(Resource&& rsrc) noexcept
: BaseObj((BaseObj&&)rsrc), hashId(rsrc.hashId), id(std::move(rsrc.id))
{
rsrc.hashId = 0;
}
Resource::Resource(const Resource& rsrc)
: BaseObj(rsrc), hashId(rsrc.hashId), id(rsrc.id)
{
}
Resource& Resource::operator=(Resource&& rsrc) noexcept
{
if (this == &rsrc)
return *this;
BaseObj::operator=((BaseObj&&)rsrc);
hashId = rsrc.hashId;
id = std::move(rsrc.id);
rsrc.hashId = 0;
return *this;
}
Resource& Resource::operator=(const Resource& rsrc)
{
if (this == &rsrc)
return *this;
BaseObj::operator=(rsrc);
hashId = rsrc.hashId;
id = rsrc.id;
return *this;
}
bool Resource::operator==(const ehs::UInt_64 otherHashId) const
{
return hashId == otherHashId;
}
bool Resource::operator!=(const ehs::UInt_64 otherHashId) const
{
return hashId != otherHashId;
}
void Resource::Release()
{
}
void Resource::SetId(ehs::Str_8 newId)
{
hashId = newId.Hash_64();
id = std::move(newId);
}
ehs::UInt_64 Resource::GetHashId() const
{
return hashId;
}
ehs::Str_8 Resource::GetId() const
{
return id;
}
bool Resource::IsValid() const
{
return hashId;
}
Resource* Resource::Clone() const
{
return new Resource(*this);
}
}

View File

@ -51,14 +51,14 @@ namespace ehs
}
Audio::Audio()
: hashId(0), sampleRate(0), dataType(DataType::FLOAT), byteDepth(0), channels(0), frames(0),
length(0.0f), data(nullptr), peak(nullptr)
: sampleRate(0), dataType(DataType::FLOAT), byteDepth(0), channels(0), frames(0), length(0.0f), data(nullptr),
peak(nullptr)
{
AddType("Audio");
}
Audio::Audio(Str_8 id, const UInt_64 sampleRate, const DataType dataType, const UInt_8 channels, const UInt_64 frames, const Byte* const data)
: hashId(id.Hash_64()), id((Str_8&&)id), dataType(dataType), byteDepth(ToByteDepth(dataType)), sampleRate(sampleRate),
: Resource((Str_8&&)id), dataType(dataType), byteDepth(ToByteDepth(dataType)), sampleRate(sampleRate),
channels(channels), frames(frames), length((float)frames / (float)sampleRate),
data(new Byte[GetSize()]), peak(new Byte[byteDepth])
{
@ -68,7 +68,7 @@ namespace ehs
}
Audio::Audio(Str_8 id, const UInt_64 sampleRate, const DataType dataType, const UInt_8 channels, const Serializer<UInt_64>& data)
: hashId(id.Hash_64()), id((Str_8&&)id), sampleRate(sampleRate), dataType(dataType), byteDepth(ToByteDepth(dataType)),
: Resource((Str_8&&)id), sampleRate(sampleRate), dataType(dataType), byteDepth(ToByteDepth(dataType)),
channels(channels), frames(data.Size() / channels / byteDepth), length((float)frames / (float)sampleRate),
data(new Byte[data.Size()]), peak(new Byte[byteDepth])
{
@ -78,7 +78,7 @@ namespace ehs
}
Audio::Audio(Str_8 id, const UInt_64 sampleRate, const DataType dataType, const UInt_8 channels, const Vector<Byte>& data)
: hashId(id.Hash_64()), id((Str_8&&)id), sampleRate(sampleRate), dataType(dataType), byteDepth(ToByteDepth(dataType)),
: Resource((Str_8&&)id), sampleRate(sampleRate), dataType(dataType), byteDepth(ToByteDepth(dataType)),
channels(channels), frames(data.Size() / channels / byteDepth), length((float)frames / (float)sampleRate),
data(new Byte[data.Size()]), peak(new Byte[byteDepth])
{
@ -88,7 +88,7 @@ namespace ehs
}
Audio::Audio(Str_8 id, const UInt_64 sampleRate, const DataType dataType, const UInt_8 channels, const Array<Byte>& data)
: hashId(id.Hash_64()), id((Str_8&&)id), sampleRate(sampleRate), dataType(dataType), byteDepth(ToByteDepth(dataType)),
: Resource((Str_8&&)id), sampleRate(sampleRate), dataType(dataType), byteDepth(ToByteDepth(dataType)),
channels(channels), frames(data.Size() / channels / byteDepth), length((float)frames / (float)sampleRate),
data(new Byte[data.Size()]), peak(new Byte[byteDepth])
{
@ -98,7 +98,7 @@ namespace ehs
}
Audio::Audio(Str_8 id, const UInt_64 sampleRate, const DataType dataType, const UInt_8 channels, const UInt_64 frames)
: hashId(id.Hash_64()), id((Str_8&&)id), sampleRate(sampleRate), dataType(dataType), byteDepth(ToByteDepth(dataType)),
: Resource((Str_8&&)id), sampleRate(sampleRate), dataType(dataType), byteDepth(ToByteDepth(dataType)),
channels(channels), frames(frames), length((float)frames / (float)sampleRate),
data(new Byte[GetSize()]), peak(new Byte[byteDepth])
{
@ -106,18 +106,17 @@ namespace ehs
}
Audio::Audio(Str_8 id)
: hashId(id.Hash_64()), id((Str_8&&)id), sampleRate(0), dataType(DataType::FLOAT), byteDepth(0), channels(0),
: Resource((Str_8&&)id), sampleRate(0), dataType(DataType::FLOAT), byteDepth(0), channels(0),
frames(0), length(0.0f), data(nullptr), peak(nullptr)
{
AddType("Audio");
}
Audio::Audio(Audio&& audio) noexcept
: BaseObj((BaseObj&&)audio), hashId(audio.hashId), id((Str_8&&)id), sampleRate(audio.sampleRate),
dataType(audio.dataType), byteDepth(audio.byteDepth), channels(audio.channels), frames(audio.frames),
length(audio.length), data(audio.data), peak(audio.peak)
: Resource((Resource&&)audio), sampleRate(audio.sampleRate), dataType(audio.dataType),
byteDepth(audio.byteDepth), channels(audio.channels), frames(audio.frames), length(audio.length),
data(audio.data), peak(audio.peak)
{
audio.hashId = 0;
audio.sampleRate = 0;
audio.dataType = DataType::FLOAT;
audio.byteDepth = ToByteDepth(audio.dataType);
@ -129,9 +128,9 @@ namespace ehs
}
Audio::Audio(const Audio& audio)
: BaseObj(audio), hashId(audio.hashId), id(audio.id), sampleRate(audio.sampleRate), dataType(audio.dataType),
byteDepth(audio.byteDepth), channels(audio.channels), frames(audio.frames), length(audio.length),
data(new Byte[GetSize()]), peak(new Byte[audio.byteDepth])
: Resource(audio), sampleRate(audio.sampleRate), dataType(audio.dataType), byteDepth(audio.byteDepth),
channels(audio.channels), frames(audio.frames), length(audio.length), data(new Byte[GetSize()]),
peak(new Byte[audio.byteDepth])
{
Util::Copy(data, audio.data, GetSize());
}
@ -141,10 +140,8 @@ namespace ehs
if (this == &audio)
return *this;
BaseObj::operator=((BaseObj&&)audio);
Resource::operator=((Resource&&)audio);
hashId = audio.hashId;
id = (Str_8&&)audio.id;
sampleRate = audio.sampleRate;
dataType = audio.dataType;
byteDepth = audio.byteDepth;
@ -156,7 +153,6 @@ namespace ehs
delete[] peak;
peak = audio.peak;
audio.hashId = 0;
audio.sampleRate = 0;
audio.dataType = DataType::FLOAT;
audio.byteDepth = ToByteDepth(audio.dataType);
@ -174,10 +170,8 @@ namespace ehs
if (this == &audio)
return *this;
BaseObj::operator=(audio);
Resource::operator=(audio);
hashId = audio.hashId;
id = audio.id;
sampleRate = audio.sampleRate;
dataType = audio.dataType;
byteDepth = audio.byteDepth;
@ -220,22 +214,6 @@ namespace ehs
peak = nullptr;
}
UInt_64 Audio::GetHashId() const
{
return hashId;
}
void Audio::SetId(Str_8 newId)
{
hashId = newId.Hash_64();
id = (Str_8&&)newId;
}
Str_8 Audio::GetId() const
{
return id;
}
UInt_64 Audio::GetSampleRate() const
{
return sampleRate;