-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathFirmware.h
More file actions
214 lines (157 loc) · 5.74 KB
/
Copy pathFirmware.h
File metadata and controls
214 lines (157 loc) · 5.74 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
// 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.
#pragma once
#include "Drums.h"
#include "System.h"
#include "Voice.h"
#include <assert.h>
#include "WDL/heapbuf.h"
#include "WDL/mutex.h"
namespace TG101 {
class Firmware
{
public:
Firmware(): mStatus(false) {}
static inline int GetSize() { return 128*1024; }
static inline const unsigned char* GetHash() { return mHash; }
// Returns true on success, or false on failure.
bool LoadFile(const char* filename, const void* hash = NULL, WDL_Mutex* mutex = NULL);
inline bool GetStatus() const { return mStatus; }
// Returns pointer to file contents on success, or NULL on failure.
static void* ReadFile(const char* filename, const void* hash, WDL_HeapBuf* buf, int size);
inline const VoiceBankTbl* GetVoiceBankTbl() const
{
assert(GetStatus() == true);
return &mVoiceBankTbl;
}
const VoiceBank* GetVoiceBank(EVoiceBankIdx idx) const
{
assert(GetStatus() == true);
assert(idx >= kVoiceBankInternal && idx <= kVoiceBankCM64Parts11To16);
static const int gm = kVoiceBankGeneralMidi + 1;
idx = (unsigned int)++idx < (unsigned int)kNumVoiceBanks ? idx : gm;
return &mProgramChangeTbl[(unsigned int)idx];
}
const VoiceCommon* GetVoice(int idx) const
{
assert(GetStatus() == true);
assert(idx >= 0 && idx < kNumVoices);
idx = (unsigned int)idx < (unsigned int)kNumVoices ? idx : kVoiceIdxGrandPno;
return &mVoiceMem[(unsigned int)idx];
}
void GetSampleSet(const SampleSet* sampleSet[2], int waveNo) const;
const SampleSet* FindSampleSet(int waveNo, int note) const;
inline const DrumBank* GetDrumBank() const
{
assert(GetStatus() == true);
return &mDrumKitTbl;
}
const DrumKit* GetDrumKit(EDrumKitIdx idx) const
{
assert(GetStatus() == true);
assert(idx >= 0 && idx < kNumDrumKits);
idx = (unsigned int)idx < (unsigned int)kNumDrumKits ? idx : kDrumKitStandard;
return &mDrumKits[(unsigned int)idx];
}
const DrumSound* GetDrumSound(const int idx) const
{
assert(GetStatus() == true);
assert(idx >= 0 && idx < kNumDrumSounds);
const DrumSound* drumSound = &mDrumSounds[(unsigned int)idx];
drumSound = (unsigned int)idx < (unsigned int)kNumDrumSounds ? drumSound : &mTestSignal;
return drumSound;
}
int LookupPitch(int cent) const
{
assert(GetStatus() == true);
assert(cent >= 0 && cent < 1200);
cent = wdl_min((unsigned int)cent, 1199);
// TN: Same as table lookup, but likely less efficient.
// const int fNumber = (int)(exp((double)cent * log(2.0)/1200.0 + log(1024.0)) - 1023.5);
const int fNumber = mPitchTbl[(unsigned int)cent];
return fNumber;
}
inline int LookupVolume(const int level) const
{
assert(GetStatus() == true);
assert(level >= 0 && level <= 127);
return mVolumeTbl[level & 127];
}
int LookupVelocity(const int curve, const int velocity) const
{
assert(GetStatus() == true);
assert(curve >= 0 && curve <= 7);
assert(velocity >= 0 && velocity <= 127);
return mVelocityTbl[curve & 7][velocity & 127];
}
inline int LookupPortamentoTime(const int time) const
{
assert(GetStatus() == true);
assert(time >= 0 && time <= 127);
return mPortaTimeTbl[time & 127];
}
inline int LookupPitchEGRate(const int rate) const
{
assert(GetStatus() == true);
assert(rate >= 0 && rate <= 63);
return mPitchEGRateTbl[rate & 63];
}
int LookupReverbTime(const int time) const
{
assert(GetStatus() == true);
assert(time >= 0 && time <= 59);
return mReverbTimeTbl[time & 63];
}
inline int LookupReverbFeedback(const int idx) const
{
assert(GetStatus() == true);
assert(idx >= 0 && idx <= 255);
return mReverbFeedbackTbl[idx & 255];
}
inline int LookupReverbGain(const int idx) const
{
assert(GetStatus() == true);
assert(idx >= 0 && idx <= 255);
return mReverbGainTbl[idx & 255];
}
// Returns const char buf[8] space padded, *without* null char.
const char* LookupDrumKitName(EDrumKitIdx idx, int prog = 0) const;
// Right trims spaces, appends null char.
char* GetDrumKitName(char buf[9], EDrumKitIdx idx, int prog = 0) const;
// Returns const char buf[8] space padded, *without* null char.
inline const char* LookupReverbTypeName(const EReverbType type) const
{
assert(GetStatus() == true);
assert(type >= 0 && type < kNumReverbTypes);
return mReverbTypeName[type & 7];
}
// Right trims spaces, appends null char.
char* GetReverbTypeName(char buf[8], EReverbType type) const;
private:
void ExtractData(const unsigned char* buf);
bool mStatus;
static VoiceBankTbl mVoiceBankTbl;
static VoiceBank mProgramChangeTbl[kNumVoiceBanks];
static VoiceCommon mVoiceMem[kNumVoices];
static DrumBank mDrumKitTbl;
static DrumKit mDrumKits[kNumDrumKits];
static SampleSetTbl mSampleSetTbl;
static SampleSet mSampleSets[kNumSampleSets];
static DrumSound mDrumSounds[kNumDrumSounds];
static unsigned short mPitchTbl[1200];
static unsigned char mVolumeTbl[128];
static unsigned char mVelocityTbl[8][128];
static unsigned char mPortaTimeTbl[128];
static unsigned char mPitchEGRateTbl[64];
static unsigned char mReverbTimeTbl[64];
static unsigned short mReverbFeedbackTbl[256];
static unsigned short mReverbGainTbl[256];
static const int kNumDrumKitNames = kNumDrumKits + 2;
static char mDrumKitName[kNumDrumKitNames][8];
static char mReverbTypeName[kNumReverbTypes][8];
static const unsigned char mHash[];
static const DrumSound mTestSignal;
};
} // namespace TG101