float TVA::nextAmp() { // FIXME: This whole method is based on guesswork Bit32u target = la32TargetAmp * TVA_TARGET_AMP_MULT; if (la32AmpIncrement == 0) { currentAmp = target; } else { if ((la32AmpIncrement & 0x80) != 0) { // Lowering amp if (largeAmpInc > currentAmp) { currentAmp = target; nextPhase(); } else { currentAmp -= largeAmpInc; if (currentAmp <= target) { currentAmp = target; nextPhase(); } } } else { // Raising amp if (MAX_CURRENT_AMP - currentAmp < largeAmpInc) { currentAmp = target; nextPhase(); } else { currentAmp += largeAmpInc; if (currentAmp >= target) { currentAmp = target; nextPhase(); } } } } // FIXME:KG: Note that the "65536.0f" here is slightly arbitrary, and needs to be confirmed. 32768.0f is more likely. // FIXME:KG: We should perhaps use something faster once we've got the details sorted out, but the real synth's amp level changes pretty smoothly. return EXP2F((float)currentAmp / TVA_TARGET_AMP_MULT / 16.0f - 1.0f) / 65536.0f; }
float LA32WaveGenerator::generateNextSample(const Bit32u ampVal, const Bit16u pitch, const Bit32u cutoffRampVal) { if (!active) { return 0.0f; } this->amp = amp; this->pitch = pitch; float sample = 0.0f; // SEMI-CONFIRMED: From sample analysis: // (1) Tested with a single partial playing PCM wave 77 with pitchCoarse 36 and no keyfollow, velocity follow, etc. // This gives results within +/- 2 at the output (before any DAC bitshifting) // when sustaining at levels 156 - 255 with no modifiers. // (2) Tested with a special square wave partial (internal capture ID tva5) at TVA envelope levels 155-255. // This gives deltas between -1 and 0 compared to the real output. Note that this special partial only produces // positive amps, so negative still needs to be explored, as well as lower levels. // // Also still partially unconfirmed is the behaviour when ramping between levels, as well as the timing. float amp = EXP2F(ampVal / -1024.0f / 4096.0f); float freq = EXP2F(pitch / 4096.0f - 16.0f) * SAMPLE_RATE; if (isPCMWave()) { // Render PCM waveform int len = pcmWaveLength; int intPCMPosition = (int)pcmPosition; if (intPCMPosition >= len && !pcmWaveLooped) { // We're now past the end of a non-looping PCM waveform so it's time to die. deactivate(); return 0.0f; } float positionDelta = freq * 2048.0f / SAMPLE_RATE; // Linear interpolation float firstSample = getPCMSample(intPCMPosition); // We observe that for partial structures with ring modulation the interpolation is not applied to the slave PCM partial. // It's assumed that the multiplication circuitry intended to perform the interpolation on the slave PCM partial // is borrowed by the ring modulation circuit (or the LA32 chip has a similar lack of resources assigned to each partial pair). if (pcmWaveInterpolated) { sample = firstSample + (getPCMSample(intPCMPosition + 1) - firstSample) * (pcmPosition - intPCMPosition); } else { sample = firstSample; } float newPCMPosition = pcmPosition + positionDelta; if (pcmWaveLooped) { newPCMPosition = fmod(newPCMPosition, (float)pcmWaveLength); } pcmPosition = newPCMPosition; } else { // Render synthesised waveform wavePos *= lastFreq / freq; lastFreq = freq; float resAmp = EXP2F(1.0f - (32 - resonance) / 4.0f); { //static const float resAmpFactor = EXP2F(-7); //resAmp = EXP2I(resonance << 10) * resAmpFactor; } // The cutoffModifier may not be supposed to be directly added to the cutoff - // it may for example need to be multiplied in some way. // The 240 cutoffVal limit was determined via sample analysis (internal Munt capture IDs: glop3, glop4). // More research is needed to be sure that this is correct, however. float cutoffVal = cutoffRampVal / 262144.0f; if (cutoffVal > MAX_CUTOFF_VALUE) { cutoffVal = MAX_CUTOFF_VALUE; } // Wave length in samples float waveLen = SAMPLE_RATE / freq; // Init cosineLen float cosineLen = 0.5f * waveLen; if (cutoffVal > MIDDLE_CUTOFF_VALUE) { cosineLen *= EXP2F((cutoffVal - MIDDLE_CUTOFF_VALUE) / -16.0f); // found from sample analysis } // Start playing in center of first cosine segment // relWavePos is shifted by a half of cosineLen float relWavePos = wavePos + 0.5f * cosineLen; if (relWavePos > waveLen) { relWavePos -= waveLen; } // Ratio of positive segment to wave length float pulseLen = 0.5f; if (pulseWidth > 128) { pulseLen = EXP2F((64 - pulseWidth) / 64.0f); //static const float pulseLenFactor = EXP2F(-192 / 64); //pulseLen = EXP2I((256 - pulseWidthVal) << 6) * pulseLenFactor; } pulseLen *= waveLen; float hLen = pulseLen - cosineLen; // Ignore pulsewidths too high for given freq if (hLen < 0.0f) { hLen = 0.0f; } // Ignore pulsewidths too high for given freq and cutoff float lLen = waveLen - hLen - 2 * cosineLen; if (lLen < 0.0f) { lLen = 0.0f; } // Correct resAmp for cutoff in range 50..66 if ((cutoffVal >= 128.0f) && (cutoffVal < 144.0f)) { resAmp *= sin(FLOAT_PI * (cutoffVal - 128.0f) / 32.0f); } // Produce filtered square wave with 2 cosine waves on slopes // 1st cosine segment if (relWavePos < cosineLen) { sample = -cos(FLOAT_PI * relWavePos / cosineLen); } else // high linear segment if (relWavePos < (cosineLen + hLen)) { sample = 1.f; } else // 2nd cosine segment if (relWavePos < (2 * cosineLen + hLen)) { sample = cos(FLOAT_PI * (relWavePos - (cosineLen + hLen)) / cosineLen); } else { // low linear segment sample = -1.f; } if (cutoffVal < 128.0f) { // Attenuate samples below cutoff 50 // Found by sample analysis sample *= EXP2F(-0.125f * (128.0f - cutoffVal)); } else { // Add resonance sine. Effective for cutoff > 50 only float resSample = 1.0f; // Resonance decay speed factor float resAmpDecayFactor = Tables::getInstance().resAmpDecayFactor[resonance >> 2]; // Now relWavePos counts from the middle of first cosine relWavePos = wavePos; // negative segments if (!(relWavePos < (cosineLen + hLen))) { resSample = -resSample; relWavePos -= cosineLen + hLen; // From the digital captures, the decaying speed of the resonance sine is found a bit different for the positive and the negative segments resAmpDecayFactor += 0.25f; } // Resonance sine WG resSample *= sin(FLOAT_PI * relWavePos / cosineLen); // Resonance sine amp float resAmpFadeLog2 = -0.125f * resAmpDecayFactor * (relWavePos / cosineLen); // seems to be exact float resAmpFade = EXP2F(resAmpFadeLog2); // Now relWavePos set negative to the left from center of any cosine relWavePos = wavePos; // negative segment if (!(wavePos < (waveLen - 0.5f * cosineLen))) { relWavePos -= waveLen; } else // positive segment if (!(wavePos < (hLen + 0.5f * cosineLen))) { relWavePos -= cosineLen + hLen; } // To ensure the output wave has no breaks, two different windows are appied to the beginning and the ending of the resonance sine segment if (relWavePos < 0.5f * cosineLen) { float syncSine = sin(FLOAT_PI * relWavePos / cosineLen); if (relWavePos < 0.0f) { // The window is synchronous square sine here resAmpFade *= syncSine * syncSine; } else { // The window is synchronous sine here resAmpFade *= syncSine; } } sample += resSample * resAmp * resAmpFade; } // sawtooth waves if (sawtoothWaveform) { sample *= cos(FLOAT_2PI * wavePos / waveLen); } wavePos++; // wavePos isn't supposed to be > waveLen if (wavePos > waveLen) { wavePos -= waveLen; } } // Multiply sample with current TVA value sample *= amp; return sample; }
Tables::Tables() { int lf; for (lf = 0; lf <= 100; lf++) { // CONFIRMED:KG: This matches a ROM table found by Mok float fVal = (2.0f - LOG10F((float)lf + 1.0f)) * 128.0f; int val = (int)(fVal + 1.0); if (val > 255) { val = 255; } levelToAmpSubtraction[lf] = (Bit8u)val; } envLogarithmicTime[0] = 64; for (lf = 1; lf <= 255; lf++) { // CONFIRMED:KG: This matches a ROM table found by Mok envLogarithmicTime[lf] = (Bit8u)ceil(64.0f + LOG2F((float)lf) * 8.0f); } #ifdef EMULATE_LAPC_I // Dummy #ifdef - we'll have runtime emulation mode selection in future. // CONFIRMED: Based on a table found by Mok in the LAPC-I control ROM // Note that this matches the MT-32 table, but with the values clamped to a maximum of 8. memset(masterVolToAmpSubtraction, 8, 71); memset(masterVolToAmpSubtraction + 71, 7, 3); memset(masterVolToAmpSubtraction + 74, 6, 4); memset(masterVolToAmpSubtraction + 78, 5, 3); memset(masterVolToAmpSubtraction + 81, 4, 4); memset(masterVolToAmpSubtraction + 85, 3, 3); memset(masterVolToAmpSubtraction + 88, 2, 4); memset(masterVolToAmpSubtraction + 92, 1, 4); memset(masterVolToAmpSubtraction + 96, 0, 5); #else // CONFIRMED: Based on a table found by Mok in the MT-32 control ROM masterVolToAmpSubtraction[0] = 255; for (int masterVol = 1; masterVol <= 100; masterVol++) { masterVolToAmpSubtraction[masterVol] = (int)(106.31 - 16.0f * LOG2F((float)masterVol)); } #endif for (int i = 0; i <= 100; i++) { pulseWidth100To255[i] = (int)(i * 255 / 100.0f + 0.5f); //synth->printDebug("%d: %d", i, pulseWidth100To255[i]); } // The LA32 chip presumably has such a table inside as the internal computaions seem to be performed using fixed point math with 12-bit fractions for (int i = 0; i < 4096; i++) { exp2[i] = EXP2F(i / 4096.0f); } // found from sample analysis resAmpFadeFactor[7] = 1.0f / 8.0f; resAmpFadeFactor[6] = 2.0f / 8.0f; resAmpFadeFactor[5] = 3.0f / 8.0f; resAmpFadeFactor[4] = 5.0f / 8.0f; resAmpFadeFactor[3] = 8.0f / 8.0f; resAmpFadeFactor[2] = 12.0f / 8.0f; resAmpFadeFactor[1] = 16.0f / 8.0f; resAmpFadeFactor[0] = 31.0f / 8.0f; for (int i = 0; i < 5120; i++) { sinf10[i] = sin(FLOAT_PI * i / 2048.0f); } }
Tables::Tables() { for (int lf = 0; lf <= 100; lf++) { // CONFIRMED:KG: This matches a ROM table found by Mok float fVal = (2.0f - LOG10F(float(lf) + 1.0f)) * 128.0f; int val = int(fVal + 1.0); if (val > 255) { val = 255; } levelToAmpSubtraction[lf] = Bit8u(val); } envLogarithmicTime[0] = 64; for (int lf = 1; lf <= 255; lf++) { // CONFIRMED:KG: This matches a ROM table found by Mok envLogarithmicTime[lf] = Bit8u(ceil(64.0f + LOG2F(float(lf)) * 8.0f)); } #if 0 // The table below is to be used in conjunction with emulation of VCA of newer generation units which is currently missing. // These relatively small values are rather intended to fine-tune the overall amplification of the VCA. // CONFIRMED: Based on a table found by Mok in the LAPC-I control ROM // Note that this matches the MT-32 table, but with the values clamped to a maximum of 8. memset(masterVolToAmpSubtraction, 8, 71); memset(masterVolToAmpSubtraction + 71, 7, 3); memset(masterVolToAmpSubtraction + 74, 6, 4); memset(masterVolToAmpSubtraction + 78, 5, 3); memset(masterVolToAmpSubtraction + 81, 4, 4); memset(masterVolToAmpSubtraction + 85, 3, 3); memset(masterVolToAmpSubtraction + 88, 2, 4); memset(masterVolToAmpSubtraction + 92, 1, 4); memset(masterVolToAmpSubtraction + 96, 0, 5); #else // CONFIRMED: Based on a table found by Mok in the MT-32 control ROM masterVolToAmpSubtraction[0] = 255; for (int masterVol = 1; masterVol <= 100; masterVol++) { masterVolToAmpSubtraction[masterVol] = Bit8u(106.31 - 16.0f * LOG2F(float(masterVol))); } #endif for (int i = 0; i <= 100; i++) { pulseWidth100To255[i] = Bit8u(i * 255 / 100.0f + 0.5f); //synth->printDebug("%d: %d", i, pulseWidth100To255[i]); } // The LA32 chip contains an exponent table inside. The table contains 12-bit integer values. // The actual table size is 512 rows. The 9 higher bits of the fractional part of the argument are used as a lookup address. // To improve the precision of computations, the lower bits are supposed to be used for interpolation as the LA32 chip also // contains another 512-row table with inverted differences between the main table values. for (int i = 0; i < 512; i++) { exp9[i] = Bit16u(8191.5f - EXP2F(13.0f + ~i / 512.0f)); } // There is a logarithmic sine table inside the LA32 chip. The table contains 13-bit integer values. for (int i = 1; i < 512; i++) { logsin9[i] = Bit16u(0.5f - LOG2F(sin((i + 0.5f) / 1024.0f * FLOAT_PI)) * 1024.0f); } // The very first value is clamped to the maximum possible 13-bit integer logsin9[0] = 8191; // found from sample analysis static const Bit8u resAmpDecayFactorTable[] = {31, 16, 12, 8, 5, 3, 2, 1}; resAmpDecayFactor = resAmpDecayFactorTable; }