aboutsummaryrefslogtreecommitdiff
path: root/cyclone/sickle/peek.c
blob: 739757755b5015201600ffda6f133f29a833acea (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
/* Copyright (c) 2002-2003 krzYszcz and others.
 * For information on usage and redistribution, and for a DISCLAIMER OF ALL
 * WARRANTIES, see the file, "LICENSE.txt," in this distribution.  */

/* LATER: 'click' method */

#include <string.h>
#include "m_pd.h"
#include "sickle/sic.h"
#include "sickle/arsic.h"

#define PEEK_MAXCHANNELS  4  /* LATER implement arsic resizing feature */

typedef struct _peek
{
    t_arsic   x_arsic;
    int       x_maxchannels;
    int       x_effchannel;  /* effective channel (clipped reqchannel) */
    int       x_reqchannel;  /* requested channel */
    int       x_clipmode;
    int       x_pokemode;
    t_float   x_value;
    t_clock  *x_clock;
    double    x_clocklasttick;
    int       x_clockset;
} t_peek;

static t_class *peek_class;

static void peek_tick(t_peek *x)
{
    arsic_redraw((t_arsic *)x);  /* LATER redraw only dirty channel(s!) */
    x->x_clockset = 0;
    x->x_clocklasttick = clock_getsystime();
}

static void peek_set(t_peek *x, t_symbol *s)
{
    arsic_setarray((t_arsic *)x, s, 1);
}

#define peek_doclip(f)  (f < -1. ? -1. : (f > 1. ? 1. : f))

/* CHECKED refman error: ``if the number received in the left inlet
   specifies a sample index that does not exist in the buffer~ object's
   currently allocated memory, nothing happens.''  This is plainly wrong,
   at least for max/msp 4.0.7 bundle: the index is clipped (just like
   in tabread/tabwrite).   As a kind of an experiment, lets make this
   the refman's way... */
static void peek_float(t_peek *x, t_float f)
{
    t_arsic *sic = (t_arsic *)x;
    t_float *vp;
    arsic_validate(sic, 0);  /* LATER rethink (efficiency, and complaining) */
    if (vp = sic->s_vectors[x->x_effchannel])
    {
	int ndx = (int)f;
	if (vp && ndx >= 0 && ndx < sic->s_vecsize)
	{
	    if (x->x_pokemode)
	    {
		double timesince;
		t_float f = x->x_value;
		vp[ndx] = (x->x_clipmode ? peek_doclip(f) : f);
		x->x_pokemode = 0;
		timesince = clock_gettimesince(x->x_clocklasttick);
		if (timesince > 1000) peek_tick(x);
		else if (!x->x_clockset)
		{
		    clock_delay(x->x_clock, 1000 - timesince);
		    x->x_clockset = 1;
		}
	    }
	    /* CHECKED: output not clipped */
	    else outlet_float(((t_object *)x)->ob_outlet, vp[ndx]);
	}
    }
}

static void peek_ft1(t_peek *x, t_floatarg f)
{
    x->x_value = f;
    x->x_pokemode = 1;
    /* CHECKED: poke-mode is reset only after receiving left inlet input
       -- it is kept across 'ft2', 'clip', and 'set' inputs. */
}

static void peek_ft2(t_peek *x, t_floatarg f)
{
    if ((x->x_reqchannel = (f > 1 ? (int)f - 1 : 0)) > x->x_maxchannels)
	x->x_effchannel = x->x_maxchannels - 1;
    else
	x->x_effchannel = x->x_reqchannel;
}

static void peek_clip(t_peek *x, t_floatarg f)
{
    x->x_clipmode = ((int)f != 0);
}

static void peek_free(t_peek *x)
{
    if (x->x_clock) clock_free(x->x_clock);
    arsic_free((t_arsic *)x);
}

static void *peek_new(t_symbol *s, t_floatarg f1, t_floatarg f2)
{
    int ch = (f1 > 0 ? (int)f1 : 0);
    t_peek *x = (t_peek *)arsic_new(peek_class, s,
				    (ch ? PEEK_MAXCHANNELS : 0), 0, 0);
    if (x)
    {
	if (ch > PEEK_MAXCHANNELS)
	    ch = PEEK_MAXCHANNELS;
	x->x_maxchannels = (ch ? PEEK_MAXCHANNELS : 1);
	x->x_effchannel = x->x_reqchannel = (ch ? ch - 1 : 0);
	/* CHECKED (refman error) clipping is disabled by default */
	x->x_clipmode = ((int)f2 != 0);
	x->x_pokemode = 0;
	inlet_new((t_object *)x, (t_pd *)x, &s_float, gensym("ft1"));
	inlet_new((t_object *)x, (t_pd *)x, &s_float, gensym("ft2"));
	outlet_new((t_object *)x, &s_float);
	x->x_clock = clock_new(x, (t_method)peek_tick);
	x->x_clocklasttick = clock_getsystime();
	x->x_clockset = 0;
    }
    return (x);
}

void peek_tilde_setup(void)
{
    peek_class = class_new(gensym("peek~"),
			   (t_newmethod)peek_new,
			   (t_method)peek_free,
			   sizeof(t_peek), 0,
			   A_DEFSYM, A_DEFFLOAT, A_DEFFLOAT, 0);
    class_addfloat(peek_class, peek_float);
    class_addmethod(peek_class, (t_method)peek_set,
		    gensym("set"), A_SYMBOL, 0);
    class_addmethod(peek_class, (t_method)peek_ft1,
		    gensym("ft1"), A_FLOAT, 0);
    class_addmethod(peek_class, (t_method)peek_ft2,
		    gensym("ft2"), A_FLOAT, 0);
    class_addmethod(peek_class, (t_method)peek_clip,
		    gensym("clip"), A_FLOAT, 0);
}