Marsyas  0.6.0-alpha
/usr/src/RPM/BUILD/marsyas-0.6.0/src/marsyas/marsystems/PeakSynthFFT.cpp
Go to the documentation of this file.
00001 /*
00002 ** Copyright (C) 1998-2006 George Tzanetakis <gtzan@cs.uvic.ca>
00003 **
00004 ** This program is free software; you can redistribute it and/or modify
00005 ** it under the terms of the GNU General Public License as published by
00006 ** the Free Software Foundation; either version 2 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 General Public License for more details.
00013 **
00014 ** You should have received a copy of the GNU General Public License
00015 ** along with this program; if not, write to the Free Software
00016 ** Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
00017 */
00018 
00019 #include "../common_source.h"
00020 #include "PeakSynthFFT.h"
00021 #include <marsyas/peakView.h>
00022 
00023 using std::ostringstream;
00024 using namespace Marsyas;
00025 
00026 //#define MTLB_DBG_LOG
00027 
00028 PeakSynthFFT::PeakSynthFFT(mrs_string name):MarSystem("PeakSynthFFT", name)
00029 {
00030 
00031   addControls();
00032 }
00033 
00034 PeakSynthFFT::PeakSynthFFT(const PeakSynthFFT& a) : MarSystem(a)
00035 {
00036   ctrl_Peaks_ = getctrl("mrs_realvec/peaks");
00037   ctrl_NbChannels_ = getctrl("mrs_natural/nbChannels");
00038 }
00039 
00040 PeakSynthFFT::~PeakSynthFFT()
00041 {
00042 }
00043 
00044 MarSystem*
00045 PeakSynthFFT::clone() const
00046 {
00047   return new PeakSynthFFT(*this);
00048 }
00049 
00050 void
00051 PeakSynthFFT::addControls()
00052 {
00053   addctrl("mrs_realvec/peaks", realvec(), ctrl_Peaks_);
00054   addctrl("mrs_natural/nbChannels", 1, ctrl_NbChannels_);
00055   setctrlState("mrs_natural/nbChannels", true);
00056   addctrl("mrs_string/panning", "MARSYAS_EMPTY");
00057   setctrlState("mrs_string/panning", true);
00058 }
00059 
00060 void
00061 PeakSynthFFT::myUpdate(MarControlPtr sender)
00062 {
00063   MarSystem::myUpdate(sender);
00064   setctrl("mrs_natural/onSamples", getctrl("mrs_natural/inSamples")->to<mrs_natural>()*getctrl("mrs_natural/nbChannels")->to<mrs_natural>());
00065 
00066   realvec conv(4);
00067   conv.setval(-1);
00068   if(getctrl("mrs_string/panning")->to<mrs_string>() != "MARSYAS_EMPTY")
00069   {
00070     string2parameters(getctrl("mrs_string/panning")->to<mrs_string>(), conv, '_');
00071   }
00072   fgVolume_ = conv(0);
00073   fgPanning_ = conv(1);
00074   bgVolume_ = conv(2);
00075   bgPanning_ = conv(3);
00076 
00077   mask_.create(getctrl("mrs_natural/inObservations")->to<mrs_natural>()/2+1);
00078   lastMask_.create(getctrl("mrs_natural/inObservations")->to<mrs_natural>()/2+1);
00079   lastMask_.setval(0);
00080 }
00081 
00082 void
00083 PeakSynthFFT::generateMask(mrs_natural type)
00084 {
00085   mrs_natural i, j;
00086   realvec peaks = ctrl_Peaks_->to<mrs_realvec>();
00087 
00088   //cout << peaks;
00089   mrs_natural nbPeaks = peaks.getSize()/peakView::nbPkParameters;
00090 
00091   // intialize background
00092   if(bgVolume_ != -1)
00093   {
00094     mask_.setval(bgVolume_);
00095     if(type == 1)
00096       mask_*=(1-bgPanning_)/2;
00097     if(type == 2)
00098       mask_*=(1+bgPanning_)/2;
00099   }
00100   else
00101     mask_.setval(0);
00102 
00103   // set level info for foreground clusters
00104   for (i=0 ; i<onObservations_/2 + 1; ++i)
00105   {
00106     for(j=0 ; j<nbPeaks; j++)
00107     {
00108       if(peaks(j+peakView::pkGroup*nbPeaks) <= -1)
00109         continue;
00110       if (i>=peaks(j+nbPeaks*peakView::pkBinLow)*onObservations_ && i<=peaks(j+nbPeaks*peakView::pkBinHigh)*onObservations_)
00111       {
00112         mrs_real vol, pan;
00113         if(fgVolume_ != -1)
00114         {
00115           // use global info
00116           vol = fgVolume_;
00117           pan = fgPanning_;
00118         }
00119         else
00120         {
00121           // use peaks info
00122           vol = peaks(j+nbPeaks*peakView::pkVolume);
00123           pan = peaks(j+nbPeaks*peakView::pkPan);
00124         }
00125 
00126         mask_(i) = vol;
00127         // left
00128         if(type == 1)
00129           mask_(i) *= (1-pan)/2;
00130         // right
00131         if(type == 2)
00132           mask_(i) *= (1+pan)/2;
00133         break;
00134       }
00135     }
00136   }
00137 //  cout << mask_;
00138 }
00139 
00140 void
00141 PeakSynthFFT::lpfMask()
00142 {
00143   mrs_real gain = 0.8, deltaGain=.3;
00144 
00145   for (mrs_natural i=0 ; i<mask_.getSize() ; ++i)
00146   {
00147     mrs_real g=gain-deltaGain*(mask_.getSize()-i)/mask_.getSize();
00148     mask_(i) = g*mask_(i)+(1-g)*lastMask_(i);
00149   }
00150   lastMask_ = mask_;
00151 }
00152 
00153 void
00154 PeakSynthFFT::myProcess(realvec& in, realvec& out)
00155 {
00156   mrs_natural nbChannels = ctrl_NbChannels_->to<mrs_natural>();
00157   mrs_natural t,o;
00158   //cout << ctrl_Peaks_->to<mrs_realvec>();
00159   for (t = 0; t < onSamples_; t++)
00160   {
00161     generateMask(t+(nbChannels-1));
00162 
00163 #ifdef MARSYAS_MATLAB
00164 #ifdef MTLB_DBG_LOG
00165     MATLAB_PUT(mask_, "mask");
00166     MATLAB_EVAL("figure(71),plot(mask),axis('tight'),grid on");
00167 #endif
00168 #endif
00169     lpfMask();
00170     for (o=0; o < onObservations_/2+1; o++)
00171     {
00172       //apply PeakSynthFFT to all channels
00173       out(o,t) = mask_(o) * in(o, 0);
00174     }
00175     for (o=onObservations_/2+1; o < onObservations_; o++)
00176     {
00177       //apply PeakSynthFFT to all channels
00178       out(o,t) = in(o, 0);
00179     }
00180   }
00181 //  cout << out;
00182 }
00183 
00184 
00185 
00186 
00187 
00188 
00189 
00190