DOSBox-X
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Defines
src/mt32/FreeverbModel.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 "mt32emu.h"
00019 #include "FreeverbModel.h"
00020 #include "FileStream.h"
00021 
00022 #include "freeverb/revmodel.h"
00023 
00024 namespace MT32Emu {
00025 
00026 FreeverbModel::FreeverbModel(float useScaleTuning, float useFiltVal, float useWet, Bit8u useRoom, float useDamp) {
00027         freeverb = NULL;
00028         scaleTuning = useScaleTuning;
00029         filtVal = useFiltVal;
00030         wet = useWet;
00031         room = useRoom;
00032         damp = useDamp;
00033 }
00034 
00035 FreeverbModel::~FreeverbModel() {
00036         delete freeverb;
00037 }
00038 
00039 void FreeverbModel::open() {
00040         if (freeverb == NULL) {
00041                 freeverb = new revmodel(scaleTuning);
00042         }
00043         freeverb->mute();
00044 
00045         // entrance Lowpass filter factor
00046         freeverb->setfiltval(filtVal);
00047 
00048         // decay speed of high frequencies in the wet signal
00049         freeverb->setdamp(damp);
00050 }
00051 
00052 void FreeverbModel::close() {
00053         delete freeverb;
00054         freeverb = NULL;
00055 }
00056 
00057 void FreeverbModel::process(const float *inLeft, const float *inRight, float *outLeft, float *outRight, unsigned long numSamples) {
00058         freeverb->process(inLeft, inRight, outLeft, outRight, numSamples);
00059 }
00060 
00061 void FreeverbModel::setParameters(Bit8u time, Bit8u level) {
00062         // wet signal level
00063         // FIXME: need to implement some sort of reverb level ramping
00064         freeverb->setwet((float)level / 7.0f * wet);
00065 
00066         // wet signal decay speed
00067         static float roomTable[] = {
00068                  0.25f,  0.37f, 0.54f, 0.71f, 0.78f, 0.86f, 0.93f, 1.00f,
00069                 -1.00f, -0.50f, 0.00f, 0.30f, 0.51f, 0.64f, 0.77f, 0.90f,
00070                  0.50f,  0.57f, 0.70f, 0.77f, 0.85f, 0.93f, 0.96f, 1.01f};
00071         freeverb->setroomsize(roomTable[8 * room + time]);
00072 }
00073 
00074 bool FreeverbModel::isActive() const {
00075         // FIXME: Not bothering to do this properly since we'll be replacing Freeverb soon...
00076         return false;
00077 }
00078 
00079 
00080 void FreeverbModel::saveState( std::ostream &stream ) {
00081         Bit8u ptr_valid;
00082 
00083 
00084         if( freeverb == NULL ) {
00085                 ptr_valid = 0xff;
00086                 stream.write(reinterpret_cast<const char*>(&ptr_valid), sizeof(ptr_valid) );
00087         }
00088         else {
00089                 ptr_valid = 0;
00090                 stream.write(reinterpret_cast<const char*>(&ptr_valid), sizeof(ptr_valid) );
00091 
00092                 freeverb->saveState( stream );
00093         }
00094 
00095 
00096         stream.write(reinterpret_cast<const char*>(&scaleTuning), sizeof(scaleTuning) );
00097         stream.write(reinterpret_cast<const char*>(&filtVal), sizeof(filtVal) );
00098         stream.write(reinterpret_cast<const char*>(&wet), sizeof(wet) );
00099         stream.write(reinterpret_cast<const char*>(&room), sizeof(room) );
00100         stream.write(reinterpret_cast<const char*>(&damp), sizeof(damp) );
00101 }
00102 
00103 
00104 void FreeverbModel::loadState( std::istream &stream ) {
00105         Bit8u ptr_valid;
00106 
00107 
00108         stream.read(reinterpret_cast<char*>(&ptr_valid), sizeof(ptr_valid) );
00109 
00110 
00111         if( ptr_valid == 0xff ) {
00112                 if( freeverb ) delete freeverb;
00113                 freeverb = NULL;
00114         }
00115         else {
00116                 if( freeverb == NULL ) freeverb = new revmodel(scaleTuning);
00117                 freeverb->loadState( stream );
00118         }
00119 
00120 
00121         stream.read(reinterpret_cast<char*>(&scaleTuning), sizeof(scaleTuning) );
00122         stream.read(reinterpret_cast<char*>(&filtVal), sizeof(filtVal) );
00123         stream.read(reinterpret_cast<char*>(&wet), sizeof(wet) );
00124         stream.read(reinterpret_cast<char*>(&room), sizeof(room) );
00125         stream.read(reinterpret_cast<char*>(&damp), sizeof(damp) );
00126 }
00127 
00128 }