forked from libpd/libpd
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathPdTypes.hpp
More file actions
449 lines (348 loc) · 12 KB
/
PdTypes.hpp
File metadata and controls
449 lines (348 loc) · 12 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
/*
* 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 <string>
#include <vector>
#include <iostream>
#include <sstream>
namespace pd {
/// \section Pd Patch
/// a pd patch
///
/// if you use the copy constructor/operator, keep in mind the libpd void*
/// pointer patch handle is copied and problems can arise if one object is used
/// to close a patch that other copies may be referring to
class Patch {
public:
Patch() :
_handle(NULL), _dollarZero(0), _dollarZeroStr("0"),
_filename(""), _path("") {}
Patch(const std::string& filename, const std::string& path) :
_handle(NULL), _dollarZero(0), _dollarZeroStr("0"),
_filename(filename), _path(path) {}
Patch(void* handle, int dollarZero,
const std::string& filename,
const std::string& path) :
_handle(handle), _dollarZero(dollarZero),
_filename(filename), _path(path) {
std::stringstream itoa;
itoa << dollarZero;
_dollarZeroStr = itoa.str();
}
/// get the raw pointer to the patch instance
void* handle() const {return _handle;}
/// get the unqiue instance $0 ID
int dollarZero() const {return _dollarZero;}
/// get the patch filename
std::string filename() const {return _filename;}
/// get the parent path for the file
std::string path() const {return _path;}
/// get the unique instance $0 ID as a string
std::string dollarZeroStr() const {return _dollarZeroStr;}
/// is the patch pointer valid?
bool isValid() const {return _handle != NULL;}
/// clear patch pointer and dollar zero (does not close patch!)
///
/// note: does not clear filename and path so the object can be reused
// for opening multiple instances
void clear() {
_handle = NULL;
_dollarZero = 0;
_dollarZeroStr = "0";
}
/// copy constructor
Patch(const Patch& from) {
_handle = from._handle;
_dollarZero = from._dollarZero;
_dollarZeroStr = from._dollarZeroStr;
_filename = from._filename;
_path = from._path;
}
/// copy operator
void operator=(const Patch& from) {
_handle = from._handle;
_dollarZero = from._dollarZero;
_dollarZeroStr = from._dollarZeroStr;
_filename = from._filename;
_path = from._path;
}
/// print info to ostream
friend std::ostream& operator<<(std::ostream& os, const Patch& from) {
return os << "Patch: \"" << from.filename() << "\" $0: "
<< from.dollarZeroStr() << " valid: " << from.isValid();
}
private:
void* _handle; //< patch handle pointer
int _dollarZero; //< the unique $0 patch ID
std::string _dollarZeroStr; //< $0 as a string
std::string _filename; //< filename
std::string _path; //< full path to parent folder
};
/// \section Pd stream interface message objects
/// bang event
struct Bang {
const std::string dest; //< dest receiver name
explicit Bang(const std::string& dest) : dest(dest) {}
};
/// float value
struct Float {
const std::string dest; //< dest receiver name
const float num; //< the float value
Float(const std::string& dest, const float num) :
dest(dest), num(num) {}
};
/// symbol value
struct Symbol {
const std::string dest; //< dest receiver name
const std::string symbol; //< the symbol value
Symbol(const std::string& dest, const std::string& symbol) :
dest(dest), symbol(symbol) {}
};
/// a compound message containing floats and symbols
class List {
public:
List() {}
/// \section Read
/// check if index is a float type
bool isFloat(const unsigned int index) const {
if(index < objects.size())
if(objects[index].type == List::FLOAT)
return true;
return false;
}
/// check if index is a symbol type
bool isSymbol(const unsigned int index) const {
if(index < objects.size())
if(objects[index].type == List::SYMBOL)
return true;
return false;
}
/// get index as a float
float getFloat(const unsigned int index) const {
if(!isFloat(index)) {
std::cerr << "Pd: List: object " << index
<< " is not a float" << std::endl;
return 0;
}
return objects[index].value;
}
/// get index as a symbol
std::string getSymbol(const unsigned int index) const {
if(!isSymbol(index)) {
std::cerr << "Pd: List: object " << index
<< " is not a symbol" << std::endl;
return "";
}
return objects[index].symbol;
}
/// \section Write
///
/// add elements to the list
///
/// List list;
/// list.addSymbol("hello");
/// list.addFloat(1.23);
///
/// add a float to the list
void addFloat(const float num) {
MsgObject o;
o.type = List::FLOAT;
o.value = num;
objects.push_back(o);
typeString += 'f';
}
/// add a symbol to the list
void addSymbol(const std::string& symbol) {
MsgObject o;
o.type = List::SYMBOL;
o.symbol = symbol;
objects.push_back(o);
typeString += 's';
}
/// \section Write Stream Interface
///
/// list << "hello" << 1.23;
///
/// add a float to the message
List& operator<<(const bool var) {
addFloat((float) var);
return *this;
}
/// add a float to the message
List& operator<<(const int var) {
addFloat((float) var);
return *this;
}
/// add a float to the message
List& operator<<(const float var) {
addFloat((float) var);
return *this;
}
/// add a float to the message
List& operator<<(const double var) {
addFloat((float) var);
return *this;
}
/// add a symbol to the message
List& operator<<(const char var) {
std::string s;
s = var;
addSymbol(s);
return *this;
}
/// add a symbol to the message
List& operator<<(const char* var) {
addSymbol((std::string) var);
return *this;
}
/// add a symbol to the message
List& operator<<(const std::string& var) {
addSymbol((std::string) var);
return *this;
}
/// \section Util
/// return number of items
const unsigned int len() const {return (unsigned int) objects.size();}
/// return OSC style type string ie "fsfs"
const std::string& types() const {return typeString;}
/// clear all objects
void clear() {
typeString = "";
objects.clear();
}
/// get list as a string
std::string toString() const {
std::string line;
std::stringstream itoa;
for(int i = 0; i < (int)objects.size(); ++i) {
if(isFloat(i)) {
itoa << getFloat(i);
line += itoa.str();
itoa.str("");
}
else
line += getSymbol(i);
line += " ";
}
return line;
}
/// print to ostream
friend std::ostream& operator<<(std::ostream& os, const List& from) {
return os << from.toString();
}
private:
std::string typeString; //< OSC style type string
// object type
enum MsgType {
FLOAT,
SYMBOL
};
// object wrapper
struct MsgObject {
MsgType type;
float value;
std::string symbol;
};
std::vector<MsgObject> objects; //< list objects
};
/// start a compound message
struct StartMessage {
explicit StartMessage() {}
};
/// finish a compound message as a list
struct FinishList {
const std::string dest; //< dest receiver name
explicit FinishList(const std::string& dest) : dest(dest) {}
};
/// finish a compound message as a typed message
struct FinishMessage {
const std::string dest; //< dest receiver name
const std::string msg; //< target msg at the dest
FinishMessage(const std::string& dest, const std::string& msg) :
dest(dest), msg(msg) {}
};
/// \section Pd stream interface midi objects
/// ref: http://www.gweep.net/~prefect/eng/reference/protocol/midispec.html
/// send a note on event (set vel = 0 for noteoff)
struct NoteOn {
const int channel; //< channel (0 - 15 * dev#)
const int pitch; //< pitch (0 - 127)
const int velocity; //< velocity (0 - 127)
NoteOn(const int channel, const int pitch, const int velocity=64) :
channel(channel), pitch(pitch), velocity(velocity) {}
};
/// change a control value aka send a CC message
struct ControlChange {
const int channel; //< channel (0 - 15 * dev#)
const int controller; //< controller (0 - 127)
const int value; //< value (0 - 127)
ControlChange(const int channel, const int controller, const int value) :
channel(channel), controller(controller), value(value) {}
};
/// change a program value (ie an instrument)
struct ProgramChange {
const int channel; //< channel (0 - 15 * dev#)
const int value; //< value (0 - 127)
ProgramChange(const int channel, const int value) :
channel(channel), value(value) {}
};
/// change the pitch bend value
struct PitchBend {
const int channel; //< channel (0 - 15 * dev#)
const int value; //< value (-8192 - 8192)
PitchBend(const int channel, const int value) :
channel(channel), value(value) {}
};
/// change an aftertouch value
struct Aftertouch {
const int channel; //< channel (0 - 15 * dev#)
const int value; //< value (0 - 127)
Aftertouch(const int channel, const int value) :
channel(channel), value(value) {}
};
/// change a poly aftertouch value
struct PolyAftertouch {
const int channel; //< channel (0 - 15 * dev#)
const int pitch; //< pitch (0 - 127)
const int value; //< value (0 - 127)
PolyAftertouch(const int channel, const int pitch, const int value) :
channel(channel), pitch(pitch), value(value) {}
};
/// a raw midi byte
struct MidiByte {
const int port; //< raw portmidi port
//< see http://en.wikipedia.org/wiki/PortMidi
const unsigned char byte; //< the raw midi byte value
MidiByte(const int port, unsigned char byte) : port(port), byte(byte) {}
};
/// start a raw midi byte stream
struct StartMidi {
const int port; //< raw portmidi port
explicit StartMidi(const int port=0) : port(port) {}
};
/// start a raw sysex byte stream
struct StartSysex {
const int port; //< raw portmidi port
explicit StartSysex(const int port=0) : port(port) {}
};
/// start a sys realtime byte stream
struct StartSysRealTime {
const int port; //< raw portmidi port
explicit StartSysRealTime(const int port=0) : port(port) {}
};
/// finish a midi byte stream
struct Finish {
explicit Finish() {}
};
} // namespace