DOSBox-X
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Defines
src/mt32/Tables.cpp
00001 /* Copyright (C) 2003, 2004, 2005, 2006, 2008, 2009 Dean Beeler, Jerome Fisher
00002  * Copyright (C) 2011, 2012, 2013 Dean Beeler, Jerome Fisher, Sergey V. Mikayev
00003  *
00004  *  This program is free software: you can redistribute it and/or modify
00005  *  it under the terms of the GNU Lesser General Public License as published by
00006  *  the Free Software Foundation, either version 2.1 of the License, or
00007  *  (at your option) any later version.
00008  *
00009  *  This program is distributed in the hope that it will be useful,
00010  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00011  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012  *  GNU Lesser General Public License for more details.
00013  *
00014  *  You should have received a copy of the GNU Lesser General Public License
00015  *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
00016  */
00017 
00018 #include <cmath>
00019 #include <cstdlib>
00020 #include <cstring>
00021 
00022 #include "mt32emu.h"
00023 #include "mmath.h"
00024 
00025 namespace MT32Emu {
00026 
00027 const Tables &Tables::getInstance() {
00028         static const Tables instance;
00029         return instance;
00030 }
00031 
00032 Tables::Tables() {
00033         int lf;
00034         for (lf = 0; lf <= 100; lf++) {
00035                 // CONFIRMED:KG: This matches a ROM table found by Mok
00036                 float fVal = (2.0f - LOG10F((float)lf + 1.0f)) * 128.0f;
00037                 int val = (int)(fVal + 1.0);
00038                 if (val > 255) {
00039                         val = 255;
00040                 }
00041                 levelToAmpSubtraction[lf] = (Bit8u)val;
00042         }
00043 
00044         envLogarithmicTime[0] = 64;
00045         for (lf = 1; lf <= 255; lf++) {
00046                 // CONFIRMED:KG: This matches a ROM table found by Mok
00047                 envLogarithmicTime[lf] = (Bit8u)ceil(64.0f + LOG2F((float)lf) * 8.0f);
00048         }
00049 
00050 #ifdef EMULATE_LAPC_I // Dummy #ifdef - we'll have runtime emulation mode selection in future.
00051         // CONFIRMED: Based on a table found by Mok in the LAPC-I control ROM
00052         // Note that this matches the MT-32 table, but with the values clamped to a maximum of 8.
00053         memset(masterVolToAmpSubtraction, 8, 71);
00054         memset(masterVolToAmpSubtraction + 71, 7, 3);
00055         memset(masterVolToAmpSubtraction + 74, 6, 4);
00056         memset(masterVolToAmpSubtraction + 78, 5, 3);
00057         memset(masterVolToAmpSubtraction + 81, 4, 4);
00058         memset(masterVolToAmpSubtraction + 85, 3, 3);
00059         memset(masterVolToAmpSubtraction + 88, 2, 4);
00060         memset(masterVolToAmpSubtraction + 92, 1, 4);
00061         memset(masterVolToAmpSubtraction + 96, 0, 5);
00062 #else
00063         // CONFIRMED: Based on a table found by Mok in the MT-32 control ROM
00064         masterVolToAmpSubtraction[0] = 255;
00065         for (int masterVol = 1; masterVol <= 100; masterVol++) {
00066                 masterVolToAmpSubtraction[masterVol] = (int)(106.31 - 16.0f * LOG2F((float)masterVol));
00067         }
00068 #endif
00069 
00070         for (int i = 0; i <= 100; i++) {
00071                 pulseWidth100To255[i] = (int)(i * 255 / 100.0f + 0.5f);
00072                 //synth->printDebug("%d: %d", i, pulseWidth100To255[i]);
00073         }
00074 
00075         // The LA32 chip contains an exponent table inside. The table contains 12-bit integer values.
00076         // The actual table size is 512 rows. The 9 higher bits of the fractional part of the argument are used as a lookup address.
00077         // To improve the precision of computations, the lower bits are supposed to be used for interpolation as the LA32 chip also
00078         // contains another 512-row table with inverted differences between the main table values.
00079         for (int i = 0; i < 512; i++) {
00080                 exp9[i] = Bit16u(8191.5f - EXP2F(13.0f + ~i / 512.0f));
00081         }
00082 
00083         // There is a logarithmic sine table inside the LA32 chip. The table contains 13-bit integer values.
00084         for (int i = 1; i < 512; i++) {
00085                 logsin9[i] = Bit16u(0.5f - LOG2F(sin((i + 0.5f) / 1024.0f * FLOAT_PI)) * 1024.0f);
00086         }
00087 
00088         // The very first value is clamped to the maximum possible 13-bit integer
00089         logsin9[0] = 8191;
00090 
00091         // found from sample analysis
00092         static const Bit8u resAmpDecayFactorTable[] = {31, 16, 12, 8, 5, 3, 2, 1};
00093         resAmpDecayFactor = resAmpDecayFactorTable;
00094 }
00095 
00096 }