-
Notifications
You must be signed in to change notification settings - Fork 3
/
MidiVolConv.cpp
318 lines (285 loc) · 7.19 KB
/
MidiVolConv.cpp
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
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
#define _USE_MATH_DEFINES
#include <iostream>
#include <fstream>
#include <string>
#include <ctype.h> // for tolower()
#include <string.h> // for stricmp
#include <math.h>
#include <stdtype.h>
#include "MidiLib.hpp"
#ifndef M_PI
#define M_PI 3.14159265358979323846
#endif
#ifndef M_LN2
#define M_LN2 0.693147180559945309417
#endif
#ifndef M_LN10
#define M_LN10 2.30258509299404568402
#endif
#ifdef _MSC_VER
#define stricmp _stricmp
#else
#define stricmp strcasecmp
#endif
// Function Prototypes
static UINT8 GetVolAlgoName(const char* algoName);
void MidiVolConv(void);
static double GetDBVol(UINT8 inVol);
static UINT8 GetMIDIVol(double dbVol, bool noVol0);
static UINT8 VolConv(UINT8 inVol, bool noVol0);
struct VOLALGO_LIST
{
UINT8 id;
const char* name;
};
#define VOLALGO_GM 0x00
#define VOLALGO_LIN 0x01
#define VOLALGO_FM 0x02
#define VOLALGO_PSG_2DB 0x03
#define VOLALGO_PSG_3DB 0x04
#define VOLALGO_WINFM 0x05
static const VOLALGO_LIST VolAlgoList[] =
{
{VOLALGO_GM, "GM"},
{VOLALGO_LIN, "Lin"},
{VOLALGO_FM, "FM"},
{VOLALGO_PSG_2DB, "PSG2"},
{VOLALGO_PSG_3DB, "PSG3"},
{VOLALGO_WINFM, "WinFM"},
{0x00, NULL}
};
#define VOLEVT_VELOCITY 0x01
#define VOLEVT_VOLUME 0x02
#define VOLEVT_EXPRESSION 0x04
#define VOLEVT_ALL (VOLEVT_VELOCITY | VOLEVT_VOLUME | VOLEVT_EXPRESSION)
static UINT8 VOLEVT_MASK;
static UINT16 CHANNEL_MASK;
static UINT8 INVOL_ALGO;
static UINT8 OUTVOL_ALGO;
static double VOL_GAIN;
MidiFile CMidi;
int main(int argc, char* argv[])
{
int argbase;
std::cout << "MIDI Volume Converter\n";
std::cout << "---------------------\n";
if (argc < 3)
{
std::cout << "Usage: " << argv[0] << " [options] input.mid output.mid\n";
std::cout << "Options:\n";
std::cout << " -s Algo - set volume algorithm (source/input) (default: -s GM)\n";
std::cout << " -d Algo - set volume algorithm (destination/output) (default: -d GM)\n";
// std::cout << " -e evts - convert specified events only (default: -e Vel,Vol,Exp)\n";
// std::cout << " Vel = Note Velocity, Vol = Volume Ctrl, Exp = Expression Ctrl\n";
std::cout << " -g gain - change volume by gain (in db, default: 0)\n";
std::cout << "Algorithms:\n";
std::cout << " GM - General MIDI algorithm\n";
std::cout << " Lin - linear volume (127 = max, 64 = half volume)\n";
std::cout << " FM - Yamaha FM (0.75 db steps)\n";
std::cout << " PSG2 - SN76489 PSG, 2 db per 8 steps\n";
std::cout << " PSG3 - AY8910 PSG, 3 db per 8 steps\n";
std::cout << " WinFM - Windows FM MIDI algorithm\n";
#ifdef _DEBUG
getchar();
#endif
return 0;
}
argbase = 1;
VOLEVT_MASK = VOLEVT_ALL;
CHANNEL_MASK = 0xFFFF; // all 16 channels active
INVOL_ALGO = VOLALGO_GM;
OUTVOL_ALGO = VOLALGO_GM;
VOL_GAIN = 0.0;
while(argbase < argc && argv[argbase][0] == '-')
{
char optChr = tolower(argv[argbase][1]);
if (optChr == 's' || optChr == 'd')
{
argbase ++;
if (argbase >= argc)
break;
UINT8 algoID = GetVolAlgoName(argv[argbase]);
if (algoID == 0xFF)
{
std::cout << "Unknown Algorithm!\n";
break;
}
if (optChr == 's')
INVOL_ALGO = algoID;
else if (optChr == 'd')
OUTVOL_ALGO = algoID;
}
else if (optChr == 'e')
{
argbase ++;
if (argbase >= argc)
break;
}
else if (optChr == 'g')
{
argbase ++;
if (argbase >= argc)
break;
VOL_GAIN = strtod(argv[argbase], NULL);
}
else
{
break;
}
argbase ++;
}
if (argc < argbase + 2)
{
printf("Not enough arguments.\n");
return 0;
}
UINT8 retVal;
std::cout << "Opening ...\n";
retVal = CMidi.LoadFile(argv[argbase + 0]);
if (retVal)
{
std::cout << "Error opening file!\n";
std::cout << "Errorcode: " << retVal;
return retVal;
}
MidiVolConv();
std::cout << "Saving ...\n";
retVal = CMidi.SaveFile(argv[argbase + 1]);
if (retVal)
{
std::cout << "Error saving file!\n";
std::cout << "Errorcode: " << retVal;
return retVal;
}
std::cout << "Cleaning ...\n";
CMidi.ClearAll();
std::cout << "Done.\n";
#ifdef _DEBUG
getchar();
#endif
return 0;
}
static UINT8 GetVolAlgoName(const char* algoName)
{
const VOLALGO_LIST* tempAlgo;
for (tempAlgo = VolAlgoList; tempAlgo->name != NULL; tempAlgo ++)
{
if (! stricmp(algoName, tempAlgo->name))
return tempAlgo->id;
}
return 0xFF;
}
void MidiVolConv(void)
{
UINT16 trkCnt;
UINT16 curTrk;
trkCnt = CMidi.GetTrackCount();
for (curTrk = 0; curTrk < trkCnt; curTrk ++)
{
MidiTrack* midiTrk = CMidi.GetTrack(curTrk);
midevt_iterator evtIt;
for (evtIt = midiTrk->GetEventBegin(); evtIt != midiTrk->GetEventEnd(); ++evtIt)
{
UINT8 evtChn = evtIt->evtType & 0x0F;
switch(evtIt->evtType & 0xF0)
{
case 0x80:
case 0x90:
if (! (CHANNEL_MASK & (1 << evtChn)))
break;
if ((VOLEVT_MASK & VOLEVT_VELOCITY) && evtIt->evtValB > 0)
evtIt->evtValB = VolConv(evtIt->evtValB, true);
break;
case 0xB0:
if (! (CHANNEL_MASK & (1 << evtChn)))
break;
switch(evtIt->evtValA)
{
case 0x07:
if ((VOLEVT_MASK & VOLEVT_VOLUME) && evtIt->evtValB > 0)
evtIt->evtValB = VolConv(evtIt->evtValB, false);
break;
case 0x0B:
if ((VOLEVT_MASK & VOLEVT_EXPRESSION) && evtIt->evtValB > 0)
evtIt->evtValB = VolConv(evtIt->evtValB, false);
break;
}
break;
}
} // end while(evtIt)
}
return;
}
static double GetDBVol(UINT8 inVol)
{
switch(INVOL_ALGO)
{
case VOLALGO_GM: // General MIDI scale
return 40.0 * log(inVol / 127.0) / M_LN10;
case VOLALGO_LIN: // linear scale
return 6.0 * log(inVol / 127.0) / M_LN2;
case VOLALGO_FM: // FM OPx scale (0.75 db per step)
return (inVol - 0x7F) / 8.0 * 6.0;
case VOLALGO_PSG_2DB: // PSG scale (8 values, one step, 2 db)
inVol /= 0x08; // truncate low 3 bits
return (inVol - 0x0F) * 2.0;
case VOLALGO_PSG_3DB: // PSG scale (8 values, one step, 3 db)
inVol /= 0x08; // truncate low 3 bits
return (inVol - 0x0F) * 3.0;
case VOLALGO_WINFM:
{
double volPerc = inVol / 127.0;
double a2 = sin(volPerc * (M_PI / 2.0));
double a3 = sqrt(a2) * 0.9;
double oplTL = 0x3F * (1.0 - a3);
return oplTL / 8.0 * -6.0 + 4.725; // add 4.725 to make up for the *0.9 above
}
}
return 0.0;
}
static UINT8 GetMIDIVol(double dbVol, bool noVol0)
{
double volVal;
UINT8 midVol;
switch(OUTVOL_ALGO)
{
case VOLALGO_GM: // General MIDI scale
volVal = pow(10.0, dbVol / 40.0);
break;
case VOLALGO_LIN: // linear scale
volVal = pow(2.0, dbVol / 6.0);
break;
case VOLALGO_FM: // FM OPx scale (0.75 db per step)
volVal = (dbVol / 6.0 * 8.0 + 127) / 127.0;
break;
case VOLALGO_PSG_2DB: // PSG scale (8 values, one step, 2 db)
volVal = (dbVol / 2.0 * 8.0 + 120) / 120.0;
break;
case VOLALGO_PSG_3DB: // PSG scale (8 values, one step, 3 db)
volVal = (dbVol / 3.0 * 8.0 + 120) / 120.0;
break;
case VOLALGO_WINFM:
{
double oplTL = (dbVol - 4.725) / -6.0 * 8.0;
double a3 = 1.0 - oplTL / 0x3F;
double a2 = pow(a3 / 0.9, 2);
volVal = asin(a2) / (M_PI / 2.0);
break;
}
default:
volVal = 1.0;
break;
}
if (volVal < 0.0)
volVal = 0.0;
else if (volVal > 1.0)
volVal = 1.0;
midVol = (UINT8)(volVal * 0x7F + 0.5);
if (noVol0 && midVol == 0)
midVol = 1;
return midVol;
}
static UINT8 VolConv(UINT8 inVol, bool noVol0)
{
return GetMIDIVol(GetDBVol(inVol) + VOL_GAIN, noVol0);
}