| 1 | /* Copyright 2013 Kjetil S. Matheussen |
| 2 | |
| 3 | This program is free software; you can redistribute it and/or |
| 4 | modify it under the terms of the GNU General Public License |
| 5 | as published by the Free Software Foundation; either version 2 |
| 6 | of the License, or (at your option) any later version. |
| 7 | |
| 8 | This program is distributed in the hope that it will be useful, |
| 9 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 10 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 11 | GNU General Public License for more details. |
| 12 | |
| 13 | You should have received a copy of the GNU General Public License |
| 14 | along with this program; if not, write to the Free Software |
| 15 | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ |
| 16 | |
| 17 | |
| 18 | #include <math.h> |
| 19 | #include <string> |
| 20 | |
| 21 | #include <QSharedMemory> |
| 22 | #include <QDateTime> |
| 23 | |
| 24 | #include "faudiostream/architecture/faust/gui/UI.h" |
| 25 | #include "faudiostream/architecture/faust/audio/dsp.h" |
| 26 | #ifdef COMPILING_STANDALONE |
| 27 | #include "myjack-dsp.h" |
| 28 | #endif |
| 29 | |
| 30 | #include "../common/nsmtracker.h" |
| 31 | |
| 32 | #include "system_compressor_wrapper_proc.h" |
| 33 | |
| 34 | |
| 35 | |
| 36 | #include <vector> |
| 37 | |
| 38 | inline int max (unsigned int a, unsigned int b) { return (a>b) ? a : b; } |
| 39 | inline int max (int a, int b) { return (a>b) ? a : b; } |
| 40 | |
| 41 | inline long max (long a, long b) { return (a>b) ? a : b; } |
| 42 | inline long max (int a, long b) { return (a>b) ? a : b; } |
| 43 | inline long max (long a, int b) { return (a>b) ? a : b; } |
| 44 | |
| 45 | inline float max (float a, float b) { return (a>b) ? a : b; } |
| 46 | inline float max (int a, float b) { return (a>b) ? a : b; } |
| 47 | inline float max (float a, int b) { return (a>b) ? a : b; } |
| 48 | inline float max (long a, float b) { return (a>b) ? a : b; } |
| 49 | inline float max (float a, long b) { return (a>b) ? a : b; } |
| 50 | |
| 51 | inline double max (double a, double b) { return (a>b) ? a : b; } |
| 52 | inline double max (int a, double b) { return (a>b) ? a : b; } |
| 53 | inline double max (double a, int b) { return (a>b) ? a : b; } |
| 54 | inline double max (long a, double b) { return (a>b) ? a : b; } |
| 55 | inline double max (double a, long b) { return (a>b) ? a : b; } |
| 56 | inline double max (float a, double b) { return (a>b) ? a : b; } |
| 57 | inline double max (double a, float b) { return (a>b) ? a : b; } |
| 58 | |
| 59 | |
| 60 | inline int min (int a, int b) { return (a<b) ? a : b; } |
| 61 | |
| 62 | inline long min (long a, long b) { return (a<b) ? a : b; } |
| 63 | inline long min (int a, long b) { return (a<b) ? a : b; } |
| 64 | inline long min (long a, int b) { return (a<b) ? a : b; } |
| 65 | |
| 66 | inline float min (float a, float b) { return (a<b) ? a : b; } |
| 67 | inline float min (int a, float b) { return (a<b) ? a : b; } |
| 68 | inline float min (float a, int b) { return (a<b) ? a : b; } |
| 69 | inline float min (long a, float b) { return (a<b) ? a : b; } |
| 70 | inline float min (float a, long b) { return (a<b) ? a : b; } |
| 71 | |
| 72 | inline double min (double a, double b) { return (a<b) ? a : b; } |
| 73 | inline double min (int a, double b) { return (a<b) ? a : b; } |
| 74 | inline double min (double a, int b) { return (a<b) ? a : b; } |
| 75 | inline double min (long a, double b) { return (a<b) ? a : b; } |
| 76 | inline double min (double a, long b) { return (a<b) ? a : b; } |
| 77 | inline double min (float a, double b) { return (a<b) ? a : b; } |
| 78 | inline double min (double a, float b) { return (a<b) ? a : b; } |
| 79 | |
| 80 | |
| 81 | #ifdef COMPILING_STANDALONE |
| 82 | extern const char *g_jack_client_name; |
| 83 | extern bool g_autoconnect; |
| 84 | #endif |
| 85 | |
| 86 | |
| 87 | |
| 88 | namespace{ |
| 89 | struct Meta |
| 90 | { |
| 91 | void declare (const char* key, const char* value) { } |
| 92 | }; |
| 93 | } |
| 94 | #include "system_compressor.cpp" |
| 95 | |
| 96 | |
| 97 | namespace{ |
| 98 | |
| 99 | class MyUI : public UI |
| 100 | { |
| 101 | |
| 102 | public: |
| 103 | void declare(float* control_port, const char* key, const char* value) { |
| 104 | } |
| 105 | |
| 106 | const char *_curr_box_name; |
| 107 | |
| 108 | void openFrameBox(const char* label) {_curr_box_name = label;} |
| 109 | void openTabBox(const char* label) {_curr_box_name = label;} |
| 110 | void openHorizontalBox(const char* label) {_curr_box_name = label;} |
| 111 | void openVerticalBox(const char* label) {_curr_box_name = label;} |
| 112 | void closeBox() {_curr_box_name = NULL;} |
| 113 | |
| 114 | std::vector<float*> _controllers; |
| 115 | std::vector<float*> _graphs; |
| 116 | |
| 117 | void remove_last_item(){ |
| 118 | printf("Popping last effect\n"); |
| 119 | _controllers.pop_back(); |
| 120 | } |
| 121 | |
| 122 | void addEffect(const char *name, float* control_port, float min_value, float default_value, float max_value){ |
| 123 | printf("Adding effect %s %p\n",name,control_port); |
| 124 | _controllers.push_back(control_port); |
| 125 | } |
| 126 | |
| 127 | void addButton(const char* label, float* zone) { |
| 128 | addEffect(label, zone, 0, 0, 1); |
| 129 | } |
| 130 | void addToggleButton(const char* label, float* zone) { |
| 131 | addEffect(label, zone, 0, 0, 1); |
| 132 | } |
| 133 | void addCheckButton(const char* label, float* zone) { |
| 134 | addEffect(label, zone, 0, 0, 1); |
| 135 | } |
| 136 | void addVerticalSlider(const char* label, float* zone, float init, float min, float max, float step) { |
| 137 | addEffect(label, zone, min, init, max); |
| 138 | } |
| 139 | void addHorizontalSlider(const char* label, float* zone, float init, float min, float max, float step) { |
| 140 | addEffect(label, zone, min, init, max); |
| 141 | } |
| 142 | void addNumEntry(const char* label, float* zone, float init, float min, float max, float step) { |
| 143 | addEffect(label, zone, min, init, max); // The INT effect format might not work. Need to go through the code first. |
| 144 | } |
| 145 | |
| 146 | // -- passive widgets |
| 147 | |
| 148 | void addNumDisplay(const char* label, float* zone, int precision) {remove_last_item();} |
| 149 | void addTextDisplay(const char* label, float* zone, const char* names[], float min, float max) {remove_last_item();} |
| 150 | void addHorizontalBargraph(const char* label, float* zone, float min, float max) { |
| 151 | *zone = (min+max)/2; // init |
| 152 | _graphs.push_back(zone); |
| 153 | //remove_last_item(); // remove metadata |
| 154 | //next_peak = zone; |
| 155 | } |
| 156 | void addVerticalBargraph(const char* label, float* zone, float min, float max) { |
| 157 | *zone = (min+max)/2; // init |
| 158 | _graphs.push_back(zone); |
| 159 | //remove_last_item(); // remove metadata |
| 160 | //next_peak = zone; |
| 161 | } |
| 162 | |
| 163 | }; |
| 164 | |
| 165 | struct Compressor_wrapper : public Faust_system_compressor { |
| 166 | MyUI ui; |
| 167 | |
| 168 | QString key; // If not empty string, the memory for the object is placed in shared memory. |
| 169 | QSharedMemory *shared; |
| 170 | |
| 171 | #ifdef COMPILING_STANDALONE |
| 172 | jackaudio audio; |
| 173 | #endif |
| 174 | |
| 175 | Compressor_wrapper(float sample_rate){ |
| 176 | shared=NULL; |
| 177 | |
| 178 | #ifdef COMPILING_STANDALONE |
| 179 | printf("\n\nCREATING 1\n\n"); |
| 180 | audio.init(g_jack_client_name, this); |
| 181 | audio.start(g_autoconnect); |
| 182 | #endif |
| 183 | |
| 184 | #ifdef COMPILING_RADIUM |
| 185 | init(sample_rate); |
| 186 | #endif |
| 187 | |
| 188 | buildUserInterface(&ui); |
| 189 | } |
| 190 | |
| 191 | |
| 192 | #ifdef COMPILING_STANDALONE |
| 193 | ~Compressor_wrapper(){ |
| 194 | printf("Closing\n"); |
| 195 | audio.stop(); |
| 196 | } |
| 197 | |
| 198 | #endif |
| 199 | |
| 200 | #if 0 |
| 201 | void* operator new(size_t size){ |
| 202 | printf("Allocating %d bytes\n",(int)size); |
| 203 | return malloc(size); |
| 204 | } |
| 205 | void operator delete(void *p){ |
| 206 | printf("Freeing %p.\n",p); |
| 207 | free(p); |
| 208 | } |
| 209 | #endif |
| 210 | |
| 211 | //#endif |
| 212 | |
| 213 | }; |
| 214 | |
| 215 | } // anon. namespace |
| 216 | |
| 217 | |
| 218 | void *COMPRESSOR_create_shared(float sample_rate){ |
| 219 | QString key = "radium_crashreporter_" + QString::number(QDateTime::currentMSecsSinceEpoch()); |
| 220 | QSharedMemory *shared = new QSharedMemory(key); |
| 221 | if(shared->create(sizeof(Compressor_wrapper))==false){ |
| 222 | fprintf(stderr,"COMPRESSOR_create_shared: Couldn't create... Error: %s\n",shared->error()==QSharedMemory::NoError?"No error (?)":shared->errorString().toUtf8().constData()); |
| 223 | return NULL; |
| 224 | } |
| 225 | |
| 226 | void *memory = shared->data(); |
| 227 | |
| 228 | Compressor_wrapper *wrapper = new(memory) Compressor_wrapper(sample_rate); |
| 229 | printf("memory: %p, shared: %p\n",memory,shared); |
| 230 | wrapper->key = key; |
| 231 | wrapper->shared = shared; |
| 232 | |
| 233 | return wrapper; |
| 234 | } |
| 235 | |
| 236 | void *COMPRESSOR_create(float sample_rate){ |
| 237 | return new Compressor_wrapper(sample_rate); |
| 238 | } |
| 239 | |
| 240 | |
| 241 | void *COMPRESSOR_create_from_shared_mem_key(const char *key){ |
| 242 | QSharedMemory *shared = new QSharedMemory(key); |
| 243 | return shared->data(); |
| 244 | } |
| 245 | |
| 246 | /* |
| 247 | const char *COMPRESSOR_get_shared_mem_key(void *das_wrapper){ |
| 248 | Compressor_wrapper *wrapper = (Compressor_wrapper*)das_wrapper; |
| 249 | return wrapper->key.toAscii(); |
| 250 | } |
| 251 | */ |
| 252 | |
| 253 | #if COMPILING_STANDALONE |
| 254 | extern char *g_shared_mem_key; |
| 255 | #endif |
| 256 | |
| 257 | void COMPRESSOR_delete(void *das_wrapper){ |
| 258 | #if COMPILING_STANDALONE |
| 259 | if(g_shared_mem_key!=NULL){ |
| 260 | COMPRESSOR_delete_ladspa(das_wrapper); |
| 261 | return; |
| 262 | } |
| 263 | #endif |
| 264 | |
| 265 | Compressor_wrapper *wrapper = (Compressor_wrapper*)das_wrapper; |
| 266 | if(wrapper->key=="") |
| 267 | delete wrapper; |
| 268 | else |
| 269 | wrapper->shared->detach(); |
| 270 | } |
| 271 | |
| 272 | float COMPRESSOR_get_parameter(void *das_wrapper,int num){ |
| 273 | #if COMPILING_STANDALONE |
| 274 | if(g_shared_mem_key!=NULL) |
| 275 | return COMPRESSOR_get_ladspa_parameter(das_wrapper,num); |
| 276 | #endif |
| 277 | |
| 278 | Compressor_wrapper *wrapper = (Compressor_wrapper*)das_wrapper; |
| 279 | float *controller = wrapper->ui._controllers.at(num); |
| 280 | return *controller; |
| 281 | } |
| 282 | |
| 283 | void COMPRESSOR_set_parameter(void *das_wrapper,int num,float value){ |
| 284 | #if COMPILING_STANDALONE |
| 285 | if(g_shared_mem_key!=NULL){ |
| 286 | COMPRESSOR_set_ladspa_parameter(das_wrapper,num,value); |
| 287 | return; |
| 288 | } |
| 289 | #endif |
| 290 | |
| 291 | Compressor_wrapper *wrapper = (Compressor_wrapper*)das_wrapper; |
| 292 | float *controller = wrapper->ui._controllers.at(num); |
| 293 | *controller = value; |
| 294 | //printf("Setting controller %d (%p) to %f (%f)\n",num,controller,value,*controller); |
| 295 | } |
| 296 | |
| 297 | float COMPRESSOR_get_graph_value(void *das_wrapper, int num){ |
| 298 | #if COMPILING_STANDALONE |
| 299 | if(g_shared_mem_key!=NULL) |
| 300 | return COMPRESSOR_get_ladspa_graph_value(das_wrapper,num); |
| 301 | #endif |
| 302 | |
| 303 | Compressor_wrapper *wrapper = (Compressor_wrapper*)das_wrapper; |
| 304 | float *controller = wrapper->ui._graphs.at(num); |
| 305 | return *controller; |
| 306 | } |
| 307 | |
| 308 | void COMPRESSOR_process(void *das_wrapper, float **inputs, float **outputs, int num_frames){ |
| 309 | Compressor_wrapper *wrapper = (Compressor_wrapper*)das_wrapper; |
| 310 | wrapper->compute(num_frames, inputs, outputs); |
| 311 | } |
| 312 |