DOSBox-X
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Defines
src/mt32/DelayReverb.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 <cstring>
00020 #include "mt32emu.h"
00021 #include "DelayReverb.h"
00022 #include "FileStream.h"
00023 
00024 namespace MT32Emu {
00025 
00026 // CONFIRMED: The values below are found via analysis of digital samples and tracing reverb RAM address / data lines. Checked with all time and level combinations.
00027 // Obviously:
00028 // rightDelay = (leftDelay - 2) * 2 + 2
00029 // echoDelay = rightDelay - 1
00030 // Leaving these separate in case it's useful for work on other reverb modes...
00031 static const Bit32u REVERB_TIMINGS[8][3]= {
00032         // {leftDelay, rightDelay, feedbackDelay}
00033         {402, 802, 801},
00034         {626, 1250, 1249},
00035         {962, 1922, 1921},
00036         {1490, 2978, 2977},
00037         {2258, 4514, 4513},
00038         {3474, 6946, 6945},
00039         {5282, 10562, 10561},
00040         {8002, 16002, 16001}
00041 };
00042 
00043 // Reverb amp is found as dryAmp * wetAmp
00044 static const Bit32u REVERB_AMP[8] = {0x20*0x18, 0x50*0x18, 0x50*0x28, 0x50*0x40, 0x50*0x60, 0x50*0x80, 0x50*0xA8, 0x50*0xF8};
00045 static const Bit32u REVERB_FEEDBACK67 = 0x60;
00046 static const Bit32u REVERB_FEEDBACK = 0x68;
00047 static const float LPF_VALUE = 0x68 / 256.0f;
00048 
00049 static const Bit32u BUFFER_SIZE = 16384;
00050 
00051 DelayReverb::DelayReverb() {
00052         buf = NULL;
00053         setParameters(0, 0);
00054 
00055 
00056         // loadstate init
00057         //bufSize = 0;
00058 }
00059 
00060 DelayReverb::~DelayReverb() {
00061         delete[] buf;
00062 }
00063 
00064 void DelayReverb::open() {
00065         if (buf == NULL) {
00066                 delete[] buf;
00067 
00068                 buf = new float[BUFFER_SIZE];
00069 
00070                 recalcParameters();
00071 
00072                 // mute buffer
00073                 bufIx = 0;
00074                 if (buf != NULL) {
00075                         for (unsigned int i = 0; i < BUFFER_SIZE; i++) {
00076                                 buf[i] = 0.0f;
00077                         }
00078                 }
00079         }
00080 }
00081 
00082 void DelayReverb::close() {
00083         delete[] buf;
00084         buf = NULL;
00085 }
00086 
00087 // This method will always trigger a flush of the buffer
00088 void DelayReverb::setParameters(Bit8u newTime, Bit8u newLevel) {
00089         time = newTime;
00090         level = newLevel;
00091         recalcParameters();
00092 }
00093 
00094 void DelayReverb::recalcParameters() {
00095         // Number of samples between impulse and eventual appearance on the left channel
00096         delayLeft = REVERB_TIMINGS[time][0];
00097         // Number of samples between impulse and eventual appearance on the right channel
00098         delayRight = REVERB_TIMINGS[time][1];
00099         // Number of samples between a response and that response feeding back/echoing
00100         delayFeedback = REVERB_TIMINGS[time][2];
00101 
00102         if (level < 3 || time < 6) {
00103                 feedback = REVERB_FEEDBACK / 256.0f;
00104         } else {
00105                 feedback = REVERB_FEEDBACK67 / 256.0f;
00106         }
00107 
00108         // Overall output amp
00109         amp = (level == 0 && time == 0) ? 0.0f : REVERB_AMP[level] / 65536.0f;
00110 }
00111 
00112 void DelayReverb::process(const float *inLeft, const float *inRight, float *outLeft, float *outRight, unsigned long numSamples) {
00113         if (buf == NULL) return;
00114 
00115         for (unsigned int sampleIx = 0; sampleIx < numSamples; sampleIx++) {
00116                 // The ring buffer write index moves backwards; reads are all done with positive offsets.
00117                 Bit32u bufIxPrev = (bufIx + 1) % BUFFER_SIZE;
00118                 Bit32u bufIxLeft = (bufIx + delayLeft) % BUFFER_SIZE;
00119                 Bit32u bufIxRight = (bufIx + delayRight) % BUFFER_SIZE;
00120                 Bit32u bufIxFeedback = (bufIx + delayFeedback) % BUFFER_SIZE;
00121 
00122                 // Attenuated input samples and feedback response are directly added to the current ring buffer location
00123                 float lpfIn = amp * (inLeft[sampleIx] + inRight[sampleIx]) + feedback * buf[bufIxFeedback];
00124 
00125                 // Single-pole IIR filter found on real devices
00126                 buf[bufIx] = buf[bufIxPrev] * LPF_VALUE - lpfIn;
00127 
00128                 outLeft[sampleIx] = buf[bufIxLeft];
00129                 outRight[sampleIx] = buf[bufIxRight];
00130 
00131                 bufIx = (BUFFER_SIZE + bufIx - 1) % BUFFER_SIZE;
00132         }
00133 }
00134 
00135 bool DelayReverb::isActive() const {
00136         if (buf == NULL) return false;
00137 
00138         float *b = buf;
00139         float max = 0.001f;
00140         for (Bit32u i = 0; i < BUFFER_SIZE; i++) {
00141                 if ((*b < -max) || (*b > max)) return true;
00142                 b++;
00143         }
00144         return false;
00145 }
00146 
00147 
00148 void DelayReverb::saveState( std::ostream &stream )
00149 {
00150         stream.write(reinterpret_cast<const char*>(&time), sizeof(time) );
00151         stream.write(reinterpret_cast<const char*>(&level), sizeof(level) );
00152         //stream.write(reinterpret_cast<const char*>(&sampleRate), sizeof(sampleRate) );
00153         //stream.write(reinterpret_cast<const char*>(&bufSize), sizeof(bufSize) );
00154         stream.write(reinterpret_cast<const char*>(&bufIx), sizeof(bufIx) );
00155 
00156         if( buf )
00157                 stream.write(reinterpret_cast<const char*>(buf), sizeof(buf) * sizeof(float) );
00158 
00159         stream.write(reinterpret_cast<const char*>(&delayLeft), sizeof(delayLeft) );
00160         stream.write(reinterpret_cast<const char*>(&delayRight), sizeof(delayRight) );
00161         stream.write(reinterpret_cast<const char*>(&delayFeedback), sizeof(delayFeedback) );
00162         //stream.write(reinterpret_cast<const char*>(&fade), sizeof(fade) );
00163         stream.write(reinterpret_cast<const char*>(&feedback), sizeof(feedback) );
00164 }
00165 
00166 
00167 void DelayReverb::loadState( std::istream &stream )
00168 {
00169         stream.read(reinterpret_cast<char*>(&time), sizeof(time) );
00170         stream.read(reinterpret_cast<char*>(&level), sizeof(level) );
00171         //stream.read(reinterpret_cast<char*>(&sampleRate), sizeof(sampleRate) );
00172         //stream.read(reinterpret_cast<char*>(&bufSize), sizeof(bufSize) );
00173         stream.read(reinterpret_cast<char*>(&bufIx), sizeof(bufIx) );
00174 
00175 
00176         if( buf ) delete[] buf;
00177         if( sizeof(buf) == 0 ) {
00178                 buf = NULL;
00179         }
00180         else {
00181                 buf = new float[ sizeof(buf) ];
00182                 stream.read(reinterpret_cast<char*>(buf), sizeof(buf) * sizeof(float) );
00183         }
00184 
00185 
00186         stream.read(reinterpret_cast<char*>(&delayLeft), sizeof(delayLeft) );
00187         stream.read(reinterpret_cast<char*>(&delayRight), sizeof(delayRight) );
00188         stream.read(reinterpret_cast<char*>(&delayFeedback), sizeof(delayFeedback) );
00189         //stream.read(reinterpret_cast<char*>(&fade), sizeof(fade) );
00190         stream.read(reinterpret_cast<char*>(&feedback), sizeof(feedback) );
00191 }
00192 
00193 }