Marsyas  0.6.0-alpha
/usr/src/RPM/BUILD/marsyas-0.6.0/src/marsyas/realtime/osc_transmitter.h
Go to the documentation of this file.
00001 /*
00002 ** Copyright (C) 2014 George Tzanetakis <gtzan@cs.uvic.ca>
00003 **
00004 ** This program is free software; you can redistribute it and/or modify
00005 ** it under the terms of the GNU General Public License as published by
00006 ** the Free Software Foundation; either version 2 of the License, or
00007 ** (at your option) any later version.
00008 **
00009 ** This program is distributed in the hope that it will be useful,
00010 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
00011 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012 ** GNU General Public License for more details.
00013 **
00014 ** You should have received a copy of the GNU General Public License
00015 ** along with this program; if not, write to the Free Software
00016 ** Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
00017 */
00018 
00019 #ifndef MARSYAS_REALTIME_OSC_SENDER_HPP
00020 #define MARSYAS_REALTIME_OSC_SENDER_HPP
00021 
00022 #include <marsyas/system/MarSystem.h>
00023 #include <marsyas/export.h>
00024 #include <marsyas/common_header.h>
00025 
00026 #include <vector>
00027 #include <string>
00028 #include <map>
00029 #include <algorithm>
00030 #include <memory>
00031 
00032 namespace Marsyas {
00033 namespace RealTime {
00034 
00035 struct OscSubscriber
00036 {
00037   virtual void process( const char * data, int size ) = 0;
00038 };
00039 
00051 class marsyas_EXPORT OscTransmitter : public MarSystem
00052 {
00053 public:
00054   OscTransmitter( MarSystem * system ):
00055     MarSystem("OscSender", "OscSender"),
00056     m_system(system)
00057   {
00058   }
00059 
00060   MarSystem *clone() const
00061   {
00062     // no-op
00063     return 0;
00064   }
00065 
00073   bool subscribe( const std::string & path, OscSubscriber * subscriber )
00074   {
00075     if (!path.size() || path[0] != '/')
00076       return false;
00077     MarControlPtr control = m_system->remoteControl(path);
00078     return subscribe(control, subscriber);
00079   }
00080 
00081   bool subscribe( MarControlPtr control, OscSubscriber * subscriber );
00082 
00090   void unsubscribe( const std::string & path, OscSubscriber * subscriber )
00091   {
00092     if (!path.size() || path[0] != '/')
00093       return;
00094     MarControlPtr control = m_system->remoteControl(path);
00095     return unsubscribe(control, subscriber);
00096   }
00097 
00098   void unsubscribe( MarControlPtr control, OscSubscriber * subscriber );
00099 
00100   void myProcess(realvec &, realvec &);
00101   void myUpdate( MarControlPtr handler );
00102 
00103 private:
00104   struct subscription
00105   {
00106     std::string path;
00107     std::vector<OscSubscriber*> subscribers;
00108     void add(OscSubscriber * subscriber)
00109     {
00110       subscribers.push_back(subscriber);
00111     }
00112     void remove(OscSubscriber * subscriber)
00113     {
00114       auto subscriber_it = find(subscribers.begin(), subscribers.end(), subscriber);
00115       if (subscriber_it != subscribers.end())
00116         subscribers.erase(subscriber_it);
00117     }
00118     bool contains(OscSubscriber * subscriber) const
00119     {
00120       return std::find(subscribers.begin(), subscribers.end(), subscriber) != subscribers.end();
00121     }
00122     bool empty() const { return subscribers.empty(); }
00123   };
00124 
00125   std::string make_osc_path( MarControlPtr control, char separator = '/' );
00126 
00127   MarSystem * m_system;
00128   static const size_t m_buffer_size = 4096;
00129   static const size_t max_key_length = 512;
00130   MARSYAS_ALIGN(8) char m_buffer[m_buffer_size];
00131   std::map<MarControl*, subscription> m_subscribers;
00132 };
00133 
00134 }
00135 }
00136 
00137 #endif // MARSYAS_REALTIME_OSC_SENDER_HPP