-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathFirmware.cpp
More file actions
274 lines (205 loc) · 6.79 KB
/
Copy pathFirmware.cpp
File metadata and controls
274 lines (205 loc) · 6.79 KB
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
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
// Copyright (C) 2022-2025 Theo Niessink <theo@taletn.com>
// This work is free. You can redistribute it and/or modify it under the
// terms of the Do What The Fuck You Want To Public License, Version 2,
// as published by Sam Hocevar. See http://www.wtfpl.net/ for more details.
#include "Firmware.h"
#include <stdio.h>
#include <string.h>
#include "WDL/sha.h"
#include "WDL/wdlendian.h"
#include "WDL/wdltypes.h"
namespace TG101 {
VoiceBankTbl Firmware::mVoiceBankTbl;
VoiceBank Firmware::mProgramChangeTbl[kNumVoiceBanks];
VoiceCommon Firmware::mVoiceMem[kNumVoices];
DrumBank Firmware::mDrumKitTbl;
DrumKit Firmware::mDrumKits[kNumDrumKits];
SampleSetTbl Firmware::mSampleSetTbl;
SampleSet Firmware::mSampleSets[kNumSampleSets];
DrumSound Firmware::mDrumSounds[kNumDrumSounds];
unsigned short Firmware::mPitchTbl[1200];
unsigned char Firmware::mVolumeTbl[128];
unsigned char Firmware::mVelocityTbl[8][128];
unsigned char Firmware::mPortaTimeTbl[128];
unsigned char Firmware::mPitchEGRateTbl[64];
unsigned char Firmware::mReverbTimeTbl[64];
unsigned short Firmware::mReverbFeedbackTbl[256];
unsigned short Firmware::mReverbGainTbl[256];
char Firmware::mDrumKitName[kNumDrumKitNames][8];
char Firmware::mReverbTypeName[kNumReverbTypes][8];
// XK731C0 (v1.10)
const unsigned char Firmware::mHash[WDL_SHA1SIZE] =
{
0x48, 0x31, 0x03, 0xA2, 0xFF, 0xC6, 0x3A, 0x90, 0xA2, 0x08,
0x6C, 0x59, 0x7B, 0xAA, 0x2B, 0x27, 0x45, 0xC3, 0xA1, 0xC2
};
// 1000 Hz sine wave
const DrumSound Firmware::mTestSignal =
{
0x00, 0x00, 0x00, 0x00, 0x00, 0x00
};
bool Firmware::LoadFile(const char* const filename, const void* const hash, WDL_Mutex* const mutex)
{
if (mutex) mutex->Enter();
bool status = mStatus;
if (!status)
{
WDL_HeapBuf buf;
const void* const ptr = Firmware::ReadFile(filename, hash, &buf, GetSize());
status = !!ptr;
if (status)
{
ExtractData((const unsigned char*)ptr);
mStatus = status;
}
}
if (mutex) mutex->Leave();
return status;
}
void* Firmware::ReadFile(const char* const filename, const void* const hash, WDL_HeapBuf* const buf, const int size)
{
assert(buf->GetSize() == 0);
void* ptr = NULL;
FILE* const fp = fopen(filename, "rb");
if (!fp) return ptr;
if (!fseek(fp, 0, SEEK_END) && ftell(fp) == size && !fseek(fp, 0, SEEK_SET))
{
ptr = buf->Resize(size);
if (ptr && (int)fread(ptr, 1, size, fp) != size) ptr = NULL;
}
fclose(fp);
if (ptr && hash)
{
WDL_SHA1 digest;
digest.add(ptr, size);
unsigned char result[WDL_SHA1SIZE];
digest.result(result);
ptr = memcmp(result, hash, WDL_SHA1SIZE) ? NULL : ptr;
}
if (!ptr) buf->Resize(0);
return ptr;
}
void Firmware::ExtractData(const unsigned char* const buf)
{
memcpy(&mVoiceBankTbl, &buf[0x14C90], sizeof(VoiceBankTbl));
memcpy(&mProgramChangeTbl[kVoiceBankInternal + 1], &buf[0x14D10], sizeof(VoiceBank));
memcpy(&mProgramChangeTbl[kVoiceBankGeneralMidi + 1], &buf[0x10000], (kNumVoiceBanks - 1) * sizeof(VoiceBank));
memcpy(&mVoiceMem, &buf[0x10410], kNumVoices * sizeof(VoiceCommon));
memcpy(&mDrumKitTbl, &buf[0x14C10], sizeof(DrumBank));
memcpy(&mDrumKits, &buf[0x18B9E], kNumDrumKits * sizeof(DrumKit));
memcpy(&mSampleSetTbl, &buf[0x1841A], sizeof(SampleSetTbl));
memcpy(&mSampleSets, &buf[0x176A2], kNumSampleSets * sizeof(SampleSet));
memcpy(&mDrumSounds, &buf[0x18532], kNumDrumSounds * sizeof(DrumSound));
const unsigned char* ptr = &buf[0x14E2C];
for (int i = 0; i < 1200; ++i)
{
unsigned short word;
memcpy(&word, ptr, 2);
mPitchTbl[i] = WDL_bswap16_if_le(word);
ptr += 2;
}
memcpy(mVolumeTbl, &buf[0x1578C], sizeof(mVolumeTbl));
memcpy(mVelocityTbl, &buf[0x1580C], sizeof(mVelocityTbl));
memcpy(mPortaTimeTbl, &buf[0x1640C], sizeof(mPortaTimeTbl));
memcpy(mPitchEGRateTbl, &buf[0x164A8], sizeof(mPitchEGRateTbl));
memcpy(mReverbTimeTbl, &buf[0x17160], 60);
memset(&mReverbTimeTbl[60], mReverbTimeTbl[59], 4);
ptr = &buf[0x171A8];
for (int i = 0; i < 256; ++i)
{
unsigned short word;
memcpy(&word, ptr, 2);
mReverbFeedbackTbl[i] = WDL_bswap16_if_le(word);
ptr += 2;
}
ptr = &buf[0x173D0];
for (int i = 0; i < 256; ++i)
{
unsigned short word;
memcpy(&word, ptr, 2);
mReverbGainTbl[i] = WDL_bswap16_if_le(word);
ptr += 2;
}
static const unsigned char ofs[kNumDrumKitNames] =
{
0, // kDrumKitStandard
8, // kDrumKitRoom
16, // kDrumKitPower
24, // kDrumKitElectronic
32, // kDrumKitAnalog
48, // kDrumKitBrush
56, // kDrumKitOrchestra
72, // kDrumKitClavinova
80, // kDrumKitRX
88, // kDrumKitCM64
40, // kDrumKitJazz
64 // -
};
ptr = &buf[0x16A80];
for (int i = 0; i < kNumDrumKitNames; ++i)
{
memcpy(mDrumKitName[i], &ptr[ofs[i]], 8);
}
ptr = &buf[0x167D8];
for (int i = 0; i < kNumReverbTypes; ++i)
{
char* const name = mReverbTypeName[i];
memcpy(name, ptr, 8);
name[7] = ' ';
ptr += 8;
}
}
void Firmware::GetSampleSet(const SampleSet* sampleSet[2], int waveNo) const
{
assert(GetStatus() == true);
assert(waveNo >= 0 && waveNo < kNumWaveNo);
waveNo = (unsigned int)waveNo < (unsigned int)kNumWaveNo ? waveNo : kWaveNoSine;
for (int i = 0; i < 2; ++i)
{
int idx = mSampleSetTbl.GetSampleSetIdx(waveNo++);
assert((idx >= 0 && idx < kNumSampleSets) || idx == kEndOfSampleSets);
idx = wdl_min(idx, kNumSampleSets);
sampleSet[i] = &mSampleSets[idx];
}
}
const SampleSet* Firmware::FindSampleSet(const int waveNo, const int note) const
{
assert(GetStatus() == true);
assert(note >= 0 && note <= 127);
const SampleSet* sampleSet[2];
GetSampleSet(sampleSet, waveNo);
assert(sampleSet[0] < &mSampleSets[kNumSampleSets]);
if (sampleSet[0]->GetNoteLimitLow() > note) return NULL;
while (sampleSet[0] < sampleSet[1] && sampleSet[0]->GetNoteLimitHigh() < note)
{
sampleSet[0]++;
#ifndef NDEBUG
if (sampleSet[0] < sampleSet[1])
{
assert(sampleSet[0] < &mSampleSets[kNumSampleSets]);
}
#endif
}
return sampleSet[0] < sampleSet[1] ? sampleSet[0] : NULL;
}
const char* Firmware::LookupDrumKitName(EDrumKitIdx idx, const int prog) const
{
assert(GetStatus() == true);
assert(idx >= 0 && idx < kNumDrumKits);
static const int jazz = kNumDrumKits;
idx = prog == 32 ? jazz : idx;
static const unsigned int noName = kNumDrumKitNames - 1;
idx = wdl_min((unsigned int)idx, noName);
return mDrumKitName[(unsigned int)idx];
}
char* Firmware::GetDrumKitName(char buf[9], const EDrumKitIdx idx, const int prog) const
{
memcpy(buf, LookupDrumKitName(idx, prog), 8);
return RightTrim(buf, 8);
}
char* Firmware::GetReverbTypeName(char buf[8], const EReverbType type) const
{
memcpy(buf, LookupReverbTypeName(type), 8);
return RightTrim(buf, 7);
}
} // namespace TG101