aboutsummaryrefslogtreecommitdiff
path: root/src/iso.c
blob: 3ca17797e79251e995f7d835d1248cab38cee3a4 (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
/* iso.c ---- queue up pitch and attack point series */
/*  by Charlie Baker (baker@foxtrot.ccmrc.ucsb.edu)  */
/*  Pd port by Olaf Matthes <olaf.matthes@gmx.de>    */

#include "m_pd.h"
#include <stdio.h>

#define MAXPOLY 32

static char *version = "iso v0.1, written for Max by Charlie Baker <baker@foxtrot.ccmrc.ucsb.edu>\n"
                       "          ported to Pd by Olaf Matthes <olaf.matthes@gmx.de>";

/* Iso object data structure */

typedef struct iso
{
	t_object iso_ob;		
	t_outlet *iso_out1;			/* outlet 1*/
	t_outlet *iso_out2;			/* outlet 2*/
	t_inlet *iso_in2;           /* inlet 2 (attack list) */
	t_clock *iso_clock;
	t_int ptchlength,atklength,curptch,curatk;
	t_float pitches[MAXPOLY];   
	t_float atks[MAXPOLY];
	t_int loop,stop;
	t_float hook,duty;
} t_iso;

static t_class *iso_class;

/* take list and create matrix */

static void iso_bang(t_iso *x)
{
	x->stop = 0;
	x->curptch = 0;
	x->curatk = 0;
	clock_delay(x->iso_clock, 0);
}


static void iso_clock_fun(t_iso *x)
{
	if (!x->stop) {
		clock_delay(x->iso_clock, (double)(x->atks[x->curatk] * x->hook)); 
		outlet_float(x->iso_out2,(t_float)(x->atks[x->curatk] * x->hook * x->duty)); 
		outlet_float(x->iso_out1,x->pitches[x->curptch] );
		if (x->loop) {
				x->curatk = ((x->curatk + 1) % x->atklength);
				x->curptch = ((x->curptch + 1) % x->ptchlength);
		}
		else {
			if (((x->curatk + 1) >= x->atklength) || ((x->curptch + 1) >= x->ptchlength)) 
				 x->stop = 1;
			else { 
				x->curptch += 1;
				x->curatk += 1;
			}
		}
	}
}

static void iso_hook(t_iso *x, t_floatarg hook)
{
	if (hook < 1.0) hook = 1.0;
	x->hook = (t_float)hook;
}
	
static void iso_duty(t_iso *x, t_floatarg duty)
{
	if (duty < 1.0) duty = 1.0;
	x->duty = (t_float)duty;
}

static void iso_list(t_iso *x, t_symbol *s, t_int argc, t_atom* argv)
{
	int i;
	if (argc > MAXPOLY) post("iso: only %d values max. allowed in list!", MAXPOLY);
	for (i = 0; i < argc; i++) x->atks[i] = argv[i].a_w.w_float;
	x->atklength = argc;
}

static void iso_pitch(t_iso *x, t_symbol *s, t_int argc, t_atom* argv)
{
	int i;
	if (argc > MAXPOLY) post("iso: only %d values max. allowed in list!", MAXPOLY);
	for (i = 0; i < argc; i++)  x->pitches[i] = argv[i].a_w.w_float;
	x->ptchlength = argc;
}

static void iso_start(t_iso *x, t_symbol *s, t_int argc, t_atom* argv)
{
	t_int start = atom_getfloatarg(0, argc, argv);
	x->stop = 0;
	if (start) {
		x->curptch = (t_int)((start - 1) % x->ptchlength);
		x->curatk = (t_int)((start - 1) % x->atklength);
			   }
	else	{
		x->curptch = 0;
		x->curatk = 0;
	}
	clock_delay(x->iso_clock, 0);
}

static void iso_stop(t_iso *x)
{
	x->stop = 1;
	x->curatk = 0;
	x->curptch = 0;
}

static void iso_pause(t_iso *x)
{
	x->stop = 1;
}

static void iso_loop(t_iso *x)
{
	x->loop = 1;
}

static void iso_resume(t_iso *x)
{
	x->stop = 0;
	clock_delay(x->iso_clock, 0);
}

static void iso_unloop(t_iso *x)
{
	x->loop = 0;
}


static void *iso_new(void) {
	t_iso *x = (t_iso *)pd_new(iso_class);	/* allocates memory and sticks in an inlet */
	x->iso_clock = clock_new(x, (t_method)iso_clock_fun);
	x->iso_out1 = outlet_new(&x->iso_ob, gensym("float"));
	x->iso_out2 = outlet_new(&x->iso_ob, gensym("float"));
	x->iso_in2 = inlet_new(&x->iso_ob, &x->iso_ob.ob_pd, gensym("list"), gensym("attack"));
	x->stop = 0;
	x->loop = 1;
	x->hook = 1.0;
	x->curptch = 0;
	x->curatk = 0;
	x->ptchlength = 1;
	x->atklength = 1;
	x->pitches[0] = 60;
	x->atks[0] = 500;
	x->duty = 1.0;
#ifndef MAXLIB
	post(version);
#endif
	return (x);					/* always return a copy of the created object */
}

static void iso_free(t_iso *x) {

	clock_free(x->iso_clock);
}

void iso_setup(void) {

    iso_class = class_new(gensym("iso"), (t_newmethod)iso_new,
    	(t_method)iso_free, sizeof(t_iso), 0, 0);
    class_addmethod(iso_class, (t_method)iso_duty, gensym("duty"), A_FLOAT, 0);
	class_addmethod(iso_class, (t_method)iso_list, gensym("attack"), A_GIMME, 0);
    class_addmethod(iso_class, (t_method)iso_start, gensym("start"), A_GIMME, 0);
    class_addmethod(iso_class, (t_method)iso_stop, gensym("stop"), 0);
    class_addmethod(iso_class, (t_method)iso_pause, gensym("pause"), 0);
    class_addmethod(iso_class, (t_method)iso_loop, gensym("loop"), 0);
    class_addmethod(iso_class, (t_method)iso_unloop, gensym("unloop"), 0);
    class_addmethod(iso_class, (t_method)iso_resume, gensym("resume"), 0);
    class_addmethod(iso_class, (t_method)iso_hook, gensym("hook"), A_FLOAT, 0);
    class_addbang(iso_class, iso_bang);
	class_addlist(iso_class, iso_pitch);
    class_sethelpsymbol(iso_class, gensym("maxlib/help-iso.pd"));
}