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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
|
#include "AudioConverter.h"
#include <factories/naudio/v1/BookFactory.h>
#include <factories/naudio/v1/LoopFactory.h>
#include <factories/naudio/v1/AudioContext.h>
#include <factories/naudio/v1/SampleFactory.h>
#include <factories/BaseFactory.h>
#include <Companion.h>
#include <cassert>
#include <cstring>
#include "hj/pyutils.h"
void AIFCWriter::End(std::string chunk, LUS::BinaryWriter& writer) {
auto buffer = writer.ToVector();
this->Chunks.push_back({ chunk, buffer });
this->totalSize += ALIGN(buffer.size(), 2) + 8;
}
void AIFCWriter::Close(LUS::BinaryWriter& out) {
out.SetEndianness(Torch::Endianness::Big);
out.Write(AIFCMagicValues::FORM);
out.Write((uint32_t)(this->totalSize + 4));
out.Write(AIFCMagicValues::AIFC);
for (auto& chunk : this->Chunks) {
out.Write((char*)chunk.id.data(), chunk.id.size());
out.Write((uint32_t)chunk.data.size());
out.Write((char*)chunk.data.data(), chunk.data.size());
if (chunk.data.size() % 2 == 1) {
out.Write((uint8_t)0);
}
}
}
// Function to serialize double to 80-bit extended-precision
void SerializeF80(double num, LUS::BinaryWriter& writer) {
// Convert the input double to a uint64_t representation
uint64_t f64;
memcpy((void*)&f64, (void*)&num, sizeof(double));
// Extract the sign bit
uint64_t f64_sign_bit = f64 & (1ULL << 63);
// Handle the special case: zero
if (num == 0.0) {
if (f64_sign_bit) {
writer.Write(static_cast<uint16_t>(0x8000)); // Sign bit set
} else {
writer.Write(static_cast<uint16_t>(0x0000)); // No sign bit
}
writer.Write(static_cast<uint64_t>(0x0000000000000000)); // Zero mantissa
return;
}
// Extract the exponent and mantissa
uint64_t exponent = (f64 >> 52) & 0x7FF; // Exponent bits
assert(exponent != 0); // Ensure not denormal
assert(exponent != 0x7FF); // Ensure not infinity/NaN
exponent -= 1023; // Adjust bias for 64-bit
uint64_t f64_mantissa_bits = f64 & ((1ULL << 52) - 1); // Mantissa bits
// Construct the 80-bit extended-precision fields
uint64_t f80_sign_bit = f64_sign_bit << (80 - 64); // Shift sign
uint64_t f80_exponent = (exponent + 0x3FFF) & 0x7FFF; // Adjust bias
uint64_t f80_mantissa_bits = (1ULL << 63) | (f64_mantissa_bits << (63 - 52)); // Add implicit bit
// Combine components into the 80-bit representation
uint16_t high = static_cast<uint16_t>(f80_sign_bit >> 48) | static_cast<uint16_t>(f80_exponent);
uint64_t low = f80_mantissa_bits;
// Write the result in big-endian order
writer.Write(high);
writer.Write(low);
}
void AudioConverter::SampleV0ToAIFC(AudioBankSample* sample, LUS::BinaryWriter& out) {
auto aifc = AIFCWriter();
auto data = sample->data;
uint32_t num_frames = data.size() * 16 / 9;
uint32_t sample_rate = -1;
if (sample->tunings.size() == 1) {
sample_rate = 32000 * sample->tunings[0];
} else {
float tmin = PyUtils::min(sample->tunings);
float tmax = PyUtils::max(sample->tunings);
if (tmin <= 0.5f && 0.5f <= tmax) {
sample_rate = 16000;
} else if (tmin <= 1.0f && 1.0f <= tmax) {
sample_rate = 32000;
} else if (tmin <= 1.5f && 1.5f <= tmax) {
sample_rate = 48000;
} else if (tmin <= 2.5f && 2.5f <= tmax) {
sample_rate = 80000;
} else {
sample_rate = 16000 * (tmin + tmax);
}
}
int16_t num_channels = 1;
int16_t sample_size = 16;
// COMM Chunk
auto comm = aifc.Start();
comm.Write(num_channels);
comm.Write(num_frames);
comm.Write(sample_size);
SerializeF80(sample_rate, comm);
comm.Write(AIFCMagicValues::VAPC);
comm.Write((char*)"\x0bVADPCM ~4-1", 12);
aifc.End("COMM", comm);
// INST Chunk
auto inst = aifc.Start();
for (size_t i = 0; i < 5; i++) {
inst.Write((int32_t)0);
}
aifc.End("INST", inst);
// VADPCMCODES Chunk
auto vcodes = aifc.Start();
vcodes.Write((char*)"stoc\x0bVADPCMCODES", 16);
vcodes.Write((int16_t)1);
vcodes.Write((int16_t)sample->book.order);
vcodes.Write((int16_t)sample->book.npredictors);
for (auto page : sample->book.table) {
vcodes.Write(page);
}
aifc.End("APPL", vcodes);
// SSND Chunk
auto ssnd = aifc.Start();
ssnd.Write((uint64_t)0);
ssnd.Write((char*)data.data(), data.size());
aifc.End("SSND", ssnd);
// VADPCMLOOPS
if (sample->loop.count != 0) {
auto vloops = aifc.Start();
vloops.Write((char*)"stoc\x0bVADPCMLOOPS", 16);
vloops.Write((uint16_t)1);
vloops.Write((uint16_t)1);
vloops.Write(sample->loop.start);
vloops.Write(sample->loop.end);
vloops.Write(sample->loop.count);
// The decoder reads back a fixed-size ALADPCMloop whose state is a
// 16-entry array; always emit all 16 (zero-filled when absent) so the
// chunk is exactly 44 bytes. Note: this must target vloops, not vcodes.
const std::vector<int16_t>* state =
sample->loop.state.has_value() ? &sample->loop.state.value() : nullptr;
for (size_t i = 0; i < 16; i++) {
vloops.Write((int16_t)(state != nullptr && i < state->size() ? (*state)[i] : 0));
}
aifc.End("APPL", vloops);
}
aifc.Close(out);
}
void AudioConverter::SampleV1ToAIFC(NSampleData* sample, LUS::BinaryWriter& out) {
auto loop =
std::static_pointer_cast<ADPCMLoopData>(Companion::Instance->GetParseDataByAddr(sample->loop)->data.value());
auto book =
std::static_pointer_cast<ADPCMBookData>(Companion::Instance->GetParseDataByAddr(sample->book)->data.value());
SampleV1ToAIFC(sample, loop.get(), book.get(), out);
}
void AudioConverter::SampleV1ToAIFC(NSampleData* sample, const ADPCMLoopData* loop, const ADPCMBookData* book,
LUS::BinaryWriter& out) {
auto entry = AudioContext::tables[AudioTableType::SAMPLE_TABLE];
auto sampleData = entry.buffer.data() + entry.info->entries[sample->sampleBankId].addr + sample->sampleAddr;
auto aifc = AIFCWriter();
std::vector<uint8_t> data(sampleData, sampleData + sample->size);
uint32_t num_frames = data.size() * 16 / 9;
uint32_t sample_rate = sample->sampleRate;
if (sample_rate == 0) {
sample_rate = 32000 * sample->tuning;
}
// Explicit yaml entries carry no tuning; a zero rate makes the AIFF
// invalid (decoders reject it), so fall back to the mixer reference.
if (sample_rate == 0) {
sample_rate = 32000;
}
int16_t num_channels = 1;
int16_t sample_size = 16;
// COMM Chunk
auto comm = aifc.Start();
comm.Write(num_channels);
comm.Write(num_frames);
comm.Write(sample_size);
SerializeF80(sample_rate, comm);
comm.Write(AIFCMagicValues::VAPC);
comm.Write((char*)"\x0bVADPCM ~4-1", 12);
aifc.End("COMM", comm);
// INST Chunk
auto inst = aifc.Start();
for (size_t i = 0; i < 5; i++) {
inst.Write((int32_t)0);
}
aifc.End("INST", inst);
// VADPCMCODES Chunk
auto vcodes = aifc.Start();
vcodes.Write((char*)"stoc\x0bVADPCMCODES", 16);
vcodes.Write((int16_t)1);
vcodes.Write((int16_t)book->order);
vcodes.Write((int16_t)book->numPredictors);
for (auto page : book->book) {
vcodes.Write(page);
}
aifc.End("APPL", vcodes);
// SSND Chunk
auto ssnd = aifc.Start();
ssnd.Write((uint64_t)0);
ssnd.Write((char*)data.data(), data.size());
aifc.End("SSND", ssnd);
// VADPCMLOOPS
if (loop->count != 0) {
auto vloops = aifc.Start();
vloops.Write((char*)"stoc\x0bVADPCMLOOPS", 16);
vloops.Write((uint16_t)1);
vloops.Write((uint16_t)1);
vloops.Write(loop->start);
vloops.Write(loop->end);
vloops.Write(loop->count);
for (size_t i = 0; i < 16; i++) {
vloops.Write(loop->predictorState[i]);
}
aifc.End("APPL", vloops);
}
aifc.Close(out);
}
|