File: SyncGrain.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 (319 lines) | stat: -rw-r--r-- 7,512 bytes parent folder | download | duplicates (5)
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
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
 
////////////////////////////////////////////////////////////////////////
// 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

//////////////////////////////////////////////////
// SyncGrain implementation      
// VL, 2002
//
//////////////////////////////////////////////////

#include "SyncGrain.h"

SyncGrain::SyncGrain(){

  m_table = 0;       // wavetable
  m_envtable = 0;    // envelope table

  m_amp = 1.f;    // overall amp
  m_inputamp =0;
  m_fr = 440.f;     // fundamental freq
  m_inputfr = 0;
  m_pitch = 1.f;  // grain pitch
  m_inputpitch = 0;
  m_grsize = 0.f; // size of grains (msecs)
  m_inputgrsize = 0;
  m_olaps = 100;  // max number of streams (overlaps)

  m_point = 0;

  if(!(m_index = new double[m_olaps])){
    m_error = 11;
    return;  // index into wavetable
  }
  if(!(m_envindex = new double[m_olaps])){
    m_error = 11;
    return;
  } // index into envtable
  if(!(m_streamon = new short[m_olaps])){
    m_error = 11;
    return;
  }

  m_count = 0;    // sampling period counter
  m_numstreams = 0;  // curr num of streams
  m_firststream = 0; // streams index (first stream) 
  m_tablesize = 0; // size of wavetable
  m_envtablesize = 0; // size of envtable
   
  for(int i = 0; i < m_olaps; i++) {
    m_streamon[i] = 0;
    m_index[i] = m_envindex[i] = 0.f;
  }
  m_start = 0.f;
  m_frac = 0.f;
  AddMsg("frequency", 21);
  AddMsg("grain size", 22);
  AddMsg("grain pitch", 23);
  AddMsg("pointer rate", 24);
  AddMsg("amplitude", 25);
  AddMsg("source table", 26);
  AddMsg("envelope table", 27);
}

SyncGrain::SyncGrain(Table* wavetable, Table* envtable, float fr, float amp,
		     float pitch, float grsize, float prate, SndObj* inputfr, 
		     SndObj* inputamp, SndObj* inputpitch, 
		     SndObj* inputgrsize, int olaps,
		     int vecsize, float sr):
  SndObj(inputfr, vecsize, sr){

  m_table = wavetable;       // wavetable
  m_envtable = envtable;    // envelope table

  m_amp = amp;    // overall amp
  m_inputamp = inputamp;
  m_fr = fr;     // fundamental freq
  m_inputfr = inputfr;
  m_pitch = pitch;  // grain pitch
  m_inputpitch = inputpitch;
  m_grsize = grsize; // size of grains (msecs)
  m_inputgrsize = inputgrsize;
  m_olaps = olaps;  // max number of streams (overlaps)

  if(!(m_index = new double[m_olaps])){
    m_error = 11;
    return;  // index into wavetable
  }
  if(!(m_envindex = new double[m_olaps])){
    m_error = 11;
    return;
  } // index into envtable
  if(!(m_streamon = new short[m_olaps])){
    m_error = 11;
    return;
  }

  m_count = 0xFFFFFFFF;    // sampling period counter
  m_numstreams = 0;  // curr num of streams
  m_firststream = 0; // streams index (first stream) 
  m_tablesize = m_table->GetLen(); // size of wavetable
  m_envtablesize = m_envtable->GetLen(); // size of envtable

  for(int i = 0; i < olaps; i++){
    m_streamon[i] = 0;
    m_index[i] = m_envindex[i] = 0.;
  }
  m_start = 0.f;
  m_point = prate;
  m_frac = 0.f;

  AddMsg("frequency", 21);
  AddMsg("grain size", 22);
  AddMsg("grain pitch", 23);
  AddMsg("pointer rate", 24);
  AddMsg("amplitude", 25);
  AddMsg("source table", 26);
  AddMsg("envelope table", 27);
}

SyncGrain::~SyncGrain(){

  // perform any necessary de-allocation etc
  // here
  delete[] m_index;
  delete[] m_envindex;
  delete[] m_streamon;

}

int
SyncGrain::Set(char* mess, float value){

  switch (FindMsg(mess)){

  case 21:
    SetFreq(value);
    return 1;

  case 22:
    SetGrainSize(value);
    return 1;

  case 23:
    SetPitch(value);
    return 1;

  case 24:
    SetPointerRate(value);
    return 1;

  case 25:
    SetAmp(value);
    return 1;

  default:
    return SndObj::Set(mess,value);
     
  }


}



int
SyncGrain::Connect(char* mess, void* input){

  switch (FindMsg(mess)){

  case 21:
    m_inputfr = (SndObj *) input;
    return 1;

  case 22:
    m_inputgrsize = (SndObj *) input; 
    return 1;

  case 23:
    m_inputpitch =  (SndObj *) input;
    return 1;

  case 25:
    m_inputamp = (SndObj *) input;
    return 1;

  case 26:
    m_table = (Table *) input;
    return 1;

  case 27:
    m_envtable = (Table *) input;
    return 1;

  default:
    return SndObj::Connect(mess, input);
     
  }


}

short
SyncGrain::DoProcess(){

  if(!m_error){

    float sig, pitch, amp, grsize, envincr, period;
    for(m_vecpos = 0; m_vecpos < m_vecsize; m_vecpos++) {
      if(m_enable) {
        
	// set the control parameters 
	// (amp, fund period, grain pitch and size, in samples)
	sig = 0.f; 
	pitch  = m_pitch + (m_inputpitch ? m_inputpitch->Output(m_vecpos) : 0); 
	period = m_frac + m_sr/(m_fr + (m_inputfr ? m_inputfr->Output(m_vecpos) : 0));
	amp = m_amp + (m_inputamp ? m_inputamp->Output(m_vecpos) : 0); 
	grsize =  (m_sr *
		   (m_grsize + (m_inputgrsize ? m_inputgrsize->Output(m_vecpos) : 0)));
	envincr = m_envtablesize/grsize;
   

	// if a grain has finished, clean up
	if((!m_streamon[m_firststream]) && (m_numstreams) ){
	  m_numstreams--; // decrease the no of streams
	  m_firststream=(m_firststream+1)%m_olaps; // first stream is the next
	}  
  
	// if a fund period has elapsed
	// start a new grain
	if(m_count >= period){
	  m_frac = m_count - period; // frac part to be accummulated
	  int newstream =(m_firststream+m_numstreams)%m_olaps;
	  m_streamon[newstream] = 1; // turn the stream on
	  m_envindex[newstream] = 0.f;   
	  m_index[newstream] = m_start;
	  m_numstreams++; // increase the stream count
	  m_count = 0;  
	  m_start += m_point*grsize;
	  while (m_start > m_tablesize) m_start-=m_tablesize;
          while (m_start < 0) m_start += m_tablesize;
	}
			 
	for(int i=m_numstreams, 
	      j=m_firststream; i; i--, j=(j+1)%m_olaps){
              	   
	  // modulus
	  while(m_index[j] > m_tablesize) 
	    m_index[j] -= m_tablesize;
	  while(m_index[j] < 0)
	    m_index[j] += m_tablesize;

	  // sum all the grain streams
	  sig += m_table->GetTable()[Ftoi(m_index[j])]
	    *m_envtable->GetTable()[Ftoi(m_envindex[j])];

	  // increment the indexes
	  // for each grain
	  m_index[j] += pitch;
	  m_envindex[j] += envincr;
			  
	  // if the envelope is finished
	  // the grain is also finished

	  if(m_envindex[j] > m_envtablesize)			  
	    m_streamon[j] = 0;
	}

        // increment the period counter
        m_count++; 
	// scale the output
        m_output[m_vecpos] = sig*amp;
			
      }
      else m_output[m_vecpos] = 0.f;
    } 
    return 1; 
  }
  else return 0;
}

char*
SyncGrain::ErrorMessage(){
 
  char* message;
   
  switch(m_error){

    // handle your error codes here 
  case 11:
    message = "Memory allocation error\n";
    break;

  default:
    message = SndObj::ErrorMessage();
    break;
  
  }

  return message;

}