aboutsummaryrefslogtreecommitdiff
path: root/src/iemlib1/sigrvu.c
blob: fac370ee70e73bb7f473988d843ed9e7229dd34f (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
/* 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 - 2003 */

#ifdef NT
#pragma warning( disable : 4244 )
#pragma warning( disable : 4305 )
#endif


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

/* ---------------- rvu~ - simple peak&rms-vu-meter. ----------------- */

typedef struct sigrvu
{
	t_object	x_obj;
	void			*x_clock_metro;
	int				x_metro_time;
	float			x_sum_rms;
	float			x_old_rms;
	float			x_rcp;
	float			x_sr;
	float			x_release_time;
	float			x_c1;
	int				x_started;
	float			x_msi;
} t_sigrvu;

t_class *sigrvu_class;
static void sigrvu_tick_metro(t_sigrvu *x);

static void sigrvu_reset(t_sigrvu *x)
{
	outlet_float(x->x_obj.ob_outlet, -99.9f);
	x->x_sum_rms = 0.0f;
	x->x_old_rms = 0.0f;
	clock_delay(x->x_clock_metro, x->x_metro_time);
}

static void sigrvu_stop(t_sigrvu *x)
{
	clock_unset(x->x_clock_metro);
	x->x_started = 0;
}

static void sigrvu_start(t_sigrvu *x)
{
	clock_delay(x->x_clock_metro, x->x_metro_time);
	x->x_started = 1;
}

static void sigrvu_float(t_sigrvu *x, t_floatarg f)
{
	if(f == 0.0)
	{
		clock_unset(x->x_clock_metro);
		x->x_started = 0;
	}
	else
	{
		clock_delay(x->x_clock_metro, x->x_metro_time);
		x->x_started = 1;
	}
}

static void sigrvu_t_release(t_sigrvu *x, t_floatarg release_time)
{
	if(release_time <= 20.0f)
		release_time = 20.0f;
	x->x_release_time = release_time;
	x->x_c1 = exp(-2.0*(float)x->x_metro_time/x->x_release_time);
}

static void sigrvu_t_metro(t_sigrvu *x, t_floatarg metro_time)
{
	if(metro_time <= 20.0f)
		metro_time = 20.0f;
	x->x_metro_time = (int)metro_time;
	x->x_c1 = exp(-2.0*(float)x->x_metro_time/x->x_release_time);
	x->x_rcp = 1.0f/(x->x_sr*(float)x->x_metro_time);
}

static t_int *sigrvu_perform(t_int *w)
{
	float *in = (float *)(w[1]);
	t_sigrvu *x = (t_sigrvu *)(w[2]);
	int n = (int)(w[3]);
	float pow, sum=x->x_sum_rms;
	int i;

	if(x->x_started)
	{
		for(i=0; i<n; i++)
		{
			sum += in[i]*in[i];
		}
		x->x_sum_rms = sum;
	}
	return(w+4);
}

static void sigrvu_dsp(t_sigrvu *x, t_signal **sp)
{
	x->x_sr = 0.001*(float)sp[0]->s_sr;
	x->x_rcp = 1.0f/(x->x_sr*(float)x->x_metro_time);
	dsp_add(sigrvu_perform, 3, sp[0]->s_vec, x, sp[0]->s_n);
	clock_delay(x->x_clock_metro, x->x_metro_time);
}

static void sigrvu_tick_metro(t_sigrvu *x)
{
	float dbr, cur_rms, c1=x->x_c1;

	cur_rms = (1.0f - c1)*x->x_sum_rms*x->x_rcp + c1*x->x_old_rms;
	/* NAN protect */
	if(IEM_DENORMAL(cur_rms))
		cur_rms = 0.0f;

	if(cur_rms <= 0.0000000001f)
		dbr = -99.9f;
	else if(cur_rms > 1000000.0f)
	{
		dbr = 60.0f;
		x->x_old_rms = 1000000.0f;
	}
	else
		dbr = 4.3429448195f*log(cur_rms);
	x->x_sum_rms = 0.0f;
	x->x_old_rms = cur_rms;
	outlet_float(x->x_obj.ob_outlet, dbr);
	clock_delay(x->x_clock_metro, x->x_metro_time);
}

static void sigrvu_ff(t_sigrvu *x)
{
	clock_free(x->x_clock_metro);
}

static void *sigrvu_new(t_floatarg metro_time, t_floatarg release_time)
{
	t_sigrvu *x=(t_sigrvu *)pd_new(sigrvu_class);
	float t;
	int i;

	if(metro_time <= 0.0f)
		metro_time = 300.0f;
	if(metro_time <= 20.0f)
		metro_time = 20.0f;
	if(release_time <= 0.0f)
		release_time = 300.0f;
	if(release_time <= 20.0f)
		release_time = 20.0f;
	x->x_metro_time = (int)metro_time;
	x->x_release_time = release_time;
	x->x_c1 = exp(-2.0*(float)x->x_metro_time/x->x_release_time);
	x->x_sum_rms = 0.0f;
	x->x_old_rms = 0.0f;
	x->x_sr = 44.1f;
	x->x_rcp = 1.0f/(x->x_sr*(float)x->x_metro_time);
	x->x_clock_metro = clock_new(x, (t_method)sigrvu_tick_metro);
	x->x_started = 1;
	outlet_new(&x->x_obj, &s_float);
	x->x_msi = 0.0f;
	return(x);
}

void sigrvu_setup(void)
{
	sigrvu_class = class_new(gensym("rvu~"), (t_newmethod)sigrvu_new,
				  (t_method)sigrvu_ff, sizeof(t_sigrvu), 0,
				  A_DEFFLOAT, A_DEFFLOAT, 0);
	CLASS_MAINSIGNALIN(sigrvu_class, t_sigrvu, x_msi);
	class_addmethod(sigrvu_class, (t_method)sigrvu_dsp, gensym("dsp"), 0);
	class_addfloat(sigrvu_class, sigrvu_float);
	class_addmethod(sigrvu_class, (t_method)sigrvu_reset, gensym("reset"), 0);
	class_addmethod(sigrvu_class, (t_method)sigrvu_start, gensym("start"), 0);
	class_addmethod(sigrvu_class, (t_method)sigrvu_stop, gensym("stop"), 0);
	class_addmethod(sigrvu_class, (t_method)sigrvu_t_release, gensym("t_release"), A_FLOAT, 0);
	class_addmethod(sigrvu_class, (t_method)sigrvu_t_metro, gensym("t_metro"), A_FLOAT, 0);
	class_sethelpsymbol(sigrvu_class, gensym("iemhelp/help-rvu~"));
}