aboutsummaryrefslogtreecommitdiff
path: root/src/iemlib2/exp_inc.c
blob: ed3e58587d88b1a85170d79158a3ed11aefdbb54 (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
/* 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, Copyright (c) IEM KUG Graz Austria 2000 - 2005 */

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

/* ------------------------ exp_inc ---------------------------- */
/* ------- exponetial/linear-incremental-bang-counter ---------- */


static t_class *exp_inc_class;

typedef struct _exp_inc
{
  t_object x_obj;
  float    x_initialval;
  float    x_currentval;
  float    x_mulfactor;
  float    x_addincrement;
  float    x_min;
  float    x_max;
} t_exp_inc;

static void exp_inc_bang(t_exp_inc *x)
{                                                   
  outlet_float(x->x_obj.ob_outlet, x->x_currentval);
  x->x_currentval = x->x_currentval * x->x_mulfactor + x->x_addincrement;
  if(x->x_currentval < x->x_min)
    x->x_currentval = x->x_min;
  if(x->x_currentval > x->x_max)
    x->x_currentval = x->x_max;
}

static void exp_inc_reset(t_exp_inc *x)
{
  x->x_currentval = x->x_initialval;
  if(x->x_currentval < x->x_min)
    x->x_currentval = x->x_min;
  if(x->x_currentval > x->x_max)
    x->x_currentval = x->x_max;
}

static void exp_inc_float(t_exp_inc *x, t_floatarg f)
{
  x->x_initialval = (float)f;
  x->x_currentval = x->x_initialval;
  if(x->x_currentval < x->x_min)
    x->x_currentval = x->x_min;
  if(x->x_currentval > x->x_max)
    x->x_currentval = x->x_max;
}

static void exp_inc_ft1(t_exp_inc *x, t_floatarg f)
{
  x->x_mulfactor = 1.0 + 0.01*(float)f;
}

static void exp_inc_ft2(t_exp_inc *x, t_floatarg f)
{
  x->x_addincrement = (float)f;
}

static void exp_inc_ft3(t_exp_inc *x, t_floatarg f)
{
  x->x_min = (float)f;
  if(x->x_currentval < x->x_min)
    x->x_currentval = x->x_min;
}

static void exp_inc_ft4(t_exp_inc *x, t_floatarg f)
{
  x->x_max = (float)f;
  if(x->x_currentval > x->x_max)
    x->x_currentval = x->x_max;
}

static void exp_inc_list(t_exp_inc *x, t_symbol *s, int ac, t_atom *av)
{
  if((ac == 5)&&IS_A_FLOAT(av,0)&&IS_A_FLOAT(av,1)&&IS_A_FLOAT(av,2)
    &&IS_A_FLOAT(av,3)&&IS_A_FLOAT(av,4))
  {
    exp_inc_ft4(x, atom_getfloatarg(4, ac, av));
    exp_inc_ft3(x, atom_getfloatarg(3, ac, av));
    exp_inc_ft2(x, atom_getfloatarg(2, ac, av));
    exp_inc_ft1(x, atom_getfloatarg(1, ac, av));
    exp_inc_float(x, atom_getfloatarg(0, ac, av));
  }
}

static void *exp_inc_new(t_symbol *s, int ac, t_atom *av)
{
  t_exp_inc *x = (t_exp_inc *)pd_new(exp_inc_class);
  
  x->x_currentval = 10.0;
  x->x_mulfactor = 1.0;
  x->x_addincrement = 0.0;
  x->x_min = 0.0;
  x->x_max = 1000.0;
  if((ac == 5)&&IS_A_FLOAT(av,0)&&IS_A_FLOAT(av,1)&&IS_A_FLOAT(av,2)
    &&IS_A_FLOAT(av,3)&&IS_A_FLOAT(av,4))
  {
    exp_inc_ft4(x, atom_getfloatarg(4, ac, av));
    exp_inc_ft3(x, atom_getfloatarg(3, ac, av));
    exp_inc_ft2(x, atom_getfloatarg(2, ac, av));
    exp_inc_ft1(x, atom_getfloatarg(1, ac, av));
    exp_inc_float(x, atom_getfloatarg(0, ac, av));
  }
  outlet_new(&x->x_obj, &s_float);
  inlet_new(&x->x_obj, &x->x_obj.ob_pd, &s_float, gensym("ft1"));
  inlet_new(&x->x_obj, &x->x_obj.ob_pd, &s_float, gensym("ft2"));
  inlet_new(&x->x_obj, &x->x_obj.ob_pd, &s_float, gensym("ft3"));
  inlet_new(&x->x_obj, &x->x_obj.ob_pd, &s_float, gensym("ft4"));
  return (x);
}

void exp_inc_setup(void)
{
  exp_inc_class = class_new(gensym("exp_inc"), (t_newmethod)exp_inc_new,
    0, sizeof(t_exp_inc), 0, A_GIMME, 0);
  class_addbang(exp_inc_class, exp_inc_bang);
  class_addlist(exp_inc_class, (t_method)exp_inc_list);
  class_addmethod(exp_inc_class, (t_method)exp_inc_reset, gensym("reset"), 0);
  class_addfloat(exp_inc_class, (t_method)exp_inc_float);
  class_addmethod(exp_inc_class, (t_method)exp_inc_ft1, gensym("ft1"), A_FLOAT, 0);
  class_addmethod(exp_inc_class, (t_method)exp_inc_ft2, gensym("ft2"), A_FLOAT, 0);
  class_addmethod(exp_inc_class, (t_method)exp_inc_ft3, gensym("ft3"), A_FLOAT, 0);
  class_addmethod(exp_inc_class, (t_method)exp_inc_ft4, gensym("ft4"), A_FLOAT, 0);
  class_sethelpsymbol(exp_inc_class, gensym("iemhelp/help-exp_inc"));
}