aboutsummaryrefslogtreecommitdiff
path: root/src/iemlib2/sigm2f.c
blob: f623905abd43a7031705f7634834400f0cc80aa7 (plain)
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
/* Copyright (c) 1997-2003 Miller Puckette.
* For information on usage and redistribution, and for a DISCLAIMER OF ALL
* WARRANTIES, see the file, "LICENSE.txt," in this distribution.

iemlib2 written by Thomas Musil (c) IEM KUG Graz Austria 2000 - 2003 */

#ifdef NT
#pragma warning( disable : 4244 )
#pragma warning( disable : 4305 )
#endif

#include "m_pd.h"
#include "iemlib.h"
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <math.h>

/* ------------------------ m2f~ ----------------------------- */

#define M2FTABSIZE 2048

float *iem_m2f_table=(float *)0L;

static t_class *sigm2f_class;

typedef struct _sigm2f
{
    t_object x_obj;
    float x_msi;
} t_sigm2f;

static void *sigm2f_new(void)
{
    t_sigm2f *x = (t_sigm2f *)pd_new(sigm2f_class);
    outlet_new(&x->x_obj, gensym("signal"));
    x->x_msi = 0;
    return (x);
}

static t_int *sigm2f_perform(t_int *w)
{
    t_float *in = (t_float *)(w[1]);
    t_float *out = (t_float *)(w[2]);
    t_sigm2f *x = (t_sigm2f *)(w[3]);
    int n = (int)(w[4]);
    float *tab = iem_m2f_table, *addr, f1, f2, frac, iinn;
    double dphase;
    int normhipart;
    union tabfudge tf;
    
    tf.tf_d = UNITBIT32;
    normhipart = tf.tf_i[HIOFFSET];

#if 0	    	/* this is the readable version of the code. */
    while (n--)
    {
	iinn = (*in++)*10.0+670.0;
    	dphase = (double)iinn + UNITBIT32;
	tf.tf_d = dphase;
	addr = tab + (tf.tf_i[HIOFFSET] & (M2FTABSIZE-1));
	tf.tf_i[HIOFFSET] = normhipart;
	frac = tf.tf_d - UNITBIT32;
	f1 = addr[0];
	f2 = addr[1];
	*out++ = f1 + frac * (f2 - f1);
    }
#endif
#if 1	    	/* this is the same, unwrapped by hand. */
    iinn = (*in++)*10.0+670.0;
    dphase = (double)iinn + UNITBIT32;
	tf.tf_d = dphase;
	addr = tab + (tf.tf_i[HIOFFSET] & (M2FTABSIZE-1));
	tf.tf_i[HIOFFSET] = normhipart;
    while (--n)
    {
        iinn = (*in++)*10.0+670.0;
        dphase = (double)iinn + UNITBIT32;
	    frac = tf.tf_d - UNITBIT32;
	tf.tf_d = dphase;
	    f1 = addr[0];
	    f2 = addr[1];
	addr = tab + (tf.tf_i[HIOFFSET] & (M2FTABSIZE-1));
	    *out++ = f1 + frac * (f2 - f1);
	tf.tf_i[HIOFFSET] = normhipart;
    }
	    frac = tf.tf_d - UNITBIT32;
	    f1 = addr[0];
	    f2 = addr[1];
	    *out++ = f1 + frac * (f2 - f1);
#endif
    return (w+5);
}

static void sigm2f_dsp(t_sigm2f *x, t_signal **sp)
{
    dsp_add(sigm2f_perform, 4, sp[0]->s_vec, sp[1]->s_vec, x, sp[0]->s_n);
}

static void sigm2f_maketable(void)
{
    union tabfudge tf;

    if(!iem_m2f_table)
    {
        int i;
	float *fp, midi, refexp=440.0*exp(-5.75*log(2.0));

	iem_m2f_table = (float *)getbytes(sizeof(float) * (M2FTABSIZE+1));
	for(i=0, fp=iem_m2f_table, midi=-67.0; i<=M2FTABSIZE; i++, fp++, midi+=0.1)
	    *fp = refexp * exp(0.057762265047 * midi);
    }
    tf.tf_d = UNITBIT32 + 0.5;
    if((unsigned)tf.tf_i[LOWOFFSET] != 0x80000000)
	bug("m2f~: unexpected machine alignment");
}

void sigm2f_setup(void)
{
    sigm2f_class = class_new(gensym("m2f~"), (t_newmethod)sigm2f_new, 0,
    	sizeof(t_sigm2f), 0, 0);
    CLASS_MAINSIGNALIN(sigm2f_class, t_sigm2f, x_msi);
    class_addmethod(sigm2f_class, (t_method)sigm2f_dsp, gensym("dsp"), 0);
    sigm2f_maketable();
    class_sethelpsymbol(sigm2f_class, gensym("iemhelp/help-m2f~"));
}