From e160337c4e45c53772f2c311aef1a7429e73ab51 Mon Sep 17 00:00:00 2001 From: John Glover Date: Thu, 23 Aug 2012 16:19:11 +0100 Subject: [sndobj] Fix bug in SndObjSynthesis. Partial IDs were not being handled correctly, added new class based on AdSyn that does not rely on IDs. --- src/sndobj/AdSyn.cpp | 95 ++++++++++++++++++++++++++++++++++++++++++--------- src/sndobj/AdSyn.h | 13 +++++-- src/sndobj/SinSyn.cpp | 37 +++++++------------- 3 files changed, 102 insertions(+), 43 deletions(-) (limited to 'src/sndobj') diff --git a/src/sndobj/AdSyn.cpp b/src/sndobj/AdSyn.cpp index 5b1a514..412a350 100644 --- a/src/sndobj/AdSyn.cpp +++ b/src/sndobj/AdSyn.cpp @@ -14,7 +14,7 @@ // // You should have received a copy of the GNU General Public License // along with this program; if not, write to the Free Software -// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA +// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA // // Copyright (c)Victor Lazzarini, 1997-2004 // See License.txt for a disclaimer of all warranties @@ -26,7 +26,7 @@ AdSyn::AdSyn(){ } AdSyn::AdSyn(SinAnal* input, int maxtracks, Table* table, - double pitch, double scale, int vecsize, double sr) + double pitch, double scale, int vecsize, double sr) :ReSyn(input, maxtracks, table, pitch, scale, 1.f, vecsize, sr){ } @@ -41,7 +41,7 @@ AdSyn::DoProcess(){ int notcontin = 0; bool contin = false; int oldtracks = m_tracks; - double* tab = m_ptable->GetTable(); + double* tab = m_ptable->GetTable(); if((m_tracks = ((SinAnal *)m_input)->GetTracks()) > m_maxtracks){ m_tracks = m_maxtracks; } @@ -52,16 +52,16 @@ AdSyn::DoProcess(){ while(i < m_tracks*3){ i3 = i/3; ampnext = m_input->Output(i)*m_scale; - freqnext = m_input->Output(i+1)*m_pitch; + freqnext = m_input->Output(i+1)*m_pitch; ID = ((SinAnal *)m_input)->GetTrackID(i3); - j = i3+notcontin; + j = i3+notcontin; if(i3 < oldtracks-notcontin){ - if(m_trackID[j]==ID){ - // if this is a continuing track + if(m_trackID[j]==ID){ + // if this is a continuing track track = j; - contin = true; + contin = true; freq = m_freqs[track]; phase = m_phases[track]; amp = m_amps[track]; @@ -72,11 +72,11 @@ AdSyn::DoProcess(){ track = j; freqnext = freq = m_freqs[track]; phase = m_phases[track]; - amp = m_amps[track]; + amp = m_amps[track]; ampnext = 0.f; } - } - else{ + } + else{ // new tracks contin = true; track = -1; @@ -93,7 +93,7 @@ AdSyn::DoProcess(){ incra = (ampnext - amp)/m_vecsize; incrph = (freqnext - freq)/m_vecsize; for(m_vecpos=0; m_vecpos < m_vecsize; m_vecpos++){ - if(m_enable) { + if(m_enable) { // table lookup oscillator phase += f*m_ratio; while(phase < 0) phase += m_size; @@ -104,7 +104,7 @@ AdSyn::DoProcess(){ a += incra; f += incrph; } - else m_output[m_vecpos] = 0.f; + else m_output[m_vecpos] = 0.f; } // keep amp, freq, and phase values for next time @@ -112,11 +112,74 @@ AdSyn::DoProcess(){ m_amps[i3] = ampnext; m_freqs[i3] = freqnext; m_phases[i3] = phase; - m_trackID[i3] = ID; + m_trackID[i3] = ID; i += 3; - } + } else notcontin++; - } + } + return 1; + } + else{ + m_error = 1; + return 0; + } +} + +SimplAdSyn::SimplAdSyn(){ +} + +SimplAdSyn::SimplAdSyn(SinAnal* input, int maxtracks, Table* table, + double pitch, double scale, int vecsize, double sr) + :AdSyn(input, maxtracks, table, pitch, scale, vecsize, sr){ +} + +SimplAdSyn::~SimplAdSyn(){ +} + +short +SimplAdSyn::DoProcess(){ + if(m_input){ + double ampnext, amp, freq, freqnext, phase; + double* tab = m_ptable->GetTable(); + if((m_tracks = ((SinAnal *)m_input)->GetTracks()) > m_maxtracks){ + m_tracks = m_maxtracks; + } + memset(m_output, 0, sizeof(double)*m_vecsize); + + for(int track = 0; track < m_tracks; track++){ + ampnext = m_input->Output(track * 3) * m_scale; + freqnext = m_input->Output((track * 3) + 1) * m_pitch; + freq = m_freqs[track]; + phase = m_phases[track]; + amp = m_amps[track]; + + // interpolation & track synthesis loop + double a, f, frac, incra, incrph; + int ndx; + a = amp; + f = freq; + incra = (ampnext - amp) / m_vecsize; + incrph = (freqnext - freq) / m_vecsize; + for(m_vecpos=0; m_vecpos < m_vecsize; m_vecpos++){ + if(m_enable) { + // table lookup oscillator + phase += f * m_ratio; + while(phase < 0) phase += m_size; + while(phase >= m_size) phase -= m_size; + ndx = Ftoi(phase); + frac = phase - ndx; + m_output[m_vecpos] += a*(tab[ndx] + (tab[ndx+1] - tab[ndx])*frac); + a += incra; + f += incrph; + } + else m_output[m_vecpos] = 0.f; + } + + // keep amp, freq, and phase values for next time + m_amps[track] = ampnext; + m_freqs[track] = freqnext; + m_phases[track] = phase; + } return 1; } else{ diff --git a/src/sndobj/AdSyn.h b/src/sndobj/AdSyn.h index 1d62d01..089e3b1 100644 --- a/src/sndobj/AdSyn.h +++ b/src/sndobj/AdSyn.h @@ -26,9 +26,7 @@ #include "ReSyn.h" class AdSyn : public ReSyn { - public: - AdSyn(); AdSyn(SinAnal* input, int maxtracks, Table* table, double pitch = 1.f, double scale=1.f, @@ -37,4 +35,15 @@ class AdSyn : public ReSyn { short DoProcess(); }; + +class SimplAdSyn : public AdSyn { + public: + SimplAdSyn(); + SimplAdSyn(SinAnal* input, int maxtracks, Table* table, + double pitch = 1.f, double scale=1.f, + int vecsize=DEF_VECSIZE, double sr=DEF_SR); + ~SimplAdSyn(); + short DoProcess(); +}; + #endif diff --git a/src/sndobj/SinSyn.cpp b/src/sndobj/SinSyn.cpp index 7d31e15..e91ed16 100644 --- a/src/sndobj/SinSyn.cpp +++ b/src/sndobj/SinSyn.cpp @@ -23,7 +23,6 @@ #include "SinSyn.h" SinSyn::SinSyn(){ - m_factor = m_vecsize/m_sr; m_facsqr = m_factor*m_factor; m_ptable = 0; @@ -45,7 +44,6 @@ SinSyn::SinSyn(){ SinSyn::SinSyn(SinAnal* input, int maxtracks, Table* table, double scale, int vecsize, double sr) :SndObj(input, vecsize, sr){ - m_ptable = table; m_size = m_ptable->GetLen(); m_LoTWOPI = m_size/TWOPI; @@ -61,7 +59,10 @@ SinSyn::SinSyn(SinAnal* input, int maxtracks, Table* table, m_phases = new double[m_maxtracks]; m_trackID = new int[m_maxtracks]; - memset(m_phases, 0, sizeof(double)*m_maxtracks); + memset(m_freqs, 0, sizeof(double) * m_maxtracks); + memset(m_amps, 0, sizeof(double) * m_maxtracks); + memset(m_phases, 0, sizeof(double) * m_maxtracks); + memset(m_trackID, 0, sizeof(int) * m_maxtracks); m_incr = 0.f; m_ratio = m_size/m_sr; @@ -70,21 +71,17 @@ SinSyn::SinSyn(SinAnal* input, int maxtracks, Table* table, AddMsg("table", 24); AddMsg("timescale", 24); memset(m_trackID, 0, sizeof(int)); - } SinSyn::~SinSyn(){ - delete[] m_freqs; delete[] m_amps; delete[] m_phases; delete[] m_trackID; - } - void -SinSyn::SetTable(Table *table) -{ +void +SinSyn::SetTable(Table *table){ m_ptable = table; m_size = m_ptable->GetLen(); m_LoTWOPI = m_size/TWOPI; @@ -93,26 +90,19 @@ SinSyn::SetTable(Table *table) int SinSyn::Connect(char* mess, void* input){ - switch (FindMsg(mess)){ - case 24: SetTable((Table *) input); return 1; default: return SndObj::Connect(mess,input); - } - } - int SinSyn::Set(char* mess, double value){ - switch(FindMsg(mess)){ - case 21: SetMaxTracks((int)value); return 1; @@ -123,36 +113,34 @@ SinSyn::Set(char* mess, double value){ default: return SndObj::Set(mess, value); - } } - void SinSyn::SetMaxTracks(int maxtracks){ - if(m_maxtracks){ - delete[] m_freqs; delete[] m_amps; delete[] m_phases; delete[] m_trackID; - } m_maxtracks = maxtracks; + m_freqs = new double[m_maxtracks]; m_amps = new double[m_maxtracks]; m_phases = new double[m_maxtracks]; m_trackID = new int[m_maxtracks]; + memset(m_freqs, 0, sizeof(double) * m_maxtracks); + memset(m_amps, 0, sizeof(double) * m_maxtracks); + memset(m_phases, 0, sizeof(double) * m_maxtracks); + memset(m_trackID, 0, sizeof(int) * m_maxtracks); } short -SinSyn::DoProcess() { - +SinSyn::DoProcess(){ if(m_input){ - double ampnext,amp,freq, freqnext, phase,phasenext; double a2, a3, phasediff, cph; int i3, i, j, ID, track; @@ -265,4 +253,3 @@ SinSyn::DoProcess() { } } - -- cgit v1.2.3