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
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
|
//
//
// chaos~
// Copyright (C) 2004 Tim Blechmann
//
// 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; see the file COPYING. If not, write to
// the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
// Boston, MA 02111-1307, USA.
#include "chaos_base.hpp"
template <class system> class chaos_dsp
: public flext_dsp
{
FLEXT_HEADER(chaos_dsp, flext_dsp);
public:
/* signal functions: */
/* for frequency = sr/2 */
void m_signal_(int n, t_sample *const *insigs,t_sample *const *outsigs);
/* sample & hold */
void m_signal_n(int n, t_sample *const *insigs,t_sample *const *outsigs);
/* linear interpolation */
void m_signal_l(int n, t_sample *const *insigs,t_sample *const *outsigs);
/* cubic interpolatio */
void m_signal_c(int n, t_sample *const *insigs,t_sample *const *outsigs);
virtual void m_signal(int n, t_sample *const *insigs,t_sample *const *outsigs)
{
(this->*m_routine)(n,insigs,outsigs);
}
virtual void m_dsp(int n, t_sample *const *insigs,t_sample *const *outsigs)
{
m_sr = Samplerate();
}
void (thisType::*m_routine)(int n, t_sample *const *insigs,t_sample *const *outsigs);
/* local data for system, output and interpolation */
system * m_system; /* the system */
t_sample * m_values; /* actual value */
t_sample * m_slopes; /* actual slope for cubic interpolation */
t_sample * m_nextvalues;
t_sample * m_nextmidpts;
t_sample * m_curves;
/* local data for signal functions */
float m_freq; /* frequency of oscillations */
float m_invfreq; /* inverse frequency */
int m_phase; /* phase counter */
float m_sr; /* sample rate */
int m_imethod; /* interpolation method */
void get_imethod(int &i)
{
i = m_imethod;
}
void set_imethod(int i)
{
int imethod = m_imethod;
if( (i >= 0) && (i <= 2) )
{
m_imethod = i;
switch (i)
{
case 0:
m_routine = &thisType::m_signal_n;
break;
case 1:
m_routine = &thisType::m_signal_l;
break;
case 2:
m_routine = &thisType::m_signal_c;
break;
}
}
else
{
post("interpolation method out of range");
return;
}
if (imethod == 0)
for (int j = 0; j != m_system->get_num_eq(); ++j)
{
m_values[j] = m_system->get_data(j);
m_slopes[j] = 0;
}
if( i == 2)
{
for (int j = 0; j != m_system->get_num_eq(); ++j)
{
m_phase = 0; /* reschedule to avoid click, find a better way later*/
m_nextvalues[j] = m_values[j];
m_nextmidpts[j] = m_values[j];
}
}
}
void get_freq(float &f)
{
f = m_freq;
}
void set_freq(float f)
{
if( (f >= 0) && (f <= m_sr*0.5) )
{
m_freq = f;
m_invfreq = 1.f / f;
}
else
post("frequency out of range");
}
FLEXT_CALLVAR_F(get_freq, set_freq);
FLEXT_CALLVAR_I(get_imethod, set_imethod);
};
/* create constructor / destructor */
#define CHAOS_DSP_INIT(SYSTEM, ATTRIBUTES) \
FLEXT_HEADER(SYSTEM##_dsp, chaos_dsp<SYSTEM>) \
\
SYSTEM##_dsp(int argc, t_atom* argv ) \
{ \
m_sr = 44100; /* assume default sampling rate */ \
m_system = new SYSTEM; \
\
int size = m_system->get_num_eq(); \
\
m_values = new t_float[size]; \
m_slopes = new t_float[size]; \
m_nextvalues = new t_float[size]; \
m_nextmidpts = new t_float[size]; \
m_curves = new t_float[size]; \
\
/* create inlets and zero arrays*/ \
for (int i = 0; i != size; ++i) \
{ \
AddOutSignal(); \
m_values[i] = 0; \
m_slopes[i] = 0; \
m_nextvalues[i] = 0; \
m_nextmidpts[i] = 0; \
m_curves[i] = 0; \
} \
\
FLEXT_ADDATTR_VAR("frequency", get_freq, set_freq); \
FLEXT_ADDATTR_VAR("interpolation_method",get_imethod, set_imethod); \
\
if (argc > 0) \
{ \
CHAOS_SYS_INIT(freq, GetAInt(argv[0])); \
} \
else \
{ \
CHAOS_SYS_INIT(freq, 440); \
} \
\
if (argc > 1) \
{ \
CHAOS_SYS_INIT(imethod, GetAInt(argv[1])); \
} \
else \
{ \
CHAOS_SYS_INIT(imethod, 0); \
} \
\
m_phase = 0; \
\
ATTRIBUTES; \
} \
\
~SYSTEM##_dsp() \
{ \
delete m_system; \
delete m_values; \
delete m_slopes; \
delete m_nextvalues; \
delete m_nextmidpts; \
delete m_curves; \
} \
\
FLEXT_ATTRVAR_F(m_freq); \
FLEXT_ATTRVAR_I(m_imethod);
template <class system>
void chaos_dsp<system>::m_signal_(int n, t_sample *const *insigs,
t_sample *const *outsigs)
{
int outlets = m_system->get_num_eq();
for (int i = 0; i!=n; ++i)
{
m_system->m_perform();
for (int j = 0; j != outlets; ++j)
{
outsigs[j][i] = m_system->get_data(j);
}
}
}
template <class system>
void chaos_dsp<system>::m_signal_n(int n, t_sample *const *insigs,
t_sample *const *outsigs)
{
int outlets = m_system->get_num_eq();
int phase = m_phase;
int offset = 0;
while (n)
{
if (phase == 0)
{
m_system->m_perform();
phase = int (m_sr * m_invfreq);
}
int next = (phase < n) ? phase : n;
n -= next;
phase -=next;
for (int i = 0; i != outlets; ++i)
{
SetSamples(outsigs[i]+offset, next, m_system->get_data(i));
}
offset += next;
}
m_phase = phase;
}
/* linear and cubic interpolation adapted from supercollider by James McCartney */
template <class system>
void chaos_dsp<system>::m_signal_l(int n, t_sample *const *insigs,
t_sample *const *outsigs)
{
int outlets = m_system->get_num_eq();
int phase = m_phase;
int i = 0;
while (n)
{
if (phase == 0)
{
m_system->m_perform();
phase = int (m_sr * m_invfreq);
for (int j = 0; j != outlets; ++j)
m_slopes[j] = (m_system->get_data(j) - m_values[j]) / phase;
}
int next = (phase < n) ? phase : n;
n -= next;
phase -=next;
while (next--)
{
for (int j = 0; j != outlets; ++j)
{
outsigs[j][i] = m_values[j];
m_values[j]+=m_slopes[j];
}
++i;
}
}
m_phase = phase;
}
template <class system>
void chaos_dsp<system>::m_signal_c(int n, t_sample *const *insigs,
t_sample *const *outsigs)
{
int outlets = m_system->get_num_eq();
int phase = m_phase;
int i = 0;
while (n)
{
if (phase == 0)
{
m_system->m_perform();
phase = int (m_sr * m_invfreq);
phase = (phase > 2) ? phase : 2;
for (int j = 0; j != outlets; ++j)
{
t_sample value = m_nextvalues[j];
m_nextvalues[j]= m_system->get_data(j);
m_values[j] = m_nextmidpts[j];
m_nextmidpts[j] = (m_nextvalues[j] + value) * 0.5f;
float fseglen = (float)phase;
m_curves[j] = 2.f * (m_nextmidpts[j] - m_values[j] -
fseglen * m_slopes[j])
/ (fseglen * fseglen + fseglen);
}
}
int next = (phase < n) ? phase : n;
n -= next;
phase -=next;
while (next--)
{
for (int j = 0; j != outlets; ++j)
{
outsigs[j][i] = m_values[j];
m_slopes[j]+=m_curves[j];
m_values[j]+=m_slopes[j];
}
++i;
}
}
m_phase = phase;
}
|