Allolib  1.0
C++ Components For Interactive Multimedia
al_PresetServer.hpp
1 #ifndef AL_PRESETSERVER_H
2 #define AL_PRESETSERVER_H
3 
4 /* Allocore --
5  Multimedia / virtual environment application class library
6 
7  Copyright (C) 2009. AlloSphere Research Group, Media Arts & Technology,
8  UCSB. Copyright (C) 2016. The Regents of the University of California. All
9  rights reserved.
10 
11  Redistribution and use in source and binary forms, with or without
12  modification, are permitted provided that the following conditions are
13  met:
14 
15  Redistributions of source code must retain the above copyright
16  notice, this list of conditions and the following disclaimer.
17 
18  Redistributions in binary form must reproduce the above
19  copyright notice, this list of conditions and the following disclaimer in the
20  documentation and/or other materials provided with the
21  distribution.
22 
23  Neither the name of the University of California nor the names
24  of its contributors may be used to endorse or promote products derived from
25  this software without specific prior written permission.
26 
27  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
28  IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
29  IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
30  PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
31  CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
32  EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
33  PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
34  OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
35  WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
36  OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
37  ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
38 
39  File description:
40  Preset classes that encapsulates storing values for groups of parameters
41  File author(s):
42  AndrĂ©s Cabrera mantaraya36@gmail.com
43 */
44 
45 
46 #include "al/ui/al_PresetHandler.hpp"
47 
48 namespace al {
49 
52  public:
62  PresetServer(std::string oscAddress = "127.0.0.1", int oscPort = 9011);
76  ~PresetServer();
77 
81  void print();
82 
88  void stopServer();
89 
90  bool serverRunning();
91 
92  void allowStore(bool allow) { mAllowStore = allow; }
93  bool allowStore() { return mAllowStore; }
94 
95  virtual void onMessage(osc::Message &m);
96 
97  PresetServer &registerPresetHandler(PresetHandler &presetHandler) {
98  mPresetHandlers.push_back(&presetHandler);
99  presetHandler.registerPresetCallback(PresetServer::changeCallback,
100  (void *)this);
101 
102  presetHandler.registerMorphTimeCallback([&](float value) {
103  this->notifyListeners(this->mOSCpath + "/morphTime", value);
104  });
105  return *this;
106  }
107 
108  PresetServer &operator<<(PresetHandler &presetHandler) {
109  return registerPresetHandler(presetHandler);
110  }
111 
112  void setAddress(std::string address);
113  std::string getAddress();
114 
115  void notifyPresetChange(bool notify) { mNotifyPresetChange = notify; }
116 
117  void attachPacketHandler(osc::PacketHandler *handler);
118 
119  protected:
120  static void changeCallback(int value, void *sender, void *userData);
121 
122  private:
123  osc::Recv *mServer;
124  std::vector<PresetHandler *> mPresetHandlers;
125  // std::mutex mServerLock;
126  std::string mOSCpath;
127  std::mutex mHandlerLock;
128  std::vector<osc::PacketHandler *> mHandlers;
129  bool mAllowStore;
130  bool mStoreMode;
131  bool mNotifyPresetChange;
132 
133  std::mutex mPresetChangeLock;
134  std::string mPresetChangeSenderAddr;
135 
136  std::vector<std::string> mDisabledListeners;
137 
138  ParameterServer *mParameterServer;
139 };
140 
141 } //namespace al
142 
143 #endif // AL_PRESETSERVER_H
void notifyListeners(std::string OSCaddress, float value, ValueSource *src=nullptr)
Notify the listeners of value changes.
The ParameterServer class creates an OSC server to receive parameter values.
The PresetHandler class handles sorting and recalling of presets.
void registerPresetCallback(std::function< void(int index, void *sender, void *userData)> cb, void *userData=nullptr)
Register a callback to be notified when a preset is loaded.
void registerMorphTimeCallback(Parameter::ParameterChangeCallback cb)
Register a callback to be notified when morph time parameter is changed.
void print()
print prints information about the server to std::out
PresetServer(ParameterServer &paramServer)
using this constructor reuses the existing osc::Recv server from the ParameterServer object
PresetServer(std::string oscAddress="127.0.0.1", int oscPort=9011)
PresetServer constructor.
void stopServer()
stopServer stops the OSC server thread. Calling this function is sometimes required when this object ...
virtual void onMessage(osc::Message &m)
Called for each message contained in packet.
Definition: al_App.hpp:23