Skip to content

Commit

Permalink
Merge remote-tracking branch 'sletz/rework_ui' into develop
Browse files Browse the repository at this point in the history
- Use lambda set/get functions to update controllers.

- Use 'alloca' which is faster than std::vector and will also work on Windows.
  • Loading branch information
mzuther committed Jul 16, 2020
2 parents dd382af + 74deefe commit 091fa6f
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 95 deletions.
91 changes: 34 additions & 57 deletions src/ProtoFaust.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ ProtoFaust::ProtoFaust()
NUM_OUTPUTS,
NUM_LED_PINS );

FaustDSP.buildUserInterface( &FaustUI );

// sorry for the following cruft -- I'd give my right arm for
// LISP-like macros in C++ ...

Expand Down Expand Up @@ -167,17 +169,6 @@ ProtoFaust::ProtoFaust()

void ProtoFaust::onAdd()
{
FaustDSP.buildUserInterface( &FaustUI );

// store GUI IDs to save time during processing
for ( auto& widget : activeWidgets ) {
attachFaustParameter( widget );
}

for ( auto& widget : passiveWidgets ) {
attachFaustParameter( widget );
}

// initialize Faust using default sample rate; see
// ProtoFaust::process()
FaustDSP.init( 44100 );
Expand All @@ -196,8 +187,10 @@ void ProtoFaust::process( const ProcessArgs& args )
FaustDSP.init( args.sampleRate );
}

std::vector<FAUSTFLOAT> temporaryInputs( numberOfChannels );
std::vector<FAUSTFLOAT> temporaryOutputs( numberOfChannels );
FAUSTFLOAT* temporaryInputs = ( FAUSTFLOAT* ) alloca( numberOfChannels *
sizeof( FAUSTFLOAT ) );
FAUSTFLOAT* temporaryOutputs = ( FAUSTFLOAT* ) alloca( numberOfChannels *
sizeof( FAUSTFLOAT ) );

// read from module's inputs; scale voltages from Rack to usual
// range in DSP processing (-1.0 to +1.0) so you don't have to
Expand All @@ -212,7 +205,7 @@ void ProtoFaust::process( const ProcessArgs& args )

// get widget values and update Faust parameters
for ( auto& widget : activeWidgets ) {
updateParameter( widget );
updateParameterIn( widget );
}

// update Faust controls
Expand All @@ -221,8 +214,8 @@ void ProtoFaust::process( const ProcessArgs& args )
FaustDSP.control( int_control, real_control );

// process one sample in Faust
FaustDSP.compute( temporaryInputs.data(),
temporaryOutputs.data(),
FaustDSP.compute( temporaryInputs,
temporaryOutputs,
int_control,
real_control );

Expand All @@ -235,7 +228,7 @@ void ProtoFaust::process( const ProcessArgs& args )

// get widget values and update Faust parameters
for ( auto& widget : passiveWidgets ) {
updateParameter( widget );
updateParameterOut( widget );
}
}

Expand All @@ -245,9 +238,16 @@ void ProtoFaust::addParameter( std::vector<WidgetAccess>& widgets,
int parameterId,
const std::string& faustStringId )
{
widgets.push_back( WidgetAccess ( widgetType,
parameterId,
faustStringId ) );
// prepare controller zone 'set' and 'get' functions
FAUSTFLOAT* zone = FaustUI.getParamZone( faustStringId );

WidgetAccess::setFunction setFun = [ = ]( FAUSTFLOAT value ) {
*zone = value;
};

WidgetAccess::getFunction getFun = [ = ]() {
return *zone;
};

switch ( widgetType ) {
case ProtoFaustWidget::TOGGLE_SWITCH:
Expand All @@ -271,6 +271,11 @@ void ProtoFaust::addParameter( std::vector<WidgetAccess>& widgets,
2.0f,
0.0f,
"" );

// special 'set' version
setFun = [ = ]( FAUSTFLOAT value ) {
*zone = value / FAUSTFLOAT( 2.0 );
};
break;

case ProtoFaustWidget::KNOB_WHITE:
Expand All @@ -285,6 +290,11 @@ void ProtoFaust::addParameter( std::vector<WidgetAccess>& widgets,
"" );
break;
}

widgets.push_back( WidgetAccess ( widgetType,
parameterId,
setFun,
getFun ) );
}


Expand Down Expand Up @@ -314,48 +324,15 @@ void ProtoFaust::addParameterLed( std::vector<WidgetAccess>& widgets,
}


void ProtoFaust::updateParameter( WidgetAccess& widget )
void ProtoFaust::updateParameterIn( WidgetAccess& widget )
{
switch ( widget.widgetType ) {
case ProtoFaustWidget::TOGGLE_SWITCH:
case ProtoFaustWidget::PUSH_BUTTON:
case ProtoFaustWidget::KNOB_WHITE:
case ProtoFaustWidget::KNOB_RED:

{
FAUSTFLOAT value = params[widget.parameterId].getValue();
FaustUI.setParamValue( widget.faustId, value );

break;
}

case ProtoFaustWidget::THREE_WAY_SWITCH: {
FAUSTFLOAT value = params[widget.parameterId].getValue();

// three-way switches only work with integer-like values,
// so scale range from (0 .. 2) to (0 .. 1)
value /= 2.0f;
FaustUI.setParamValue( widget.faustId, value );

break;
}

case ProtoFaustWidget::LED_RGB:

lights[widget.parameterId].setBrightness(
FaustUI.getParamValue( widget.faustId ) );

break;
}
widget.faustSet( params[widget.parameterId].getValue() );
}


void ProtoFaust::attachFaustParameter( WidgetAccess& widget )
void ProtoFaust::updateParameterOut( WidgetAccess& widget )
{
widget.faustId = FaustUI.getParamIndex(
widget.faustStringId.c_str() );
lights[widget.parameterId].setBrightness( widget.faustGet() );
}


Model* modelProtoFaust = createModel<ProtoFaust, ProtoFaustWidget>(
"ProtoFaust" );
6 changes: 3 additions & 3 deletions src/ProtoFaust.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ struct ProtoFaust : Module {
const FAUSTFLOAT voltageScaling = 5.0f;

faust::FaustDSP FaustDSP;
faust::VCVRACKUI FaustUI;
faust::MapUI FaustUI;

ProtoFaust();

Expand All @@ -145,8 +145,8 @@ struct ProtoFaust : Module {
int parameterId,
const std::string& faustStringId );

void attachFaustParameter( WidgetAccess& widget );
void updateParameter( WidgetAccess& widget );
void updateParameterIn( WidgetAccess& widget );
void updateParameterOut( WidgetAccess& widget );
};

#endif // PROTO_FAUST_HPP
23 changes: 14 additions & 9 deletions src/WidgetAccess.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,24 +27,29 @@
#ifndef WIDGET_ACCESS_HPP
#define WIDGET_ACCESS_HPP

#include <algorithm>
#include <functional>


struct WidgetAccess {
public:
typedef std::function<void( FAUSTFLOAT )> setFunction;
typedef std::function<FAUSTFLOAT()> getFunction;

int widgetType;
int parameterId;
int faustId;
std::string faustStringId;

setFunction faustSet;
getFunction faustGet;

WidgetAccess( int widget_type,
int parameter_id,
const std::string& faust_string_id ) :
setFunction set,
getFunction get ) :
widgetType( widget_type ),
parameterId( parameter_id ),
faustStringId( faust_string_id )
{
// set in ProtoFaust::attachFaustParameter()
faustId = -1;
};
faustSet( set ),
faustGet( get )
{};
};

#endif // WIDGET_ACCESS_HPP
30 changes: 4 additions & 26 deletions src/faust/architecture_rack.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@

#include <cstring>

#include "faust/gui/APIUI.h"
#include "faust/gui/MapUI.h"
#include "faust/gui/meta.h"
#include "faust/dsp/one-sample-dsp.h"

Expand All @@ -60,30 +60,8 @@

/*******************BEGIN ARCHITECTURE SECTION (part 2/2)***************/

// *INDENT-ON* --> give astyle free reign ...

class VCVRACKUI : public APIUI
{
public:
VCVRACKUI() : APIUI() {}
virtual ~VCVRACKUI() {}

virtual void declare( FAUSTFLOAT* zone, const char* key, const char* val ) override
{
APIUI::declare( zone, key, val );
}


virtual void addParameter( const char* label,
FAUSTFLOAT* zone,
FAUSTFLOAT init,
FAUSTFLOAT min,
FAUSTFLOAT max,
FAUSTFLOAT step,
ItemType type ) override
{
APIUI::addParameter( label, zone, init, min, max, step, type );
}
};
// EMPTY !!!

/********************END ARCHITECTURE SECTION (part 2/2)****************/

// *INDENT-ON* --> give astyle free reign ...

0 comments on commit 091fa6f

Please sign in to comment.