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 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765
|
/************************************************************************
FAUST Architecture File
Copyright (C) 2021 GRAME, Centre National de Creation Musicale
---------------------------------------------------------------------
This Architecture section 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 3 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, see <http://www.gnu.org/licenses/>.
EXCEPTION : As a special exception, you may create a larger work
that contains this FAUST architecture section and distribute
that work under terms of your choice, so long as this FAUST
architecture section is not modified.
************************************************************************/
#include <libgen.h>
#include <iostream>
#include <string>
#include <vector>
#include "faust/dsp/libfaust-box.h"
#include "faust/dsp/llvm-dsp.h"
#include "faust/dsp/interpreter-dsp.h"
#include "faust/dsp/poly-dsp.h"
#include "faust/audio/jack-dsp.h"
#include "faust/gui/GTKUI.h"
#include "faust/gui/MidiUI.h"
#include "faust/misc.h"
using namespace std;
// Run a DSP with a GUI
static void runAudio(dsp* dsp, const char* name, int argc, char* argv[])
{
// Allocate audio driver
jackaudio audio;
audio.init("Test", dsp);
// Create GUI
GTKUI gtk_ui = GTKUI((char*)name, &argc, &argv);
dsp->buildUserInterface(>k_ui);
// Start real-time processing
audio.start();
// Start GUI
gtk_ui.run();
// Cleanup
audio.stop();
}
/**
* Return the current runtime sample rate.
*
* Reproduce the 'SR' definition in platform.lib: SR = min(192000.0, max(1.0, fconstant(int fSamplingFreq, <math.h>)));
*
* @return the current runtime sample rate.
*/
inline Box SR()
{
return boxMin(boxReal(192000.0), boxMax(boxReal(1.0), boxFConst(SType::kSInt, "fSamplingFreq", "<math.h>")));
}
/**
* Return the current runtime buffer size.
*
* Reproduce the 'BS' definition in platform.lib: BS = fvariable(int count, <math.h>);
*
* @return the current runtime buffer size.
*/
inline Box BS()
{
return boxFVar(SType::kSInt, "count", "<math.h>");
}
/**
* Creates a foreign function*
*/
inline Box SINH()
{
return boxFFun(SType::kSReal, {"sinhf", "sinh", "sinhl", "sinhfx"}, { SType::kSReal }, "<FOO.h>", "");
}
#define COMPILER(exp) \
{ \
createLibContext(); \
exp \
destroyLibContext(); \
} \
static void compile(const string& name_app, Box box, int argc = 0, const char* argv[] = nullptr)
{
string error_msg, source = createSourceFromBoxes(name_app, box, "cpp", argc, argv, error_msg);
if (source != "") {
cout << source;
} else {
cerr << error_msg;
}
}
// process = ffunction(float sinhf|sinh|sinhl|sinhfx(float), <math.h>, "");
static void test0()
{
COMPILER
(
compile("test0", SINH());
)
}
// process = 7,3.14;
static void test1()
{
COMPILER
(
Box box = boxPar(boxInt(7), boxReal(3.14));
compile("test1", box);
)
}
// process = _,3.14 : +;
static void test2()
{
COMPILER
(
Box box = boxSeq(boxPar(boxWire(), boxReal(3.14)), boxAdd());
compile("test2", box);
)
}
// Alternate version with the binary 'boxAdd' version
// process = +(_,3.14);
static void test3()
{
COMPILER
(
Box box = boxAdd(boxWire(), boxReal(3.14));
compile("test3", box);
)
}
// process = _,_ : +;
static void test4()
{
COMPILER
(
Box box = boxSeq(boxPar(boxWire(), boxWire()), boxAdd());
compile("test4", box);
)
}
// Connection error
// process = _ : +;
static void test5()
{
COMPILER
(
Box box = boxSeq(boxWire(), boxMul());
compile("test5", box);
)
}
// process = @(_,7);
static void test6()
{
COMPILER
(
Box box = boxDelay(boxWire(), boxInt(7));
compile("test6", box);
)
}
// process = @(_,7);
static void test7()
{
createLibContext();
Box box = boxDelay(boxWire(), boxInt(7));
// Vector compilation
const char* argv[] = { "-vec", "-lv", "1" };
compile("test7", box, 3, argv);
destroyLibContext();
}
// process = _ <: @(500) + 0.5, @(3000) * 1.5;
static void test8()
{
COMPILER
(
Box box = boxSplit(boxWire(), boxPar(boxAdd(boxDelay(boxWire(), boxReal(500)), boxReal(0.5)),
boxMul(boxDelay(boxWire(), boxReal(3000)), boxReal(1.5))));
compile("test8", box);
)
}
// Equivalent signal expressions
static void equivalent1()
{
COMPILER
(
Box b1 = boxAdd(boxDelay(boxWire(), boxReal(500)), boxReal(0.5));
Box box = boxPar(b1, b1);
compile("equivalent1", box);
)
}
static void equivalent2()
{
COMPILER
(
Box box = boxPar(boxAdd(boxDelay(boxWire(), boxReal(500)), boxReal(0.5)),
boxAdd(boxDelay(boxWire(), boxReal(500)), boxReal(0.5)));
compile("equivalent2", box);
)
}
// process = _,hslider("Freq [midi:ctrl 7][style:knob]", 100, 100, 2000, 1) : *;
static void test9()
{
COMPILER
(
Box box = boxMul(boxWire(), boxHSlider("Freq [midi:ctrl 7][style:knob]", boxReal(100), boxReal(100), boxReal(2000), boxReal(1)));
compile("test9", box);
)
}
/*
import("stdfaust.lib");
freq = vslider("h:Oscillator/freq", 440, 50, 1000, 0.1);
gain = vslider("h:Oscillator/gain", 0, 0, 1, 0.01);
process = freq * gain;
*/
static void test10()
{
COMPILER
(
Box box = boxMul(boxVSlider("h:Oscillator/freq", boxReal(440), boxReal(50), boxReal(1000), boxReal(0.1)),
boxVSlider("h:Oscillator/gain", boxReal(0), boxReal(0), boxReal(1), boxReal(0.01)));
compile("test10", box);
)
}
// import("stdfaust.lib");
// process = ma.SR, ma.BS;
static void test11()
{
COMPILER
(
Box box = boxPar(SR(), BS());
compile("test11", box);
)
}
// process = waveform { 0, 100, 200, 300, 400 };
static void test12()
{
COMPILER
(
tvec waveform;
// Fill the waveform content vector
for (int i = 0; i < 5; i++) {
waveform.push_back(boxReal(100*i));
}
Box box = boxWaveform(waveform); // the waveform content
compile("test12", box);
)
}
// process = _ <: +;
static void test13()
{
COMPILER
(
Box box = boxSplit(boxWire(), boxAdd());
compile("test13", box);
)
}
// process = _,_ <: !,_,_,! :> _,_;
static void test14()
{
COMPILER
(
Box box = boxSplit(boxPar(boxWire(), boxWire()),
boxMerge(boxPar4(boxCut(), boxWire(), boxWire(), boxCut()),
boxPar(boxWire(), boxWire())));
compile("test14", box);
)
}
// process = + ~ _;
static void test15()
{
COMPILER
(
Box box = boxRec(boxAdd(), boxWire());
compile("test15", box);
)
}
/*
import("stdfaust.lib");
process = phasor(440)
with {
decimalpart = _,int(_) : -;
phasor(f) = f/ma.SR : (+ <: decimalpart) ~ _;
};
*/
static Box decimalpart()
{
return boxSub(boxWire(), boxIntCast(boxWire()));
}
static Box phasor(Box f)
{
return boxSeq(boxDiv(f, SR()), boxRec(boxSplit(boxAdd(), decimalpart()), boxWire()));
}
static void test16()
{
COMPILER
(
Box box = phasor(boxReal(440));
compile("test16", box);
)
}
/*
import("stdfaust.lib");
process = osc(440), osc(440)
with {
decimalpart(x) = x-int(x);
phasor(f) = f/ma.SR : (+ : decimalpart) ~ _;
osc(f) = sin(2 * ma.PI * phasor(f));
};
*/
static Box osc(Box f)
{
return boxSin(boxMul(boxMul(boxReal(2.0), boxReal(3.141592653)), phasor(f)));
}
static void test17()
{
COMPILER
(
Box box = boxPar(osc(boxReal(440)), osc(boxReal(440)));
compile("test17", box);
)
}
// process = 0,0 : soundfile("sound[url:{'tango.wav'}]", 2);
static void test18()
{
COMPILER
(
Box box = boxSoundfile("sound[url:{'tango.wav'}]", boxInt(2), boxInt(0), boxInt(0));
compile("test18", box);
)
}
// process = 10,1,int(_) : rdtable;
static void test19()
{
COMPILER
(
Box box = boxReadOnlyTable(boxInt(10), boxInt(1), boxIntCast(boxWire()));
compile("test19", box);
)
}
// process = 10,1,int(_),int(_),int(_) : rwtable;
static void test20()
{
COMPILER
(
Box box = boxWriteReadTable(boxInt(10), boxInt(1), boxIntCast(boxWire()), boxIntCast(boxWire()), boxIntCast(boxWire()));
compile("test20", box);
)
}
/*
import("stdfaust.lib");
process = osc(f1), osc(f2)
with {
decimalpart(x) = x-int(x);
phasor(f) = f/ma.SR : (+ : decimalpart) ~ _;
osc(f) = sin(2 * ma.PI * phasor(f));
f1 = vslider("Freq1", 300, 100, 2000, 0.01);
f2 = vslider("Freq2", 500, 100, 2000, 0.01);
};
*/
// Using the LLVM backend.
static void test21(int argc, char* argv[])
{
cout << "test21\n";
string error_msg;
llvm_dsp_factory* factory = nullptr;
createLibContext();
{
Box sl1 = boxVSlider("h:Oscillator/Freq1", boxReal(300), boxReal(100), boxReal(2000), boxReal(0.01));
Box sl2 = boxVSlider("h:Oscillator/Freq2", boxReal(300), boxReal(100), boxReal(2000), boxReal(0.01));
Box group = boxPar(osc(sl1), osc(sl2));
Box box = boxVGroup("test21", group);
factory = createDSPFactoryFromBoxes("FaustDSP", box, 0, nullptr, "", error_msg);
}
destroyLibContext();
// The factory can be used outside of the createLibContext/destroyLibContext scope
if (factory) {
dsp* dsp = factory->createDSPInstance();
assert(dsp);
runAudio(dsp, "Organ", argc, argv);
delete dsp;
deleteDSPFactory(factory);
} else {
cerr << "Cannot create factory" << error_msg << endl;
}
}
// Using the Interpreter backend.
static void test22(int argc, char* argv[])
{
cout << "test22\n";
string error_msg;
interpreter_dsp_factory* factory = nullptr;
createLibContext();
{
Box sl1 = boxHSlider("v:Oscillator/Freq1", boxReal(300), boxReal(100), boxReal(2000), boxReal(0.01));
Box sl2 = boxHSlider("v:Oscillator/Freq2", boxReal(300), boxReal(100), boxReal(2000), boxReal(0.01));
Box group = boxPar(osc(sl1), osc(sl2));
Box box = boxHGroup("test22", group);
factory = createInterpreterDSPFactoryFromBoxes("FaustDSP", box, 0, nullptr, error_msg);
}
destroyLibContext();
// The factory can be used outside of the createLibContext/destroyLibContext scope
if (factory) {
dsp* dsp = factory->createDSPInstance();
assert(dsp);
runAudio(dsp, "Organ", argc, argv);
delete dsp;
deleteInterpreterDSPFactory(factory);
} else {
cerr << "Cannot create factory" << error_msg << endl;
}
}
// Using the Interpreter backend.
static void test23(int argc, char* argv[])
{
cout << "test23\n";
interpreter_dsp_factory* factory = nullptr;
string error_msg;
createLibContext();
{
Box sl1 = boxHSlider("v:Oscillator/Freq1", boxReal(300),
boxReal(100), boxReal(2000), boxReal(0.01));
Box sl2 = boxHSlider("v:Oscillator/Freq2", boxReal(300),
boxReal(100), boxReal(2000), boxReal(0.01));
Box group = boxPar(osc(sl1), osc(sl2));
Box box = boxTGroup("test23", group);
// Print the box
cout << "Print the box\n";
cout << printBox(box, false, INT_MAX);
// Print the box in short form
cout << "Print the box in short form\n";
cout << printBox(box, false, 128);
// Print the box in shared mode
cout << "Print the box with shared identifiers\n";
cout << printBox(box, true, INT_MAX);
// Compile the 'box' to 'signals'
tvec signals = boxesToSignals(box, error_msg);
// Print the signals
cout << "Print the signals\n";
for (size_t i = 0; i < signals.size(); i++) {
cout << printSignal(signals[i], false, INT_MAX) << endl;
}
cout << "Print the signals in short form\n";
for (size_t i = 0; i < signals.size(); i++) {
cout << printSignal(signals[i], false, 128) << endl;
}
// Print the signals in shared mode
cout << "Print the signals in shared mode\n";
for (size_t i = 0; i < signals.size(); i++) {
cout << printSignal(signals[i], true, INT_MAX) << endl;
}
// Then compile the 'signals' to a DSP factory
factory = createInterpreterDSPFactoryFromSignals("FaustDSP",
signals, 0,
nullptr, error_msg);
}
destroyLibContext();
// The factory can be used outside of the createLibContext/destroyLibContext scope
if (factory) {
dsp* dsp = factory->createDSPInstance();
assert(dsp);
runAudio(dsp, "Organ", argc, argv);
delete dsp;
deleteInterpreterDSPFactory(factory);
} else {
cerr << error_msg;
}
}
/*
import("stdfaust.lib");
process = organ, organ
with {
decimalpart(x) = x-int(x);
phasor(f) = f/ma.SR : (+ : decimalpart) ~ _;
osc(f) = sin(2 * ma.PI * phasor(f));
freq = nentry("freq", 100, 100, 3000, 0.01);
gate = button("gate");
gain = nentry("gain", 0.5, 0, 1, 0.01);
organ = gate * (osc(freq) * gain + osc(2 * freq) * gain);
};
*/
// Simple polyphonic DSP.
static void test24(int argc, char* argv[])
{
cout << "test24\n";
interpreter_dsp_factory* factory = nullptr;
string error_msg;
createLibContext();
{
// Follow the freq/gate/gain convention, see: https://faustdoc.grame.fr/manual/midi/#standard-polyphony-parameters
Box freq = boxNumEntry("freq", boxReal(100), boxReal(100), boxReal(3000), boxReal(0.01));
Box gate = boxButton("gate");
Box gain = boxNumEntry("gain", boxReal(0.5), boxReal(0), boxReal(1), boxReal(0.01));
Box organ = boxMul(gate, boxAdd(boxMul(osc(freq), gain), boxMul(osc(boxMul(freq, boxInt(2))), gain)));
// Stereo
Box box = boxPar(organ, organ);
factory = createInterpreterDSPFactoryFromBoxes("FaustDSP", box, 0, nullptr, error_msg);
}
destroyLibContext();
// The factory can be used outside of the createLibContext/destroyLibContext scope
if (factory) {
dsp* dsp = factory->createDSPInstance();
assert(dsp);
// Allocate polyphonic DSP
dsp = new mydsp_poly(dsp, 8, true, true);
// Allocate MIDI/audio driver
jackaudio_midi audio;
audio.init("Organ", dsp);
// Create GUI
GTKUI gtk_ui = GTKUI((char*)"Organ", &argc, (char***)&argv);
dsp->buildUserInterface(>k_ui);
// Create MIDI controller
MidiUI midi_ui = MidiUI(&audio);
dsp->buildUserInterface(&midi_ui);
// Start real-time processing
audio.start();
// Start MIDI
midi_ui.run();
// Start GUI
gtk_ui.run();
// Cleanup
audio.stop();
delete dsp;
deleteInterpreterDSPFactory(factory);
} else {
cerr << "Cannot create factory" << error_msg << endl;
}
}
// Compile a complete DSP program to a box expression, then to a source string
// in several target languages
static void test25()
{
cout << "test25\n";
vector<const char*> lang = { "c", "cpp", "cmajor", "codebox", "csharp", "dlang", "fir", "interp", "jax", "jsfx", "julia", "rust", "wast" };
// Context has to be created/destroyed each time
for (const auto& it : lang) {
createLibContext();
{
int inputs = 0;
int outputs = 0;
string error_msg;
// Create the oscillator
Box osc = DSPToBoxes("FaustDSP", "import(\"stdfaust.lib\"); process = os.osc(440);", 0, nullptr, &inputs, &outputs, error_msg);
if (!osc) {
cerr << error_msg;
destroyLibContext();
return;
}
// Compile it to the target language
string source = createSourceFromBoxes("FaustDSP", osc, it, 0, nullptr, error_msg);
if (source != "") {
cout << source;
} else {
cerr << error_msg;
}
}
destroyLibContext();
}
}
// Compile a complete DSP program to a box expression, then use the result in another expression
static void test26()
{
cout << "test26\n";
createLibContext();
{
int inputs = 0;
int outputs = 0;
string error_msg;
// Create the filter without parameter
Box filter = DSPToBoxes("FaustDSP", "import(\"stdfaust.lib\"); process = fi.lowpass(5);", 0, nullptr, &inputs, &outputs, error_msg);
if (!filter) {
cerr << error_msg;
destroyLibContext();
return;
}
// Create the filter parameters and connect
Box cutoff = boxHSlider("cutoff", boxReal(300), boxReal(100), boxReal(2000), boxReal(0.01));
Box cutoffAndInput = boxPar(cutoff, boxWire());
Box filteredInput = boxSeq(cutoffAndInput, filter);
getBoxType(filteredInput, &inputs, &outputs);
std::cout << "getBoxType inputs: " << inputs << " outputs: " << outputs << std::endl;
// Compile it
string source = createSourceFromBoxes("FaustDSP", filteredInput, "cpp", 0, nullptr, error_msg);
if (source != "") {
cout << source;
} else {
cerr << error_msg;
}
}
destroyLibContext();
}
list<GUI*> GUI::fGuiList;
ztimedmap GUI::gTimedZoneMap;
int main(int argc, char* argv[])
{
test0();
test1();
test2();
test3();
test4();
test5();
test6();
test7();
equivalent1();
equivalent2();
test8();
test9();
test10();
test11();
test12();
test13();
test14();
test15();
test16();
test17();
test18();
test19();
test20();
// Test 'DSPToBoxes' API (1)
test25();
// Test 'DSPToBoxes/createSourceFromBoxes' API (2)
test26();
// Test with audio, GUI and LLVM backend
test21(argc, argv);
// Test with audio, GUI and Interp backend
test22(argc, argv);
// Test with audio, GUI and Interp backend and using 'boxesToSignals' function
test23(argc, argv);
// Test with audio, GUI, MIDI and Interp backend
test24(argc, argv);
return 0;
}
|