| 1 |
/* |
| 2 |
* This file is part of libsidplayfp, a SID player engine. |
| 3 |
* |
| 4 |
* Copyright 2011-2016 Leandro Nini <[email protected]> |
| 5 |
* Copyright 2007-2010 Antti Lankila |
| 6 |
* |
| 7 |
* This program is free software; you can redistribute it and/or modify |
| 8 |
* it under the terms of the GNU General Public License as published by |
| 9 |
* the Free Software Foundation; either version 2 of the License, or |
| 10 |
* (at your option) any later version. |
| 11 |
* |
| 12 |
* This program is distributed in the hope that it will be useful, |
| 13 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 |
* GNU General Public License for more details. |
| 16 |
* |
| 17 |
* You should have received a copy of the GNU General Public License |
| 18 |
* along with this program; if not, write to the Free Software |
| 19 |
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
| 20 |
*/ |
| 21 |
|
| 22 |
#include "WaveformCalculator.h" |
| 23 |
|
| 24 |
#include <cmath> |
| 25 |
|
| 26 |
namespace reSIDfp |
| 27 |
{ |
| 28 |
|
| 29 |
WaveformCalculator* WaveformCalculator::getInstance() |
| 30 |
{ |
| 31 |
static WaveformCalculator instance; |
| 32 |
return &instance; |
| 33 |
} |
| 34 |
|
| 35 |
/** |
| 36 |
* Parameters derived with the Monte Carlo method based on |
| 37 |
* samplings by kevtris. Code and data available in the project repository [1]. |
| 38 |
* |
| 39 |
* The score here reported is the acoustic error |
| 40 |
* calculated XORing the estimated and the sampled values. |
| 41 |
* In parentheses the number of mispredicted bits |
| 42 |
* on a total of 32768. |
| 43 |
* |
| 44 |
* [1] https://github.com/libsidplayfp/combined-waveforms |
| 45 |
*/ |
| 46 |
const CombinedWaveformConfig config[2][4] = |
| 47 |
{ |
| 48 |
{ /* kevtris chip G (6581 R2) */ |
| 49 |
{0.90251f, 0.f, 0.f, 1.9147f, 1.6747f, 0.62376f }, // error 1689 (280) |
| 50 |
{0.93088f, 2.4843f, 0.f, 1.0353f, 1.1484f, 0.f }, // error 6128 (130) |
| 51 |
{0.90988f, 2.26303f, 1.13126f, 1.0035f, 1.13801f, 0.f }, // error 14243 (632) |
| 52 |
{0.91f, 1.192f, 0.f, 1.0169f, 1.2f, 0.637f }, // error 64 (2) |
| 53 |
}, |
| 54 |
{ /* kevtris chip V (8580 R5) */ |
| 55 |
{0.9632f, 0.f, 0.975f, 1.7467f, 2.36132f, 0.975395f}, // error 1380 (169) |
| 56 |
{0.92886f, 1.67696f, 0.f, 1.1014f, 1.4352f, 0.f }, // error 8007 (218) |
| 57 |
{0.94043f, 1.7937f, 0.981f, 1.1213f, 1.4259f, 0.f }, // error 11957 (362) |
| 58 |
{0.96211f, 0.98695f, 1.00387f, 1.46499f, 1.98375f, 0.77777f }, // error 2369 (89) |
| 59 |
}, |
| 60 |
}; |
| 61 |
|
| 62 |
/** |
| 63 |
* Generate bitstate based on emulation of combined waves. |
| 64 |
* |
| 65 |
* @param config model parameters matrix |
| 66 |
* @param waveform the waveform to emulate, 1 .. 7 |
| 67 |
* @param accumulator the high bits of the accumulator value |
| 68 |
*/ |
| 69 |
short calculateCombinedWaveform(const CombinedWaveformConfig& config, int waveform, int accumulator) |
| 70 |
{ |
| 71 |
float o[12]; |
| 72 |
|
| 73 |
// Saw |
| 74 |
for (unsigned int i = 0; i < 12; i++) |
| 75 |
{ |
| 76 |
o[i] = (accumulator & (1 << i)) != 0 ? 1.f : 0.f; |
| 77 |
} |
| 78 |
|
| 79 |
// convert to Triangle |
| 80 |
if ((waveform & 3) == 1) |
| 81 |
{ |
| 82 |
const bool top = (accumulator & 0x800) != 0; |
| 83 |
|
| 84 |
for (int i = 11; i > 0; i--) |
| 85 |
{ |
| 86 |
o[i] = top ? 1.0f - o[i - 1] : o[i - 1]; |
| 87 |
} |
| 88 |
|
| 89 |
o[0] = 0.f; |
| 90 |
} |
| 91 |
|
| 92 |
// or to Saw+Triangle |
| 93 |
else if ((waveform & 3) == 3) |
| 94 |
{ |
| 95 |
// bottom bit is grounded via T waveform selector |
| 96 |
o[0] *= config.stmix; |
| 97 |
|
| 98 |
for (int i = 1; i < 12; i++) |
| 99 |
{ |
| 100 |
/* |
| 101 |
* Enabling the S waveform pulls the XOR circuit selector transistor down |
| 102 |
* (which would normally make the descending ramp of the triangle waveform), |
| 103 |
* so ST does not actually have a sawtooth and triangle waveform combined, |
| 104 |
* but merely combines two sawtooths, one rising double the speed the other. |
| 105 |
* |
| 106 |
* http://www.lemon64.com/forum/viewtopic.php?t=25442&postdays=0&postorder=asc&start=165 |
| 107 |
*/ |
| 108 |
o[i] = o[i - 1] * (1.f - config.stmix) + o[i] * config.stmix; |
| 109 |
} |
| 110 |
} |
| 111 |
|
| 112 |
// topbit for Saw |
| 113 |
if ((waveform & 2) == 2) |
| 114 |
{ |
| 115 |
o[11] *= config.topbit; |
| 116 |
} |
| 117 |
|
| 118 |
// ST, P* waveforms |
| 119 |
if (waveform == 3 || waveform > 4) |
| 120 |
{ |
| 121 |
float distancetable[12 * 2 + 1]; |
| 122 |
distancetable[12] = 1.f; |
| 123 |
for (int i = 12; i > 0; i--) |
| 124 |
{ |
| 125 |
distancetable[12-i] = 1.0f / pow(config.distance1, i); |
| 126 |
distancetable[12+i] = 1.0f / pow(config.distance2, i); |
| 127 |
} |
| 128 |
|
| 129 |
float tmp[12]; |
| 130 |
|
| 131 |
for (int i = 0; i < 12; i++) |
| 132 |
{ |
| 133 |
float avg = 0.f; |
| 134 |
float n = 0.f; |
| 135 |
|
| 136 |
for (int j = 0; j < 12; j++) |
| 137 |
{ |
| 138 |
const float weight = distancetable[i - j + 12]; |
| 139 |
avg += o[j] * weight; |
| 140 |
n += weight; |
| 141 |
} |
| 142 |
|
| 143 |
// pulse control bit |
| 144 |
if (waveform > 4) |
| 145 |
{ |
| 146 |
const float weight = distancetable[i - 12 + 12]; |
| 147 |
avg += config.pulsestrength * weight; |
| 148 |
n += weight; |
| 149 |
} |
| 150 |
|
| 151 |
tmp[i] = (o[i] + avg / n) * 0.5f; |
| 152 |
} |
| 153 |
|
| 154 |
for (int i = 0; i < 12; i++) |
| 155 |
{ |
| 156 |
o[i] = tmp[i]; |
| 157 |
} |
| 158 |
} |
| 159 |
|
| 160 |
short value = 0; |
| 161 |
|
| 162 |
for (unsigned int i = 0; i < 12; i++) |
| 163 |
{ |
| 164 |
if (o[i] > config.bias) |
| 165 |
{ |
| 166 |
value |= 1 << i; |
| 167 |
} |
| 168 |
} |
| 169 |
|
| 170 |
return value; |
| 171 |
} |
| 172 |
|
| 173 |
matrix_t* WaveformCalculator::buildTable(ChipModel model) |
| 174 |
{ |
| 175 |
const CombinedWaveformConfig* cfgArray = config[model == MOS6581 ? 0 : 1]; |
| 176 |
|
| 177 |
cw_cache_t::iterator lb = CACHE.lower_bound(cfgArray); |
| 178 |
|
| 179 |
if (lb != CACHE.end() && !(CACHE.key_comp()(cfgArray, lb->first))) |
| 180 |
{ |
| 181 |
return &(lb->second); |
| 182 |
} |
| 183 |
|
| 184 |
matrix_t wftable(8, 4096); |
| 185 |
|
| 186 |
for (unsigned int idx = 0; idx < 1 << 12; idx++) |
| 187 |
{ |
| 188 |
wftable[0][idx] = 0xfff; |
| 189 |
wftable[1][idx] = static_cast<short>((idx & 0x800) == 0 ? idx << 1 : (idx ^ 0xfff) << 1); |
| 190 |
wftable[2][idx] = static_cast<short>(idx); |
| 191 |
wftable[3][idx] = calculateCombinedWaveform(cfgArray[0], 3, idx); |
| 192 |
wftable[4][idx] = 0xfff; |
| 193 |
wftable[5][idx] = calculateCombinedWaveform(cfgArray[1], 5, idx); |
| 194 |
wftable[6][idx] = calculateCombinedWaveform(cfgArray[2], 6, idx); |
| 195 |
wftable[7][idx] = calculateCombinedWaveform(cfgArray[3], 7, idx); |
| 196 |
} |
| 197 |
#ifdef HAVE_CXX11 |
| 198 |
return &(CACHE.emplace_hint(lb, cw_cache_t::value_type(cfgArray, wftable))->second); |
| 199 |
#else |
| 200 |
return &(CACHE.insert(lb, cw_cache_t::value_type(cfgArray, wftable))->second); |
| 201 |
#endif |
| 202 |
} |
| 203 |
|
| 204 |
} // namespace reSIDfp |