Marsyas  0.6.0-alpha
/usr/src/RPM/BUILD/marsyas-0.6.0/src/marsyas/marsystems/PeakSynthOsc.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 "PeakSynthOsc.h"
00020 #include <marsyas/peakView.h>
00021 
00022 using std::ostringstream;
00023 using namespace Marsyas;
00024 
00025 PeakSynthOsc::PeakSynthOsc(mrs_string name):MarSystem("PeakSynthOsc",name)
00026 {
00027   addControls();
00028 }
00029 
00030 PeakSynthOsc::PeakSynthOsc(const PeakSynthOsc& a):MarSystem(a)
00031 {
00032   ctrl_samplingFreq_ = getctrl("mrs_real/samplingFreq");
00033   ctrl_peakGroup2Synth_ = getctrl("mrs_natural/peakGroup2Synth");
00034   ctrl_isSilence_ = getctrl("mrs_bool/isSilence");
00035   ctrl_synSize_ = getctrl("mrs_natural/synSize");
00036   ctrl_delay_ = getctrl("mrs_natural/delay");
00037   ctrl_harmonize_ = getctrl("mrs_realvec/harmonize");
00038 }
00039 
00040 PeakSynthOsc::~PeakSynthOsc()
00041 {
00042 }
00043 
00044 MarSystem*
00045 PeakSynthOsc::clone() const
00046 {
00047   return new PeakSynthOsc(*this);
00048 }
00049 
00050 void
00051 PeakSynthOsc::addControls()
00052 {
00053   addctrl("mrs_real/samplingFreq", MRS_DEFAULT_SLICE_SRATE, ctrl_samplingFreq_);
00054   ctrl_samplingFreq_->setState(true);
00055 
00056   addctrl("mrs_natural/peakGroup2Synth", 0, ctrl_peakGroup2Synth_);
00057   addctrl("mrs_bool/isSilence", true, ctrl_isSilence_);
00058 
00059   addctrl("mrs_natural/synSize", MRS_DEFAULT_SLICE_NSAMPLES, ctrl_synSize_);
00060   ctrl_synSize_->setState(true);
00061 
00062   addctrl("mrs_natural/delay", 0, ctrl_delay_);
00063   ctrl_delay_->setState(true);
00064 
00065   addctrl("mrs_realvec/harmonize", realvec(), ctrl_harmonize_);
00066 }
00067 
00068 void
00069 PeakSynthOsc::myUpdate(MarControlPtr sender)
00070 {
00071   (void) sender;  //suppress warning of unused parameter(s)
00072   ctrl_onSamples_->setValue(ctrl_synSize_, NOUPDATE);
00073   ctrl_onObservations_->setValue(1, NOUPDATE);
00074   ctrl_osrate_->setValue(ctrl_samplingFreq_, NOUPDATE);
00075   ctrl_onObsNames_->setValue("audio,", NOUPDATE);
00076 
00077   delay_ = ctrl_delay_->to<mrs_natural>();
00078   factor_ = TWOPI/ctrl_samplingFreq_->to<mrs_real>();
00079 }
00080 
00081 void
00082 PeakSynthOsc::sine(realvec& out, mrs_real f, mrs_real a, mrs_real p)
00083 {
00084   if(f > 0.0 && a > 0.0)
00085   {
00086     for (mrs_natural i=0 ; i < onSamples_ ; ++i)
00087       out(i) += a*cos(factor_*f*(i-delay_)+p); // consider -fftSize/2 for synth in phase
00088   }
00089 }
00090 
00091 void
00092 PeakSynthOsc::myProcess(realvec& in, realvec& out)
00093 {
00094   out.setval(0);
00095   silence_ = true;
00096 
00097   pkGrp2Synth_ = ctrl_peakGroup2Synth_->to<mrs_natural>();
00098   Nb_ = in.getSize()/peakView::nbPkParameters ; //number of peaks in the current frame
00099   nbH_ = ctrl_harmonize_->to<mrs_realvec>().getSize();
00100 
00101   if(nbH_)
00102     for(mrs_natural j=0 ; j<(nbH_-1)/2 ; j++)
00103     {
00104       mulF_ = ctrl_harmonize_->to<mrs_realvec>()(1+j*2);
00105       mulA_ = ctrl_harmonize_->to<mrs_realvec>()(2+j*2);
00106       //cout << "mulF_" << mulF_ << "mulA_" << mulA_ << endl;
00107       for (mrs_natural i=0; i < Nb_; ++i)
00108       {
00109         // either synthesize peaks with a corresponding GroupID or all with a group ID >= 0
00110         mrs_bool synthMe = (pkGrp2Synth_ < 0)? (in(i+peakView::pkGroup*Nb_) >= 0) : (in(i+peakView::pkGroup*Nb_) == pkGrp2Synth_);
00111         if( synthMe )
00112         {
00113           sine(out, in(i)*mulF_, in(i+Nb_)*mulA_, in(i+2*Nb_));
00114           silence_ = false;
00115         }
00116       }
00117     }
00118   else
00119     for (mrs_natural i=0; i < Nb_; ++i)
00120     {
00121       // either synthesize peaks with a corresponding GroupID or all with a group ID >= 0
00122       mrs_bool synthMe = (pkGrp2Synth_ < 0)? (in(i+peakView::pkGroup*Nb_) >= 0) : (in(i+peakView::pkGroup*Nb_) == pkGrp2Synth_);
00123       if( synthMe )
00124       {
00125         sine(out, in(i), in(i+Nb_), in(i+2*Nb_));
00126         silence_ = false;
00127       }
00128     }
00129 
00130   //signal if at least one peak was synthesized or not
00131   ctrl_isSilence_->setValue(silence_);
00132 }