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
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
|
/*
xsample - extended sample objects for Max/MSP and pd (pure data)
Copyright (c) 2001-2007 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.
*/
#include "main.h"
#include <stdio.h>
#ifdef _MSC_VER
#pragma warning (disable:4244)
#endif
extern "C++" {
class xrecord:
public xsample
{
FLEXT_HEADER_S(xrecord,xsample,setup)
public:
xrecord(int argc,const t_atom *argv);
void m_pos(float pos)
{
curpos = LIKELY(pos)?CASTINT<long>(pos/s2u+.5):0;
Update(xsc_pos);
Refresh();
}
inline void mg_pos(float &v) const { v = curpos*s2u; }
void m_start();
void m_stop();
inline void m_append(bool app)
{
appmode = app;
Update(xsc_play);
if(!appmode) m_pos(0);
}
void m_draw(int argc,const t_atom *argv);
protected:
int inchns;
bool sigmode,appmode;
float drintv;
bool dorec,doloop;
int mixmode;
long curpos; // in samples
virtual void DoReset();
virtual void DoUpdate(unsigned int flags);
virtual void CbSignal();
virtual void m_help();
virtual void m_print();
private:
static void setup(t_classid c);
TMPLSIGFUN(s_rec);
DEFSIGCALL(recfun);
FLEXT_CALLBACK(m_start)
FLEXT_CALLBACK(m_stop)
FLEXT_CALLVAR_F(mg_pos,m_pos)
FLEXT_CALLBACK(m_all)
FLEXT_CALLSET_F(m_min)
FLEXT_CALLSET_F(m_max)
FLEXT_CALLBACK_F(m_min)
FLEXT_CALLBACK_F(m_max)
FLEXT_ATTRVAR_B(doloop)
FLEXT_ATTRVAR_I(mixmode)
FLEXT_ATTRVAR_B(sigmode)
FLEXT_CALLSET_B(m_append)
FLEXT_ATTRGET_B(appmode)
FLEXT_CALLBACK_V(m_draw)
};
}
FLEXT_LIB_DSP_V("xrecord~",xrecord)
void xrecord::setup(t_classid c)
{
DefineHelp(c,"xrecord~");
FLEXT_CADDBANG(c,0,m_start);
FLEXT_CADDMETHOD_(c,0,"start",m_start);
FLEXT_CADDMETHOD_(c,0,"stop",m_stop);
FLEXT_CADDATTR_VAR(c,"pos",mg_pos,m_pos);
FLEXT_CADDATTR_VAR(c,"min",mg_min,m_min);
FLEXT_CADDATTR_VAR(c,"max",mg_max,m_max);
FLEXT_CADDMETHOD_(c,0,"all",m_all);
FLEXT_CADDMETHOD_(c,0,"draw",m_draw);
FLEXT_CADDATTR_VAR1(c,"loop",doloop);
FLEXT_CADDATTR_VAR1(c,"mixmode",mixmode);
FLEXT_CADDATTR_VAR1(c,"sigmode",sigmode);
FLEXT_CADDATTR_VAR(c,"append",appmode,m_append);
}
xrecord::xrecord(int argc,const t_atom *argv):
inchns(1),
sigmode(false),appmode(true),
drintv(0),
dorec(false),doloop(false),
mixmode(0)
{
int argi = 0;
#if FLEXT_SYS == FLEXT_SYS_MAX
if(argc > argi && CanbeInt(argv[argi])) {
inchns = GetAInt(argv[argi]);
argi++;
}
#endif
if(argc > argi && IsSymbol(argv[argi])) {
buf.Set(GetSymbol(argv[argi]),true);
argi++;
#if FLEXT_SYS == FLEXT_SYS_MAX
// oldstyle command line?
if(argi == 1 && argc == 2 && CanbeInt(argv[argi])) {
inchns = GetAInt(argv[argi]);
argi++;
post("%s: old style command line detected - please change to '%s [channels] [buffer]'",thisName(),thisName());
}
#endif
}
for(int ci = 0; ci < inchns; ++ci) {
char tmp[40];
STD::sprintf(tmp,ci == 0?"Messages/audio channel %i":"Audio channel %i",ci+1);
AddInSignal(tmp); // audio signals
}
AddInSignal("On/Off/Fade/Mix signal (0..1)"); // on/off signal
AddInFloat("Starting point of recording"); // min
AddInFloat("Ending point of recording"); // max
AddOutSignal("Current position of recording"); // pos signal
AddOutFloat("Starting point (rounded to frame)"); // min
AddOutFloat("Ending point (rounded to frame)"); // max
AddOutBang("Bang on loop end/rollover"); // loop bang
FLEXT_ADDMETHOD(inchns+1,m_min);
FLEXT_ADDMETHOD(inchns+2,m_max);
}
void xrecord::m_start()
{
ChkBuffer();
if(!sigmode && !appmode) { curpos = 0; Update(xsc_pos); }
dorec = true;
Update(xsc_startstop);
Refresh();
}
void xrecord::m_stop()
{
ChkBuffer();
dorec = false;
Update(xsc_startstop);
Refresh();
}
void xrecord::DoReset()
{
xsample::DoReset();
curpos = 0;
}
void xrecord::m_draw(int argc,const t_atom *argv)
{
if(argc >= 1) {
drintv = GetAInt(argv[0]);
if(dorec) buf.SetRefrIntv(drintv);
}
else
buf.Dirty(true);
}
TMPLDEF void xrecord::s_rec(int n,t_sample *const *invecs,t_sample *const *outvecs)
{
SIGCHNS(BCHNS,buf.Channels(),ICHNS,inchns);
const t_sample *const *sig = invecs;
register int si = 0;
const t_sample *on = invecs[inchns];
t_sample *pos = outvecs[0];
bool lpbang = false;
register const float pf = sclmul;
register long o = curpos;
if(o < curmin) o = curmin;
if(dorec && LIKELY(curmax > curmin)) {
while(n) {
long ncur = curmax-o; // at max to buffer or recording end
if(UNLIKELY(ncur <= 0)) { // end of buffer
if(doloop) {
ncur = curmax-(o = curmin);
}
else {
// loop expired;
dorec = false;
Update(xsc_startstop);
}
lpbang = true;
}
if(UNLIKELY(!dorec)) break;
if(UNLIKELY(ncur > n)) ncur = n;
register int i;
register t_sample *bf = buf.Data()+o*BCHNS;
register float p = scale(o);
if(sigmode) {
if(appmode) {
// append to current position
switch(mixmode) {
case 0:
for(i = 0; i < ncur; ++i,++si) {
if(!(*(on++) < 0)) {
for(int ci = 0; ci < ICHNS; ++ci)
bf[ci] = sig[ci][si];
bf += BCHNS;
*(pos++) = p,p += pf,++o;
}
else
*(pos++) = p;
}
break;
case 1:
for(i = 0; i < ncur; ++i,++si) {
register const t_sample g = *(on++);
if(!(g < 0)) {
for(int ci = 0; ci < ICHNS; ++ci)
bf[ci] = bf[ci]*(1.-g)+sig[ci][si]*g;
bf += BCHNS;
*(pos++) = p,p += pf,++o;
}
else
*(pos++) = p;
}
break;
case 2:
for(i = 0; i < ncur; ++i,++si) {
if(!(*(on++) < 0)) {
for(int ci = 0; ci < ICHNS; ++ci)
bf[ci] += sig[ci][si];
bf += BCHNS;
*(pos++) = p,p += pf,++o;
}
else
*(pos++) = p;
}
break;
}
}
else {
// don't append
switch(mixmode) {
case 0: {
for(i = 0; i < ncur; ++i,++si) {
if(!(*(on++) < 0))
{
for(int ci = 0; ci < ICHNS; ++ci)
bf[ci] = sig[ci][si];
bf += BCHNS;
*(pos++) = p,p += pf,++o;
}
else {
*(pos++) = p = scale(o = 0);
bf = buf.Data();
}
}
break;
}
case 1: {
for(i = 0; i < ncur; ++i,++si) {
register const t_sample g = *(on++);
if(!(g < 0)) {
for(int ci = 0; ci < ICHNS; ++ci)
bf[ci] = bf[ci]*(1.-g)+sig[ci][si]*g;
bf += BCHNS;
*(pos++) = p,p += pf,++o;
}
else {
*(pos++) = p = scale(o = 0);
bf = buf.Data();
}
}
break;
}
case 2: {
for(i = 0; i < ncur; ++i,++si) {
if(!(*(on++) < 0))
{
for(int ci = 0; ci < ICHNS; ++ci)
bf[ci] += sig[ci][si];
bf += BCHNS;
*(pos++) = p,p += pf,++o;
}
else {
*(pos++) = p = scale(o = 0);
bf = buf.Data();
}
}
break;
}
}
}
}
else {
// message mode
// Altivec optimization for that!
switch(mixmode) {
case 0: {
for(int ci = 0; ci < ICHNS; ++ci) {
register t_sample *b = bf+ci;
register const float *s = sig[ci]+si;
for(i = 0; i < ncur; ++i,b += BCHNS,++s)
*b = *s;
}
si += ncur;
break;
}
case 1: {
for(i = 0; i < ncur; ++i,++si) {
register const t_sample w = *(on++);
for(int ci = 0; ci < ICHNS; ++ci)
bf[ci] = bf[ci]*(1.-w)+sig[ci][si]*w;
bf += BCHNS;
}
break;
}
case 2: {
for(int ci = 0; ci < ICHNS; ++ci) {
register t_sample *b = bf+ci;
register const float *s = sig[ci]+si;
for(i = 0; i < ncur; ++i,b += BCHNS,++s)
*b += *s;
}
si += ncur;
break;
}
}
for(i = 0; i < ncur; ++i) {
*(pos++) = p,p += pf,++o;
}
}
n -= ncur;
}
curpos = o;
buf.Dirty();
}
if(n) {
register float p = scale(o);
while(n--) *(pos++) = p;
}
if(lpbang) ToOutBang(3);
}
void xrecord::CbSignal()
{
int ret = ChkBuffer(true);
if(ret) {
// call the appropriate dsp function
const lock_t l = Lock();
recfun(Blocksize(),InSig(),OutSig());
Unlock(l);
Refresh();
}
else
// set position signal to zero
ZeroSamples(OutSig()[0],Blocksize());
}
void xrecord::DoUpdate(unsigned int flags)
{
xsample::DoUpdate(flags);
if(flags&(xsc_pos|xsc_range)) {
if(curpos < curmin) curpos = curmin;
else if(curpos > curmax) curpos = curmax;
}
if(flags&xsc_range) {
ToOutFloat(1,curmin*s2u);
ToOutFloat(2,curmax*s2u);
}
if(flags&xsc_transport && buf.Ok()) {
if(dorec)
buf.SetRefrIntv(drintv);
else {
buf.Dirty(true);
buf.SetRefrIntv(0);
}
}
if(flags&xsc_play) {
switch(buf.Channels()*1000+inchns) {
case 1001: SETSIGFUN(recfun,TMPLFUN(s_rec,1,1)); break;
case 1002: SETSIGFUN(recfun,TMPLFUN(s_rec,1,2)); break;
case 2001: SETSIGFUN(recfun,TMPLFUN(s_rec,2,1)); break;
case 2002: SETSIGFUN(recfun,TMPLFUN(s_rec,2,2)); break;
case 4001:
case 4002:
case 4003: SETSIGFUN(recfun,TMPLFUN(s_rec,4,-1)); break;
case 4004: SETSIGFUN(recfun,TMPLFUN(s_rec,4,4)); break;
default: SETSIGFUN(recfun,TMPLFUN(s_rec,-1,-1)); break;
}
}
}
void xrecord::m_help()
{
post("%s - part of xsample objects, version " XSAMPLE_VERSION,thisName());
#ifdef FLEXT_DEBUG
post("compiled on " __DATE__ " " __TIME__);
#endif
post("(C) Thomas Grill, 2001-2007");
#if FLEXT_SYS == FLEXT_SYS_MAX
post("Arguments: %s [channels=1] [buffer]",thisName());
#else
post("Arguments: %s [buffer]",thisName());
#endif
post("Inlets: 1:Messages/Audio signal, 2:Trigger signal, 3:Min point, 4: Max point");
post("Outlets: 1:Position signal, 2:Min point, 3:Max point");
post("Methods:");
post("\thelp: shows this help");
post("\tset [name]: set buffer or reinit");
post("\tenable 0/1: turn dsp calculation off/on");
post("\treset: reset min/max recording points and recording offset");
post("\tprint: print current settings");
post("\t@sigmode 0/1: specify message or signal triggered recording");
post("\t@append 0/1: reset recording position or append to current position");
post("\t@loop 0/1: switches looping off/on");
post("\t@mixmode 0/1/2: specify how audio signal should be mixed in (none,mixed,added)");
post("\tmin {unit}: set minimum recording point");
post("\tmax {unit}: set maximum recording point");
post("\tall: select entire buffer length");
post("\tpos {unit}: set recording position (obeying the current scale mode)");
post("\tbang/start: start recording");
post("\tstop: stop recording");
post("\trefresh: checks buffer and refreshes outlets");
post("\t@units 0/1/2/3: set units to frames/buffer size/ms/s");
post("\t@sclmode 0/1/2/3: set range of position to units/units in loop/buffer/loop");
post("\tdraw [{float}]: redraw buffer immediately (arg omitted) or periodic (in ms)");
post("");
}
void xrecord::m_print()
{
static const char sclmode_txt[][20] = {"units","units in loop","buffer","loop"};
// print all current settings
post("%s - current settings:",thisName());
post("bufname = '%s', length = %.3f, channels = %i",buf.Name(),(float)(buf.Frames()*s2u),buf.Channels());
post("in channels = %i, frames/unit = %.3f, scale mode = %s",inchns,(float)(1./s2u),sclmode_txt[sclmode]);
post("sigmode = %s, append = %s, loop = %s, mixmode = %i",sigmode?"yes":"no",appmode?"yes":"no",doloop?"yes":"no",mixmode);
post("");
}
|