summaryrefslogtreecommitdiff
path: root/src/port/resource/importers/AudioSequenceFactory.cpp
blob: 5b42a699b2a39e59cddcbfd1fe0a14581583db33 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#include "AudioSequenceFactory.h"
#include "../type/AudioBank.h"
#include "port/Engine.h"
#include <nlohmann/json.hpp>

std::vector<std::string> extension = {".wav", ".ogg", ".mp3", ".flac", ".WAV", ".OGG", ".MP3", ".FLAC"};

std::shared_ptr<Ship::IResource>
SM64::AudioSequenceFactoryV0::ReadResource(std::shared_ptr<Ship::File> file,
                                           std::shared_ptr<Ship::ResourceInitData> initData) {
    if (!FileHasValidFormatAndReader(file, initData)) {
        return nullptr;
    }

    std::shared_ptr<AudioSequence> bank = std::make_shared<AudioSequence>(initData);
    auto reader = std::get<std::shared_ptr<Ship::BinaryReader>>(file->Reader);

    uint8_t id = reader->ReadUInt32();
    size_t bankCount = reader->ReadUInt32();
    for(size_t i = 0; i < bankCount; i++){
        std::string bankName = reader->ReadString();
        bank->banks.push_back(GameEngine::GetBankIdByName(bankName));
    }

    size_t sampleSize = reader->ReadUInt32();
    for(size_t i = 0; i < sampleSize; i++){
        bank->sampleData.push_back(reader->ReadUByte());
    }

    bank->mData.bankCount = bankCount;
    bank->mData.banks = bank->banks.data();
    bank->mData.data = bank->sampleData.data();
    bank->mData.id = id;

    for (const auto& ext : extension) {
        auto custom = Ship::Context::GetRawInstance()->GetResourceManager()->LoadFileProcess(initData->Path + ext);

        if (custom != nullptr) {
            bank->sampleData = std::vector<uint8_t>(custom->Buffer->begin(), custom->Buffer->end());
            uint8_t* data = bank->sampleData.data();
            size_t size = bank->sampleData.size();

            auto metadata = Ship::Context::GetRawInstance()->GetResourceManager()->LoadFileProcess(initData->Path + ".json");
            if (metadata != nullptr) {
                auto json = nlohmann::json::parse(std::string(metadata->Buffer->begin(), metadata->Buffer->end()));
                HMAS_Info info;

                info.name = json.value("name", "");
                info.author = json.value("author", "");
                info.date = json.value("date", "");
                if (json.contains("loop")) {
                    info.loop.start = json["loop"].value("start", -1);
                    info.loop.end = json["loop"].value("end", -1);
                }

                GameEngine::Instance->gHMAS->RegisterSound(id, data, size, info);
            } else {
                GameEngine::Instance->gHMAS->RegisterSound(id, data, size);
            }
            break;
        }
    }

    return bank;
}