aboutsummaryrefslogtreecommitdiff
path: root/modules/dist.c
blob: 20fb221418bf48f47b26063815b3283f0eb3be36 (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
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
/*
 *   dist.c  -  wave shaping extern 
 *   Copyright (c) 2000-2003 by Tom Schouten
 *
 *   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.
 */

#include "extlib_util.h"


#define     CLIP          0
#define     INVERSE       1
#define     INVERSESQ     2
#define     INVERSECUB    3
#define     RAT1          4
#define     RAT2          5
#define     FULLRECT      6
#define     HALFRECT      7
#define     PULSE         8
#define     NEWTON1       9
#define     UPPERCLIP    10
#define     COMPARATOR   11



typedef struct distctl
{
  t_float c_gain;
  t_float c_delay;
  char c_type;
} t_distctl;

typedef struct dist
{
  t_object x_obj;
  t_float x_f;
  t_distctl x_ctl;
} t_dist;

void dist_bang(t_dist *x)
{

}

void dist_gain(t_dist *x,  t_floatarg f)
{
  x->x_ctl.c_gain = f;

}


static t_int *dist_perform(t_int *w)
{


  t_float *in    = (float *)(w[3]);
  t_float *out    = (float *)(w[4]);
  t_distctl *ctl  = (t_distctl *)(w[1]);
  t_float gain  = ctl->c_gain;
  t_int i;
  t_int n = (t_int)(w[2]);
  t_float x,y,v;
  t_float z = ctl->c_delay;

  switch(ctl->c_type){
  case CLIP:	
    for (i = 0; i < n; i++)
      {
	x = *in++ * gain;
	x = (x >  1) ? ( 1.) : x;
	x = (x < -1) ? (-1.) : x;
	*out++ = 0.9999 * x;

      }
    break;

  case INVERSE:	
    for (i = 0; i < n; i++)
      {
	x = *in++ * gain;
	x = (x > 1) ? (2. - 1/x) : x;
	x = (x < -1) ? (-2. - 1/x) : x;
	*out++ = x/2.0001;

      }
    break;

  case INVERSESQ:	
    for (i = 0; i < n; i++)
      {
	x = *in++ * gain;
	x = (x > 1) ? (2. - 1/x) : x;
	x = (x < -1) ? (-2. - 1/x) : x;
	x /= 2;
	*out++ = 1.999*x*x-1;

      }
    break;

  case INVERSECUB:	
    for (i = 0; i < n; i++)
      {
	x = *in++ * gain;
	x = (x > 1) ? (2. - 1/x) : x;
	x = (x < -1) ? (-2. - 1/x) : x;
	x /= 2;
	*out++ = .9999 * x*x*x;

      }
    break;

  case RAT1: /*(2*d./((1+(d).^2)))*/   
    for (i = 0; i < n; i++)
      {
	x = *in++ * gain;
	y = (1. + x*x);
	x = 1.9999*x/y;
	*out++ = x;
      }
    break;

  case RAT2: /*(2*d./((1+(d).^16)))*/   
    for (i = 0; i < n; i++)
      {
	x = *in++ * gain;
	y = x*x;
	y *= y;
	y *= y;
	y *= y;
	y = (1. + y);
	x = 1.2*x/y;
	*out++ = x;
      }
    break;

  case FULLRECT:
    for (i = 0; i < n; i++)
      {
	x = *in++ * gain;
	x = (x>0) ? x : -x;
	x = (x>1) ? 1 : x;
	*out++ = 1.9999*(x-.5);
      }
    break;

  case HALFRECT:
    for (i = 0; i < n; i++)
      {
	x = *in++ * gain;
	x = (x>0) ? x : 0;
	x = (x>1) ? 1 : x;
	*out++ = 1.9999*(x-.5);
      }
    break;

  case PULSE:
    for (i = 0; i < n; i++)
      {
	x = *in++ * gain;
	y = (x>0) ? (1):(-1);
	x = (z*y > 0) ? (0) : (y);
	*out++ = .9999 * x;
	z = x;
	
      }
    ctl->c_delay = z;
    break;

  case NEWTON1:
    for (i = 0; i < n; i++)
      {
	x = *in++ * gain;
	y = 1./(1.+x*x);

	z = .5;
	z = .5*(y/z + z);
	z = .5*(y/z + z);
	z = .5*(y/z + z);

	/*	z = .5*(y/z + z);
	 *	z = .5*(y/z + z);
	 *	z = .5*(y/z + z);
	 */

	*out++ = x * z; 
	
      }
    ctl->c_delay = z;
    break;

  case UPPERCLIP:
    for (i = 0; i < n; i++)
      {
	x = *in++ * gain;

	x = (x < 0.0f) ? 0.0f : x;
	x = (x > 0.9999f) ? 0.9999f : x;

	*out++ = x; 
	
      }
    break;

  case COMPARATOR:
    for (i = 0; i < n; i++)
      {
	x = *in++ * gain;

	x = (x > 0.0f) ? 1.0f : -1.0f;

	*out++ = x; 
	
      }
    break;

  default:
    
    for (i = 0; i < n; i++) *out++ = *in++;
    break;

  }
    
  return (w+5);
}

static void dist_dsp(t_dist *x, t_signal **sp)
{
    dsp_add(dist_perform, 4, &x->x_ctl, sp[0]->s_n, sp[0]->s_vec, sp[1]->s_vec);

}                                  
void dist_free(void)
{

}

t_class *dist_class;

void *dist_new(t_floatarg type)
{
    t_dist *x = (t_dist *)pd_new(dist_class);
    inlet_new(&x->x_obj, &x->x_obj.ob_pd, gensym("float"), gensym("gain"));  
    outlet_new(&x->x_obj, gensym("signal")); 

    dist_gain(x, 1);
    x->x_ctl.c_type = (char)type;
    x->x_ctl.c_delay = 0;

    return (void *)x;
}

void dist_tilde_setup(void)
{
  //post("dist~ v0.1");
    dist_class = class_new(gensym("dist~"), (t_newmethod)dist_new,
    	(t_method)dist_free, sizeof(t_dist), 0, A_DEFFLOAT, 0);
    CLASS_MAINSIGNALIN(dist_class, t_dist, x_f); 
    class_addmethod(dist_class, (t_method)dist_bang, gensym("bang"), 0);
    class_addmethod(dist_class, (t_method)dist_dsp, gensym("dsp"), 0); 
    class_addmethod(dist_class, (t_method)dist_gain, gensym("gain"), A_FLOAT, 0); 

}