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