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
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
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
|
/*
flext - C++ layer for Max/MSP and pd (pure data) externals
Copyright (c) 2001-2005 Thomas Grill (gr@grrrr.org)
For information on usage and redistribution, and for a DISCLAIMER OF ALL
WARRANTIES, see the file, "license.txt," in this distribution.
*/
/*! \file flbuf.cpp
\brief Implementation of the buffer abstraction class.
*/
#include "flext.h"
#include "flfeatures.h"
#if FLEXT_SYS != FLEXT_SYS_JMAX
#if FLEXT_SYS == FLEXT_SYS_PD
#define DIRTY_INTERVAL 0 // buffer dirty check in msec
#endif
#if FLEXT_SYS == FLEXT_SYS_MAX
// defined in flsupport.cpp
extern const t_symbol *sym_buffer,*sym_size;
#endif
flext::buffer::buffer(const t_symbol *bn,bool delayed):
sym(NULL),data(NULL),
chns(0),frames(0)
{
#if FLEXT_SYS == FLEXT_SYS_PD
arr = NULL;
interval = DIRTY_INTERVAL;
isdirty = false;
ticking = false;
tick = clock_new(this,(t_method)cb_tick);
#endif
if(bn) Set(bn,delayed);
ClearDirty();
}
flext::buffer::~buffer()
{
#if FLEXT_SYS == FLEXT_SYS_PD
clock_free(tick);
#endif
}
int flext::buffer::Set(const t_symbol *s,bool nameonly)
{
int ret = 0;
bool valid = data != NULL; // valid now? (before change)
if(s && sym != s) {
ret = 1;
data = NULL;
frames = 0;
chns = 0;
}
if(s && *GetString(s)) sym = s;
if(!sym) {
if(valid) ret = -1;
}
else if(!nameonly) {
#if FLEXT_SYS == FLEXT_SYS_PD
int frames1;
t_sample *data1;
arr = (t_garray *)pd_findbyclass(const_cast<t_symbol *>(sym), garray_class);
if(!arr)
{
if (*GetString(sym)) FLEXT_LOG1("buffer: no such array '%s'",GetString(sym));
// sym = NULL;
if(valid) ret = -1;
}
else if(!garray_getfloatarray(arr, &frames1, &data1))
{
error("buffer: bad template '%s'",GetString(sym));
data = NULL;
frames = 0;
if(valid) ret = -1;
}
else {
ret = 0;
garray_usedindsp(arr);
if(frames != frames1) { frames = frames1; if(!ret) ret = 1; }
if(data != data1) { data = data1; if(!ret) ret = 1; }
chns = 1;
}
#elif FLEXT_SYS == FLEXT_SYS_MAX
if(sym->s_thing) {
const t_buffer *p = (const t_buffer *)sym->s_thing;
FLEXT_ASSERT(!NOGOOD(p));
if(ob_sym(p) != sym_buffer) {
post("buffer: object '%s' not valid (type %s)",GetString(sym),GetString(ob_sym(p)));
if(valid) ret = -2;
}
else {
#ifdef FLEXT_DEBUG
// post("flext: buffer object '%s' - valid:%i samples:%i channels:%i frames:%i",GetString(sym),p->b_valid,p->b_frames,p->b_nchans,p->b_frames);
#endif
if(data != p->b_samples) { data = p->b_samples; if(!ret) ret = 1; }
if(chns != p->b_nchans) { chns = p->b_nchans; if(!ret) ret = 1; }
if(frames != p->b_frames) { frames = p->b_frames; if(!ret) ret = 1; }
}
}
else {
FLEXT_LOG1("buffer: symbol '%s' not defined", GetString(sym));
/*if(valid)*/ ret = -1;
}
#else
#error not implemented
#endif
}
return ret;
}
bool flext::buffer::Update()
{
FLEXT_ASSERT(sym);
bool upd = false;
#if FLEXT_SYS == FLEXT_SYS_PD
if(!arr) return data == NULL;
int frames1;
t_sample *data1;
if(!garray_getfloatarray(arr, &frames1, &data1)) {
data = NULL;
chns = 0;
frames = 0;
upd = true;
}
else if(data != data1 || frames != frames1) {
data = data1;
frames = frames1;
upd = true;
}
#elif FLEXT_SYS == FLEXT_SYS_MAX
const t_buffer *p = (const t_buffer *)sym->s_thing;
if(p) {
FLEXT_ASSERT(!NOGOOD(p) && ob_sym(p) == sym_buffer);
if(data != p->b_samples || chns != p->b_nchans || frames != p->b_frames) {
data = p->b_samples;
chns = p->b_nchans;
frames = p->b_frames;
upd = true;
}
}
else {
// buffer~ has e.g. been renamed
data = NULL;
chns = 0;
frames = 0;
upd = true;
}
#else
#error not implemented
#endif
return upd;
}
flext::buffer::lock_t flext::buffer::Lock()
{
FLEXT_ASSERT(sym);
#if FLEXT_SYS == FLEXT_SYS_PD
FLEXT_ASSERT(arr);
#ifdef _FLEXT_HAVE_PD_GARRAYLOCKS
garray_lock(arr);
#endif
return false;
#elif FLEXT_SYS == FLEXT_SYS_MAX
t_buffer *p = (t_buffer *)sym->s_thing;
FLEXT_ASSERT(p);
#ifdef _FLEXT_HAVE_MAX_INUSEFLAG
long old = p->b_inuse;
p->b_inuse = 1;
return old;
#else
return 0;
#endif
#else
#error not implemented
#endif
}
void flext::buffer::Unlock(flext::buffer::lock_t prv)
{
FLEXT_ASSERT(sym);
#if FLEXT_SYS == FLEXT_SYS_PD
FLEXT_ASSERT(arr);
#ifdef _FLEXT_HAVE_PD_GARRAYLOCKS
garray_unlock(arr);
#endif
#elif FLEXT_SYS == FLEXT_SYS_MAX
t_buffer *p = (t_buffer *)sym->s_thing;
FLEXT_ASSERT(p);
#ifdef _FLEXT_HAVE_MAX_INUSEFLAG
p->b_inuse = prv;
#endif
#else
#error not implemented
#endif
}
void flext::buffer::Frames(int fr,bool keep,bool zero)
{
FLEXT_ASSERT(sym);
#if FLEXT_SYS == FLEXT_SYS_PD
// is this function guaranteed to keep memory and set rest to zero?
::garray_resize(arr,(float)fr);
Update();
#elif FLEXT_SYS == FLEXT_SYS_MAX
t_sample *tmp = NULL;
int sz = frames;
if(fr < sz) sz = fr;
if(keep) {
// copy buffer data to tmp storage
tmp = (t_sample *)NewAligned(sz*sizeof(t_sample));
FLEXT_ASSERT(tmp);
CopySamples(tmp,data,sz);
}
t_atom msg;
t_buffer *buf = (t_buffer *)sym->s_thing;
// b_msr reflects buffer sample rate... is this what we want?
// Max bug: adding half a sample to prevent roundoff errors....
float ms = (fr+0.5)/buf->b_msr;
SetFloat(msg,ms);
::typedmess((object *)buf,(t_symbol *)sym_size,1,&msg);
Update();
if(tmp) {
// copy data back
CopySamples(data,tmp,sz);
FreeAligned(tmp);
if(zero && sz < fr) ZeroSamples(data+sz,fr-sz);
}
else
if(zero) ZeroSamples(data,fr);
#else
#error
#endif
}
#if FLEXT_SYS == FLEXT_SYS_PD
void flext::buffer::SetRefrIntv(float intv)
{
interval = intv;
if(interval == 0 && ticking) {
clock_unset(tick);
ticking = false;
}
}
#elif FLEXT_SYS == FLEXT_SYS_MAX
void flext::buffer::SetRefrIntv(float) {}
#else
#error
#endif
void flext::buffer::Dirty(bool force)
{
FLEXT_ASSERT(sym);
#if FLEXT_SYS == FLEXT_SYS_PD
if((!ticking) && (interval || force)) {
ticking = true;
cb_tick(this); // immediately redraw
}
else {
if(force) clock_delay(tick,0);
isdirty = true;
}
#elif FLEXT_SYS == FLEXT_SYS_MAX
t_buffer *p = (t_buffer *)sym->s_thing;
FLEXT_ASSERT(p && !NOGOOD(p));
p->b_modtime = gettime();
#else
#error Not implemented
#endif
}
#if FLEXT_SYS == FLEXT_SYS_PD
void flext::buffer::cb_tick(buffer *b)
{
if(b->arr) garray_redraw(b->arr);
#ifdef FLEXT_DEBUG
else error("buffer: array is NULL");
#endif
if(b->isdirty && b->interval) {
b->isdirty = false;
b->ticking = true;
clock_delay(b->tick,b->interval);
}
else
b->ticking = false;
}
#endif
void flext::buffer::ClearDirty()
{
#if FLEXT_SYS == FLEXT_SYS_PD
cleantime = clock_getlogicaltime();
#elif FLEXT_SYS == FLEXT_SYS_MAX
cleantime = gettime();
#else
#error Not implemented
#endif
}
bool flext::buffer::IsDirty() const
{
if(!sym) return false;
#if FLEXT_SYS == FLEXT_SYS_PD
#ifdef _FLEXT_HAVE_PD_GARRAYUPDATETIME
return arr && (isdirty || garray_updatetime(arr) > cleantime);
#else
// Don't know.... (no method in PD judging whether buffer has been changed from outside flext...)
return true;
#endif
#elif FLEXT_SYS == FLEXT_SYS_MAX
t_buffer *p = (t_buffer *)sym->s_thing;
FLEXT_ASSERT(p && !NOGOOD(p));
return p->b_modtime > cleantime;
#else
#error Not implemented
#endif
}
#endif // Jmax
|