blob: a6c536470b3a77cd84eaa406a5f209e42614c833 (
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
|
#include "AudioFactory.h"
#include <vector>
#include "audio/AudioManager.h"
#include "audio/samples_table.h"
#include "binarytools/BinaryReader.h"
bool AudioFactory::process(LUS::BinaryWriter* writer, nlohmann::json& data, std::vector<uint8_t>& buffer) {
auto metadata = data["offsets"];
std::string path = data["path"];
if(path.find("bank_sets") != std::string::npos){
WRITE_HEADER(LUS::ResourceType::Blob, 0);
char* raw = (char*) (buffer.data() + metadata[1]["us"][0]);
WRITE_U32(metadata[0]);
for(int i = 0; i < 0x23; i++){
int16_t value;
memcpy(&value, raw + (i * 2), 2);
WRITE_I16(BSWAP16(value));
}
writer->Write(raw + 0x46, 0x5A);
return true;
}
if(path.find("sound/banks") != std::string::npos){
auto banks = AudioManager::Instance->get_banks();
auto bankId = metadata["us"][0];
auto bank = banks[bankId];
if(bankId == 10){
int bp = 1;
}
WRITE_HEADER(LUS::ResourceType::Bank, 0);
if(path.find("sound/banks/us/20_bank") != std::string::npos){
int bp = 0;
}
WRITE_U32(bank.insts.size());
for(auto &instrument : bank.insts){
WRITE_U8(instrument.valid);
if(!instrument.valid){
continue;
}
WRITE_U8(instrument.releaseRate);
WRITE_U8(instrument.normalRangeLo);
WRITE_U8(instrument.normalRangeHi);
auto envelope = bank.envelopes[instrument.envelope];
if(!envelope.entries.empty()){
WRITE_U32(envelope.entries.size());
for(auto &entry : envelope.entries){
writer->Write(entry.delay);
writer->Write(entry.arg);
}
} else {
WRITE_U32(0);
}
uint32_t soundFlags = 0;
bool hasLo = instrument.soundLo.has_value();
bool hasMed = instrument.soundMed.has_value();
bool hasHi = instrument.soundHi.has_value();
if(hasLo){
soundFlags |= (1 << 0);
}
if(hasMed){
soundFlags |= (1 << 1);
}
if(hasHi){
soundFlags |= (1 << 2);
}
WRITE_U32(soundFlags);
if(hasLo){
auto value = instrument.soundLo.value();
uint32_t idx = AudioManager::Instance->get_index(bank.samples[value.offset]);
writer->Write(std::string(gSampleTable[idx]));
writer->Write(value.tuning);
}
if(hasMed){
auto value = instrument.soundMed.value();
uint32_t idx = AudioManager::Instance->get_index(bank.samples[value.offset]);
writer->Write(std::string(gSampleTable[idx]));
writer->Write(value.tuning);
}
if(hasHi){
auto value = instrument.soundHi.value();
uint32_t idx = AudioManager::Instance->get_index(bank.samples[value.offset]);
writer->Write(std::string(gSampleTable[idx]));
writer->Write(value.tuning);
}
}
WRITE_U32(bank.drums.size());
for(auto &drum : bank.drums){
WRITE_U8(drum.releaseRate);
WRITE_U8(drum.pan);
auto envelope = bank.envelopes[drum.envelope];
if(!envelope.entries.empty()){
std::cout << "Envelope entries: " << envelope.entries.size() << std::endl;
WRITE_U32(envelope.entries.size());
for(auto &entry : envelope.entries){
writer->Write(entry.delay);
writer->Write(entry.arg);
}
} else {
WRITE_U32(0);
}
uint32_t idx = AudioManager::Instance->get_index(bank.samples[drum.sound.offset]);
std::string out = std::string(gSampleTable[idx]);
writer->Write(out);
writer->Write(drum.sound.tuning);
}
return true;
}
if(metadata.size() < 2 || !metadata[1].contains("us")){
return false;
}
/*
* HINT: Uncomment this to write the aiff to a file
LUS::BinaryWriter aifcWriter;
AudioManager::Instance->create_aifc(metadata[1]["us"][1], aifcWriter);
std::vector<char> aifcData = aifcWriter.ToVector();
aifcWriter.Close();
write_aiff(aifcData, *writer);
*/
WRITE_HEADER(LUS::ResourceType::Audio, 0);
AudioBankSample entry = AudioManager::Instance->get_aifc(metadata[1]["us"][1]);
if(path.find("00_mario_waaaooow") != std::string::npos){
int bp = 0;
}
// Write AdpcmLoop
WRITE_U32(entry.loop.start);
WRITE_U32(entry.loop.end);
WRITE_I32(entry.loop.count);
WRITE_U32(entry.loop.pad);
if(entry.loop.state.has_value()){
std::vector<int16_t> state = entry.loop.state.value();
WRITE_U32(state.size());
for(auto &value : state){
writer->Write(value);
}
} else {
WRITE_U32(0);
}
// Write AdpcmBook
WRITE_I32(entry.book.order);
WRITE_I32(entry.book.npredictors);
WRITE_U32(entry.book.table.size());
for(auto &value : entry.book.table){
writer->Write(value);
}
// Write sample
WRITE_I32(entry.data.size());
for(auto &value : entry.data){
WRITE_U8(value);
}
writer->Write(entry.name);
return true;
}
|