Skip to content

Commit

Permalink
Add faust-dynamic-engine-test.
Browse files Browse the repository at this point in the history
  • Loading branch information
sletz committed Aug 30, 2023
1 parent 55c6f81 commit a1e655e
Show file tree
Hide file tree
Showing 4 changed files with 164 additions and 22 deletions.
37 changes: 22 additions & 15 deletions architecture/faust/dsp/faust-dynamic-engine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ that work under terms of your choice, so long as this FAUST
architecture section is not modified.
***************************************************************************/

#include <math.h>
#include <string.h>
#include <stdlib.h>
#include <string>
Expand All @@ -35,15 +34,20 @@ architecture section is not modified.

#if COREAUDIO_DRIVER
#include "faust/audio/coreaudio-dsp.h"
#elif IOS_DRIVER
#endif
#if IOS_DRIVER
#include "faust/audio/coreaudio-ios-dsp.h"
#elif ANDROID_DRIVER
#endif
#if ANDROID_DRIVER
#include "faust/audio/oboe-dsp.h"
#elif ALSA_DRIVER
#endif
#if ALSA_DRIVER
#include "faust/audio/alsa-dsp.h"
#elif JACK_DRIVER
#endif
#if JACK_DRIVER
#include "faust/audio/jack-dsp.h"
#elif PORTAUDIO_DRIVER
#endif
#if PORTAUDIO_DRIVER
#include "faust/audio/portaudio-dsp.h"
#endif

Expand All @@ -58,15 +62,15 @@ architecture section is not modified.

using namespace std;

enum { kPortAudioRenderer = 0, kJackRenderer, kCoreAudioRenderer, kiOSRenderer, kAlsaRenderer, kAndroidRenderer };
enum RendererType { kPortAudioRenderer = 0, kJackRenderer, kCoreAudioRenderer, kiOSRenderer, kAlsaRenderer, kAndroidRenderer };

//**************************************************************
// Globals
//**************************************************************

static string gLastError;

// DSP wrapper
// DSP wrapper

struct dsp_aux {

Expand Down Expand Up @@ -103,7 +107,7 @@ struct dsp_aux {
#else
dsp_aux():fDriver(nullptr)
{
fDSP = new mydsp();
fDSP = nullptr;
createJSON("dummy_dsp");
}
#endif
Expand Down Expand Up @@ -132,8 +136,9 @@ struct dsp_aux {
fJSON = json.JSON();
}

bool init(const char* name, int sr, int bsize, int renderer)
bool init(RendererType renderer, int sr, int bsize)
{

switch (renderer) {
#ifdef JACK_DRIVER
case kJackRenderer:
Expand All @@ -153,7 +158,7 @@ struct dsp_aux {
break;
#endif

#ifdef COREAUDIO_DRIVER
#ifdef IOS_DRIVER
case kiOSRenderer:
fDriver = new iosaudio(sr, bsize);
break;
Expand All @@ -171,13 +176,15 @@ struct dsp_aux {
break;
#endif
};

if (fDriver) {
fDriver->init(fNameApp, fDSP);
fDSP->buildUserInterface(&fParams);
#if SOUNDFILE
// Use bundle path
fSoundInterface = new SoundUI(SoundUI::getBinaryPath(), -1, nullptr);
std::cout << "SoundUI::getBinaryPath() " << SoundUI::getBinaryPath() << std::endl;
fSoundInterface = new SoundUI(SoundUI::getBinaryPath());
fDSP->buildUserInterface(fSoundInterface);
#endif
return true;
} else {
Expand Down Expand Up @@ -323,9 +330,9 @@ extern "C"

const char* getLastError() { return gLastError.c_str(); }

bool initDsp(dsp* dsp_ext, const char* name, int sr, int bsize, int renderer)
bool initDsp(dsp* dsp_ext, RendererType renderer, int sr, int bsize)
{
return reinterpret_cast<dsp_aux*>(dsp_ext)->init(name, sr, bsize, renderer);
return reinterpret_cast<dsp_aux*>(dsp_ext)->init(renderer, sr, bsize);
}

void destroyDsp(dsp* dsp_ext)
Expand Down
12 changes: 7 additions & 5 deletions architecture/faust/dsp/faust-dynamic-engine.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,19 +23,20 @@
***************************************************************************/

/* This file describe a simple C API for Faust objects including the audio drivers.
* Faust objects are cterated with createDsp, then initialized with initDsp andthe sampling rate and buffer size.
* Faust objects are built with createDsp, then initialized with initDsp and the sampling rate and buffer size.
* Then start() is called to open the drivers and start processing audio until stop() is called.
** All parameters can be accessed and controlled (typically using getParamsCountDsp/setParamValueDsp/getParamValueDsp).
* All parameters can be accessed and controlled (typically using getParamsCountDsp/setParamValueDsp/getParamValueDsp).
*
*
* To be compiled with something like:
* c++ -std=c++11 -DLLVM_DSP -DPORTAUDIO_DRIVER -DSOUNDFILE faust-dynamic-engine.cpp
* c++ -std=c++11 -DLLVM_DSP -DPORTAUDIO_DRIVER -DSOUNDFILE faust-dynamic-engine.cpp
* and linked with libfaust (either static or dynamic version, libsndfile (https://github.com/libsndfile/libsndfile)
* and PortAudio (https://www.portaudio.com).
*
* JACK (https://jackaudio.org) can possibly be used insted o PortAudio to provide a "multiple connectable DSP" experince
* that is: each DSP is created with createDsp/initDsp, then connected to others with getNumInputsDsp/getNumOutputsDsp and
** connectDsp/disconnectDsp/isConnectedDsp functions, or using and external connection tool like QjackCtl (https://qjackctl.sourceforge.io).
* connectDsp/disconnectDsp/isConnectedDsp functions, or using an external connection tool
* like QjackCtl (https://qjackctl.sourceforge.io).
*/

#ifndef FAUSTFLOAT
Expand Down Expand Up @@ -82,7 +83,7 @@ extern "C" {
* @param target The LLVM machine target: like 'i386-apple-macosx10.6.0:opteron',
* using an empty string takes the current machine settings,
* and i386-apple-macosx10.6.0:generic kind of syntax for a generic processor
* @param opt_level Optimization level.
* @param opt_level LLVM IR to IR optimization level (from -1 to 4, -1 means 'maximum possible value').
* @return A pointer to the created DSP instance on success, otherwise a null pointer. Use getLastError to access the error.
*/
dsp* createDsp(const char* name_app, const char* dsp_content, int argc, const char* argv[], const char* target, int opt_level);
Expand Down Expand Up @@ -372,4 +373,5 @@ extern "C" {
#ifdef __cplusplus
}
#endif

/************************** END faust-dynamic-engine.h **************************/
13 changes: 11 additions & 2 deletions tests/llvm-tests/Makefile
Original file line number Diff line number Diff line change
@@ -1,13 +1,22 @@
LIB := $(shell faust --libdir)
LIB_OPT := /opt/local/lib
INC := $(shell faust --includedir)
system := $(shell uname -s)

DESTDIR ?=
PREFIX ?= /usr/local
OPT_LIB ?=

ifeq ($(system), Darwin)
OPT_LIB=-framework CoreFoundation
endif

prefix := $(DESTDIR)$(PREFIX)

all: llvm-test llvm-algebra-test llvm-test-c
all: llvm-test llvm-algebra-test llvm-test-c faust-dynamic-engine-test

faust-dynamic-engine-test: faust-dynamic-engine-test.cpp $(LIB)/libfaust.a
$(CXX) -std=c++11 -O3 faust-dynamic-engine-test.cpp -DLLVM_DSP -DJACK_DRIVER -DPORTAUDIO_DRIVER -DSOUNDFILE -lsndfile $(OPT_LIB) -I/opt/local/include -ljack -lportaudio -I$(INC) -L$(LIB) -L$(LIB_OPT) $(LIB)/libfaust.a -lpthread `llvm-config --ldflags --libs all --system-libs` -o faust-dynamic-engine-test

llvm-test: llvm-test.cpp $(LIB)/libfaust.a
$(CXX) -std=c++11 -O3 llvm-test.cpp -I$(INC) -L$(LIB) -L$(LIB_OPT) $(LIB)/libfaust.a -lpthread `llvm-config --ldflags --libs all --system-libs` -o llvm-test
Expand Down Expand Up @@ -36,5 +45,5 @@ test-c: llvm-test-c
./llvm-test-c foo.dsp

clean:
rm -f llvm-test llvm-test-c llvm-algebra-test
rm -f llvm-test llvm-test-c llvm-algebra-test faust-dynamic-engine-test

124 changes: 124 additions & 0 deletions tests/llvm-tests/faust-dynamic-engine-test.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@

#include <assert.h>
#include <unistd.h>

#include "faust/dsp/faust-dynamic-engine.cpp"

static void test1()
{
// Create a DSP engine with a simple sine oscillator
const char* code = "import(\"stdfaust.lib\"); process = os.osc(500);";
dsp* DSP1 = createDsp("Osc", code, 0, NULL, "", -1);
assert(DSP1);

// Initialize the DSP engine with JACK as renderer
bool res = initDsp(DSP1, kJackRenderer, -1, -1);
assert(res);

// Start the DSP engine
startDsp(DSP1);

char c;
printf("Press 'q' to move to next test\n");
while ((c = getchar() != 'q')) {
usleep(100000);
}

// Stop the DSP engine and destroy it
stopDsp(DSP1);
destroyDsp(DSP1);
}

static void test2()
{
// Create a DSP engine with a soundfile player
const char* code = "process = 0,_~+(1):soundfile(\"sound[url:{'tango.wav'}]\",2):!,!,_,_;";
dsp* DSP1 = createDsp("Player", code, 0, NULL, "", -1);
assert(DSP1);

// Initialize the DSP engine with JACK as renderer
bool res = initDsp(DSP1, kJackRenderer, -1, -1);
assert(res);

// Start the DSP engine
startDsp(DSP1);

char c;
printf("Press 'q' to move to next test\n");
while ((c = getchar() != 'q')) {
usleep(100000);
}

// Stop the DSP engine and destroy it
stopDsp(DSP1);
destroyDsp(DSP1);
}

static void test3()
{
// Create a DSP engine with a soundfile player
const char* code1 = "process = 0,_~+(1):soundfile(\"sound[url:{'tango.wav'}]\",2):!,!,_,_;";
dsp* DSP1 = createDsp("Player", code1, 0, NULL, "", -1);
assert(DSP1);

// Initialize the DSP engine with JACK as renderer
bool res1 = initDsp(DSP1, kJackRenderer, -1, -1);
assert(res1);

// Start the DSP engine
startDsp(DSP1);

// Create a DSP engine with a freeverb
const char* code2 = "import(\"stdfaust.lib\"); process = dm.freeverb_demo;";
dsp* DSP2 = createDsp("Reverb", code2, 0, NULL, "", -1);
assert(DSP2);

// Initialize the DSP engine with JACK as renderer
bool res2 = initDsp(DSP2, kJackRenderer, -1, -1);
assert(res1);

// Start the DSP engine
startDsp(DSP2);

for (int param = 0; param < getParamsCountDsp(DSP2); param++) {
printf("Param %d: %s\n", param, getParamAddressDsp(DSP2, param));
}

// Set the wet parameter of the freeverb to 0.9
setParamValueDsp(DSP2, getParamIndexDsp(DSP2, "/Freeverb/Wet"), 0.9);

// Disconnect all connections to JACK
disconnectDsp(DSP1, NULL, 0, 0);
disconnectDsp(DSP1, NULL, 1, 1);

// Connect Player to Reverb
connectDsp(DSP1, DSP2, 0, 0);
connectDsp(DSP1, DSP2, 1, 1);

// Connect Reverb to JACK
connectDsp(DSP2, NULL, 0, 0);
connectDsp(DSP2, NULL, 1, 1);

char c;
printf("Press 'q' to move to next test\n");
while ((c = getchar() != 'q')) {
usleep(100000);
}

// Stop the DSP engine and destroy it
stopDsp(DSP1);
destroyDsp(DSP1);

// Stop the DSP engine and destroy it
stopDsp(DSP2);
destroyDsp(DSP2);
}

int main()
{
test1();
test2();
test3();
return 0;
}

0 comments on commit a1e655e

Please sign in to comment.