aboutsummaryrefslogtreecommitdiff
path: root/system/pdp_ut.c
blob: 83b4cb044814cb1d8dc5cbb3b405d0efbde229fa (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
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
/*
 *   Pure Data Packet - Utility toolkit objects.
 *   Copyright (c) by Tom Schouten <pdp@zzz.kotnet.org>
 *
 *   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., 675 Mass Ave, Cambridge, MA 02139, USA.
 *
 */


/* This file contains some small utility pd objects that make working with 
   pdp objects a lot easier. Mainly as glue to be used in the abstractions 
   in the distro. */

#include "pdp.h"
#include <math.h>

/* this object does an add, scale, clip operation */

t_class *pdp_ut_addscaleclip_class;

typedef struct pdp_ut_addscaleclip_struct
{
    t_object x_obj;
    t_outlet *x_outlet0;
    t_float x_min;
    t_float x_max;
    t_float x_offset;
    t_float x_scale;
} t_pdp_ut_addscaleclip;


static void pdp_ut_addscaleclip_float(t_pdp_ut_addscaleclip *x, t_floatarg f)
{
    f += x->x_offset;
    f *= x->x_scale;
    f = (f < x->x_min) ? x->x_min : f;
    f = (f > x->x_max) ? x->x_max : f;
    outlet_float(x->x_outlet0, f);
}

static void pdp_ut_addscaleclip_free(t_pdp_ut_addscaleclip *x){}

void *pdp_ut_addscaleclip_new(t_floatarg offset, t_floatarg scale, t_floatarg min, t_floatarg max)
{
    t_pdp_ut_addscaleclip *x = (t_pdp_ut_addscaleclip *)pd_new(pdp_ut_addscaleclip_class);
    x->x_outlet0 = outlet_new(&x->x_obj, &s_float); 
    x->x_offset = offset;
    x->x_scale = scale;
    x->x_min = min;
    x->x_max = max;
    return (void *)x;
}

void pdp_ut_addscaleclip_setup(void)
{
    pdp_ut_addscaleclip_class = class_new(gensym("pdp_ut_addscaleclip"), (t_newmethod)pdp_ut_addscaleclip_new,
			      (t_method)pdp_ut_addscaleclip_free, sizeof(t_pdp_ut_addscaleclip), 0, 
					 A_FLOAT, A_FLOAT, A_FLOAT, A_FLOAT, A_NULL);
    class_addfloat(pdp_ut_addscaleclip_class,  pdp_ut_addscaleclip_float);
}


/* pdp_ut_logmap does a logarithmic parameter mapping [0->1] x -> min(max/min)^x max an add, scale, clip operation */
/* pdp_ut_logmap_comp does x -> min(max/min)^(1-x) */
/* pdp_ut_linmap dos x -> min + (max - min * x */

t_class *pdp_ut_linmap_class;
t_class *pdp_ut_logmap_class;
t_class *pdp_ut_logmap_comp_class;

typedef struct pdp_ut_map_struct
{
    t_object x_obj;
    t_outlet *x_outlet0;
    t_float x_min;
    t_float x_max;
} t_pdp_ut_map;


static void pdp_ut_logmap_float(t_pdp_ut_map *x, t_floatarg f)
{
    f = (f < 0.0f) ? 0.0f : f;
    f = (f > 1.0f) ? 1.0f : f;

    f = x->x_min * pow((x->x_max / x->x_min), f);

    outlet_float(x->x_outlet0, f);
}

static void pdp_ut_linmap_float(t_pdp_ut_map *x, t_floatarg f)
{
    f = (f < 0.0f) ? 0.0f : f;
    f = (f > 1.0f) ? 1.0f : f;

    f = x->x_min + ((x->x_max - x->x_min) * f);

    outlet_float(x->x_outlet0, f);
}

static void pdp_ut_logmap_comp_float(t_pdp_ut_map *x, t_floatarg f)
{
    f = (f < 0.0f) ? 0.0f : f;
    f = (f > 1.0f) ? 1.0f : f;

    f = x->x_min * pow((x->x_max / x->x_min), (1.0f - f));

    outlet_float(x->x_outlet0, f);
}

static void pdp_ut_map_free(t_pdp_ut_map *x){}


void pdp_ut_map_init(t_pdp_ut_map *x, t_floatarg min, t_floatarg max)
{
    x->x_outlet0 = outlet_new(&x->x_obj, &s_float); 
    x->x_min = min;
    x->x_max = max;
}

void *pdp_ut_logmap_new(t_floatarg min, t_floatarg max)
{
    t_pdp_ut_map *x = (t_pdp_ut_map *)pd_new(pdp_ut_logmap_class);
    pdp_ut_map_init(x, min, max);
    return (void *)x;
}

void *pdp_ut_linmap_new(t_floatarg min, t_floatarg max)
{
    t_pdp_ut_map *x = (t_pdp_ut_map *)pd_new(pdp_ut_linmap_class);
    pdp_ut_map_init(x, min, max);
    return (void *)x;
}

void *pdp_ut_logmap_comp_new(t_floatarg min, t_floatarg max)
{
    t_pdp_ut_map *x = (t_pdp_ut_map *)pd_new(pdp_ut_logmap_comp_class);
    pdp_ut_map_init(x, min, max);
    return (void *)x;
}

void pdp_ut_logmap_setup(void)
{
    pdp_ut_logmap_class = class_new(gensym("pdp_ut_logmap"), (t_newmethod)pdp_ut_logmap_new,
			      (t_method)pdp_ut_map_free, sizeof(t_pdp_ut_map), 0, 
					 A_FLOAT, A_FLOAT, A_NULL);
    class_addfloat(pdp_ut_logmap_class,  pdp_ut_logmap_float);
}

void pdp_ut_logmap_comp_setup(void)
{
    pdp_ut_logmap_comp_class = class_new(gensym("pdp_ut_logmap_comp"), (t_newmethod)pdp_ut_logmap_comp_new,
			      (t_method)pdp_ut_map_free, sizeof(t_pdp_ut_map), 0, 
					 A_FLOAT, A_FLOAT, A_NULL);
    class_addfloat(pdp_ut_logmap_comp_class,  pdp_ut_logmap_comp_float);
}

void pdp_ut_linmap_setup(void)
{
    pdp_ut_linmap_class = class_new(gensym("pdp_ut_linmap"), (t_newmethod)pdp_ut_linmap_new,
			      (t_method)pdp_ut_map_free, sizeof(t_pdp_ut_map), 0, 
					 A_FLOAT, A_FLOAT, A_NULL);
    class_addfloat(pdp_ut_linmap_class,  pdp_ut_linmap_float);
}



#ifdef __cplusplus
extern "C"
{
#endif

void pdp_ut_setup(void)
{
    pdp_ut_addscaleclip_setup();
    pdp_ut_logmap_setup();
    pdp_ut_logmap_comp_setup();
    pdp_ut_linmap_setup();
}


#ifdef __cplusplus
}
#endif