Mercurial > hg > touchkeys
comparison Source/TouchKeys/Osc.h @ 0:3580ffe87dc8
First commit of TouchKeys public pre-release.
author | Andrew McPherson <andrewm@eecs.qmul.ac.uk> |
---|---|
date | Mon, 11 Nov 2013 18:19:35 +0000 |
parents | |
children | 36fe60d0aadb |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:3580ffe87dc8 |
---|---|
1 /* | |
2 TouchKeys: multi-touch musical keyboard control software | |
3 Copyright (c) 2013 Andrew McPherson | |
4 | |
5 This program is free software: you can redistribute it and/or modify | |
6 it under the terms of the GNU General Public License as published by | |
7 the Free Software Foundation, either version 3 of the License, or | |
8 (at your option) any later version. | |
9 | |
10 This program is distributed in the hope that it will be useful, | |
11 but WITHOUT ANY WARRANTY; without even the implied warranty of | |
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
13 GNU General Public License for more details. | |
14 | |
15 You should have received a copy of the GNU General Public License | |
16 along with this program. If not, see <http://www.gnu.org/licenses/>. | |
17 | |
18 ===================================================================== | |
19 | |
20 Osc.h: classes for handling reception and transmission of OSC messages, | |
21 using the liblo library. | |
22 */ | |
23 | |
24 #ifndef OSC_H | |
25 #define OSC_H | |
26 | |
27 #include <iostream> | |
28 #include <iomanip> | |
29 #include <fstream> | |
30 #include <set> | |
31 #include <map> | |
32 #include <string> | |
33 #include <vector> | |
34 #include <set> | |
35 #include "../JuceLibraryCode/JuceHeader.h" | |
36 #include "lo/lo.h" | |
37 | |
38 using namespace std; | |
39 | |
40 class OscMessageSource; | |
41 | |
42 // This is an abstract base class implementing a single function oscHandlerMethod(). Objects that | |
43 // want to register to receive OSC messages should inherit from OscHandler. Notice that all listener | |
44 // add/remove methods are private or protected. The subclass of OscHandler should add any relevant | |
45 // listeners, or optionally expose a public interface to add listeners. (Never call the methods in | |
46 // OscMessageSource externally.) | |
47 | |
48 class OscHandler | |
49 { | |
50 public: | |
51 OscHandler() : oscController_(NULL) {} | |
52 | |
53 // The OSC controller will call this method when it gets a matching message that's been registered | |
54 virtual bool oscHandlerMethod(const char *path, const char *types, int numValues, lo_arg **values, void *data) = 0; | |
55 void setOscController(OscMessageSource *c) { oscController_ = c; } | |
56 | |
57 virtual ~OscHandler(); // In the destructor, remove all OSC listeners | |
58 protected: | |
59 bool addOscListener(const string& path); | |
60 bool removeOscListener(const string& path); | |
61 bool removeAllOscListeners(); | |
62 | |
63 OscMessageSource *oscController_; | |
64 set<string> oscListenerPaths_; | |
65 }; | |
66 | |
67 // Base class for anything that acts as a source of OSC messages. Could be | |
68 // received externally or internally generated. | |
69 | |
70 class OscMessageSource | |
71 { | |
72 friend class OscHandler; | |
73 | |
74 public: | |
75 OscMessageSource() {} | |
76 | |
77 protected: | |
78 bool addListener(const string& path, OscHandler *object); // Add a listener object for a specific path | |
79 bool removeListener(const string& path, OscHandler *object); // Remove a listener object from a specific path | |
80 bool removeListener(OscHandler *object); // Remove a listener object from all paths | |
81 | |
82 void updateListeners(); // Propagate changes to the listeners to the main object | |
83 | |
84 //ReadWriteLock oscListenerMutex_; // This mutex protects the OSC listener table from being modified mid-message | |
85 CriticalSection oscListenerMutex_; // This mutex protects the OSC listener table from being modified mid-message | |
86 CriticalSection oscUpdaterMutex_; // This mutex controls the insertion of objects in add/removeListener | |
87 | |
88 multimap<string, OscHandler*> noteListeners_; // Map from OSC path name to handler (possibly multiple handlers per object) | |
89 multimap<string, OscHandler*> noteListenersToAdd_; // Collection of listeners to add on the next cycle | |
90 multimap<string, OscHandler*> noteListenersToRemove_; // Collection of listeners to remove on the next cycle | |
91 set<OscHandler*> noteListenersForBlanketRemoval_; // Collection of listeners to remove from all paths | |
92 }; | |
93 | |
94 // This class specifically implements OSC messages coming from external sources | |
95 | |
96 class OscReceiver : public OscMessageSource | |
97 { | |
98 public: | |
99 OscReceiver(lo_server_thread thread, const char *prefix) { | |
100 oscServerThread_ = thread; | |
101 globalPrefix_.assign(prefix); | |
102 useThru_ = false; | |
103 lo_server_thread_add_method(thread, NULL, NULL, OscReceiver::staticHandler, (void *)this); | |
104 } | |
105 | |
106 void setThruAddress(lo_address thruAddr, const char *prefix) { | |
107 thruAddress_ = thruAddr; | |
108 thruPrefix_.assign(prefix); | |
109 useThru_ = true; | |
110 } | |
111 | |
112 // staticHandler() is called by liblo with new OSC messages. Its only function is to pass control | |
113 // to the object-specific handler method, which has access to all internal variables. | |
114 | |
115 int handler(const char *path, const char *types, lo_arg **argv, int argc, lo_message msg, void *data); | |
116 static int staticHandler(const char *path, const char *types, lo_arg **argv, int argc, lo_message msg, void *userData) { | |
117 return ((OscReceiver *)userData)->handler(path, types, argv, argc, msg, userData); | |
118 } | |
119 | |
120 ~OscReceiver() { | |
121 lo_server_thread_del_method(oscServerThread_, NULL, NULL); | |
122 } | |
123 | |
124 private: | |
125 lo_server_thread oscServerThread_; // Thread that handles received OSC messages | |
126 | |
127 // OSC thru | |
128 bool useThru_; // Whether or not we retransmit any messages | |
129 lo_address thruAddress_; // Address to which we retransmit | |
130 string thruPrefix_; // Prefix that must be matched to be retransmitted | |
131 | |
132 // State variables | |
133 string globalPrefix_; // Prefix for all OSC paths | |
134 }; | |
135 | |
136 class OscTransmitter | |
137 { | |
138 public: | |
139 OscTransmitter() : enabled_(true), debugMessages_(false) {} | |
140 | |
141 // Enable or disable transmission | |
142 void setEnabled(bool enable) { enabled_ = enable; } | |
143 bool enabled() { return enabled_; } | |
144 | |
145 // Add and remove addresses to send to | |
146 int addAddress(const char * host, const char * port, int proto = LO_UDP); | |
147 void removeAddress(int index); | |
148 void clearAddresses(); | |
149 vector<lo_address> addresses() { return addresses_; } | |
150 | |
151 void sendMessage(const char * path, const char * type, ...); | |
152 void sendMessage(const char * path, const char * type, const lo_message& message); | |
153 void sendByteArray(const char * path, const unsigned char * data, int length); | |
154 | |
155 void setDebugMessages(bool debug) { debugMessages_ = debug; } | |
156 | |
157 ~OscTransmitter(); | |
158 | |
159 private: | |
160 vector<lo_address> addresses_; | |
161 bool enabled_; | |
162 bool debugMessages_; | |
163 }; | |
164 | |
165 #endif // OSC_H |