forked from libpd/libpd
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathPdBase.hpp
More file actions
1321 lines (1161 loc) · 47.8 KB
/
PdBase.hpp
File metadata and controls
1321 lines (1161 loc) · 47.8 KB
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
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright (c) 2012-2017 Dan Wilcox <danomatika@gmail.com>
*
* BSD Simplified License.
* For information on usage and redistribution, and for a DISCLAIMER OF ALL
* WARRANTIES, see the file, "LICENSE.txt," in this distribution.
*
* See https://github.com/libpd/libpd for documentation
*
* This file was originally written for the ofxPd openFrameworks addon:
* https://github.com/danomatika/ofxPd
*
*/
#pragma once
#include "z_libpd.h"
#include "z_queued.h"
#include "z_print_util.h"
#include <map>
#include "PdTypes.hpp"
#include "PdReceiver.hpp"
#include "PdMidiReceiver.hpp"
// needed for libpd audio passing
#ifndef USEAPI_DUMMY
#define USEAPI_DUMMY
#endif
#ifndef HAVE_UNISTD_H
#define HAVE_UNISTD_H
#endif
typedef struct _atom t_atom;
namespace pd {
/// a Pure Data instance
///
/// use this class directly or extend it and any of its virtual functions
///
/// note: libpd currently does not support multiple states and it is
/// suggested that you use only one PdBase-derived object at a time
///
/// calls from multiple PdBase instances currently use a global context
/// kept in a singleton object, thus only one Receiver & one MidiReceiver
/// can be used within a single program
///
/// multiple context support will be added if/when it is included within
/// libpd
///
class PdBase {
public:
PdBase() {
clear();
PdContext::instance().addBase();
}
virtual ~PdBase() {
clear();
PdContext::instance().removeBase();
}
/// \section Initializing Pd
/// initialize resources and set up the audio processing
///
/// set the audio latency by setting the libpd ticks per buffer:
/// ticks per buffer * lib pd block size (always 64)
///
/// ie 4 ticks per buffer * 64 = buffer len of 512
///
/// you can call this again after loading patches & setting receivers
/// in order to update the audio settings
///
/// the lower the number of ticks, the faster the audio processing
/// if you experience audio dropouts (audible clicks), increase the
/// ticks per buffer
///
/// set queued = true to use the built in ringbuffers for message and
/// midi event passing, you will then need to call receiveMessages() and
/// receiveMidi() in order to pass messages from the ringbuffers to your
/// PdReceiver and PdMidiReceiver implementations
///
/// the queued ringbuffers are useful when you need to receive events
/// on a gui thread and don't want to use locking (aka the mutex)
///
/// return true if setup successfully
///
/// note: must be called before processing
///
virtual bool init(const int numInChannels, const int numOutChannels,
const int sampleRate, bool queued=false) {
PdContext::instance().clear();
return PdContext::instance().init(numInChannels,
numOutChannels,
sampleRate,
queued);
}
/// clear resources
virtual void clear() {
PdContext::instance().clear();
unsubscribeAll();
}
/// \section Adding Search Paths
/// add to the pd search path
/// takes an absolute or relative path (in data folder)
///
/// note: fails silently if path not found
///
virtual void addToSearchPath(const std::string& path) {
libpd_add_to_search_path(path.c_str());
}
/// clear the current pd search path
virtual void clearSearchPath() {
libpd_clear_search_path();
}
/// \section Opening Patches
/// open a patch file (aka somefile.pd) in a specified path
/// returns a Patch object
///
/// use Patch::isValid() to check if a patch was opened successfully:
///
/// Patch p1 = pd.openPatch("somefile.pd", "/some/path/");
/// if(!p1.isValid()) {
/// cout << "aww ... p1 couldn't be opened" << std::endl;
/// }
virtual pd::Patch openPatch(const std::string& patch,
const std::string& path) {
// [; pd open file folder(
void* handle = libpd_openfile(patch.c_str(), path.c_str());
if(handle == NULL) {
return Patch(); // return empty Patch
}
int dollarZero = libpd_getdollarzero(handle);
return Patch(handle, dollarZero, patch, path);
}
/// open a patch file using the filename and path of an existing patch
///
/// set the filename within the patch object or use a previously opened
/// object
///
/// // open an instance of "somefile.pd"
/// Patch p2("somefile.pd", "/some/path"); // set file and path
/// pd.openPatch(p2);
///
/// // open a new instance of "somefile.pd"
/// Patch p3 = pd.openPatch(p2);
///
/// // p2 and p3 refer to 2 different instances of "somefile.pd"
///
virtual pd::Patch openPatch(pd::Patch& patch) {
return openPatch(patch.filename(), patch.path());
}
/// close a patch file
/// takes only the patch's basename (filename without extension)
virtual void closePatch(const std::string& patch) {
// [; pd-name menuclose 1(
std::string patchname = (std::string) "pd-"+patch;
libpd_start_message(PdContext::instance().maxMsgLen);
libpd_add_float(1.0f);
libpd_finish_message(patchname.c_str(), "menuclose");
}
/// close a patch file, takes a patch object
/// note: clears the given Patch object
virtual void closePatch(pd::Patch& patch) {
if(!patch.isValid()) {
return;
}
libpd_closefile(patch.handle());
patch.clear();
}
/// \section Audio Processing
///
/// one of these must be called for audio dsp and message io to occur
///
/// inBuffer must be an array of the right size and never null
/// use inBuffer = new type[0] if no input is desired
///
/// outBuffer must be an array of size outBufferSize from openAudio call
///
/// note: raw does not interlace the buffers
///
/// process one pd tick, writes raw float data to/from buffers
/// returns false on error
bool processRaw(const float *inBuffer, float *outBuffer) {
return libpd_process_raw(inBuffer, outBuffer) == 0;
}
/// process short buffers for a given number of ticks
/// returns false on error
bool processShort(int ticks, const short *inBuffer, short *outBuffer) {
return libpd_process_short(ticks, inBuffer, outBuffer) == 0;
}
/// process float buffers for a given number of ticks
/// returns false on error
bool processFloat(int ticks, const float *inBuffer, float *outBuffer) {
bool ret = libpd_process_float(ticks, inBuffer, outBuffer) == 0;
return ret;
}
/// process double buffers for a given number of ticks
/// returns false on error
bool processDouble(int ticks, const double *inBuffer,
double *outBuffer) {
return libpd_process_double(ticks, inBuffer, outBuffer) == 0;
}
/// \section Audio Processing Control
/// start/stop audio processing
///
/// in general, once started, you won't need to turn off audio
///
/// shortcut for [; pd dsp 1( & [; pd dsp 0(
///
virtual void computeAudio(bool state) {
PdContext::instance().computeAudio(state);
}
/// \section Message Receiving
/// subscribe to messages sent by a pd send source
///
/// aka this like a virtual pd receive object
///
/// [r source]
/// |
///
virtual void subscribe(const std::string& source) {
if(exists(source)) {
std::cerr << "Pd: unsubscribe: ignoring duplicate source" << std::endl;
return;
}
;
void* pointer = libpd_bind(source.c_str());
if(pointer != NULL) {
std::map<std::string,void*>& sources = PdContext::instance().sources;
sources.insert(std::pair<std::string,void*>(source, pointer));
}
}
/// unsubscribe from messages sent by a pd send source
virtual void unsubscribe(const std::string& source) {
std::map<std::string,void*>& sources = PdContext::instance().sources;
std::map<std::string,void*>::iterator iter;
iter = sources.find(source);
if(iter == sources.end()) {
std::cerr << "Pd: unsubscribe: ignoring unknown source" << std::endl;
return;
}
libpd_unbind(iter->second);
sources.erase(iter);
}
/// is a pd send source subscribed?
virtual bool exists(const std::string& source) {
std::map<std::string,void*>& sources = PdContext::instance().sources;
if(sources.find(source) != sources.end()) {
return true;
}
return false;
}
//// receivers will be unsubscribed from *all* pd send sources
virtual void unsubscribeAll() {
std::map<std::string,void*>& sources = PdContext::instance().sources;
std::map<std::string,void*>::iterator iter;
for(iter = sources.begin(); iter != sources.end(); ++iter) {
libpd_unbind(iter->second);
}
sources.clear();
}
/// \section Receiving from the Message Queues
///
/// process the internal message queue if using the ringbuffer
///
/// internally, libpd will use a ringbuffer to pass messages & midi without
/// needing to require locking (mutexes) if you call init() with queued = true
///
/// call these in a loop somewhere in order to receive waiting messages
/// or midi data which are then sent to your PdReceiver & PdMidiReceiver
///
/// process waiting messages
virtual void receiveMessages() {
libpd_queued_receive_pd_messages();
}
/// process waiting midi messages
virtual void receiveMidi() {
libpd_queued_receive_midi_messages();
}
/// \section Event Receiving via Callbacks
/// set the incoming event receiver, disables the event queue
///
/// automatically receives from all currently subscribed sources
///
/// set this to NULL to disable callback receiving and re-enable the
/// event queue
///
void setReceiver(pd::PdReceiver* receiver) {
PdContext::instance().receiver = receiver;
}
/// \section Midi Receiving via Callbacks
/// set the incoming midi event receiver, disables the midi queue
///
/// automatically receives from all midi channels
///
/// set this to NULL to disable midi events and re-enable the midi queue
///
void setMidiReceiver(pd::PdMidiReceiver* midiReceiver) {
PdContext::instance().midiReceiver = midiReceiver;
}
/// \section Send Functions
/// send a bang message
virtual void sendBang(const std::string& dest) {
libpd_bang(dest.c_str());
}
/// send a float
virtual void sendFloat(const std::string& dest, float value) {
libpd_float(dest.c_str(), value);
}
/// send a symbol
virtual void sendSymbol(const std::string& dest, const std::string& symbol) {
libpd_symbol(dest.c_str(), symbol.c_str());
}
/// \section Sending Compound Messages
///
/// pd.startMessage();
/// pd.addSymbol("hello");
/// pd.addFloat(1.23);
/// pd.finishList("test"); // "test" is the receiver name in pd
///
/// sends [list hello 1.23( -> [r test],
/// you will need to use the [list trim] object on the receiving end
///
/// finishMsg sends a typed message -> [; test msg1 hello 1.23(
///
/// pd.startMessage();
/// pd.addSymbol("hello");
/// pd.addFloat(1.23);
/// pd.finishMessage("test", "msg1");
///
/// start a compound list or message
virtual void startMessage() {
PdContext& context = PdContext::instance();
if(context.bMsgInProgress) {
std::cerr << "Pd: Can not start message, message in progress" << std::endl;
return;
}
if(libpd_start_message(context.maxMsgLen) == 0) {
context.bMsgInProgress = true;
context.msgType = MSG;
}
}
/// add a float to the current compound list or message
virtual void addFloat(const float num) {
PdContext& context = PdContext::instance();
if(!context.bMsgInProgress) {
std::cerr << "Pd: Can not add float, message not in progress" << std::endl;
return;
}
if(context.msgType != MSG) {
std::cerr << "Pd: Can not add float, midi byte stream in progress" << std::endl;
return;
}
if(context.curMsgLen+1 >= context.maxMsgLen) {
std::cerr << "Pd: Can not add float, max message len of " << context.maxMsgLen << " reached" << std::endl;
return;
}
libpd_add_float(num);
context.curMsgLen++;
}
/// add a symbol to the current compound list or message
virtual void addSymbol(const std::string& symbol) {
PdContext& context = PdContext::instance();
if(!context.bMsgInProgress) {
std::cerr << "Pd: Can not add symbol, message not in progress" << std::endl;;
return;
}
if(context.msgType != MSG) {
std::cerr << "Pd: Can not add symbol, midi byte stream in progress" << std::endl;;
return;
}
if(context.curMsgLen+1 >= context.maxMsgLen) {
std::cerr << "Pd: Can not add symbol, max message len of " << context.maxMsgLen << " reached" << std::endl;
return;
}
libpd_add_symbol(symbol.c_str());
context.curMsgLen++;
}
/// finish and send as a list
virtual void finishList(const std::string& dest) {
PdContext& context = PdContext::instance();
if(!context.bMsgInProgress) {
std::cerr << "Pd: Can not finish list, message not in progress" << std::endl;
return;
}
if(context.msgType != MSG) {
std::cerr << "Pd: Can not finish list, midi byte stream in progress" << std::endl;
return;
}
libpd_finish_list(dest.c_str());
context.bMsgInProgress = false;
context.curMsgLen = 0;
}
/// finish and send as a list with a specific message name
virtual void finishMessage(const std::string& dest, const std::string& msg) {
PdContext& context = PdContext::instance();
if(!context.bMsgInProgress) {
std::cerr << "Pd: Can not finish message, message not in progress" << std::endl;
return;
}
if(context.msgType != MSG) {
std::cerr << "Pd: Can not finish message, midi byte stream in progress" << std::endl;
return;
}
libpd_finish_message(dest.c_str(), msg.c_str());
context.bMsgInProgress = false;
context.curMsgLen = 0;
}
/// send a list using the PdBase List type
///
/// List list;
/// list.addSymbol("hello");
/// list.addFloat(1.23);
/// pd.sstd::endlist("test", list);
///
/// sends [list hello 1.23( -> [r test]
///
/// stream operators work as well:
///
/// list << "hello" << 1.23;
/// pd.sstd::endlist("test", list);
///
virtual void sendList(const std::string& dest, const pd::List& list) {
PdContext& context = PdContext::instance();
if(context.bMsgInProgress) {
std::cerr << "Pd: Can not send list, message in progress" << std::endl;
return;
}
libpd_start_message(list.len());
context.bMsgInProgress = true;
// step through list
for(int i = 0; i < (int)list.len(); ++i) {
if(list.isFloat(i))
addFloat(list.getFloat(i));
else if(list.isSymbol(i))
addSymbol(list.getSymbol(i));
}
finishList(dest);
}
/// send a message using the PdBase List type
///
/// List list;
/// list.addSymbol("hello");
/// list.addFloat(1.23);
/// pd.sendMessage("test", "msg1", list);
///
/// sends a typed message -> [; test msg1 hello 1.23(
///
/// stream operators work as well:
///
// list << "hello" << 1.23;
/// pd.sendMessage("test", "msg1", list);
///
virtual void sendMessage(const std::string& dest,
const std::string& msg,
const pd::List& list = pd::List()) {
PdContext& context = PdContext::instance();
if(context.bMsgInProgress) {
std::cerr << "Pd: Can not send message, message in progress" << std::endl;
return;
}
libpd_start_message(list.len());
context.bMsgInProgress = true;
// step through list
for(int i = 0; i < (int)list.len(); ++i) {
if(list.isFloat(i))
addFloat(list.getFloat(i));
else if(list.isSymbol(i))
addSymbol(list.getSymbol(i));
}
finishMessage(dest, msg);
}
/// \section Sending MIDI
///
/// any out of range messages will be silently ignored
///
/// number ranges:
/// * channel 0 - 15 * dev# (dev #0: 0-15, dev #1: 16-31, etc)
/// * pitch 0 - 127
/// * velocity 0 - 127
/// * controller value 0 - 127
/// * program value 1 - 128
/// * bend value -8192 - 8191
/// * touch value 0 - 127
///
/// send a MIDI note on
///
/// pd does not use note off MIDI messages, so send a note on with vel = 0
///
virtual void sendNoteOn(const int channel,
const int pitch,
const int velocity=64) {
libpd_noteon(channel, pitch, velocity);
}
/// send a MIDI control change
virtual void sendControlChange(const int channel,
const int controller,
const int value) {
libpd_controlchange(channel, controller, value);
}
/// send a MIDI program change
///
/// in pd: [pgmin] and [pgmout] are 0 - 127
///
virtual void sendProgramChange(const int channel, const int value) {
libpd_programchange(channel, value);
}
/// send a MIDI pitch bend
///
/// in pd: [bendin] takes 0 - 16383 while [bendout] returns -8192 - 8192
///
virtual void sendPitchBend(const int channel, const int value) {
libpd_pitchbend(channel, value);
}
/// send a MIDI aftertouch
virtual void sendAftertouch(const int channel, const int value) {
libpd_aftertouch(channel, value);
}
/// send a MIDI poly aftertouch
virtual void sendPolyAftertouch(const int channel,
const int pitch,
const int value) {
libpd_polyaftertouch(channel, pitch, value);
}
/// send a raw MIDI byte
///
/// value is a raw midi byte value 0 - 255
/// port is the raw portmidi port #, similar to a channel
///
/// for some reason, [midiin], [sysexin] & [realtimein] add 2 to the
/// port num, so sending to port 1 in PdBase returns port 3 in pd
///
/// however, [midiout], [sysexout], & [realtimeout] do not add to the
/// port num, so sending port 1 to [midiout] returns port 1 in PdBase
///
virtual void sendMidiByte(const int port, const int value) {
libpd_midibyte(port, value);
}
/// send a raw MIDI sysex byte
virtual void sendSysex(const int port, const int value) {
libpd_sysex(port, value);
}
/// send a raw MIDI realtime byte
virtual void sendSysRealTime(const int port, const int value) {
libpd_sysrealtime(port, value);
}
/// \section Stream Interface
///
/// single messages
///
/// pd << Bang("test"); /// "test" is the receiver name in pd
/// pd << Float("test", 100);
/// pd << Symbol("test", "a symbol");
///
/// send a bang message
PdBase& operator<<(const pd::Bang& var) {
if(PdContext::instance().bMsgInProgress) {
std::cerr << "Pd: Can not send Bang, message in progress" << std::endl;
return *this;
}
sendBang(var.dest.c_str());
return *this;
}
/// send a float message
PdBase& operator<<(const pd::Float& var) {
if(PdContext::instance().bMsgInProgress) {
std::cerr << "Pd: Can not send Float, message in progress" << std::endl;
return *this;
}
sendFloat(var.dest.c_str(), var.num);
return *this;
}
/// send a symbol message
PdBase& operator<<(const pd::Symbol& var) {
if(PdContext::instance().bMsgInProgress) {
std::cerr << "Pd: Can not send Symbol, message in progress" << std::endl;
return *this;
}
sendSymbol(var.dest.c_str(), var.symbol.c_str());
return *this;
}
/// \section Stream Interface for Compound Messages
///
/// pd << StartMessage() << 100 << 1.2 << "a symbol" << FinishList("test");
///
/// start a compound message
PdBase& operator<<(const pd::StartMessage& var) {
startMessage();
return *this;
}
/// finish a compound message and send it as a list
PdBase& operator<<(const pd::FinishList& var) {
finishList(var.dest);
return *this;
}
/// finish a compound message and send it as a message
PdBase& operator<<(const pd::FinishMessage& var) {
finishMessage(var.dest, var.msg);
return *this;
}
// add a boolean as a float to the compound message
PdBase& operator<<(const bool var) {
addFloat((float) var);
return *this;
}
// add an integer as a float to the compound message
PdBase& operator<<(const int var) {
PdContext& context = PdContext::instance();
switch(context.msgType) {
case MSG:
addFloat((float) var);
break;
case MIDI:
sendMidiByte(context.midiPort, var);
break;
case SYSEX:
sendSysex(context.midiPort, var);
break;
case SYSRT:
sendSysRealTime(context.midiPort, var);
break;
}
return *this;
}
// add a float to the compound message
PdBase& operator<<(const float var) {
addFloat((float) var);
return *this;
}
// add a double as a float to the compound message
PdBase& operator<<(const double var) {
addFloat((float) var);
return *this;
}
// add a character as a symbol to the compound message
PdBase& operator<<(const char var) {
std::string s;
s = var;
addSymbol(s);
return *this;
}
// add a C-string char buffer as a symbol to the compound message
PdBase& operator<<(const char* var) {
addSymbol((std::string) var);
return *this;
}
// add a string as a symbol to the compound message
PdBase& operator<<(const std::string& var) {
addSymbol(var);
return *this;
}
/// \section Stream Interface for MIDI
///
/// pd << NoteOn(64) << NoteOn(64, 60) << NoteOn(64, 60, 1);
/// pd << ControlChange(100, 64) << ProgramChange(100, 1);
/// pd << Aftertouch(127, 1) << PolyAftertouch(64, 127, 1);
/// pd << PitchBend(2000, 1);
///
/// send a MIDI note on
PdBase& operator<<(const pd::NoteOn& var) {
sendNoteOn(var.channel, var.pitch, var.velocity);
return *this;
}
/// send a MIDI control change
PdBase& operator<<(const pd::ControlChange& var) {
sendControlChange(var.channel, var.controller, var.value);
return *this;
}
/// send a MIDI program change
PdBase& operator<<(const pd::ProgramChange& var) {
sendProgramChange(var.channel, var.value);
return *this;
}
/// send a MIDI pitch bend
PdBase& operator<<(const pd::PitchBend& var) {
sendPitchBend(var.channel, var.value);
return *this;
}
/// send a MIDI aftertouch
PdBase& operator<<(const pd::Aftertouch& var) {
sendAftertouch(var.channel, var.value);
return *this;
}
/// send a MIDI poly aftertouch
PdBase& operator<<(const pd::PolyAftertouch& var) {
sendPolyAftertouch(var.channel, var.pitch, var.value);
return *this;
}
/// \section Stream Interface for Raw Bytes
///
/// pd << StartMidi() << 0xEF << 0x45 << Finish();
/// pd << StartSysex() << 0xE7 << 0x45 << 0x56 << 0x17 << Finish();
///
/// start a raw byte MIDI message
PdBase& operator<<(const pd::StartMidi& var) {
PdContext& context = PdContext::instance();
if(context.bMsgInProgress) {
std::cerr << "Pd: Can not start MidiByte stream, "
<< "message in progress" << std::endl;
return *this;
}
context.bMsgInProgress = true;
context.msgType = MIDI;
context.midiPort = var.port;
return *this;
}
/// start a raw byte MIDI sysex message
PdBase& operator<<(const pd::StartSysex& var) {
PdContext& context = PdContext::instance();
if(context.bMsgInProgress) {
std::cerr << "Pd: Can not start Sysex stream, "
<< "message in progress" << std::endl;
return *this;
}
context.bMsgInProgress = true;
context.msgType = SYSEX;
context.midiPort = var.port;
return *this;
}
/// start a raw byte MIDI realtime message
PdBase& operator<<(const pd::StartSysRealTime& var) {
PdContext& context = PdContext::instance();
if(context.bMsgInProgress) {
std::cerr << "Pd: Can not start SysRealRime stream, "
<< "message in progress" << std::endl;
return *this;
}
context.bMsgInProgress = true;
context.msgType = SYSRT;
context.midiPort = var.port;
return *this;
}
/// finish and send a raw byte MIDI message
PdBase& operator<<(const pd::Finish& var) {
PdContext& context = PdContext::instance();
if(!context.bMsgInProgress) {
std::cerr << "Pd: Can not finish midi byte stream, "
<< "stream not in progress" << std::endl;
return *this;
}
if(context.msgType == MSG) {
std::cerr << "Pd: Can not finish midi byte stream, "
<< "message in progress" << std::endl;
return *this;
}
context.bMsgInProgress = false;
context.curMsgLen = 0;
return *this;
}
/// is a message or byte stream currently in progress?
bool isMessageInProgress() {
return PdContext::instance().bMsgInProgress;
}
/// \section Array Access
/// get the size of a pd array
/// returns 0 if array not found
int arraySize(const std::string& name) {
int len = libpd_arraysize(name.c_str());;
if(len < 0) {
std::cerr << "Pd: Cannot get size of unknown array \""
<< name << "\"" << std::endl;
return 0;
}
return len;
}
/// read from a pd array
///
/// resizes given vector to readLen, checks readLen and offset
///
/// returns true on success, false on failure
///
/// calling without setting readLen and offset reads the whole array:
///
/// vector<float> array1;
/// readArray("array1", array1);
///
virtual bool readArray(const std::string& name,
std::vector<float>& dest,
int readLen=-1, int offset=0) {
int len = libpd_arraysize(name.c_str());
if(len < 0) {
std::cerr << "Pd: Cannot read unknown array \""
<< name << "\"" << std::endl;
return false;
}
// full array len?
if(readLen < 0) {
readLen = len;
}
// check read len
else if(readLen > len) {
std::cerr << "Pd: Given read len " << readLen << " > len "
<< len << " of array \"" << name << "\"" << std::endl;
return false;
}
// check offset
if(offset+readLen > len) {
std::cerr << "Pd: Given read len and offset > len " << readLen
<< " of array \"" << name << "\"" << std::endl;
return false;
}
// resize if necessary
if(dest.size() != readLen) {
dest.resize(readLen, 0);
}
if(libpd_read_array(&dest[0], name.c_str(), offset, readLen) < 0) {
std::cerr << "Pd: libpd_read_array failed for array \""
<< name << "\"" << std::endl;
return false;
}
return true;
}
/// write to a pd array
///
/// calling without setting writeLen and offset writes the whole array:
///
/// writeArray("array1", array1);
///
virtual bool writeArray(const std::string& name,
std::vector<float>& source,
int writeLen=-1, int offset=0) {
int len = libpd_arraysize(name.c_str());
if(len < 0) {
std::cerr << "Pd: Cannot write to unknown array \""
<< name << "\"" << std::endl;
return false;
}
// full array len?
if(writeLen < 0) {
writeLen = len;
}
// check write len
else if(writeLen > len) {
std::cerr << "Pd: Given write len " << writeLen << " > len " << len
<< " of array \"" << name << "\"" << std::endl;
return false;
}
// check offset
if(offset+writeLen > len) {
std::cerr << "Pd: Given write len and offset > len " << writeLen
<< " of array \"" << name << "\"" << std::endl;
return false;
}
if(libpd_write_array(name.c_str(), offset,
&source[0], writeLen) < 0) {
std::cerr << "Pd: libpd_write_array failed for array \""
<< name << "\"" << std::endl;
return false;
}
return true;
}
/// clear array and set to a specific value
virtual void clearArray(const std::string& name, int value=0) {
int len = libpd_arraysize(name.c_str());
if(len < 0) {
std::cerr << "Pd: Cannot clear unknown array \""
<< name << "\"" << std::endl;
return;
}
std::vector<float> array;
array.resize(len, value);
if(libpd_write_array(name.c_str(), 0, &array[0], len) < 0) {
std::cerr << "Pd: libpd_write_array failed while clearing array \""
<< name << "\"" << std::endl;
}
}
/// \section Utils
/// has the global pd instance been initialized?
bool isInited() {
return PdContext::instance().isInited();
}
/// is the global pd instance using the ringerbuffer queue
/// for message padding?
bool isQueued() {
return PdContext::instance().isQueued();
}
/// get the blocksize of pd (sample length per channel)
static int blockSize() {
// shouldn't need to lock this for now, it's always 64
return libpd_blocksize();
}
/// set the max length of messages and lists, default: 32
void setMaxMessageLen(unsigned int len) {
PdContext::instance().maxMsgLen = len;
}
/// get the max length of messages and lists
unsigned int maxMessageLen() {
return PdContext::instance().maxMsgLen;
}
protected:
/// compound message status
enum MsgType {
MSG,
MIDI,
SYSEX,
SYSRT
};