-
Notifications
You must be signed in to change notification settings - Fork 124
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
564752e
commit 4aadbbd
Showing
13 changed files
with
911 additions
and
107 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
#include "base.h" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,221 @@ | ||
#pragma once | ||
#include <cstdint> | ||
#include <string> | ||
#include <cassert> | ||
#include <iostream> | ||
#define LibDLVoiceCodecBegin namespace libdlvcodec { | ||
#define LibDLVoiceCodecEnd } | ||
#define LIBDVCND [[nodiscard]] | ||
|
||
#define LibDLVoiceCodecThrow(message) throw std::exception((std::string("[In \"") + __FILE__ + "\" Line " + std::to_string(__LINE__) + "] " + (message)).c_str()) | ||
|
||
LibDLVoiceCodecBegin | ||
|
||
using int8 = int8_t; | ||
using int16 = int16_t; | ||
using int32 = int32_t; | ||
using int64 = int64_t; | ||
using float32 = float; | ||
using float64 = double; | ||
using byte = unsigned char; | ||
using lpvoid = void*; | ||
using uint8 = uint8_t; | ||
using uint16 = uint16_t; | ||
using uint32 = uint32_t; | ||
using uint64 = uint64_t; | ||
|
||
template<typename T> | ||
class BaseAllocator | ||
{ | ||
public: | ||
BaseAllocator() = default; | ||
virtual ~BaseAllocator() = default; | ||
virtual T* allocate(size_t size) | ||
{ | ||
return new T[size]; | ||
} | ||
virtual void destroy(const T* ptr) | ||
{ | ||
delete ptr; | ||
} | ||
}; | ||
|
||
template<typename Type, typename Allocator = BaseAllocator<Type>> | ||
class MResource | ||
{ | ||
public: | ||
using reference = Type&; | ||
using rvalue = Type&&; | ||
using ptr_t = Type*; | ||
|
||
MResource() = default; | ||
|
||
MResource(size_t _Count) | ||
{ | ||
data_ = allocator_.allocate(_Count * 2); | ||
size_ = _Count; | ||
} | ||
|
||
MResource(size_t _Count, Type _Value) | ||
{ | ||
data_ = allocator_.allocate(_Count * 2); | ||
size_ = _Count; | ||
auto _ptr = data_; | ||
const auto _end = data_ + size_; | ||
while (_ptr != _end) | ||
{ | ||
*_ptr = _Value; | ||
++_ptr; | ||
} | ||
} | ||
|
||
MResource(ptr_t _Ptr, size_t _Size) | ||
{ | ||
data_ = _Ptr; | ||
size_ = _Size; | ||
} | ||
|
||
MResource(const MResource& _Left) | ||
{ | ||
size_ = _Left.size_; | ||
data_ = allocator_.allocate(_Left.size_); | ||
auto _ptr = data_, _ptrl = _Left.data_; | ||
const auto _end = data_ + size_; | ||
while (_ptr != _end) | ||
{ | ||
*_ptr = *_ptrl; | ||
++_ptr; | ||
++_ptrl; | ||
} | ||
} | ||
|
||
MResource(MResource&& _Right) noexcept | ||
{ | ||
size_ = _Right.size_; | ||
data_ = _Right.data_; | ||
_Right.size_ = 0ull; | ||
_Right.data_ = nullptr; | ||
} | ||
|
||
~MResource() | ||
{ | ||
if (data_) | ||
{ | ||
allocator_.destroy(data_); | ||
data_ = nullptr; | ||
} | ||
} | ||
|
||
LIBDVCND ptr_t data() const | ||
{ | ||
return data_; | ||
} | ||
|
||
LIBDVCND ptr_t begin() const | ||
{ | ||
return data_; | ||
} | ||
|
||
LIBDVCND ptr_t end() const | ||
{ | ||
return data_ + size_; | ||
} | ||
|
||
ptr_t release() | ||
{ | ||
const ptr_t _pdata = data_; | ||
data_ = nullptr; | ||
return _pdata; | ||
} | ||
|
||
reference operator[](size_t _Index) const | ||
{ | ||
assert(_Index < size_); | ||
return *(data_ + _Index); | ||
} | ||
|
||
MResource& operator=(const MResource& _Left) | ||
{ | ||
if (&_Left == this) | ||
return *this; | ||
size_ = _Left.size_; | ||
data_ = allocator_.allocate(_Left.size_); | ||
auto _ptr = data_, _ptrl = _Left.data_; | ||
const auto _end = data_ + size_; | ||
while (_ptr != _end) | ||
{ | ||
*_ptr = *_ptrl; | ||
++_ptr; | ||
++_ptrl; | ||
} | ||
return *this; | ||
} | ||
|
||
MResource& operator=(MResource&& _Right) noexcept | ||
{ | ||
size_ = _Right.size_; | ||
data_ = _Right.data_; | ||
_Right.size_ = 0ull; | ||
_Right.data_ = nullptr; | ||
return *this; | ||
} | ||
protected: | ||
ptr_t data_ = nullptr; | ||
size_t size_ = 0ull; | ||
Allocator allocator_; | ||
}; | ||
|
||
template<typename T> | ||
std::ostream& operator<<(std::ostream& _Stream, const MResource<T>& _Data) | ||
{ | ||
_Stream << '['; | ||
for (const auto& i : _Data) | ||
_Stream << i << ", "; | ||
_Stream << "]\n"; | ||
return _Stream; | ||
} | ||
|
||
class FileWrapper | ||
{ | ||
public: | ||
FileWrapper() = default; | ||
~FileWrapper() | ||
{ | ||
if (file_) | ||
fclose(file_); | ||
file_ = nullptr; | ||
} | ||
FileWrapper(const FileWrapper& _Left) = delete; | ||
FileWrapper& operator=(const FileWrapper& _Left) = delete; | ||
FileWrapper(FileWrapper&& _Right) noexcept | ||
{ | ||
file_ = _Right.file_; | ||
_Right.file_ = nullptr; | ||
} | ||
FileWrapper& operator=(FileWrapper&& _Right) noexcept | ||
{ | ||
file_ = _Right.file_; | ||
_Right.file_ = nullptr; | ||
return *this; | ||
} | ||
void open(const std::wstring& _Path, const std::wstring& _Mode) | ||
{ | ||
#ifdef _WIN32 | ||
_wfopen_s(&file_, _Path.c_str(), _Mode.c_str()); | ||
#else | ||
file_ = _wfopen(_Path.c_str(), _Mode.c_str()); | ||
#endif | ||
} | ||
operator FILE* () const | ||
{ | ||
return file_; | ||
} | ||
LIBDVCND bool enabled() const | ||
{ | ||
return file_; | ||
} | ||
private: | ||
FILE* file_ = nullptr; | ||
}; | ||
|
||
LibDLVoiceCodecEnd |
125 changes: 125 additions & 0 deletions
125
MoeVoiceStudioSvc - Core - Cmd/LibDLVoiceCodec/value.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,125 @@ | ||
#include "value.h" | ||
|
||
LibDLVoiceCodecBegin | ||
|
||
Value& Value::load(const std::wstring& _Path) | ||
{ | ||
FileWrapper file; | ||
file.open(_Path, L"rb"); | ||
if (!file.enabled()) | ||
LibDLVoiceCodecThrow("Failed to open file!"); | ||
char Header[4]; | ||
fread(Header, 1, 4, file); | ||
if (Header[0] != 'L' || Header[1] != 'S' || Header[2] != 'B' || Header[3] != 'V') | ||
LibDLVoiceCodecThrow("File does not recognize!"); | ||
size_t MemberNameSize = 0; | ||
WeightDict weight; | ||
char MemberName[1025]; | ||
/* | ||
MemberNameSize (size_t) | ||
MemberName (char[MemberNameSize]) | ||
_WeightData.Size (size_t) | ||
_WeightData.ShapeSize (size_t) | ||
_WeightData.Shape (int64_t[_WeightData.ShapeSize]) | ||
_WeightData.Data (byte[_WeightData.Size]) | ||
*/ | ||
while (fread(&MemberNameSize, 1, sizeof(size_t), file) == sizeof(size_t)) | ||
{ | ||
if (MemberNameSize == 0) | ||
LibDLVoiceCodecThrow("Size of attrib name must higer than 0!"); | ||
if (MemberNameSize > 1024) | ||
LibDLVoiceCodecThrow("Size of attrib name must lower than 1024!"); | ||
if (fread(MemberName, 1, MemberNameSize, file) != MemberNameSize) | ||
LibDLVoiceCodecThrow("Unexpected EOF!"); | ||
MemberName[MemberNameSize] = 0; | ||
std::string AttribName = MemberName; | ||
if (weight.find(AttribName) != weight.end()) | ||
continue; | ||
|
||
WeightData _WeightData; | ||
|
||
if (fread(&_WeightData.Size, 1, sizeof(size_t), file) != sizeof(size_t)) | ||
LibDLVoiceCodecThrow("Unexpected EOF!"); | ||
if (_WeightData.Size == 0) | ||
continue; | ||
|
||
if (fread(&_WeightData.ShapeSize, 1, sizeof(size_t), file) != sizeof(size_t)) | ||
LibDLVoiceCodecThrow("Unexpected EOF!"); | ||
if (_WeightData.ShapeSize == 0) | ||
continue; | ||
|
||
size_t __SIZE = sizeof(int64_t) * _WeightData.ShapeSize; | ||
_WeightData.Shape = std::vector<int64_t>(_WeightData.ShapeSize); | ||
if (fread(_WeightData.Shape.data(), 1, __SIZE, file) != __SIZE) | ||
LibDLVoiceCodecThrow("Unexpected EOF!"); | ||
|
||
__SIZE = _WeightData.Size; | ||
_WeightData.Data = MResource<byte>(_WeightData.Size); | ||
if (fread(_WeightData.Data.data(), 1, __SIZE, file) != __SIZE) | ||
LibDLVoiceCodecThrow("Unexpected EOF!"); | ||
|
||
weight[AttribName] = std::move(_WeightData); | ||
} | ||
loadData(weight); | ||
return *this; | ||
} | ||
|
||
Value& Value::save(const std::wstring& _Path) | ||
{ | ||
FileWrapper file; | ||
file.open(_Path, L"rb"); | ||
if (!file.enabled()) | ||
LibDLVoiceCodecThrow("Failed to open file!"); | ||
constexpr char Header[5] = "LSBV"; | ||
fwrite(Header, 1, 4, file); | ||
saveData(file); | ||
return *this; | ||
} | ||
|
||
void Value::loadData(WeightDict& _Dict) | ||
{ | ||
LibDLVoiceCodecThrow("Not implemented error!"); | ||
} | ||
|
||
void Value::saveData(FileWrapper& _File) | ||
{ | ||
LibDLVoiceCodecThrow("Not implemented error!"); | ||
} | ||
|
||
void Tensor::loadData(WeightDict& _Dict) | ||
{ | ||
const auto res = _Dict.find(RegName_); | ||
if (res != _Dict.end()) | ||
{ | ||
Shape_ = res->second.Shape; | ||
size_t TotalSize = 1; | ||
for (const auto i : Shape_) | ||
TotalSize *= i; | ||
if (TotalSize * sizeof(DType) != res->second.Size) | ||
LibDLVoiceCodecThrow("Expected size does not match actual size!"); | ||
Data_ = std::move(res->second.Data); | ||
} | ||
} | ||
|
||
void Tensor::saveData(FileWrapper& _File) | ||
{ | ||
|
||
} | ||
|
||
void Module::loadData(WeightDict& _Dict) | ||
{ | ||
for(const auto& it : Layers_) | ||
{ | ||
it.second->loadData(_Dict); | ||
} | ||
} | ||
|
||
void Module::saveData(FileWrapper& _File) | ||
{ | ||
for (const auto& it : Layers_) | ||
{ | ||
it.second->saveData(_File); | ||
} | ||
} | ||
|
||
LibDLVoiceCodecEnd |
Oops, something went wrong.