-
Notifications
You must be signed in to change notification settings - Fork 0
/
TRextern.h
667 lines (577 loc) · 18 KB
/
TRextern.h
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
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
//
// TRextern.h
// TRextern
//
// Created by Ragnar Hrafnkelsson on 01/12/2017.
// Copyright © 2017 Reactify. All rights reserved.
//
#pragma once
#include <memory>
#include <vector>
#include <string>
#ifdef PD
#include "m_pd.h"
#else
#include "ext.h"
#include "z_dsp.h"
#endif
//! Pointer to class
static t_class* m_class;
using InletRef = std::shared_ptr<class Inlet>;
using OutletRef = std::shared_ptr<class Outlet>;
//! Base external object
class TRextern {
public:
TRextern();
virtual ~TRextern();
//! Override to perfom setup
virtual void setup( int /*argc*/, t_atom* /*argv*/ ) {}
//! Override to free resources before exit
virtual void exit() {}
//! Override to process audio
virtual void process( t_sample **const /*inBuffers*/, t_sample **const /*outBuffers*/, long /*size*/ ) {};
//! Override to receive control values from inlets
virtual void bangReceived ( InletRef /*inlet*/ ) {}
virtual void intReceived ( InletRef /*inlet*/, long /*value*/ ) {}
virtual void floatReceived ( InletRef /*inlet*/, t_sample /*value*/ ) {}
virtual void symbolReceived( InletRef /*inlet*/, t_symbol* /*symbol*/ ) {}
// Audio in/out
virtual void setupIO( int inChannels, int outChannels ) final;
int const& inChannelCount() const { return mInChannels; }
int const& outChannelCount() const { return mOutChannels; }
//! Control in/out
InletRef addInletBang ( std::string identifier );
//! Passing optional value pointer creates a passive inlet
// which won't pass changes on to receivers below. TODO: passive inlets for Max?
InletRef addInletFloat ( std::string identifier, t_sample *f = nullptr );
InletRef addInletSymbol( std::string identifier, t_symbol *s = nullptr );
OutletRef addOutlet( std::string identifier );
const std::vector<InletRef>& getInlets() const { return mInlets; }
const std::vector<OutletRef>& getOutlets() const { return mOutlets; }
// Do not call. Used internally
virtual void layoutInOuts() final;
#ifdef PD
t_object* mObject; // Pointer to internal object-properties. Do not use.
#else
t_pxobject* mObject; // Pointer to internal object-properties. Do not use.
#endif
struct _external* mParent;
protected:
private:
//! Audio IO is handled internally
InletRef addInletSignal ( std::string identifier );
OutletRef addOutletSignal( std::string identifier );
void cleanup();
int mInChannels;
int mOutChannels;
std::vector<InletRef> mInlets;
std::vector<OutletRef> mOutlets;
};
//!
#ifndef PD
typedef void t_inlet;
typedef void t_outlet;
#endif
struct NonCopyable {
NonCopyable & operator=(const NonCopyable&) = delete;
NonCopyable(const NonCopyable&) = delete;
NonCopyable() = default;
};
class Inlet : NonCopyable {
friend TRextern;
public:
~Inlet();
std::string const& getId() const { return mId; }
t_symbol const* getType() const { return mType; }
bool isSignal() const;
protected:
//! Meant for internal instantation only
static InletRef create( t_inlet* inlet, t_symbol* type, std::string identifier );
t_inlet* mInlet;
private:
Inlet() {};
std::string mId;
t_symbol* mType;
};
class Outlet : NonCopyable {
friend TRextern;
public:
~Outlet();
std::string const getId() const;
t_symbol const* getType() const { return mType; }
void sendBang() const;
void sendFloat ( t_sample f ) const;
void sendSymbol( t_symbol *s ) const;
//void sendList( t_symbol *s );
bool isSignal() const;
protected:
//! Meant for internal instantation only
static OutletRef create( t_outlet* outlet, t_symbol* type, std::string identifier );
t_outlet* mOutlet;
private:
Outlet() {};
std::string mId;
t_symbol* mType;
};
//! Class dataspace
typedef struct _external {
#ifdef PD
t_object x_obj; // Internal object-properties
#else
t_pxobject x_obj;
long m_in; // space for the inlet number used by proxies
#endif
TRextern* impl;
} t_external;
// Forward declarations and class methods
void *ext_new( t_symbol *s, int argc, t_atom *argv );
#ifdef PD
void ext_dsp( t_external *x, t_signal **sp );
//! Bang receivers
typedef void (*t_bangfunc) (t_external *);
#define addBangFunc(num) \
void ext_bangin_##num( t_external *x ) { \
auto impl = x->impl; \
impl->bangReceived( impl->getInlets()[num-1] ); \
}
addBangFunc(1)
addBangFunc(2)
addBangFunc(3)
addBangFunc(4)
addBangFunc(5)
addBangFunc(6)
addBangFunc(7)
static std::vector<t_bangfunc> bangfuncs = {
ext_bangin_1,
ext_bangin_2,
ext_bangin_3,
ext_bangin_4,
ext_bangin_5,
ext_bangin_6,
ext_bangin_7
};
//! Float receivers
typedef void (*t_floatfunc)(t_external *, t_sample);
#define addFloatFunc(num) \
void ext_floatin_##num( t_external *x, t_sample f ) { \
auto impl = x->impl; \
impl->floatReceived( impl->getInlets()[num-1], f ); \
}
addFloatFunc(1)
addFloatFunc(2)
addFloatFunc(3)
addFloatFunc(4)
addFloatFunc(5)
addFloatFunc(6)
addFloatFunc(7)
static std::vector<t_floatfunc> floatfuncs = {
ext_floatin_1,
ext_floatin_2,
ext_floatin_3,
ext_floatin_4,
ext_floatin_5,
ext_floatin_6,
ext_floatin_7
};
//! Symbol receivers
typedef void (*t_symbolfunc)(t_external *, t_symbol*);
#define addSymbolFunc(num) \
void ext_symbolin_##num( t_external *x, t_symbol* s ) { \
auto impl = x->impl; \
impl->symbolReceived( impl->getInlets()[num-1], s ); \
}
addSymbolFunc(1)
addSymbolFunc(2)
addSymbolFunc(3)
addSymbolFunc(4)
addSymbolFunc(5)
addSymbolFunc(6)
addSymbolFunc(7)
static std::vector<t_symbolfunc> symbolfuncs = {
ext_symbolin_1,
ext_symbolin_2,
ext_symbolin_3,
ext_symbolin_4,
ext_symbolin_5,
ext_symbolin_6,
ext_symbolin_7
};
#else // Max
InletRef inletFromProxy( t_external *x ) {
auto idx = proxy_getinlet((t_object *)x);
return x->impl->getInlets()[idx];
}
void ext_bangin( t_external *x ) {
auto it = inletFromProxy(x);
if ( it->getType() == gensym("bang") ) {
x->impl->bangReceived( it );
} else {
post("Inlet expects %s", it->getType()->s_name);
}
}
void ext_floatin( t_external *x, t_sample value ) {
auto it = inletFromProxy(x);
if ( it->getType() == gensym("float") ) {
x->impl->floatReceived( it, value );
} else {
post("Inlet expects %s", it->getType()->s_name);
}
}
void ext_intin( t_external *x, long value ) {
auto it = inletFromProxy(x);
if ( it->getType() == gensym("int") ) {
x->impl->intReceived( it, value );
} else {
post("Inlet expects %s", it->getType()->s_name);
}
}
void ext_symbolin( t_external *x, t_symbol *s ) {
auto it = inletFromProxy(x);
if ( it->getType() == gensym("symbol") ) {
x->impl->symbolReceived( it, s );
} else {
post("Inlet expects %s", it->getType()->s_name);
}
}
#endif
//! TRextern Implmentation
//------------------------------------------------------------------------------
TRextern::TRextern() : mInChannels(0), mOutChannels(0) {}
//------------------------------------------------------------------------------
TRextern::~TRextern() {
cleanup();
}
//------------------------------------------------------------------------------
void TRextern::setupIO( int inChannels, int outChannels ) {
#ifdef PD
class_addmethod( m_class, (t_method)ext_dsp, gensym("dsp"), A_NULL );
#else
// Max dsp setup happens in layoutInOuts()
#endif
for ( auto i = 0; i < inChannels; i++ ) {
addInletSignal( "SignalIn " + std::to_string(i+1) );
}
for ( auto i = 0; i < outChannels; i++ ) {
addOutletSignal( "SignalOut " + std::to_string(i+1) );
}
mInChannels = inChannels;
mOutChannels = outChannels;
}
//------------------------------------------------------------------------------
InletRef TRextern::addInletBang( std::string identifier ) {
t_inlet* it = nullptr;
#ifdef PD
auto idx = mInlets.size();
auto symbol = gensym(("ext_bangin_" + std::to_string(idx+1)).c_str());
auto func = (t_method)bangfuncs[idx];
class_addmethod( m_class, func, symbol, A_NULL );
it = inlet_new( mObject, &mObject->ob_pd, &s_bang, symbol );
#endif
mInlets.push_back( Inlet::create( it, gensym("bang"), identifier ) );
return mInlets.back();
}
//------------------------------------------------------------------------------
InletRef TRextern::addInletFloat( std::string identifier, t_sample *f ) {
t_inlet* it = nullptr;
#ifdef PD
if ( f ) {
it = floatinlet_new( mObject, f );
} else {
auto idx = mInlets.size();
auto symbol = gensym(("ext_floatin_" + std::to_string(idx+1)).c_str());
auto func = (t_method)floatfuncs[idx];
class_addmethod( m_class, func, symbol, A_FLOAT, A_NULL );
it = inlet_new( mObject, &mObject->ob_pd, &s_float, symbol );
}
#endif
mInlets.push_back( Inlet::create( it, gensym("float"), identifier ) );
return mInlets.back();
}
//------------------------------------------------------------------------------
InletRef TRextern::addInletSymbol( std::string identifier, t_symbol* s ) {
t_inlet* it = nullptr;
#ifdef PD
if ( s ) {
it = symbolinlet_new( mObject, &s );
} else {
auto idx = mInlets.size();
auto symbol = gensym(("ext_symbolin_" + std::to_string(idx+1)).c_str());
auto func = (t_method)symbolfuncs[idx];
class_addmethod( m_class, func, symbol, A_SYMBOL, A_NULL );
it = inlet_new( mObject, &mObject->ob_pd, &s_symbol, symbol );
}
#endif
mInlets.push_back( Inlet::create( it, gensym("symbol"), identifier ) );
return mInlets.back();
}
//------------------------------------------------------------------------------
InletRef TRextern::addInletSignal( std::string identifier ) {
t_inlet* it = nullptr;
auto signal = gensym("signal");
#ifdef PD
it = inlet_new( mObject, &mObject->ob_pd, signal, signal );
#else
// Max audio inlets are empty placeholders
// Audio inlets are created by calling dsp_setup( mObject, inChannels );
#endif
mInlets.push_back( Inlet::create( it, signal, identifier ) );
return mInlets.back();
}
//! Outlets
//------------------------------------------------------------------------------
OutletRef TRextern::addOutlet( std::string identifier ) {
t_outlet* ot = nullptr;
#ifdef PD
ot = outlet_new( mObject, gensym( identifier.c_str()) );
#endif
mOutlets.push_back( Outlet::create( ot, gensym("control"), identifier ) );
return mOutlets.back();
}
//------------------------------------------------------------------------------
OutletRef TRextern::addOutletSignal( std::string identifier ) {
t_outlet* ot = nullptr;
#ifdef PD
ot = outlet_new( mObject, &s_signal );
#endif
mOutlets.push_back( Outlet::create( ot, gensym("signal"), identifier ) );
return mOutlets.back();
}
// Max inlet ordering is done from right to left so we need to
// loop through and create in right order after initial setup
void TRextern::layoutInOuts() {
#ifndef PD
// First we set up control inlets
for ( auto i = mInlets.size(); i-- > 0 ; ) {
auto it = mInlets[i];
if ( it->isSignal() ) {
it->mInlet = proxy_new( mParent, i, &mParent->m_in );
}
}
// Audio inlets. Audio inlets are always placed at the far left of an object
dsp_setup( mObject, inChannelCount() );
for ( auto i = mOutlets.size(); i-- > 0 ; ) {
auto ot = mOutlets[i];
ot->mOutlet = outlet_new( mParent, ot->getType()->s_name );
}
#endif
}
//------------------------------------------------------------------------------
void TRextern::cleanup() {
post("Cleaning up");
mInlets.clear();
mOutlets.clear();
exit();
}
//! Inlet
//------------------------------------------------------------------------------
InletRef Inlet::create( t_inlet* inlet, t_symbol* type, std::string identifier ) {
auto i = new Inlet;
i->mInlet = inlet;
i->mType = type;
i->mId = identifier;
return InletRef( i );
}
//------------------------------------------------------------------------------
Inlet::~Inlet() {
post("Deleting inlet");
if ( mInlet ) {
#ifdef PD
inlet_free( mInlet );
#else
proxy_delete( mInlet );
#endif
}
}
//------------------------------------------------------------------------------
bool Inlet::isSignal() const {
return mType == gensym("signal");
}
//! Outlet
//------------------------------------------------------------------------------
OutletRef Outlet::create( t_outlet* outlet, t_symbol* type, std::string identifier ) {
auto i = new Outlet;
i->mOutlet = outlet;
i->mId = identifier;
i->mType = type;
return OutletRef( i );
}
//------------------------------------------------------------------------------
Outlet::~Outlet() {
post("Deleting outlet");
#ifdef PD
outlet_free( mOutlet );
#else
outlet_delete( mOutlet );
#endif
}
const std::string Outlet::getId() const {
#ifdef PD
return std::string(outlet_getsymbol(mOutlet)->s_name);
#else
return mId;
#endif
}
//------------------------------------------------------------------------------
void Outlet::sendBang() const {
outlet_bang(mOutlet);
}
//------------------------------------------------------------------------------
void Outlet::sendFloat( t_sample f ) const {
outlet_float(mOutlet, f);
}
//------------------------------------------------------------------------------
void Outlet::sendSymbol( t_symbol *s ) const {
#ifdef PD
outlet_symbol(mOutlet, s);
#else
#warning TODO MAX
#endif
}
bool Outlet::isSignal() const {
return mType == gensym("signal");
}
#ifdef PD
//! DSP routines
//------------------------------------------------------------------------------
t_int *ext_perform( t_int *w ) {
size_t vIndex = 1;
auto impl = ((t_external *)w[vIndex++])->impl;
auto const numIn = impl->inChannelCount();
auto const numOut = impl->outChannelCount();
t_sample **const bIn = (t_sample **)alloca(numIn * sizeof(t_sample *));
for ( auto i = 0; i < numIn; i++ ) {
bIn[i] = (t_sample *)w[i+vIndex];
vIndex++;
}
t_sample **const bOut = (t_sample **)alloca(numOut * sizeof(t_sample *));
for ( auto i = 0; i < numOut; i++ ) {
bOut[i] = (t_sample *)w[i+vIndex];
vIndex++;
}
impl->process( bIn, bOut, (long)w[vIndex++] /*n*/ );
return (w+vIndex);
}
// #define USE_DEBUG
//------------------------------------------------------------------------------
void ext_dsp( t_external *x, t_signal **sp ) {
auto impl = x->impl;
#ifndef USE_DEBUG
auto const ins = impl->inChannelCount();
auto const outs = impl->outChannelCount();
auto const numChannels = ins + outs;
// + 2 for x and sp[0]->s_n (class ptr and vector size)
auto const vSize = numChannels + 2;
// Add properties to vector.
// First element is pointer to class
t_int v[vSize];
v[0] = (t_int)x;
// Inlets
auto vIndex = 1;
for ( auto i = 0; i < ins; i++ ) {
v[vIndex] = (t_int)sp[i]->s_vec;
vIndex++;
}
// Signal vector is ordered according to graphical representation of
// object so first audio outlet will come after all audio inlets
for ( auto i = ins; i < numChannels; i++ ) {
v[vIndex] = (t_int)sp[i]->s_vec;
vIndex++;
}
// Add vector size last. All signal vectors of a patch are same size
v[vIndex] = (t_int)sp[0]->s_n;
dsp_addv(ext_perform, vSize, v);
#else
dsp_add(ext_perform, 5, x,
sp[0]->s_vec, sp[1]->s_vec, sp[2]->s_vec, sp[0]->s_n);
#endif
}
#else // Max
//------------------------------------------------------------------------------
void ext_perform64(t_external *x, t_object *dsp64, t_sample **ins, long numins, t_sample **outs, long numouts, long sampleframes, long flags, void *userparam)
{
x->impl->process( ins, outs, sampleframes );
}
//------------------------------------------------------------------------------
void ext_dsp64(t_external *x, t_object *dsp64, short *count, t_sample samplerate, long maxvectorsize, long flags)
{
object_method(dsp64, gensym("dsp_add64"), x, ext_perform64, 0, NULL);
}
#endif
//------------------------------------------------------------------------------
t_external *ext_alloc() {
#ifdef PD
return (t_external *)pd_new(m_class);
#else
return (t_external *)object_alloc(m_class);
#endif
}
//------------------------------------------------------------------------------
void ext_free( t_external *x ) {
#ifndef PD
dsp_free((t_pxobject *)x);
#endif
delete x->impl;
}
//------------------------------------------------------------------------------
void tr_initialise (std::string title )
{
#ifdef PD
m_class = class_new (gensym (title.c_str()),
(t_newmethod)ext_new,
(t_method)ext_free,
sizeof (t_external),
CLASS_NOINLET,
A_GIMME,
A_NULL);
#else
m_class = class_new (title,
(method)ext_new,
(method)ext_free,
sizeof (t_external),
0L,
A_GIMME,
0);
class_addmethod(m_class, (method)ext_bangin, "bang", 0);
class_addmethod(m_class, (method)ext_floatin, "float", A_FLOAT, 0);
class_addmethod(m_class, (method)ext_intin, "int", A_LONG, 0);
class_addmethod(m_class, (method)ext_symbolin,"symbol", A_SYM, 0);
// class_addmethod(m_class, (method)ext_list, "list", A_GIMME, 0);
// class_addmethod(m_class, (method)ext_anything, "anything", A_GIMME, 0);
#warning TODO MAX
// TODO: Support for non DSP objects
// If dsp
class_addmethod(m_class, (method)ext_dsp64, "dsp64", A_CANT, 0);
class_dspinit(m_class);
// endif
class_register(CLASS_BOX, m_class);
#endif
}
// Replaces occurences of '_tilde' with ~
std::string tr_tildefy (std::string title)
{
static std::string tilde ("_tilde");
if (title.find (tilde) != std::string::npos)
title.replace (title.find (tilde), tilde.length(), "~" );
return title;
}
#define PD_SETUP(NAME) NAME ## _setup
#define TREXTERN_CREATE( CLASS ) \
\
void *ext_new( t_symbol* /*s*/, int argc, t_atom *argv ) { \
t_external *x = ext_alloc(); \
x->impl = new CLASS; \
x->impl->mObject = &x->x_obj; \
x->impl->mParent = x; \
x->impl->setup(argc, argv); \
x->impl->layoutInOuts(); \
return (x); \
} \
\
extern "C" __attribute__((visibility("default"))) \
void PD_SETUP(CLASS)(void) { \
tr_initialise(tr_tildefy(#CLASS)); \
} \
\
void ext_main(void* /*r*/) { \
tr_initialise(tr_tildefy(#CLASS)); \
} \