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
|
/* For information on usage and redistribution, and for a DISCLAIMER OF ALL
* WARRANTIES, see the file, "LICENSE.txt," in this distribution.
iemlib1 written by Thomas Musil, Copyright (c) IEM KUG Graz Austria 2000 - 2006 */
#include "m_pd.h"
#include "iemlib.h"
#include <math.h>
/* ---------------- pvu~ - simple peak-vu-meter. ----------------- */
typedef struct _pvu_tilde
{
t_object x_obj;
void *x_outlet_meter;
void *x_outlet_over;
void *x_clock;
t_float x_cur_peak;
t_float x_old_peak;
t_float x_threshold_over;
t_float x_c1;
t_float x_metro_time;
t_float x_release_time;
int x_overflow_counter;
int x_started;
t_float x_float_sig_in;
} t_pvu_tilde;
static t_class *pvu_tilde_class;
static void pvu_tilde_tick(t_pvu_tilde *x);
static void pvu_tilde_reset(t_pvu_tilde *x)
{
outlet_float(x->x_outlet_over, 0.0);
outlet_float(x->x_outlet_meter, -199.9);
x->x_overflow_counter = 0;
x->x_cur_peak = 0.0;
x->x_old_peak = 0.0;
clock_delay(x->x_clock, x->x_metro_time);
}
static void pvu_tilde_stop(t_pvu_tilde *x)
{
clock_unset(x->x_clock);
x->x_started = 0;
}
static void pvu_tilde_start(t_pvu_tilde *x)
{
clock_delay(x->x_clock, x->x_metro_time);
x->x_started = 1;
}
static void pvu_tilde_float(t_pvu_tilde *x, t_floatarg f)
{
if(f == 0.0)
{
clock_unset(x->x_clock);
x->x_started = 0;
}
else
{
clock_delay(x->x_clock, x->x_metro_time);
x->x_started = 1;
}
}
static void pvu_tilde_t_release(t_pvu_tilde *x, t_floatarg release_time)
{
if(release_time <= 5.0)
release_time = 5.0;
x->x_release_time = release_time;
x->x_c1 = exp(-x->x_metro_time/release_time);
}
static void pvu_tilde_t_metro(t_pvu_tilde *x, t_floatarg metro_time)
{
if(metro_time <= 5.0)
metro_time = 5.0;
x->x_metro_time = (int)metro_time;
x->x_c1 = exp(-metro_time/x->x_release_time);
}
static void pvu_tilde_threshold(t_pvu_tilde *x, t_floatarg thresh)
{
x->x_threshold_over = thresh;
}
static t_int *pvu_tilde_perform(t_int *w)
{
t_sample *in = (t_sample *)(w[1]);
t_pvu_tilde *x = (t_pvu_tilde *)(w[2]);
int n = (int)(w[3]);
t_float peak = x->x_cur_peak;
t_float absolute;
int i;
if(x->x_started)
{
for(i=0; i<n; i++)
{
absolute = (t_float)fabs(*in++);
if(absolute > peak)
peak = absolute;
}
x->x_cur_peak = peak;
}
return(w+4);
}
static void pvu_tilde_dsp(t_pvu_tilde *x, t_signal **sp)
{
dsp_add(pvu_tilde_perform, 3, sp[0]->s_vec, x, sp[0]->s_n);
clock_delay(x->x_clock, x->x_metro_time);
}
static void pvu_tilde_tick(t_pvu_tilde *x)
{
t_float db;
int i;
x->x_old_peak *= x->x_c1;
/* NAN protect */
if(IEM_DENORMAL(x->x_old_peak))
x->x_old_peak = 0.0;
if(x->x_cur_peak > x->x_old_peak)
x->x_old_peak = x->x_cur_peak;
if(x->x_old_peak <= 0.0000000001)
db = -199.9;
else if(x->x_old_peak > 1000000.0)
{
db = 120.0;
x->x_old_peak = 1000000.0;
}
else
db = 8.6858896381*log(x->x_old_peak);
if(db >= x->x_threshold_over)
{
x->x_overflow_counter++;
outlet_float(x->x_outlet_over, (t_float)x->x_overflow_counter);
}
outlet_float(x->x_outlet_meter, db);
x->x_cur_peak = 0.0;
clock_delay(x->x_clock, x->x_metro_time);
}
static void *pvu_tilde_new(t_floatarg metro_time, t_floatarg release_time, t_floatarg threshold)
{
t_pvu_tilde *x;
t_float t;
x = (t_pvu_tilde *)pd_new(pvu_tilde_class);
if(metro_time <= 0.0)
metro_time = 300.0;
if(metro_time <= 5.0)
metro_time = 5.0;
if(release_time <= 0.0)
release_time = 300.0;
if(release_time <= 5.0)
release_time = 5.0;
if(threshold == 0.0)
threshold = -0.01;
x->x_threshold_over = threshold;
x->x_overflow_counter = 0;
x->x_metro_time = metro_time;
x->x_release_time = release_time;
x->x_c1 = exp(-metro_time/release_time);
x->x_cur_peak = 0.0;
x->x_old_peak = 0.0;
x->x_clock = clock_new(x, (t_method)pvu_tilde_tick);
x->x_outlet_meter = outlet_new(&x->x_obj, &s_float);/* left */
x->x_outlet_over = outlet_new(&x->x_obj, &s_float); /* right */
x->x_started = 1;
x->x_float_sig_in = 0.0;
return(x);
}
static void pvu_tilde_ff(t_pvu_tilde *x)
{
clock_free(x->x_clock);
}
void pvu_tilde_setup(void )
{
pvu_tilde_class = class_new(gensym("pvu~"), (t_newmethod)pvu_tilde_new,
(t_method)pvu_tilde_ff, sizeof(t_pvu_tilde), 0, A_DEFFLOAT, A_DEFFLOAT, A_DEFFLOAT, 0);
CLASS_MAINSIGNALIN(pvu_tilde_class, t_pvu_tilde, x_float_sig_in);
class_addmethod(pvu_tilde_class, (t_method)pvu_tilde_dsp, gensym("dsp"), 0);
class_addfloat(pvu_tilde_class, pvu_tilde_float);
class_addmethod(pvu_tilde_class, (t_method)pvu_tilde_reset, gensym("reset"), 0);
class_addmethod(pvu_tilde_class, (t_method)pvu_tilde_start, gensym("start"), 0);
class_addmethod(pvu_tilde_class, (t_method)pvu_tilde_stop, gensym("stop"), 0);
class_addmethod(pvu_tilde_class, (t_method)pvu_tilde_t_release, gensym("t_release"), A_FLOAT, 0);
class_addmethod(pvu_tilde_class, (t_method)pvu_tilde_t_metro, gensym("t_metro"), A_FLOAT, 0);
class_addmethod(pvu_tilde_class, (t_method)pvu_tilde_threshold, gensym("threshold"), A_FLOAT, 0);
}
|