/
configuration.cpp
662 lines (530 loc) · 18.3 KB
/
configuration.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
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
/*
** configuration.cpp
** Handle zmusic's configuration.
**
**---------------------------------------------------------------------------
** Copyright 2019 Christoph Oelckers
** All rights reserved.
**
** Redistribution and use in source and binary forms, with or without
** modification, are permitted provided that the following conditions
** are met:
**
** 1. Redistributions of source code must retain the above copyright
** notice, this list of conditions and the following disclaimer.
** 2. Redistributions in binary form must reproduce the above copyright
** notice, this list of conditions and the following disclaimer in the
** documentation and/or other materials provided with the distribution.
** 3. The name of the author may not be used to endorse or promote products
** derived from this software without specific prior written permission.
**
** THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
** IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
** OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
** IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
** INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
** NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
** THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
**---------------------------------------------------------------------------
**
*/
#include <algorithm>
#include "timidity/timidity.h"
#include "timiditypp/timidity.h"
#include "oplsynth/oplio.h"
#include "../../libraries/dumb/include/dumb.h"
#include "zmusic.h"
#include "musinfo.h"
#include "midiconfig.h"
struct Dummy
{
void ChangeSettingInt(const char*, int) {}
void ChangeSettingNum(const char*, double) {}
void ChangeSettingString(const char*, const char*) {}
};
#define devType() ((currSong)? (currSong)->GetDeviceType() : MDEV_DEFAULT)
MiscConfig miscConfig;
Callbacks musicCallbacks;
void ZMusic_SetCallbacks(const Callbacks* cb)
{
dumb_decode_vorbis = cb->DumbVorbisDecode;
musicCallbacks = *cb;
}
void ZMusic_SetGenMidi(const uint8_t* data)
{
memcpy(oplConfig.OPLinstruments, data, 175 * 36);
oplConfig.genmidiset = true;
}
void ZMusic_SetWgOpn(const void* data, unsigned len)
{
opnConfig.default_bank.resize(len);
memcpy(opnConfig.default_bank.data(), data, len);
}
void ZMusic_SetDmxGus(const void* data, unsigned len)
{
gusConfig.dmxgus.resize(len);
memcpy(gusConfig.dmxgus.data(), data, len);
}
template<class valtype>
void ChangeAndReturn(valtype &variable, valtype value, valtype *realv)
{
variable = value;
if (realv) *realv = value;
}
#define FLUID_CHORUS_MOD_SINE 0
#define FLUID_CHORUS_MOD_TRIANGLE 1
#define FLUID_CHORUS_DEFAULT_TYPE FLUID_CHORUS_MOD_SINE
//==========================================================================
//
// Timidity++ uses a static global set of configuration variables.
// THese can be changed live while the synth is playing but need synchronization.
//
// Currently the synth is not reentrant due to this and a handful
// of other global variables.
//
//==========================================================================
template<class T> void ChangeVarSync(T& var, T value)
{
std::lock_guard<std::mutex> lock(TimidityPlus::ConfigMutex);
var = value;
}
//==========================================================================
//
// Timidity++ reverb is a bit more complicated because it is two properties in one value.
//
//==========================================================================
/*
* reverb=0 no reverb 0
* reverb=1 old reverb 1
* reverb=1,n set reverb level to n (-1 to -127)
* reverb=2 "global" old reverb 2
* reverb=2,n set reverb level to n (-1 to -127) - 128
* reverb=3 new reverb 3
* reverb=3,n set reverb level to n (-1 to -127) - 256
* reverb=4 "global" new reverb 4
* reverb=4,n set reverb level to n (-1 to -127) - 384
*/
static int local_timidity_reverb_level;
static int local_timidity_reverb;
static void TimidityPlus_SetReverb()
{
int value = 0;
int mode = local_timidity_reverb;
int level = local_timidity_reverb_level;
if (mode == 0 || level == 0) value = mode;
else value = (mode - 1) * -128 - level;
ChangeVarSync(TimidityPlus::timidity_reverb, value);
}
using namespace ZMusic;
//==========================================================================
//
// change an integer value
//
//==========================================================================
bool ChangeMusicSetting(ZMusic::EIntConfigKey key, MusInfo *currSong, int value, int *pRealValue)
{
switch (key)
{
default:
return false;
case adl_chips_count:
ChangeAndReturn(adlConfig.adl_chips_count, value, pRealValue);
return devType() == MDEV_ADL;
case adl_emulator_id:
ChangeAndReturn(adlConfig.adl_emulator_id, value, pRealValue);
return devType() == MDEV_ADL;
case adl_run_at_pcm_rate:
ChangeAndReturn(adlConfig.adl_run_at_pcm_rate, value, pRealValue);
return devType() == MDEV_ADL;
case adl_fullpan:
ChangeAndReturn(adlConfig.adl_fullpan, value, pRealValue);
return devType() == MDEV_ADL;
case adl_bank:
ChangeAndReturn(adlConfig.adl_bank, value, pRealValue);
return devType() == MDEV_ADL;
case adl_use_custom_bank:
ChangeAndReturn(adlConfig.adl_use_custom_bank, value, pRealValue);
return devType() == MDEV_ADL;
case adl_volume_model:
ChangeAndReturn(adlConfig.adl_volume_model, value, pRealValue);
return devType() == MDEV_ADL;
case fluid_reverb:
if (currSong != NULL)
currSong->ChangeSettingInt("fluidsynth.synth.reverb.active", value);
ChangeAndReturn(fluidConfig.fluid_reverb, value, pRealValue);
return false;
case fluid_chorus:
if (currSong != NULL)
currSong->ChangeSettingInt("fluidsynth.synth.chorus.active", value);
ChangeAndReturn(fluidConfig.fluid_chorus, value, pRealValue);
return false;
case fluid_voices:
if (value < 16)
value = 16;
else if (value > 4096)
value = 4096;
if (currSong != NULL)
currSong->ChangeSettingInt("fluidsynth.synth.polyphony", value);
ChangeAndReturn(fluidConfig.fluid_voices, value, pRealValue);
return false;
case fluid_interp:
// Values are: 0 = FLUID_INTERP_NONE
// 1 = FLUID_INTERP_LINEAR
// 4 = FLUID_INTERP_4THORDER (the FluidSynth default)
// 7 = FLUID_INTERP_7THORDER
// (And here I thought it was just a linear list.)
// Round undefined values to the nearest valid one.
if (value < 0)
value = 0;
else if (value == 2)
value = 1;
else if (value == 3 || value == 5)
value = 4;
else if (value == 6 || value > 7)
value = 7;
if (currSong != NULL)
currSong->ChangeSettingInt("fluidsynth.synth.interpolation", value);
ChangeAndReturn(fluidConfig.fluid_interp, value, pRealValue);
return false;
case fluid_samplerate:
// This will only take effect for the next song. (Q: Is this even needed?)
ChangeAndReturn(fluidConfig.fluid_samplerate, std::max<int>(value, 0), pRealValue);
return false;
// I don't know if this setting even matters for us, since we aren't letting
// FluidSynth drives its own output.
case fluid_threads:
if (value < 1)
value = 1;
else if (value > 256)
value = 256;
ChangeAndReturn(fluidConfig.fluid_threads, value, pRealValue);
return false;
case fluid_chorus_voices:
if (value < 0)
value = 0;
else if (value > 99)
value = 99;
if (currSong != NULL)
currSong->ChangeSettingNum("fluidsynth.z.chorus", value);
ChangeAndReturn(fluidConfig.fluid_chorus_voices, value, pRealValue);
return false;
case fluid_chorus_type:
if (value != FLUID_CHORUS_MOD_SINE && value != FLUID_CHORUS_MOD_TRIANGLE)
value = FLUID_CHORUS_DEFAULT_TYPE;
if (currSong != NULL)
currSong->ChangeSettingNum("fluidsynth.z.chorus", value); // Uses float to simplify the checking code in the renderer.
ChangeAndReturn(fluidConfig.fluid_chorus_type, value, pRealValue);
return false;
case opl_numchips:
if (value <= 0)
value = 1;
else if (value > MAXOPL2CHIPS)
value = MAXOPL2CHIPS;
if (currSong != NULL)
currSong->ChangeSettingInt("opl.numchips", value);
ChangeAndReturn(oplConfig.numchips, value, pRealValue);
return false;
case opl_core:
if (value < 0) value = 0;
else if (value > 3) value = 3;
ChangeAndReturn(oplConfig.core, value, pRealValue);
return devType() == MDEV_OPL;
case opl_fullpan:
ChangeAndReturn(oplConfig.fullpan, value, pRealValue);
return false;
case opn_chips_count:
ChangeAndReturn(opnConfig.opn_chips_count, value, pRealValue);
return devType() == MDEV_OPN;
case opn_emulator_id:
ChangeAndReturn(opnConfig.opn_emulator_id, value, pRealValue);
return devType() == MDEV_OPN;
case opn_run_at_pcm_rate:
ChangeAndReturn(opnConfig.opn_run_at_pcm_rate, value, pRealValue);
return devType() == MDEV_OPN;
case opn_fullpan:
ChangeAndReturn(opnConfig.opn_fullpan, value, pRealValue);
return devType() == MDEV_OPN;
case opn_use_custom_bank:
ChangeAndReturn(opnConfig.opn_use_custom_bank, value, pRealValue);
return devType() == MDEV_OPN;
case gus_dmxgus:
ChangeAndReturn(gusConfig.gus_dmxgus, value, pRealValue);
return devType() == MDEV_GUS;
case gus_midi_voices:
ChangeAndReturn(gusConfig.midi_voices, value, pRealValue);
return devType() == MDEV_GUS;
case gus_memsize:
ChangeAndReturn(gusConfig.gus_memsize, value, pRealValue);
return devType() == MDEV_GUS && gusConfig.gus_dmxgus;
case timidity_modulation_wheel:
ChangeVarSync(TimidityPlus::timidity_modulation_wheel, value);
if (pRealValue) *pRealValue = value;
return false;
case timidity_portamento:
ChangeVarSync(TimidityPlus::timidity_portamento, value);
if (pRealValue) *pRealValue = value;
return false;
case timidity_reverb:
if (value < 0 || value > 4) value = 0;
else TimidityPlus_SetReverb();
local_timidity_reverb = value;
if (pRealValue) *pRealValue = value;
return false;
case timidity_reverb_level:
if (value < 0 || value > 127) value = 0;
else TimidityPlus_SetReverb();
local_timidity_reverb_level = value;
if (pRealValue) *pRealValue = value;
return false;
case timidity_chorus:
ChangeVarSync(TimidityPlus::timidity_chorus, value);
if (pRealValue) *pRealValue = value;
return false;
case timidity_surround_chorus:
ChangeVarSync(TimidityPlus::timidity_surround_chorus, value);
if (pRealValue) *pRealValue = value;
return devType() == MDEV_TIMIDITY;
case timidity_channel_pressure:
ChangeVarSync(TimidityPlus::timidity_channel_pressure, value);
if (pRealValue) *pRealValue = value;
return false;
case timidity_lpf_def:
ChangeVarSync(TimidityPlus::timidity_lpf_def, value);
if (pRealValue) *pRealValue = value;
return false;
case timidity_temper_control:
ChangeVarSync(TimidityPlus::timidity_temper_control, value);
if (pRealValue) *pRealValue = value;
return false;
case timidity_modulation_envelope:
ChangeVarSync(TimidityPlus::timidity_modulation_envelope, value);
if (pRealValue) *pRealValue = value;
return devType() == MDEV_TIMIDITY;
case timidity_overlap_voice_allow:
ChangeVarSync(TimidityPlus::timidity_overlap_voice_allow, value);
if (pRealValue) *pRealValue = value;
return false;
case timidity_drum_effect:
ChangeVarSync(TimidityPlus::timidity_drum_effect, value);
if (pRealValue) *pRealValue = value;
return false;
case timidity_pan_delay:
ChangeVarSync(TimidityPlus::timidity_pan_delay, value);
if (pRealValue) *pRealValue = value;
return false;
case timidity_key_adjust:
if (value < -24) value = -24;
else if (value > 24) value = 24;
ChangeVarSync(TimidityPlus::timidity_key_adjust, value);
if (pRealValue) *pRealValue = value;
return false;
case wildmidi_reverb:
if (currSong != NULL)
currSong->ChangeSettingInt("wildmidi.reverb", value);
wildMidiConfig.reverb = value;
if (pRealValue) *pRealValue = value;
return false;
case wildmidi_enhanced_resampling:
if (currSong != NULL)
currSong->ChangeSettingInt("wildmidi.resampling", value);
wildMidiConfig.enhanced_resampling = value;
if (pRealValue) *pRealValue = value;
return false;
case snd_midiprecache:
ChangeAndReturn(miscConfig.snd_midiprecache, value, pRealValue);
return false;
case snd_streambuffersize:
if (value < 16)
{
value = 16;
}
else if (value > 1024)
{
value = 1024;
}
ChangeAndReturn(miscConfig.snd_streambuffersize, value, pRealValue);
return false;
case mod_samplerate:
ChangeAndReturn(dumbConfig.mod_samplerate, value, pRealValue);
return false;
case mod_volramp:
ChangeAndReturn(dumbConfig.mod_volramp, value, pRealValue);
return false;
case mod_interp:
ChangeAndReturn(dumbConfig.mod_interp, value, pRealValue);
return false;
case mod_autochip:
ChangeAndReturn(dumbConfig.mod_autochip, value, pRealValue);
return false;
case mod_autochip_size_force:
ChangeAndReturn(dumbConfig.mod_autochip_size_force, value, pRealValue);
return false;
case mod_autochip_size_scan:
ChangeAndReturn(dumbConfig.mod_autochip_size_scan, value, pRealValue);
return false;
case mod_autochip_scan_threshold:
ChangeAndReturn(dumbConfig.mod_autochip_scan_threshold, value, pRealValue);
return false;
case snd_mididevice:
miscConfig.snd_mididevice = value;
return false;
case snd_outputrate:
miscConfig.snd_outputrate = value;
return false;
}
return false;
}
bool ChangeMusicSetting(ZMusic::EFloatConfigKey key, MusInfo* currSong, float value, float *pRealValue)
{
switch (key)
{
default:
return false;
case fluid_gain:
if (value < 0)
value = 0;
else if (value > 10)
value = 10;
if (currSong != NULL)
currSong->ChangeSettingNum("fluidsynth.synth.gain", value);
ChangeAndReturn(fluidConfig.fluid_gain, value, pRealValue);
return false;
case fluid_reverb_roomsize:
if (value < 0)
value = 0;
else if (value > 1.2f)
value = 1.2f;
if (currSong != NULL)
currSong->ChangeSettingNum("fluidsynth.z.reverb", value);
ChangeAndReturn(fluidConfig.fluid_reverb_roomsize, value, pRealValue);
return false;
case fluid_reverb_damping:
if (value < 0)
value = 0;
else if (value > 1)
value = 1;
if (currSong != NULL)
currSong->ChangeSettingNum("fluidsynth.z.reverb", value);
ChangeAndReturn(fluidConfig.fluid_reverb_damping, value, pRealValue);
return false;
case fluid_reverb_width:
if (value < 0)
value = 0;
else if (value > 100)
value = 100;
if (currSong != NULL)
currSong->ChangeSettingNum("fluidsynth.z.reverb", value);
ChangeAndReturn(fluidConfig.fluid_reverb_width, value, pRealValue);
return false;
case fluid_reverb_level:
if (value < 0)
value = 0;
else if (value > 1)
value = 1;
if (currSong != NULL)
currSong->ChangeSettingNum("fluidsynth.z.reverb", value);
ChangeAndReturn(fluidConfig.fluid_reverb_level, value, pRealValue);
return false;
case fluid_chorus_level:
if (value < 0)
value = 0;
else if (value > 1)
value = 1;
if (currSong != NULL)
currSong->ChangeSettingNum("fluidsynth.z.chorus", value);
ChangeAndReturn(fluidConfig.fluid_chorus_level, value, pRealValue);
return false;
case fluid_chorus_speed:
if (value < 0.29f)
value = 0.29f;
else if (value > 5)
value = 5;
if (currSong != NULL)
currSong->ChangeSettingNum("fluidsynth.z.chorus", value);
ChangeAndReturn(fluidConfig.fluid_chorus_speed, value, pRealValue);
return false;
// depth is in ms and actual maximum depends on the sample rate
case fluid_chorus_depth:
if (value < 0)
value = 0;
else if (value > 21)
value = 21;
if (currSong != NULL)
currSong->ChangeSettingNum("fluidsynth.z.chorus", value);
ChangeAndReturn(fluidConfig.fluid_chorus_depth, value, pRealValue);
return false;
case timidity_drum_power:
if (value < 0) value = 0;
else if (value > MAX_AMPLIFICATION / 100.f) value = MAX_AMPLIFICATION / 100.f;
ChangeVarSync(TimidityPlus::timidity_drum_power, value);
if (pRealValue) *pRealValue = value;
return false;
// For testing mainly.
case timidity_tempo_adjust:
if (value < 0.25) value = 0.25;
else if (value > 10) value = 10;
ChangeVarSync(TimidityPlus::timidity_tempo_adjust, value);
if (pRealValue) *pRealValue = value;
return false;
case min_sustain_time:
if (value < 0) value = 0;
ChangeVarSync(TimidityPlus::min_sustain_time, value);
if (pRealValue) *pRealValue = value;
return false;
case gme_stereodepth:
if (currSong != nullptr)
currSong->ChangeSettingNum("GME.stereodepth", value);
ChangeAndReturn(miscConfig.gme_stereodepth, value, pRealValue);
return false;
case mod_dumb_mastervolume:
if (value < 0) value = 0;
ChangeAndReturn(dumbConfig.mod_dumb_mastervolume, value, pRealValue);
return false;
case snd_musicvolume:
miscConfig.snd_musicvolume = value;
return false;
case relative_volume:
miscConfig.relative_volume = value;
return false;
case snd_mastervolume:
miscConfig.snd_mastervolume = value;
return false;
}
return false;
}
bool ChangeMusicSetting(ZMusic::EStringConfigKey key, MusInfo* currSong, const char *value)
{
switch (key)
{
default:
return false;
case adl_custom_bank:
adlConfig.adl_custom_bank = value;
return devType() == MDEV_ADL;
case fluid_lib:
fluidConfig.fluid_lib = value;
return false; // only takes effect for next song.
case fluid_patchset:
fluidConfig.fluid_patchset = value;
return devType() == MDEV_FLUIDSYNTH;
case opn_custom_bank:
opnConfig.opn_custom_bank = value;
return devType() == MDEV_OPN && opnConfig.opn_use_custom_bank;
case gus_config:
gusConfig.gus_config = value;
return devType() == MDEV_GUS;
case gus_patchdir:
gusConfig.gus_patchdir = value;
return devType() == MDEV_GUS && gusConfig.gus_dmxgus;
case timidity_config:
timidityConfig.timidity_config = value;
return devType() == MDEV_TIMIDITY;
case wildmidi_config:
wildMidiConfig.config = value;
return devType() == MDEV_TIMIDITY;
}
return false;
}