1 /* Copyright (C) 2003, 2004, 2005, 2006, 2008, 2009 Dean Beeler, Jerome Fisher
2  * Copyright (C) 2011-2016 Dean Beeler, Jerome Fisher, Sergey V. Mikayev
3  *
4  *  This program is free software: you can redistribute it and/or modify
5  *  it under the terms of the GNU Lesser General Public License as published by
6  *  the Free Software Foundation, either version 2.1 of the License, or
7  *  (at your option) any later version.
8  *
9  *  This program is distributed in the hope that it will be useful,
10  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
11  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  *  GNU Lesser General Public License for more details.
13  *
14  *  You should have received a copy of the GNU Lesser General Public License
15  *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
16  */
17 
18 #include "internals.h"
19 
20 #include "Tables.h"
21 #include "mmath.h"
22 
23 namespace MT32Emu {
24 
25 // UNUSED: const int MIDDLEC = 60;
26 
getInstance()27 const Tables &Tables::getInstance() {
28 	static const Tables instance;
29 	return instance;
30 }
31 
Tables()32 Tables::Tables() {
33 	int lf;
34 	for (lf = 0; lf <= 100; lf++) {
35 		// CONFIRMED:KG: This matches a ROM table found by Mok
36 		float fVal = (2.0f - LOG10F((float)lf + 1.0f)) * 128.0f;
37 		int val = (int)(fVal + 1.0);
38 		if (val > 255) {
39 			val = 255;
40 		}
41 		levelToAmpSubtraction[lf] = (Bit8u)val;
42 	}
43 
44 	envLogarithmicTime[0] = 64;
45 	for (lf = 1; lf <= 255; lf++) {
46 		// CONFIRMED:KG: This matches a ROM table found by Mok
47 		envLogarithmicTime[lf] = (Bit8u)ceil(64.0f + LOG2F((float)lf) * 8.0f);
48 	}
49 
50 #if 0
51 	// The table below is to be used in conjunction with emulation of VCA of newer generation units which is currently missing.
52 	// These relatively small values are rather intended to fine-tune the overall amplification of the VCA.
53 	// CONFIRMED: Based on a table found by Mok in the LAPC-I control ROM
54 	// Note that this matches the MT-32 table, but with the values clamped to a maximum of 8.
55 	memset(masterVolToAmpSubtraction, 8, 71);
56 	memset(masterVolToAmpSubtraction + 71, 7, 3);
57 	memset(masterVolToAmpSubtraction + 74, 6, 4);
58 	memset(masterVolToAmpSubtraction + 78, 5, 3);
59 	memset(masterVolToAmpSubtraction + 81, 4, 4);
60 	memset(masterVolToAmpSubtraction + 85, 3, 3);
61 	memset(masterVolToAmpSubtraction + 88, 2, 4);
62 	memset(masterVolToAmpSubtraction + 92, 1, 4);
63 	memset(masterVolToAmpSubtraction + 96, 0, 5);
64 #else
65 	// CONFIRMED: Based on a table found by Mok in the MT-32 control ROM
66 	masterVolToAmpSubtraction[0] = 255;
67 	for (int masterVol = 1; masterVol <= 100; masterVol++) {
68 		masterVolToAmpSubtraction[masterVol] = (Bit8u)(106.31 - 16.0f * LOG2F((float)masterVol));
69 	}
70 #endif
71 
72 	for (int i = 0; i <= 100; i++) {
73 		pulseWidth100To255[i] = (Bit8u)(i * 255 / 100.0f + 0.5f);
74 		//synth->printDebug("%d: %d", i, pulseWidth100To255[i]);
75 	}
76 
77 	// The LA32 chip contains an exponent table inside. The table contains 12-bit integer values.
78 	// The actual table size is 512 rows. The 9 higher bits of the fractional part of the argument are used as a lookup address.
79 	// To improve the precision of computations, the lower bits are supposed to be used for interpolation as the LA32 chip also
80 	// contains another 512-row table with inverted differences between the main table values.
81 	for (int i = 0; i < 512; i++) {
82 		exp9[i] = Bit16u(8191.5f - EXP2F(13.0f + ~i / 512.0f));
83 	}
84 
85 	// There is a logarithmic sine table inside the LA32 chip. The table contains 13-bit integer values.
86 	for (int i = 1; i < 512; i++) {
87 		logsin9[i] = Bit16u(0.5f - LOG2F(sin((i + 0.5f) / 1024.0f * FLOAT_PI)) * 1024.0f);
88 	}
89 
90 	// The very first value is clamped to the maximum possible 13-bit integer
91 	logsin9[0] = 8191;
92 
93 	// found from sample analysis
94 	static const Bit8u resAmpDecayFactorTable[] = {31, 16, 12, 8, 5, 3, 2, 1};
95 	resAmpDecayFactor = resAmpDecayFactorTable;
96 }
97 
98 } // namespace MT32Emu
99