aboutsummaryrefslogtreecommitdiff
path: root/src/iemlib2/speedlim.c
blob: 9e29e0a194fdb1380bc7fad8cb51c8357a67e857 (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
/* 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 */

#ifdef _MSC_VER
#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>

/* ----------------------- speedlim -------------------------- */
/* -- reduces the flow of float-messages to one message per -- */
/* ----------------- initial argument time in ms ------------- */

static t_class *speedlim_class;

typedef struct _speedlim
{
  t_object  x_obj;
  t_clock   *x_clock;
  float     x_delay;
  int       x_output_is_locked;
  int       x_there_was_n_event;
  float     x_curval;
  float     x_lastout;
} t_speedlim;

static void speedlim_stop(t_speedlim *x)
{
  x->x_output_is_locked = 0;
  x->x_there_was_n_event = 0;
  clock_unset(x->x_clock);
}

static void speedlim_tick(t_speedlim *x)
{
  if(x->x_there_was_n_event)
  {
    x->x_output_is_locked = 1;
    x->x_there_was_n_event = 0;
    outlet_float(x->x_obj.ob_outlet, x->x_curval);
    clock_delay(x->x_clock, x->x_delay);
  }
  else
  {
    x->x_output_is_locked = 0;
    x->x_there_was_n_event = 0;
  }
}

static void speedlim_float(t_speedlim *x, t_float val)
{
  x->x_curval = val;
  if(!x->x_output_is_locked)
  {
    x->x_output_is_locked = 1;
    x->x_there_was_n_event = 0;
    outlet_float(x->x_obj.ob_outlet, x->x_curval);
    clock_delay(x->x_clock, x->x_delay);
  }
  else
    x->x_there_was_n_event = 1;
}

static void speedlim_ft1(t_speedlim *x, t_float delay)
{
  if(delay < 0.0)
    delay = 0.0;
  x->x_delay = delay;
}

static void speedlim_free(t_speedlim *x)
{
  clock_free(x->x_clock);
}

static void *speedlim_new(t_float delay)
{
  t_speedlim *x = (t_speedlim *)pd_new(speedlim_class);
  
  if(delay < 0.0)
    delay = 0.0;
  x->x_delay = delay;
  x->x_curval = 0.0f;
  x->x_output_is_locked = 0;
  x->x_there_was_n_event = 0;
  x->x_clock = clock_new(x, (t_method)speedlim_tick);
  outlet_new(&x->x_obj, &s_float);
  inlet_new(&x->x_obj, &x->x_obj.ob_pd, &s_float, gensym("ft1"));
  return (x);
}

void speedlim_setup(void)
{
  speedlim_class = class_new(gensym("speedlim"), (t_newmethod)speedlim_new,
    (t_method)speedlim_free, sizeof(t_speedlim), 0, A_DEFFLOAT, 0);
  class_addmethod(speedlim_class, (t_method)speedlim_stop, gensym("stop"), 0);
  class_addfloat(speedlim_class, (t_method)speedlim_float);
  class_addmethod(speedlim_class, (t_method)speedlim_ft1, gensym("ft1"), A_FLOAT, 0);
  class_sethelpsymbol(speedlim_class, gensym("iemhelp/help-speedlim"));
}