Marsyas
0.6.0-alpha
|
00001 /* 00002 ** Copyright (C) 1998-2011 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 "BlitOsc.h" 00020 #include "math.h" 00021 00022 using namespace Marsyas; 00023 00024 BlitOsc::BlitOsc(mrs_string name):MarSystem("BlitOsc", name) 00025 { 00026 addControls(); 00027 } 00028 00029 BlitOsc::BlitOsc(const BlitOsc& a) : MarSystem(a) 00030 { 00031 // For any MarControlPtr in a MarSystem 00032 // it is necessary to perform this getctrl 00033 // in the copy constructor in order for cloning to work 00034 } 00035 00036 BlitOsc::~BlitOsc() 00037 { 00038 } 00039 00040 MarSystem* 00041 BlitOsc::clone() const 00042 { 00043 return new BlitOsc(*this); 00044 } 00045 00046 void BlitOsc::addControls() 00047 { 00048 //Add specific controls needed by this MarSystem. 00049 addctrl("mrs_real/frequency", 440.0); 00050 addctrl("mrs_bool/noteon", false); 00051 addctrl("mrs_natural/type", 0); 00052 00053 setctrlState("mrs_real/frequency", true); 00054 setctrlState("mrs_natural/type", true); 00055 setctrlState("mrs_bool/noteon", true); 00056 } 00057 00058 00059 void BlitOsc::myUpdate(MarControlPtr sender) 00060 { 00061 ap1.set_delay(1.9); 00062 ap2.set_delay(1.3); 00063 00064 frequency_ = (getctrl("mrs_real/frequency")->to<mrs_real>()); 00065 type_ = (getctrl("mrs_natural/type")->to<mrs_natural>()); 00066 noteon_ = (getctrl("mrs_bool/noteon")->to<mrs_bool>()); 00067 israte_ = (getctrl("mrs_real/israte")->to<mrs_real>()); 00068 00069 phase_ = 0; 00070 inv_ = 1; 00071 00072 switch (type_) 00073 { 00074 case 0: // Saw 00075 //std::cout << frequency_ << std::endl; 00076 dc_ = frequency_/israte_; 00077 break; 00078 case 1: // Square 00079 // The frequency has to be doubled to compensate for 00080 // the frequency be being halved by the square wave 00081 // being bipolar 00082 frequency_ *= 2; 00083 dc_ = 0; 00084 break; 00085 } 00086 00087 // d is how many samples to delay 00088 // because it is possible a fractional amount we split it 00089 // into the integer part, and the fractional part. 00090 mrs_real d = israte_/frequency_; 00091 // N_ is the integer part. 00092 N_ = (mrs_natural)floor(d); 00093 // frac_ is the fractional part 00094 frac_ = (d - N_); 00095 // delay is used as an overflow counter for the 00096 // fractional part. 00097 delay_ = frac_; 00098 00099 // no change to network flow 00100 MarSystem::myUpdate(sender); 00101 } 00102 00103 void BlitOsc::myProcess(realvec& in, realvec& out) 00104 { 00105 (void) in; 00106 for (mrs_natural t = 0; t < inSamples_; t++) 00107 { 00108 if (phase_ >= N_ - 1) 00109 { 00110 phase_ = 0; 00111 00112 // The amount of delay is incresed by 1 to optimize the allpass filter. 00113 // N_ is compensated accordingly for this delay at the top of this if 00114 // statement. 00115 ap1.set_delay(delay_ + 1); 00116 00117 switch (type_) 00118 { 00119 case 0: // Saw 00120 out(0,t) = le(ap2(ap1(1)) - dc_); 00121 break; 00122 case 1: // Square 00123 out(0,t) = le(ap2(ap1(1 * inv_))); 00124 inv_ = (-inv_); 00125 break; 00126 } 00127 00128 // This is the logic to tune the fractional part of the period. 00129 delay_ += frac_; 00130 if (delay_ >= 1) 00131 { 00132 delay_ -= 1; 00133 phase_ = -1; 00134 } 00135 } 00136 else 00137 { 00138 phase_++; 00139 out(0,t) = le(ap2(ap1(0)) - dc_); 00140 } 00141 } 00142 }