~ubuntu-branches/ubuntu/precise/puredata/precise

« back to all changes in this revision

Viewing changes to .pc/02_kfreebsd.diff/src/d_osc.c

  • Committer: Bazaar Package Importer
  • Author(s): IOhannes m zmoelnig (gpg-key at iem)
  • Date: 2010-04-23 20:58:31 UTC
  • mfrom: (1.2.7 upstream)
  • Revision ID: james.westby@ubuntu.com-20100423205831-w732by2cge3pdtp9
Tags: 0.42.6-1
* New upstream release
* bumped standards version to 3.8.4
* converted source format to "3.0 (quilt)"
* explicit enumeration of manpages

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* Copyright (c) 1997-1999 Miller Puckette.
 
2
* For information on usage and redistribution, and for a DISCLAIMER OF ALL
 
3
* WARRANTIES, see the file, "LICENSE.txt," in this distribution.  */
 
4
 
 
5
/* sinusoidal oscillator and table lookup; see also tabosc4~ in d_array.c.
 
6
*/
 
7
 
 
8
#include "m_pd.h"
 
9
#include "math.h"
 
10
 
 
11
#define UNITBIT32 1572864.  /* 3*2^19; bit 32 has place value 1 */
 
12
 
 
13
    /* machine-dependent definitions.  These ifdefs really
 
14
    should have been by CPU type and not by operating system! */
 
15
#ifdef IRIX
 
16
    /* big-endian.  Most significant byte is at low address in memory */
 
17
#define HIOFFSET 0    /* word offset to find MSB */
 
18
#define LOWOFFSET 1    /* word offset to find LSB */
 
19
#define int32 long  /* a data type that has 32 bits */
 
20
#endif /* IRIX */
 
21
 
 
22
#ifdef MSW
 
23
    /* little-endian; most significant byte is at highest address */
 
24
#define HIOFFSET 1
 
25
#define LOWOFFSET 0
 
26
#define int32 long
 
27
#endif
 
28
 
 
29
#if defined(__FreeBSD__) || defined(__APPLE__)
 
30
#include <machine/endian.h>
 
31
#endif
 
32
 
 
33
#ifdef __linux__
 
34
#include <endian.h>
 
35
#endif
 
36
 
 
37
#if defined(__unix__) || defined(__APPLE__)
 
38
#if !defined(BYTE_ORDER) || !defined(LITTLE_ENDIAN)                         
 
39
#error No byte order defined                                                    
 
40
#endif                                                                          
 
41
 
 
42
#if BYTE_ORDER == LITTLE_ENDIAN                                             
 
43
#define HIOFFSET 1                                                              
 
44
#define LOWOFFSET 0                                                             
 
45
#else                                                                           
 
46
#define HIOFFSET 0    /* word offset to find MSB */                             
 
47
#define LOWOFFSET 1    /* word offset to find LSB */                            
 
48
#endif /* __BYTE_ORDER */                                                       
 
49
#include <sys/types.h>
 
50
#define int32 int32_t
 
51
#endif /* __unix__ or __APPLE__*/
 
52
 
 
53
union tabfudge
 
54
{
 
55
    double tf_d;
 
56
    int32 tf_i[2];
 
57
};
 
58
 
 
59
/* -------------------------- phasor~ ------------------------------ */
 
60
static t_class *phasor_class, *scalarphasor_class;
 
61
 
 
62
#if 1   /* in the style of R. Hoeldrich (ICMC 1995 Banff) */
 
63
 
 
64
typedef struct _phasor
 
65
{
 
66
    t_object x_obj;
 
67
    double x_phase;
 
68
    float x_conv;
 
69
    float x_f;      /* scalar frequency */
 
70
} t_phasor;
 
71
 
 
72
static void *phasor_new(t_floatarg f)
 
73
{
 
74
    t_phasor *x = (t_phasor *)pd_new(phasor_class);
 
75
    x->x_f = f;
 
76
    inlet_new(&x->x_obj, &x->x_obj.ob_pd, &s_float, gensym("ft1"));
 
77
    x->x_phase = 0;
 
78
    x->x_conv = 0;
 
79
    outlet_new(&x->x_obj, gensym("signal"));
 
80
    return (x);
 
81
}
 
82
 
 
83
static t_int *phasor_perform(t_int *w)
 
84
{
 
85
    t_phasor *x = (t_phasor *)(w[1]);
 
86
    t_float *in = (t_float *)(w[2]);
 
87
    t_float *out = (t_float *)(w[3]);
 
88
    int n = (int)(w[4]);
 
89
    double dphase = x->x_phase + (double)UNITBIT32;
 
90
    union tabfudge tf;
 
91
    int normhipart;
 
92
    float conv = x->x_conv;
 
93
 
 
94
    tf.tf_d = UNITBIT32;
 
95
    normhipart = tf.tf_i[HIOFFSET];
 
96
    tf.tf_d = dphase;
 
97
 
 
98
    while (n--)
 
99
    {
 
100
        tf.tf_i[HIOFFSET] = normhipart;
 
101
        dphase += *in++ * conv;
 
102
        *out++ = tf.tf_d - UNITBIT32;
 
103
        tf.tf_d = dphase;
 
104
    }
 
105
    tf.tf_i[HIOFFSET] = normhipart;
 
106
    x->x_phase = tf.tf_d - UNITBIT32;
 
107
    return (w+5);
 
108
}
 
109
 
 
110
static void phasor_dsp(t_phasor *x, t_signal **sp)
 
111
{
 
112
    x->x_conv = 1./sp[0]->s_sr;
 
113
    dsp_add(phasor_perform, 4, x, sp[0]->s_vec, sp[1]->s_vec, sp[0]->s_n);
 
114
}
 
115
 
 
116
static void phasor_ft1(t_phasor *x, t_float f)
 
117
{
 
118
    x->x_phase = f;
 
119
}
 
120
 
 
121
static void phasor_setup(void)
 
122
{
 
123
    phasor_class = class_new(gensym("phasor~"), (t_newmethod)phasor_new, 0,
 
124
        sizeof(t_phasor), 0, A_DEFFLOAT, 0);
 
125
    CLASS_MAINSIGNALIN(phasor_class, t_phasor, x_f);
 
126
    class_addmethod(phasor_class, (t_method)phasor_dsp, gensym("dsp"), 0);
 
127
    class_addmethod(phasor_class, (t_method)phasor_ft1,
 
128
        gensym("ft1"), A_FLOAT, 0);
 
129
}
 
130
 
 
131
#endif  /* Hoeldrich version */
 
132
 
 
133
/* ------------------------ cos~ ----------------------------- */
 
134
 
 
135
float *cos_table;
 
136
 
 
137
static t_class *cos_class;
 
138
 
 
139
typedef struct _cos
 
140
{
 
141
    t_object x_obj;
 
142
    float x_f;
 
143
} t_cos;
 
144
 
 
145
static void *cos_new(void)
 
146
{
 
147
    t_cos *x = (t_cos *)pd_new(cos_class);
 
148
    outlet_new(&x->x_obj, gensym("signal"));
 
149
    x->x_f = 0;
 
150
    return (x);
 
151
}
 
152
 
 
153
static t_int *cos_perform(t_int *w)
 
154
{
 
155
    t_float *in = (t_float *)(w[1]);
 
156
    t_float *out = (t_float *)(w[2]);
 
157
    int n = (int)(w[3]);
 
158
    float *tab = cos_table, *addr, f1, f2, frac;
 
159
    double dphase;
 
160
    int normhipart;
 
161
    union tabfudge tf;
 
162
    
 
163
    tf.tf_d = UNITBIT32;
 
164
    normhipart = tf.tf_i[HIOFFSET];
 
165
 
 
166
#if 0           /* this is the readable version of the code. */
 
167
    while (n--)
 
168
    {
 
169
        dphase = (double)(*in++ * (float)(COSTABSIZE)) + UNITBIT32;
 
170
        tf.tf_d = dphase;
 
171
        addr = tab + (tf.tf_i[HIOFFSET] & (COSTABSIZE-1));
 
172
        tf.tf_i[HIOFFSET] = normhipart;
 
173
        frac = tf.tf_d - UNITBIT32;
 
174
        f1 = addr[0];
 
175
        f2 = addr[1];
 
176
        *out++ = f1 + frac * (f2 - f1);
 
177
    }
 
178
#endif
 
179
#if 1           /* this is the same, unwrapped by hand. */
 
180
        dphase = (double)(*in++ * (float)(COSTABSIZE)) + UNITBIT32;
 
181
        tf.tf_d = dphase;
 
182
        addr = tab + (tf.tf_i[HIOFFSET] & (COSTABSIZE-1));
 
183
        tf.tf_i[HIOFFSET] = normhipart;
 
184
    while (--n)
 
185
    {
 
186
        dphase = (double)(*in++ * (float)(COSTABSIZE)) + UNITBIT32;
 
187
            frac = tf.tf_d - UNITBIT32;
 
188
        tf.tf_d = dphase;
 
189
            f1 = addr[0];
 
190
            f2 = addr[1];
 
191
        addr = tab + (tf.tf_i[HIOFFSET] & (COSTABSIZE-1));
 
192
            *out++ = f1 + frac * (f2 - f1);
 
193
        tf.tf_i[HIOFFSET] = normhipart;
 
194
    }
 
195
            frac = tf.tf_d - UNITBIT32;
 
196
            f1 = addr[0];
 
197
            f2 = addr[1];
 
198
            *out++ = f1 + frac * (f2 - f1);
 
199
#endif
 
200
    return (w+4);
 
201
}
 
202
 
 
203
static void cos_dsp(t_cos *x, t_signal **sp)
 
204
{
 
205
    dsp_add(cos_perform, 3, sp[0]->s_vec, sp[1]->s_vec, sp[0]->s_n);
 
206
}
 
207
 
 
208
static void cos_maketable(void)
 
209
{
 
210
    int i;
 
211
    float *fp, phase, phsinc = (2. * 3.14159) / COSTABSIZE;
 
212
    union tabfudge tf;
 
213
    
 
214
    if (cos_table) return;
 
215
    cos_table = (float *)getbytes(sizeof(float) * (COSTABSIZE+1));
 
216
    for (i = COSTABSIZE + 1, fp = cos_table, phase = 0; i--;
 
217
        fp++, phase += phsinc)
 
218
            *fp = cos(phase);
 
219
 
 
220
        /* here we check at startup whether the byte alignment
 
221
            is as we declared it.  If not, the code has to be
 
222
            recompiled the other way. */
 
223
    tf.tf_d = UNITBIT32 + 0.5;
 
224
    if ((unsigned)tf.tf_i[LOWOFFSET] != 0x80000000)
 
225
        bug("cos~: unexpected machine alignment");
 
226
}
 
227
 
 
228
static void cos_setup(void)
 
229
{
 
230
    cos_class = class_new(gensym("cos~"), (t_newmethod)cos_new, 0,
 
231
        sizeof(t_cos), 0, A_DEFFLOAT, 0);
 
232
    CLASS_MAINSIGNALIN(cos_class, t_cos, x_f);
 
233
    class_addmethod(cos_class, (t_method)cos_dsp, gensym("dsp"), 0);
 
234
    cos_maketable();
 
235
}
 
236
 
 
237
/* ------------------------ osc~ ----------------------------- */
 
238
 
 
239
static t_class *osc_class, *scalarosc_class;
 
240
 
 
241
typedef struct _osc
 
242
{
 
243
    t_object x_obj;
 
244
    double x_phase;
 
245
    float x_conv;
 
246
    float x_f;      /* frequency if scalar */
 
247
} t_osc;
 
248
 
 
249
static void *osc_new(t_floatarg f)
 
250
{
 
251
    t_osc *x = (t_osc *)pd_new(osc_class);
 
252
    x->x_f = f;
 
253
    outlet_new(&x->x_obj, gensym("signal"));
 
254
    inlet_new(&x->x_obj, &x->x_obj.ob_pd, &s_float, gensym("ft1"));
 
255
    x->x_phase = 0;
 
256
    x->x_conv = 0;
 
257
    return (x);
 
258
}
 
259
 
 
260
static t_int *osc_perform(t_int *w)
 
261
{
 
262
    t_osc *x = (t_osc *)(w[1]);
 
263
    t_float *in = (t_float *)(w[2]);
 
264
    t_float *out = (t_float *)(w[3]);
 
265
    int n = (int)(w[4]);
 
266
    float *tab = cos_table, *addr, f1, f2, frac;
 
267
    double dphase = x->x_phase + UNITBIT32;
 
268
    int normhipart;
 
269
    union tabfudge tf;
 
270
    float conv = x->x_conv;
 
271
    
 
272
    tf.tf_d = UNITBIT32;
 
273
    normhipart = tf.tf_i[HIOFFSET];
 
274
#if 0
 
275
    while (n--)
 
276
    {
 
277
        tf.tf_d = dphase;
 
278
        dphase += *in++ * conv;
 
279
        addr = tab + (tf.tf_i[HIOFFSET] & (COSTABSIZE-1));
 
280
        tf.tf_i[HIOFFSET] = normhipart;
 
281
        frac = tf.tf_d - UNITBIT32;
 
282
        f1 = addr[0];
 
283
        f2 = addr[1];
 
284
        *out++ = f1 + frac * (f2 - f1);
 
285
    }
 
286
#endif
 
287
#if 1
 
288
        tf.tf_d = dphase;
 
289
        dphase += *in++ * conv;
 
290
        addr = tab + (tf.tf_i[HIOFFSET] & (COSTABSIZE-1));
 
291
        tf.tf_i[HIOFFSET] = normhipart;
 
292
        frac = tf.tf_d - UNITBIT32;
 
293
    while (--n)
 
294
    {
 
295
        tf.tf_d = dphase;
 
296
            f1 = addr[0];
 
297
        dphase += *in++ * conv;
 
298
            f2 = addr[1];
 
299
        addr = tab + (tf.tf_i[HIOFFSET] & (COSTABSIZE-1));
 
300
        tf.tf_i[HIOFFSET] = normhipart;
 
301
            *out++ = f1 + frac * (f2 - f1);
 
302
        frac = tf.tf_d - UNITBIT32;
 
303
    }
 
304
            f1 = addr[0];
 
305
            f2 = addr[1];
 
306
            *out++ = f1 + frac * (f2 - f1);
 
307
#endif
 
308
 
 
309
    tf.tf_d = UNITBIT32 * COSTABSIZE;
 
310
    normhipart = tf.tf_i[HIOFFSET];
 
311
    tf.tf_d = dphase + (UNITBIT32 * COSTABSIZE - UNITBIT32);
 
312
    tf.tf_i[HIOFFSET] = normhipart;
 
313
    x->x_phase = tf.tf_d - UNITBIT32 * COSTABSIZE;
 
314
    return (w+5);
 
315
}
 
316
 
 
317
static void osc_dsp(t_osc *x, t_signal **sp)
 
318
{
 
319
    x->x_conv = COSTABSIZE/sp[0]->s_sr;
 
320
    dsp_add(osc_perform, 4, x, sp[0]->s_vec, sp[1]->s_vec, sp[0]->s_n);
 
321
}
 
322
 
 
323
static void osc_ft1(t_osc *x, t_float f)
 
324
{
 
325
    x->x_phase = COSTABSIZE * f;
 
326
}
 
327
 
 
328
static void osc_setup(void)
 
329
{    
 
330
    osc_class = class_new(gensym("osc~"), (t_newmethod)osc_new, 0,
 
331
        sizeof(t_osc), 0, A_DEFFLOAT, 0);
 
332
    CLASS_MAINSIGNALIN(osc_class, t_osc, x_f);
 
333
    class_addmethod(osc_class, (t_method)osc_dsp, gensym("dsp"), 0);
 
334
    class_addmethod(osc_class, (t_method)osc_ft1, gensym("ft1"), A_FLOAT, 0);
 
335
 
 
336
    cos_maketable();
 
337
}
 
338
 
 
339
/* ---------------- vcf~ - 2-pole bandpass filter. ----------------- */
 
340
 
 
341
typedef struct vcfctl
 
342
{
 
343
    float c_re;
 
344
    float c_im;
 
345
    float c_q;
 
346
    float c_isr;
 
347
} t_vcfctl;
 
348
 
 
349
typedef struct sigvcf
 
350
{
 
351
    t_object x_obj;
 
352
    t_vcfctl x_cspace;
 
353
    t_vcfctl *x_ctl;
 
354
    float x_f;
 
355
} t_sigvcf;
 
356
 
 
357
t_class *sigvcf_class;
 
358
 
 
359
static void *sigvcf_new(t_floatarg q)
 
360
{
 
361
    t_sigvcf *x = (t_sigvcf *)pd_new(sigvcf_class);
 
362
    inlet_new(&x->x_obj, &x->x_obj.ob_pd, &s_signal, &s_signal);
 
363
    inlet_new(&x->x_obj, &x->x_obj.ob_pd, gensym("float"), gensym("ft1"));
 
364
    outlet_new(&x->x_obj, gensym("signal"));
 
365
    outlet_new(&x->x_obj, gensym("signal"));
 
366
    x->x_ctl = &x->x_cspace;
 
367
    x->x_cspace.c_re = 0;
 
368
    x->x_cspace.c_im = 0;
 
369
    x->x_cspace.c_q = q;
 
370
    x->x_cspace.c_isr = 0;
 
371
    x->x_f = 0;
 
372
    return (x);
 
373
}
 
374
 
 
375
static void sigvcf_ft1(t_sigvcf *x, t_floatarg f)
 
376
{
 
377
    x->x_ctl->c_q = (f > 0 ? f : 0.f);
 
378
}
 
379
 
 
380
static t_int *sigvcf_perform(t_int *w)
 
381
{
 
382
    float *in1 = (float *)(w[1]);
 
383
    float *in2 = (float *)(w[2]);
 
384
    float *out1 = (float *)(w[3]);
 
385
    float *out2 = (float *)(w[4]);
 
386
    t_vcfctl *c = (t_vcfctl *)(w[5]);
 
387
    int n = (t_int)(w[6]);
 
388
    int i;
 
389
    float re = c->c_re, re2;
 
390
    float im = c->c_im;
 
391
    float q = c->c_q;
 
392
    float qinv = (q > 0? 1.0f/q : 0);
 
393
    float ampcorrect = 2.0f - 2.0f / (q + 2.0f);
 
394
    float isr = c->c_isr;
 
395
    float coefr, coefi;
 
396
    float *tab = cos_table, *addr, f1, f2, frac;
 
397
    double dphase;
 
398
    int normhipart, tabindex;
 
399
    union tabfudge tf;
 
400
    
 
401
    tf.tf_d = UNITBIT32;
 
402
    normhipart = tf.tf_i[HIOFFSET];
 
403
 
 
404
    for (i = 0; i < n; i++)
 
405
    {
 
406
        float cf, cfindx, r, oneminusr;
 
407
        cf = *in2++ * isr;
 
408
        if (cf < 0) cf = 0;
 
409
        cfindx = cf * (float)(COSTABSIZE/6.28318f);
 
410
        r = (qinv > 0 ? 1 - cf * qinv : 0);
 
411
        if (r < 0) r = 0;
 
412
        oneminusr = 1.0f - r;
 
413
        dphase = ((double)(cfindx)) + UNITBIT32;
 
414
        tf.tf_d = dphase;
 
415
        tabindex = tf.tf_i[HIOFFSET] & (COSTABSIZE-1);
 
416
        addr = tab + tabindex;
 
417
        tf.tf_i[HIOFFSET] = normhipart;
 
418
        frac = tf.tf_d - UNITBIT32;
 
419
        f1 = addr[0];
 
420
        f2 = addr[1];
 
421
        coefr = r * (f1 + frac * (f2 - f1));
 
422
 
 
423
        addr = tab + ((tabindex - (COSTABSIZE/4)) & (COSTABSIZE-1));
 
424
        f1 = addr[0];
 
425
        f2 = addr[1];
 
426
        coefi = r * (f1 + frac * (f2 - f1));
 
427
 
 
428
        f1 = *in1++;
 
429
        re2 = re;
 
430
        *out1++ = re = ampcorrect * oneminusr * f1 
 
431
            + coefr * re2 - coefi * im;
 
432
        *out2++ = im = coefi * re2 + coefr * im;
 
433
    }
 
434
    if (PD_BIGORSMALL(re))
 
435
        re = 0;
 
436
    if (PD_BIGORSMALL(im))
 
437
        im = 0;
 
438
    c->c_re = re;
 
439
    c->c_im = im;
 
440
    return (w+7);
 
441
}
 
442
 
 
443
static void sigvcf_dsp(t_sigvcf *x, t_signal **sp)
 
444
{
 
445
    x->x_ctl->c_isr = 6.28318f/sp[0]->s_sr;
 
446
    dsp_add(sigvcf_perform, 6,
 
447
        sp[0]->s_vec, sp[1]->s_vec, sp[2]->s_vec, sp[3]->s_vec, 
 
448
            x->x_ctl, sp[0]->s_n);
 
449
 
 
450
}
 
451
 
 
452
void sigvcf_setup(void)
 
453
{
 
454
    sigvcf_class = class_new(gensym("vcf~"), (t_newmethod)sigvcf_new, 0,
 
455
        sizeof(t_sigvcf), 0, A_DEFFLOAT, 0);
 
456
    CLASS_MAINSIGNALIN(sigvcf_class, t_sigvcf, x_f);
 
457
    class_addmethod(sigvcf_class, (t_method)sigvcf_dsp, gensym("dsp"), 0);
 
458
    class_addmethod(sigvcf_class, (t_method)sigvcf_ft1,
 
459
        gensym("ft1"), A_FLOAT, 0);
 
460
}
 
461
 
 
462
/* -------------------------- noise~ ------------------------------ */
 
463
static t_class *noise_class;
 
464
 
 
465
typedef struct _noise
 
466
{
 
467
    t_object x_obj;
 
468
    int x_val;
 
469
} t_noise;
 
470
 
 
471
static void *noise_new(void)
 
472
{
 
473
    t_noise *x = (t_noise *)pd_new(noise_class);
 
474
    static int init = 307;
 
475
    x->x_val = (init *= 1319); 
 
476
    outlet_new(&x->x_obj, gensym("signal"));
 
477
    return (x);
 
478
}
 
479
 
 
480
static t_int *noise_perform(t_int *w)
 
481
{
 
482
    t_sample *out = (t_sample *)(w[1]);
 
483
    int *vp = (int *)(w[2]);
 
484
    int n = (int)(w[3]);
 
485
    int val = *vp;
 
486
    while (n--)
 
487
    {
 
488
        *out++ = ((float)((val & 0x7fffffff) - 0x40000000)) *
 
489
            (float)(1.0 / 0x40000000);
 
490
        val = val * 435898247 + 382842987;
 
491
    }
 
492
    *vp = val;
 
493
    return (w+4);
 
494
}
 
495
 
 
496
static void noise_dsp(t_noise *x, t_signal **sp)
 
497
{
 
498
    dsp_add(noise_perform, 3, sp[0]->s_vec, &x->x_val, sp[0]->s_n);
 
499
}
 
500
 
 
501
static void noise_setup(void)
 
502
{
 
503
    noise_class = class_new(gensym("noise~"), (t_newmethod)noise_new, 0,
 
504
        sizeof(t_noise), 0, 0);
 
505
    class_addmethod(noise_class, (t_method)noise_dsp, gensym("dsp"), 0);
 
506
}
 
507
 
 
508
 
 
509
/* ----------------------- global setup routine ---------------- */
 
510
void d_osc_setup(void)
 
511
{
 
512
    phasor_setup();
 
513
    cos_setup();
 
514
    osc_setup();
 
515
    sigvcf_setup();
 
516
    noise_setup();
 
517
}
 
518