File: SinSyn.cpp

package info (click to toggle)
sndobj 2.6.6.1-3
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd, wheezy
  • size: 4,368 kB
  • ctags: 5,467
  • sloc: ansic: 55,155; cpp: 19,258; python: 348; makefile: 125
file content (270 lines) | stat: -rw-r--r-- 5,945 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
 
////////////////////////////////////////////////////////////////////////
// This file is part of the SndObj library
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// 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 
//
// Copyright (c)Victor Lazzarini, 1997-2004
// See License.txt for a disclaimer of all warranties
// and licensing information

#include "SinSyn.h"

#include <cstring>

SinSyn::SinSyn(){

  m_factor = m_vecsize/m_sr;
  m_facsqr = m_factor*m_factor;
  m_ptable = 0;
  m_size = 0;
  m_LoTWOPI = 0.f;
  m_maxtracks = 0;
  m_freqs = 0;
  m_amps = 0;
  m_phases = 0;
  m_trackID = 0;
  m_scale =  0.f;
  m_ratio = 0.f;
  m_tracks = 0;
  AddMsg("max tracks", 21);
  AddMsg("scale", 23);
  AddMsg("table", 24);
}

SinSyn::SinSyn(SinAnal* input, int maxtracks, Table* table, 
	       float scale, int vecsize, float sr)	  
  :SndObj(input, vecsize, sr){

  m_ptable = table;
  m_size = m_ptable->GetLen();
  m_LoTWOPI = m_size/TWOPI;

  m_factor = m_vecsize/m_sr;
  m_facsqr = m_factor*m_factor;
  m_maxtracks = maxtracks;
  m_tracks = 0;
  m_scale = scale;
  m_input = input;
  m_freqs = new float[m_maxtracks];
  m_amps = new float[m_maxtracks];
  m_phases = new float[m_maxtracks];
  m_trackID = new int[m_maxtracks];

  memset(m_phases, 0, sizeof(float)*m_maxtracks);

  m_incr = 0.f;
  m_ratio = m_size/m_sr;
  AddMsg("max tracks", 21);
  AddMsg("scale", 23);
  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)
{
  m_ptable = table;
  m_size = m_ptable->GetLen();
  m_LoTWOPI = m_size/TWOPI;
  m_ratio = m_size/m_sr;
}

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, float value){

  switch(FindMsg(mess)){

  case 21:
    SetMaxTracks((int)value);
    return 1;

  case 23:
    SetScale(value);
    return 1;
	
  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 float[m_maxtracks];
  m_amps = new float[m_maxtracks];
  m_phases = new float[m_maxtracks];
  m_trackID = new int[m_maxtracks];

}

short
SinSyn::DoProcess() {
	
  if(m_input){
		
    float ampnext,amp,freq, freqnext, phase,phasenext;
    float a2, a3, phasediff, cph;
    int i3, i, j, ID, track;
    int notcontin = 0;
    bool contin = false;
    int oldtracks = m_tracks;
    float* tab = m_ptable->GetTable(); 
    if((m_tracks = ((SinAnal *)m_input)->GetTracks()) >
       m_maxtracks) m_tracks = m_maxtracks;
		
    memset(m_output, 0, sizeof(float)*m_vecsize);
   		
    // for each track
    i = j = 0;
    while(i < m_tracks*3){			
      i3 = i/3;
      ampnext =  m_input->Output(i)*m_scale;
      freqnext = m_input->Output(i+1)*TWOPI; 
      phasenext = m_input->Output(i+2);
      ID = ((SinAnal *)m_input)->GetTrackID(i3);

      j = i3+notcontin;
	
      if(i3 < oldtracks-notcontin){
          
	if(m_trackID[j]==ID){	
	  // if this is a continuing track  	
	  track = j;
	  contin = true;	
	  freq = m_freqs[track];
	  phase = m_phases[track];
	  amp = m_amps[track];
					
	}
	else {
	  // if this is  a dead track
	  contin = false;
	  track = j;
	  freqnext = freq = m_freqs[track];
	  phase = m_phases[track];
	  phasenext = phase + freq*m_factor;
	  amp = m_amps[track]; 
	  ampnext = 0.f;
	}
      }
			
      else{ 
	// new tracks
	contin = true;
	track = -1;
	freq = freqnext;
	phase = phasenext - freq*m_factor;
	amp = 0.f;
      }
			
      // phasediff
      phasediff = phasenext - phase;		
      while(phasediff >= PI) phasediff -= TWOPI;
      while(phasediff < -PI) phasediff += TWOPI;
      // update phasediff to match the freq
      cph = ((freq+freqnext)*m_factor/2. - phasediff)/TWOPI;
      phasediff += TWOPI * Ftoi(cph + 0.5);
      // interpolation coefs
      a2 = 3./m_facsqr * (phasediff - m_factor/3.*(2*freq+freqnext));
      a3 = 1./(3*m_facsqr)  * (freqnext - freq - 2*a2*m_factor);

      // interpolation resynthesis loop	
      float inc1, inc2, a, ph, cnt, frac;
      int ndx;
      a = amp;
      ph = phase;
      cnt = 0;
      inc1 = (ampnext - amp)/m_vecsize;
      inc2 = 1/m_sr;  
      for(m_vecpos=0; m_vecpos < m_vecsize; m_vecpos++){
		
	if(m_enable) {    
 	  // table lookup oscillator
	  ph *= m_LoTWOPI;
	  while(ph < 0) ph += m_size;  
	  while(ph >= m_size) ph -= m_size;
	  ndx = Ftoi(ph);
	  frac = ph - ndx;
	  m_output[m_vecpos] += a*(tab[ndx] + (tab[ndx+1] - tab[ndx])*frac); 
	  a += inc1;
	  cnt += inc2;
	  ph = phase + cnt*(freq + cnt*(a2 + a3*cnt));
					
	}
	else m_output[m_vecpos] = 0.f;		
      }
     
      // keep amp, freq, and phase values for next time
      if(contin){
	m_amps[i3] = ampnext;
	m_freqs[i3] = freqnext;
	while(phasenext < 0) phasenext += TWOPI;
	while(phasenext >= TWOPI) phasenext -= TWOPI;
	m_phases[i3] = phasenext;
	m_trackID[i3] = ID;    
	i += 3;
      } else notcontin++;
      } 
   
    return 1;
  }
  else {
    m_error  = 1;
    return 0;
  }
 
}