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
|
/************************************************************************
************************************************************************
FAUST compiler
Copyright (C) 2003-2015 GRAME, Centre National de Creation Musicale
---------------------------------------------------------------------
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
************************************************************************
************************************************************************/
#ifndef remote_dsp_aux__h
#define remote_dsp_aux__h
#include <string>
#include <vector>
#include <map>
#include <list>
#include <assert.h>
#include <curl/curl.h>
#include <sstream>
#include <iostream>
#include <fstream>
#include "faust/gui/MapUI.h"
#include "faust/gui/meta.h"
#include "faust/dsp/dsp.h"
#include "faust/dsp/proxy-dsp.h"
#include "faust/midi/jack-midi.h"
#include "smartpointer.h"
#include "lo/lo.h"
#include "TMutex.h"
#include <jack/net.h>
#ifdef WIN32
#define EXPORT __declspec(dllexport)
#else
#define EXPORT __attribute__ ((visibility("default")))
#endif
#define LocalFactoryDSPTableType list<llvm_dsp_factory*>
#define LocalFactoryDSPTableIt LocalFactoryDSPTableType::iterator
#define RemoteFactoryDSPTable pair<list<remote_dsp_aux*>, list<remote_audio_aux*> >
#define RemoteFactoryDSPTableType map<Sremote_dsp_factory, RemoteFactoryDSPTable>
#define RemoteFactoryDSPTableIt RemoteFactoryDSPTableType::iterator
using namespace std;
class llvm_dsp_factory;
enum {
ERROR_FACTORY_NOTFOUND,
ERROR_INSTANCE_NOTCREATED,
ERROR_NETJACK_NOTSTARTED,
ERROR_NETJACK_READ,
ERROR_NETJACK_WRITE,
ERROR_CURL_CONNECTION
};
// To be used as a singleton
struct remote_DNS {
struct member {
int pid;
string hostname;
lo_timetag timetag;
member()
{
pid = 0;
hostname = "";
timetag.sec = 0;
timetag.frac = 0;
}
};
lo_server_thread fLoThread;
map<string, member> fClients;
TMutex fLocker;
static void errorHandler(int num, const char* m, const char* path);
static int pingHandler(const char* path,
const char* types,
lo_arg** argv,
int argc, void* data,
void* user_data);
remote_DNS();
virtual ~remote_DNS();
};
typedef int (*remoteDSPErrorCallback) (int error_code, void* arg);
class remote_dsp_aux;
class remote_audio_aux;
class remote_dsp_factory;
typedef class SMARTP<remote_dsp_factory> Sremote_dsp_factory;
// The proxy DSP factory
class remote_dsp_factory : public smartable {
friend class remote_dsp_aux;
private:
string fSHAKey; // Unique Index to bind a Remote_Factory to its llvm_factory on the server side
string fExpandedDSP;
string fServerURL; // URL of remote server
string fPoly;
string fVoices;
string fGroup;
JSONUIDecoder* fJSONDecoder;
vector<string> fPathnameList;
public:
remote_dsp_factory(const string& ip_server, int port_server, const string& sha_key);
virtual ~remote_dsp_factory();
void decodeJSON(const string& json);
remote_dsp_aux* createRemoteDSPInstance(int argc, const char* argv[],
remoteDSPErrorCallback error_callback,
void* error_callback_arg, int& error);
remote_audio_aux* createRemoteAudioInstance(int argc, const char* argv[], int& error);
bool init(int argc, const char* argv[],
const string& name_app,
const string& dsp_content,
string& error_msg,
int opt_level);
void metadataRemoteDSPFactory(Meta* m);
string getURL() { return fServerURL; }
string getName();
string getSHAKey();
string getDSPCode();
string getPoly();
string getVoices();
string getGroup();
vector<string> getRemoteDSPFactoryLibraryList() { return fPathnameList; }
bool sendFinalRequest(void* obj, const string& cmd);
static LocalFactoryDSPTableType gLocalFactoryDSPTable;
static RemoteFactoryDSPTableType gRemoteFactoryDSPTable;
static CURL* gCurl;
static remote_DNS* gDNS;
};
// The proxy DSP instance
class remote_dsp_aux : public dsp, public jack_midi {
private:
int fBufferSize; // Buffer size of NetJack connection
int fSampleRate; // Sample rate of NetJack connection
remote_dsp_factory* fFactory; // Factory it is created from
jack_net_master_t* fNetJack; // NetJack Connection
float** fAudioInputs; // Audio buffer
float** fAudioOutputs; // Audio buffer
float** fControlInputs; // control buffer
float** fControlOutputs; // control buffer
remoteDSPErrorCallback fErrorCallback;
void* fErrorCallbackArg;
bool fRunning;
JSONUIDecoder* fJSONDecoder;
void fillBufferWithZerosOffset(int channels, int offset, int size, FAUSTFLOAT** buffer);
void setupBuffers(FAUSTFLOAT** input, FAUSTFLOAT** output, int offset);
void sendSlice(int buffer_size);
void recvSlice(int buffer_size);
public:
remote_dsp_aux(remote_dsp_factory* factory);
virtual ~remote_dsp_aux();
bool init(int argc, const char* argv[],
remoteDSPErrorCallback errror_callback,
void* errror_callback_arg, int& error);
virtual int getNumInputs();
virtual int getNumOutputs();
virtual void buildUserInterface(UI* ui);
virtual int getSampleRate();
virtual void init(int sample_rate);
virtual void instanceInit(int sample_rate);
virtual void instanceConstants(int sample_rate);
virtual void instanceResetUserInterface();
virtual void instanceClear();
virtual dsp* clone();
virtual void metadata(Meta* m);
virtual void compute(int count, FAUSTFLOAT** input, FAUSTFLOAT** output);
remote_dsp_factory* getFactory() { return fFactory; }
};
// The proxy audio instance
class remote_audio_aux {
private:
remote_dsp_factory* fFactory;
public:
remote_audio_aux(remote_dsp_factory* factory);
virtual ~remote_audio_aux();
bool init(int argc, const char* argv[], int& error);
remote_dsp_factory* getFactory() { return fFactory; }
bool start();
bool stop();
};
// The proxy machine
class remote_dsp_machine_aux {
private:
string fIP;
int fPort;
string fTarget;
public:
remote_dsp_machine_aux(const string& ip, int port, const string& target)
:fIP(ip), fPort(port), fTarget(target)
{}
virtual ~remote_dsp_machine_aux() {}
string getIP() { return fIP; }
int getPort() { return fPort; }
string getTarget() { return fTarget; }
};
//---------------------- Public C++ interface --------
class EXPORT remote_dsp : public dsp, public midi {
public:
int getNumInputs();
int getNumOutputs();
void buildUserInterface(UI* ui);
void init(int sample_rate);
void instanceInit(int sample_rate);
void instanceConstants(int sample_rate);
void instanceResetUserInterface();
void instanceClear();
dsp* clone();
void metadata(Meta* m);
int getSampleRate();
void compute(int count, FAUSTFLOAT** input, FAUSTFLOAT** output);
// MIDI polyphonic control
MapUI* keyOn(int channel, int pitch, int velocity);
void keyOff(int channel, int pitch, int velocity);
void keyPress(int channel, int pitch, int press);
void chanPress(int channel, int press);
void ctrlChange(int channel, int ctrl, int value);
void pitchWheel(int channel, int wheel);
void progChange(int channel, int pgm);
};
class EXPORT remote_audio {
public:
bool start();
bool stop();
};
class EXPORT remote_dsp_machine {
public:
std::string getIP();
int getPort();
std::string getTarget();
static remote_dsp_machine* create(const std::string& ip, int port, const std::string& target);
static void destroy(remote_dsp_machine* machine);
};
// Factories
EXPORT remote_dsp_factory* getRemoteDSPFactoryFromSHAKey(const string& sha_key, const string& ip_server, int port_server);
EXPORT remote_dsp_factory* createRemoteDSPFactoryFromFile(const string& filename, int argc, const char* argv[],
const string& ip_server, int port_server,
string& error_msg, int opt_level = -1);
EXPORT remote_dsp_factory* createRemoteDSPFactoryFromString(const string& name_app, const string& dsp_content,
int argc, const char* argv[],
const string& ip_server, int port_server,
string& error, int opt_level = -1);
EXPORT bool deleteRemoteDSPFactory(remote_dsp_factory* factory);
EXPORT void deleteAllRemoteDSPFactories();
EXPORT void metadataRemoteDSPFactory(remote_dsp_factory* factory, Meta* m);
EXPORT vector<string> getRemoteDSPFactoryLibraryList(remote_dsp_factory* factory);
// DSP instance
EXPORT remote_dsp* createRemoteDSPInstance(remote_dsp_factory* factory,
int argc, const char* argv[],
remoteDSPErrorCallback error_callback,
void* errror_callback_arg,
int& error);
EXPORT void deleteRemoteDSPInstance(remote_dsp* dsp);
// Audio instance
EXPORT remote_audio* createRemoteAudioInstance(remote_dsp_factory* factory,
int argc, const char* argv[],
int& error);
EXPORT void deleteRemoteAudioInstance(remote_audio* audio);
// Remote machine access
EXPORT bool getRemoteDSPMachines(map<string, remote_dsp_machine* >* machine_list);
EXPORT bool getRemoteDSPFactories(const string& ip_server, int port_server, vector<pair<string, string> >* factories_list);
#endif
|