1 /************************************************************************ 2 3 IMPORTANT NOTE : this file contains two clearly delimited sections : 4 the ARCHITECTURE section (in two parts) and the USER section. Each section 5 is governed by its own copyright and license. Please check individually 6 each section for license and copyright information. 7 *************************************************************************/ 8 9 /*******************BEGIN ARCHITECTURE SECTION (part 1/2)****************/ 10 11 /************************************************************************ 12 FAUST Audio Unit UI 13 Copyright (C) 2013 Reza Payami 14 All rights reserved. 15 ----------------------------BSD License------------------------------ 16 Redistribution and use in source and binary forms, with or without 17 modification, are permitted provided that the following conditions 18 are met: 19 20 * Redistributions of source code must retain the above copyright 21 notice, this list of conditions and the following disclaimer. 22 * Redistributions in binary form must reproduce the above 23 copyright notice, this list of conditions and the following 24 disclaimer in the documentation and/or other materials provided 25 with the distribution. 26 * Neither the name of Remy Muller nor the names of its 27 contributors may be used to endorse or promote products derived 28 from this software without specific prior written permission. 29 30 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 31 "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 32 LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 33 FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE 34 COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 35 INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 36 BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 37 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 38 CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 39 STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 40 ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED 41 OF THE POSSIBILITY OF SUCH DAMAGE. 42 43 ----------------------------Audio Unit SDK---------------------------------- 44 In order to compile a AU (TM) Synth plugin with this architecture file 45 you will need the proprietary AU SDK from Apple. Please check 46 the corresponding license. 47 48 ************************************************************************/ 49 #include <set> 50 51 #include "faust/gui/UI.h" 52 53 using namespace std; 54 55 class auUI; 56 57 class auUIObject { 58 public: 59 string fLabel; 60 float* fZone; 61 range(float min,float max,float val)62 float range(float min, float max, float val) { // AU parameters are normalized in the range [0;1] 63 val = min + val * (max - min); 64 return (val < min) ? min : (val > max) ? max : val; 65 } 66 67 public: auUIObject(const char * label,FAUSTFLOAT * zone)68 auUIObject(const char* label, FAUSTFLOAT* zone) : 69 fLabel(label), fZone(zone) { 70 } 71 ~auUIObject()72 virtual ~auUIObject() { 73 } 74 GetName(char * text)75 virtual void GetName(char *text) { 76 std::strcpy(text, fLabel.c_str()); 77 } 78 SetValue(double f)79 virtual void SetValue(double f) { 80 *fZone = range(0.0f, 1.0f, (float) f); 81 } 82 GetValue()83 virtual float GetValue() { 84 return *fZone; 85 } 86 GetDisplay(char * text)87 virtual void GetDisplay(char *text) { 88 std::sprintf(text, "%f", *fZone); 89 } 90 GetID()91 virtual long GetID() { /* returns the sum of all the ASCII characters contained in the parameter's label */ 92 int i; 93 long acc; 94 for (i = 0, acc = 0; i < fLabel.length(); i++) 95 acc += (fLabel.c_str())[i]; 96 return acc; 97 } 98 }; 99 100 /**********************************************************************************/ 101 class auToggleButton: public auUIObject { 102 103 public: 104 auToggleButton(const char * label,FAUSTFLOAT * zone)105 auToggleButton(const char* label, FAUSTFLOAT* zone) : 106 auUIObject(label, zone) { 107 } 108 ~auToggleButton()109 virtual ~auToggleButton() { 110 } 111 GetValue()112 virtual float GetValue() { 113 return *fZone; 114 } 115 SetValue(double f)116 virtual void SetValue(double f) { 117 *fZone = (f > 0.5f) ? 1.0f : 0.0f; 118 } 119 GetDisplay(char * text)120 virtual void GetDisplay(char *text) { 121 (*fZone > 0.5f) ? std::strcpy(text, "ON") : std::strcpy(text, "OFF"); 122 } 123 }; 124 125 /**********************************************************************************/ 126 class auCheckButton: public auUIObject { 127 128 public: 129 auCheckButton(const char * label,FAUSTFLOAT * zone)130 auCheckButton(const char* label, FAUSTFLOAT* zone) : 131 auUIObject(label, zone) { 132 } 133 ~auCheckButton()134 virtual ~auCheckButton() { 135 } 136 GetValue()137 virtual float GetValue() { 138 return *fZone; 139 } 140 SetValue(double f)141 virtual void SetValue(double f) { 142 *fZone = (f > 0.5f) ? 1.0f : 0.0f; 143 } 144 GetDisplay(char * text)145 virtual void GetDisplay(char *text) { 146 (*fZone > 0.5f) ? std::strcpy(text, "ON") : std::strcpy(text, "OFF"); 147 } 148 }; 149 150 /**********************************************************************************/ 151 class auButton: public auUIObject { 152 153 public: 154 auButton(const char * label,FAUSTFLOAT * zone)155 auButton(const char* label, FAUSTFLOAT* zone) : 156 auUIObject(label, zone) { 157 } 158 ~auButton()159 virtual ~auButton() { 160 } 161 GetValue()162 virtual float GetValue() { 163 return *fZone; 164 } 165 SetValue(double f)166 virtual void SetValue(double f) { 167 *fZone = (f > 0.5f) ? 1.0f : 0.0f; 168 } 169 GetDisplay(char * text)170 virtual void GetDisplay(char *text) { 171 (*fZone > 0.5f) ? std::strcpy(text, "ON") : std::strcpy(text, "OFF"); 172 } 173 }; 174 175 /**********************************************************************************/ 176 class auSlider: public auUIObject { 177 178 public: 179 float fInit; 180 float fMin; 181 float fMax; 182 float fStep; 183 bool fIsVertical; 184 185 public: 186 auSlider(const char * label,FAUSTFLOAT * zone,float init,float min,float max,float step,bool isVertical)187 auSlider(const char* label, FAUSTFLOAT* zone, float init, float min, float max, 188 float step, bool isVertical) : 189 auUIObject(label, zone), fInit(init), fMin(min), fMax(max), fStep(step), fIsVertical(isVertical) { 190 } 191 ~auSlider()192 virtual ~auSlider() { 193 } 194 GetValue()195 virtual float GetValue() { 196 return (*fZone - fMin) / (fMax - fMin); 197 } // normalize 198 SetValue(double f)199 virtual void SetValue(double f) { 200 *fZone = range(fMin, fMax, (float) f); 201 } // expand 202 }; 203 204 205 /**********************************************************************************/ 206 207 208 class auBargraph: public auUIObject { 209 210 public: 211 float fInit; 212 float fMin; 213 float fMax; 214 float fStep; 215 bool fIsVertical; 216 217 public: 218 auBargraph(const char * label,FAUSTFLOAT * zone,float min,float max,bool isVertical)219 auBargraph(const char* label, FAUSTFLOAT* zone, float min, float max, bool isVertical) : 220 auUIObject(label, zone), fMin(min), fMax(max), fIsVertical(isVertical){ 221 } 222 ~auBargraph()223 virtual ~auBargraph() { 224 } 225 GetValue()226 virtual float GetValue() { 227 return (*fZone - fMin) / (fMax - fMin); 228 } // normalize 229 SetValue(double f)230 virtual void SetValue(double f) { 231 *fZone = range(fMin, fMax, (float) f); 232 } // expand 233 }; 234 235 /**********************************************************************************/ 236 237 class auBox: public auUIObject { 238 239 public: 240 vector<auUIObject*> children; 241 bool isVertical; 242 auBox* parent; 243 244 public: 245 auBox(const char * label,auBox * inParent,bool inIsVertical)246 auBox(const char* label, auBox* inParent, bool inIsVertical) : 247 auUIObject(label, NULL), parent(inParent), isVertical(inIsVertical) { 248 } 249 add(auUIObject * child)250 void add(auUIObject* child) { 251 children.push_back(child); 252 } 253 ~auBox()254 virtual ~auBox() { 255 } 256 257 }; 258 259 260 /**********************************************************************************/ 261 //eunum Direction {HORIZONTAL, VERTICAL}; //TODO 262 263 class auUI: public UI { 264 public: 265 266 vector<auUIObject*> fUITable; 267 std::set <float*>knobSet; 268 269 auBox* currentBox = NULL; 270 auBox* boundingBox = NULL; 271 272 public: 273 auUI()274 auUI() { 275 currentBox = boundingBox = new auBox("", NULL, true); 276 } 277 ~auUI()278 virtual ~auUI() { 279 for (vector<auUIObject*>::iterator iter = fUITable.begin(); 280 iter != fUITable.end(); iter++) 281 delete *iter; 282 //TODO delete boxes 283 } 284 285 declare(float * zone,const char * key,const char * value)286 virtual void declare(float* zone, const char* key, const char* value) 287 { 288 if (zone == 0) 289 { 290 if (strcmp(key, "hidden") == 0) 291 { 292 293 } 294 } 295 else 296 { 297 if (strcmp(key,"size") == 0) 298 { 299 } 300 else if (strcmp(key,"tooltip") == 0) 301 { 302 } 303 else if (strcmp(key,"unit") == 0) 304 { 305 } 306 if (strcmp(key,"hidden") == 0) 307 { 308 309 } 310 else if (strcmp(key,"style") == 0) 311 { 312 if (strstr(value, "knob")) //TODO 313 { 314 315 knobSet.insert(zone); 316 } 317 } 318 else if (strcmp(key,"color") == 0) 319 { 320 } 321 else if (strcmp(key,"accx") == 0 322 || strcmp(key,"accy") == 0 323 || strcmp(key,"accz") == 0 324 || strcmp(key,"gyrox") == 0 325 || strcmp(key,"gyroy") == 0 326 || strcmp(key,"gyroz") == 0 327 || strcmp(key,"compass") == 0) 328 { 329 } 330 331 } 332 } 333 334 addButton(const char * label,FAUSTFLOAT * zone)335 void addButton(const char* label, FAUSTFLOAT* zone) { 336 auButton* button = new auButton(label, zone); 337 fUITable.push_back(button); 338 currentBox->add(button); 339 } 340 openTabBox(const char * label)341 void openTabBox(const char* label) { 342 343 } 344 addCheckButton(const char * label,FAUSTFLOAT * zone)345 void addCheckButton(const char* label, FAUSTFLOAT* zone) { 346 auCheckButton* checkButton= new auCheckButton(label, zone); 347 fUITable.push_back(checkButton); 348 currentBox->add(checkButton); 349 } 350 addVerticalSlider(const char * label,FAUSTFLOAT * zone,float init,float min,float max,float step)351 void addVerticalSlider(const char* label, FAUSTFLOAT* zone, float init, float min, 352 float max, float step) { 353 auSlider* slider = new auSlider(label, zone, init, min, max, step, true); 354 fUITable.push_back(slider); 355 currentBox->add(slider); 356 } 357 addHorizontalSlider(const char * label,FAUSTFLOAT * zone,float init,float min,float max,float step)358 void addHorizontalSlider(const char* label, FAUSTFLOAT* zone, float init, float min, 359 float max, float step) { 360 auSlider* slider = new auSlider(label, zone, init, min, max, step, false); 361 fUITable.push_back(slider); 362 currentBox->add(slider); 363 } 364 addNumEntry(const char * label,FAUSTFLOAT * zone,float init,float min,float max,float step)365 void addNumEntry(const char* label, FAUSTFLOAT* zone, float init, float min, float max, 366 float step) { 367 auSlider* slider = new auSlider(label, zone, init, min, max, step, false); 368 fUITable.push_back(slider); 369 currentBox->add(slider); 370 } 371 addHorizontalBargraph(const char * label,FAUSTFLOAT * zone,float min,float max)372 void addHorizontalBargraph(const char* label, FAUSTFLOAT* zone, float min, float max) { 373 auBargraph* bargraph = new auBargraph(label, zone, min, max, false); 374 fUITable.push_back(bargraph); 375 currentBox->add(bargraph); 376 } 377 addVerticalBargraph(const char * label,FAUSTFLOAT * zone,float min,float max)378 void addVerticalBargraph(const char* label, FAUSTFLOAT* zone, float min, float max) { 379 auBargraph* bargraph = new auBargraph(label, zone, min, max, true); 380 fUITable.push_back(bargraph); 381 currentBox->add(bargraph); 382 } 383 openHorizontalBox(const char * label)384 void openHorizontalBox(const char* label) { 385 auBox* box = new auBox(label, currentBox, false); 386 currentBox->add(box); 387 currentBox = box; 388 } 389 openVerticalBox(const char * label)390 void openVerticalBox(const char* label) { 391 auBox* box = new auBox(label, currentBox, true); 392 currentBox->add(box); 393 currentBox = box; 394 } 395 closeBox()396 void closeBox() { 397 if (currentBox) //TODO else? 398 currentBox = currentBox->parent; 399 } 400 SetValue(int index,double f)401 void SetValue(int index, double f) { 402 assert(index < fUITable.size()); 403 fUITable[index]->SetValue(f); 404 } 405 GetValue(long index)406 float GetValue(long index) { 407 assert(index < fUITable.size()); 408 return fUITable[index]->GetValue(); 409 } 410 GetDisplay(long index,char * text)411 void GetDisplay(long index, char *text) { 412 assert(index < fUITable.size()); 413 fUITable[index]->GetDisplay(text); 414 } 415 GetName(long index,char * text)416 void GetName(long index, char *text) { 417 assert(index < fUITable.size()); 418 fUITable[index]->GetName(text); 419 } 420 GetNumParams()421 long GetNumParams() { 422 return fUITable.size(); 423 } 424 makeID()425 long makeID() 426 /* Creates a (unique) id by summing all the parameter's labels, 427 * then wrapping it in the range [0;maxNumberOfId] and adding 428 * this number to the offset made by the Four Character ID: 'FAUS' 429 */ 430 { 431 const long maxNumberOfId = 128; 432 long baseid = 'FAUS'; 433 long id = 0; 434 for (int i = 0; i < fUITable.size(); i++) 435 id += fUITable[i]->GetID(); 436 return baseid + id % maxNumberOfId; 437 } 438 addNumDisplay(char * label,float * zone,int precision)439 void addNumDisplay(char* label, float* zone, int precision) { 440 } 441 addTextDisplay(char * label,float * zone,char * names[],float min,float max)442 void addTextDisplay(char* label, float* zone, char* names[], float min, 443 float max) { 444 } 445 446 447 }; 448 449 450