count-looped

visuelle-musik/16-step-sequencer-library/count-looped

Stores a number which gets incremented on each `INC` pulse.
count-looped
@/count-looped
Stores a number which gets incremented on each `INC` pulse.
TRIGpulse
Triggers a single increment.
INCnumber
Value to add on each increment. Use a negative value (e.g. -1) to make decrements.
BEGINnumber
First step to be activated
ENDnumber
Last Step to be activated, you may use WIDTH to calculate this , alternatively!
WIDTHnumber
Number of steps to be activated after BEGIN, you may use END to determine this , alternatively!
RSTpulse
Resets the accumulated value to zero.
count-looped
OUT
TRIG
INC
BEGIN
END
WIDTH
RST
OUTnumber
The accumulated value. (range 1-16)
To use the node in your project you should have the visuelle-musik/16-step-sequencer-library library installed. Use the “File → Add Library” menu item in XOD IDE if you don’t have it yet. See Using libraries for more info.

C++ implementation

node {
    void evaluate(Context ctx) 
    {
        int count = (int)getValue<output_OUT>(ctx);
        int begin = (int)getValue<input_BEGIN>(ctx);
        int end = (int)getValue<input_END>(ctx);
        int width = (int)getValue<input_WIDTH>(ctx);
        if( width > 0 ) // Use width instead of end if available
            end = min( 16, begin + width);      // Truncate to max. allowed border-value if needed
        int increment = (int)getValue<input_INC>(ctx); 
        
        if( begin > end )                  // Reverse direction?
        {
            int original_begin = begin;
            begin = end;
            end = original_begin;
            increment %= end-begin+1;     // Reduce increment to max possible stepwidth 
            increment *= -1;              // Reverse increment to fit with intended stepping-direction  
        } 
        else
            increment %= end-begin+1;     // Reduce increment to max possible stepwidth
        
        if (isInputDirty<input_RST>(ctx))
            count = begin;
        else if (isInputDirty<input_TRIG>(ctx))
        {    
            count += increment;
            if( count > end )
                count = begin;
            else if( count < begin)
                count = end;
        }
        emitValue<output_OUT>(ctx, count);
    }
}