aboutsummaryrefslogtreecommitdiff
path: root/cyclone/sickle/phasewrap.c
blob: 535d5e803f8f6dcc0c1ebad16f2d9a0aa2729a2d (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
128
129
130
131
/* Copyright (c) 2002-2003 krzYszcz and others.
 * For information on usage and redistribution, and for a DISCLAIMER OF ALL
 * WARRANTIES, see the file, "LICENSE.txt," in this distribution.  */

#include <math.h>
#include "m_pd.h"
#include "shared.h"
#include "sickle/sic.h"

typedef struct _phasewrap
{
    t_sic  x_sic;
    int    x_algo;
} t_phasewrap;

static t_class *phasewrap_class;

static t_int *phasewrap_perform(t_int *w)
{
    int nblock = (int)(w[1]);
    t_float *in = (t_float *)(w[2]);
    t_float *out = (t_float *)(w[3]);
    t_shared_wrappy wrappy;
    while (nblock--)
    {
	/* FIXME here we have pi -> pi, 3pi -> -pi, -pi -> -pi, -3pi -> pi,
	   while in msp it is pi -> -pi, 3pi -> -pi, -pi -> pi, -3pi -> pi */

	double dnorm = *in++ * (1. / SHARED_2PI);
	wrappy.w_d = dnorm + SHARED_UNITBIT0;
	/* Speeding up the int-to-double conversion below would be nice,
	   but all attempts failed.  Even this is slower (which works only
	   for nonnegative input):

	   wrappy.w_i[SHARED_HIOFFSET] = SHARED_UNITBIT0_HIPART;
	   *out++ = (dnorm - (wrappy.w_d - SHARED_UNITBIT0)) * SHARED_2PI;
	*/
	dnorm -= wrappy.w_i[SHARED_LOWOFFSET];
	*out++ = dnorm * SHARED_2PI;
    }
    return (w + 4);
}

/* This is the slowest algo.  It is slower than fmod in all cases,
   except for input being zero. */
static t_int *phasewrap_perform1(t_int *w)
{
    int nblock = (int)(w[1]);
    t_float *in = (t_float *)(w[2]);
    t_float *out = (t_float *)(w[3]);
    while (nblock--)
    {
	float f = *in++;
	double dnorm;
	if (f < -SHARED_PI)
	{
	    dnorm = (double)f * (1. / SHARED_2PI) + .5;
	    *out++ = (dnorm - (int)dnorm) * SHARED_2PI + SHARED_PI;
	}
	else if (f > SHARED_PI)
	{
	    dnorm = (double)f * (1. / SHARED_2PI) + .5;
	    *out++ = (dnorm - (int)dnorm) * SHARED_2PI - SHARED_PI;
	}
	else *out++ = f;
    }
    return (w + 4);
}

static t_int *phasewrap_perform2(t_int *w)
{
    int nblock = (int)(w[1]);
    t_float *in = (t_float *)(w[2]);
    t_float *out = (t_float *)(w[3]);
    while (nblock--)
    {
	double dnorm = *in++ + SHARED_PI;
	if (dnorm < 0)
	    *out++ = SHARED_PI - fmod(-dnorm, SHARED_2PI);
	else
	    *out++ = fmod(dnorm, SHARED_2PI) - SHARED_PI;
    }
    return (w + 4);
}

static void phasewrap_dsp(t_phasewrap *x, t_signal **sp)
{
    switch (x->x_algo)
    {
    case 1:
	dsp_add(phasewrap_perform1, 3, sp[0]->s_n, sp[0]->s_vec, sp[1]->s_vec);
	break;
    case 2:
	dsp_add(phasewrap_perform2, 3, sp[0]->s_n, sp[0]->s_vec, sp[1]->s_vec);
	break;
    default:
	dsp_add(phasewrap_perform, 3, sp[0]->s_n, sp[0]->s_vec, sp[1]->s_vec);
    }
}

static void phasewrap__algo(t_phasewrap *x, t_floatarg f)
{
    x->x_algo = f;
}

static void *phasewrap_new(t_symbol *s, int ac, t_atom *av)
{
    t_phasewrap *x = (t_phasewrap *)pd_new(phasewrap_class);
    if (s == gensym("_phasewrap1~"))
	x->x_algo = 1;
    else if (s == gensym("_phasewrap2~"))
	x->x_algo = 2;
    else
	x->x_algo = 0;
    outlet_new((t_object *)x, &s_signal);
    return (x);
}

void phasewrap_tilde_setup(void)
{
    phasewrap_class = class_new(gensym("phasewrap~"),
				(t_newmethod)phasewrap_new, 0,
				sizeof(t_phasewrap), 0, A_GIMME, 0);
    class_addcreator((t_newmethod)phasewrap_new,
		     gensym("_phasewrap1~"), A_GIMME, 0);
    class_addcreator((t_newmethod)phasewrap_new,
		     gensym("_phasewrap2~"), A_GIMME, 0);
    sic_setup(phasewrap_class, phasewrap_dsp, SIC_FLOATTOSIGNAL);
    class_addmethod(phasewrap_class, (t_method)phasewrap__algo,
		    gensym("_algo"), A_FLOAT, 0);
}