g_magicglass.c 11 KB
Newer Older
1
2
3
4
5
6
#include "m_pd.h"
#include <stdio.h>
#include "string.h"
#include "m_pd.h"
#include "m_imp.h"
#include "s_stuff.h"
7
8
#include "g_magicglass.h"

9
10
11
12
13
14
15
16
17
18
19
20
#define MG_CLOCK_CLEAR_DELAY 500.5
#define MG_CLOCK_FLASH_DELAY 50
#define MG_SAMPLE_COUNT 2205

EXTERN int glist_getfont(t_glist *x);

t_class *magicGlass_class;

void magicGlass_clearText(t_magicGlass *x);

void magicGlass_bind(t_magicGlass *x, t_object *obj, int outno)
{
21
	//fprintf(stderr,"magicglass_bind\n");
22
23
    if (x->x_connectedObj != obj)
    {
24
        if (x->x_connectedObj)
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
        {
            obj_disconnect(x->x_connectedObj,
                           x->x_connectedOutno,
                           &x->x_obj,
                           0);
        }
        x->x_connectedObj = obj;
        x->x_connectedOutno = outno;
		x->x_maxSize = 1;
		magicGlass_clearText(x);
        obj_connect(obj, outno, &x->x_obj, 0);
    }
}

void magicGlass_unbind(t_magicGlass *x)
{
41
	//fprintf(stderr,"magicglass_unbind\n");
42
    if (x->x_connectedObj)
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
    {
        obj_disconnect(x->x_connectedObj,
                       x->x_connectedOutno,
                       &x->x_obj,
                       0);
    }
    x->x_dspOn = 0;
    x->x_maxSample = -999999;
    x->x_sampleCount = 0;
    x->x_connectedObj = NULL;
    x->x_connectedOutno = 0;
	x->x_maxSize = 1;
}

void magicGlass_updateText(t_magicGlass *x, int moved)
{
59
	//fprintf(stderr,"magicglass_updateText\n");
60
61
    int bgSize;
	/* change second argument (10.0) to provide optimal scaling in the following entry */
62
	float font = (float)(sys_hostfontsize(glist_getfont(x->x_c)))/10.0;
63
64
65
66
	if (font <= 1.0) {
		x->x_display_font = 9;
		font = 1.0;
	} else {
67
		x->x_display_font = sys_hostfontsize(glist_getfont(x->x_c));
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
	}

    if (x->x_visible)
    {
		if (!moved) {
			char *color;
			if (x->x_issignal || strcmp(x->x_old_string, x->x_string)) {
				color = "#ffffff";
			}
			else {
				color = "#e87216";
				clock_delay(x->x_flashClock, MG_CLOCK_FLASH_DELAY);
			}
		    sys_vgui(".x%x.c itemconfigure magicGlassText -text {%s} -fill %s\n",
		            x->x_c,
		            x->x_string,
					color);
		} else {
		    sys_vgui(".x%x.c itemconfigure magicGlassText -text {%s}\n",
		            x->x_c,
		            x->x_string);
		}

        if (strlen(x->x_string) > 0)
        {
			if (strlen(x->x_string) > x->x_maxSize) x->x_maxSize = strlen(x->x_string);
        }
		bgSize = x->x_x + (int)((30.0 * font) + ((font * 7.0) * (float)x->x_maxSize));
        sys_vgui(".x%x.c coords magicGlassText %d %d\n",
                 x->x_c,
                 x->x_x + 20,
                 x->x_y);
        sys_vgui(".x%x.c coords magicGlassLine %d %d %d %d %d %d\n",
                 x->x_c,
                 x->x_x + 3,
                 x->x_y,
                 x->x_x + 13,
                 x->x_y + 5,
                 x->x_x + 13,
                 x->x_y - 5);
        sys_vgui(".x%x.c coords magicGlassBg %d %d %d %d\n",
                 x->x_c,
                 x->x_x + 13,
                 x->x_y - (int)(12.0 * font),
                 bgSize,
                 x->x_y + (int)(12.0 * font));
    }
}

void magicGlass_drawNew(t_magicGlass *x)
{
119
	//fprintf(stderr,"magicglass_drawNew\n");
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
    sys_vgui(".x%x.c create rectangle 0 0 0 0 -outline #ffffff -fill #000000 -tags magicGlassBg\n",
             x->x_c);
    sys_vgui(".x%x.c create polygon 0 0 0 0 0 0 -fill #000000 -width 4 -tags magicGlassLine\n",
             x->x_c);
    sys_vgui(".x%x.c create text 0 0 -text {} -anchor w -fill #e87216 -font {{%s} %d %s} -tags magicGlassText\n",
             x->x_c, sys_font, x->x_display_font, sys_fontweight);
    sys_vgui(".x%x.c raise magicGlassBg\n",
             x->x_c);
    sys_vgui(".x%x.c raise magicGlassText\n",
             x->x_c);
    magicGlass_updateText(x, 0);
	clock_delay(x->x_flashClock, MG_CLOCK_FLASH_DELAY);
}

void magicGlass_undraw(t_magicGlass *x)
{
136
	//fprintf(stderr,"magicglass_undraw\n");
137
138
139
140
141
142
143
    sys_vgui(".x%x.c delete magicGlassBg\n", x->x_c);
    sys_vgui(".x%x.c delete magicGlassLine\n", x->x_c);
    sys_vgui(".x%x.c delete magicGlassText\n", x->x_c);
}

void magicGlass_flashText(t_magicGlass *x)
{
144
	//fprintf(stderr,"magicglass_flashText\n");
145
146
147
148
149
150
    sys_vgui(".x%x.c itemconfigure magicGlassText -fill #ffffff\n",
         x->x_c);
}

void magicGlass_clearText(t_magicGlass *x)
{
151
	//fprintf(stderr,"magicglass_clearText\n");
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
	strcpy(x->x_old_string, x->x_string);
    x->x_string[0] = 0;
    magicGlass_updateText(x, 0);
}

void magicGlass_bang(t_magicGlass *x) 
{
	x->x_issignal = 0;
	strcpy(x->x_old_string, x->x_string);
    strcpy(x->x_string, "bang");
    magicGlass_updateText(x, 0);
    clock_delay(x->x_clearClock, MG_CLOCK_CLEAR_DELAY);
}

void magicGlass_float(t_magicGlass *x, t_float f) 
{
	x->x_issignal = 0;
	strcpy(x->x_old_string, x->x_string);
    sprintf(x->x_string, "%g", f);
    magicGlass_updateText(x, 0);
    clock_delay(x->x_clearClock, MG_CLOCK_CLEAR_DELAY);
}

void magicGlass_symbol(t_magicGlass *x, t_symbol *sym)
{
	x->x_issignal = 0;
	strcpy(x->x_old_string, x->x_string);
    sprintf(x->x_string, "symbol %s", sym->s_name);
    magicGlass_updateText(x, 0);
    clock_delay(x->x_clearClock, MG_CLOCK_CLEAR_DELAY);
}

void magicGlass_anything(t_magicGlass *x, t_symbol *sym, int argc, t_atom *argv)
{
    char aString[4096];
    char valueString[4096];
    int i;

	x->x_issignal = 0;

	strcpy(x->x_old_string, x->x_string);
    strcpy(aString, sym->s_name);
    valueString[0] = 0;
    for (i = 0; i < argc; i++)
    {
        if (argv[i].a_type == A_SYMBOL)
        {
            sprintf(valueString, " %s", argv[i].a_w.w_symbol->s_name);
            strcat(aString, valueString);
        }
        else if (argv[i].a_type == A_FLOAT)
        {
            sprintf(valueString, " %g", argv[i].a_w.w_float);
            strcat(aString, valueString);
        }
    }
    strcpy(x->x_string, aString);
    magicGlass_updateText(x, 0);
    clock_delay(x->x_clearClock, MG_CLOCK_CLEAR_DELAY);
}

void magicGlass_list(t_magicGlass *x, t_symbol *sym, int argc, t_atom *argv)
{
    char aString[4096];
    char valueString[4096];
    int i;

	x->x_issignal = 0;

    aString[0] = 0;
    valueString[0] = 0;

	strcpy(x->x_old_string, x->x_string);
	strcpy(aString, sym->s_name);
    for (i = 0; i < argc; i++)
    {
        if (argv[i].a_type == A_SYMBOL)
        {
            sprintf(valueString, " %s", argv[i].a_w.w_symbol->s_name);
            strcat(aString, valueString);
        }
        else if (argv[i].a_type == A_FLOAT)
        {
            sprintf(valueString, " %g", argv[i].a_w.w_float);
            strcat(aString, valueString);
        }
    }
    strcpy(x->x_string, aString);
    magicGlass_updateText(x, 0);
    clock_delay(x->x_clearClock, MG_CLOCK_CLEAR_DELAY);
}

244
void magicGlass_setCanvas(t_magicGlass *x, t_glist *c)
245
246
247
248
249
250
{
    x->x_c = c;
}

void magicGlass_show(t_magicGlass *x)
{
251
	//fprintf(stderr,"magicglass_show\n");
252
253
    if (!x->x_visible) {
		x->x_sampleCount = 0;
Ivica Bukvic's avatar
Ivica Bukvic committed
254
		x->x_minSample =  999999;
255
256
257
258
259
260
261
262
263
		x->x_maxSample = -999999;
		x->x_string[0] = 0;
        x->x_visible = 1;
        magicGlass_drawNew(x);
    }
}

void magicGlass_hide(t_magicGlass *x)
{
264
265
	//fprintf(stderr,"magicglass_hide\n");
    if (x->x_visible) {
266
        magicGlass_undraw(x);
267
		x->x_sampleCount = 0;
Ivica Bukvic's avatar
Ivica Bukvic committed
268
		x->x_minSample =  999999;
269
270
271
272
		x->x_maxSample = -999999;
		x->x_string[0] = 0;
		x->x_visible = 0;
	}
273
274
275
276
}

void magicGlass_moveText(t_magicGlass *x, int pX, int pY)
{
277
	//fprintf(stderr,"magicglass_moveText\n");
278
279
280
281
282
283
284
285
286
    int bgSize;
    
    x->x_x = pX;
    x->x_y = pY;
    magicGlass_updateText(x, 1);
}

int magicGlass_bound(t_magicGlass *x)
{
287
	//fprintf(stderr,"magicglass_bound\n");
288
289
290
291
292
293
294
295
    if (x->x_connectedObj)
        return 1;
    else
        return 0;
}

int magicGlass_isOn(t_magicGlass *x)
{
296
	//fprintf(stderr,"magicglass_isOn\n");
297
298
299
300
301
302
303
304
    if (x->x_viewOn)
        return 1;
    else
        return 0;
}

void magicGlass_setOn(t_magicGlass *x, int i)
{
305
	//fprintf(stderr,"magicglass_setOn\n");
306
307
308
309
310
311
312
313
314
315
316
317
    if (i)
    {
        x->x_viewOn = 1;
    }
    else
    {
        x->x_viewOn = 0;
    }
}

void magicGlass_setDsp(t_magicGlass *x, int i)
{
318
	//fprintf(stderr,"magicglass_setDsp\n");
319
320
321
322
323
	if (i != x->x_dspOn) {
		if (i)
		{
		    x->x_dspOn = 1;
		    x->x_sampleCount = 0;
Ivica Bukvic's avatar
Ivica Bukvic committed
324
			x->x_minSample =  999999;
325
326
327
328
329
330
331
332
333
334
335
336
		    x->x_maxSample = -999999;
		}
		else
		{
		    x->x_dspOn = 0;
		}
	}
}

t_int *magicGlass_perform(t_int *w)
{
    t_magicGlass *x = (t_magicGlass *)(w[1]);
337
    if (x->x_dspOn && x->x_connectedObj)
338
    {
339
		//fprintf(stderr,"magicglass_perform\n");
340
341
342
		float *in = (float *)(w[2]);
		int N = (int)(w[3]);
		int i;
343
344
345
346
        for (i = 0; i < N; i++)
        {
            if (in[i] > x->x_maxSample)
                x->x_maxSample = in[i];
Ivica Bukvic's avatar
Ivica Bukvic committed
347
348
            if (in[i] < x->x_minSample)
				x->x_minSample = in[i];
349
350
351
            x->x_sampleCount++;
            if (x->x_sampleCount >= MG_SAMPLE_COUNT)
            {
Ivica Bukvic's avatar
Ivica Bukvic committed
352
353
354
355
356
357
358
359
				char l[64], m[64], h[64];
				sprintf(l, "%s%#g", (x->x_minSample < 0.0f ? "" : " "), x->x_minSample);
				l[6] = '\0';
				sprintf(m, "%s%#g", (in[i] < 0.0f ? "" : " "), in[i]);
				m[6] = '\0';
				sprintf(h, "%s%#g", (x->x_maxSample < 0.0f ? "" : " "), x->x_maxSample);
				h[6] = '\0';
                sprintf(x->x_string, "~ %s %s %s", l, m, h);
360
                magicGlass_updateText(x, 0);
Ivica Bukvic's avatar
Ivica Bukvic committed
361
362
				//x->x_minSample =  999999;
                //x->x_maxSample = -999999;
363
364
365
366
367
368
369
370
371
                x->x_sampleCount = 0;
            }
        }
    }
    return (w + 4);
}

void magicGlass_dsp(t_magicGlass *x, t_signal **sp)
{
372
	//fprintf(stderr,"magicglass_dsp\n");
373
374
375
376
    dsp_add(magicGlass_perform, 3, x, sp[0]->s_vec, sp[0]->s_n);
	x->x_issignal = 1;
}

377
void *magicGlass_new(t_glist *c)
378
{
379
	//fprintf(stderr,"magicglass_new\n");
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
    t_magicGlass *x = (t_magicGlass *)pd_new(magicGlass_class);
    x->x_connectedObj= NULL;
    x->x_connectedOutno = 0;
    x->x_visible = 0;
    x->x_c = c;
    x->x_sigF = 0;
    x->x_dspOn = 0;
    x->x_viewOn = 0;
    x->x_maxSample = -999999;
    x->x_sampleCount = 0;
    x->x_clearClock = clock_new(x, (t_method)magicGlass_clearText);
    x->x_flashClock = clock_new(x, (t_method)magicGlass_flashText);
	x->x_maxSize = 1;
	x->x_issignal = 0;
	x->x_display_font = 9;
    return x;
}

void magicGlass_free(t_magicGlass *x)
{
400
	//fprintf(stderr,"magicglass_free\n");
401
	magicGlass_unbind(x);
402
403
    x->x_dspOn = 0;
    clock_free(x->x_clearClock);
Ivica Bukvic's avatar
Ivica Bukvic committed
404
	clock_free(x->x_flashClock);
405
	x = NULL;
406
407
408
409
410
}

void magicGlass_setup(void)
{
    magicGlass_class = class_new(gensym("magicGlass"),
411
                                 0,
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
                                 (t_method)magicGlass_free,
                                 sizeof(t_magicGlass),
                                 0,
                                 A_DEFFLOAT,
                                 0);
    CLASS_MAINSIGNALIN(magicGlass_class, t_magicGlass, x_sigF);
    class_addmethod(magicGlass_class,
      	            (t_method)magicGlass_dsp,
	            gensym("dsp"),
	            0);
    class_addbang(magicGlass_class, (t_method)magicGlass_bang);
    class_addfloat(magicGlass_class, (t_method)magicGlass_float);
    class_addsymbol(magicGlass_class, (t_method)magicGlass_symbol);
    class_addanything(magicGlass_class, (t_method)magicGlass_anything);
    class_addlist(magicGlass_class, (t_method)magicGlass_list);
}