Skip to content

Commit

Permalink
nova ugens: prototype new input generators via functors
Browse files Browse the repository at this point in the history
Signed-off-by: Tim Blechmann <[email protected]>
  • Loading branch information
timblechmann committed Feb 6, 2013
1 parent 482aa90 commit 68f55aa
Show file tree
Hide file tree
Showing 2 changed files with 128 additions and 12 deletions.
24 changes: 12 additions & 12 deletions source/NovaUGens/FeedbackAM.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,12 @@
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/

#include "SC_PlugIn.hpp"
#include "NovaUGensCommon.hpp"

static InterfaceTable *ft;

struct FBAM:
public SCUnit
public NovaUnit
{
public:
FBAM():
Expand Down Expand Up @@ -50,7 +50,7 @@ struct FBAM:
void next_i(int inNumSamples)
{
auto fb = makeScalar(fb_);
next<false>(inNumSamples, fb);
next(inNumSamples, fb);
}

void next_k(int inNumSamples)
Expand All @@ -61,18 +61,18 @@ struct FBAM:
if (newFeedback != fb_) {
auto fb = makeSlope(newFeedback, fb_);
fb_ = newFeedback;
next<false>(inNumSamples, fb);
next(inNumSamples, fb);
} else
next_i(inNumSamples);
}

void next_a(int inNumSamples)
{
auto fb = makeSignal(1);
next<true>(inNumSamples, fb);
auto fb = makeSignal(1, [](float f) { return sc_clip(f, 0.f, 1.f);});
next(inNumSamples, fb);
}

template <bool clip, typename FeedBack>
template <typename FeedBack>
void next(int inNumSamples, FeedBack & fb)
{
const float * inSig = zin(0);
Expand All @@ -86,10 +86,10 @@ struct FBAM:
const float x2 = ZXP(inSig);
const float x3 = ZXP(inSig);

float fb0 = clip ? sc_clip(fb.consume(), 0, 1.9) : fb.consume();
float fb1 = clip ? sc_clip(fb.consume(), 0, 1.9) : fb.consume();
float fb2 = clip ? sc_clip(fb.consume(), 0, 1.9) : fb.consume();
float fb3 = clip ? sc_clip(fb.consume(), 0, 1.9) : fb.consume();
float fb0 = fb.consume();
float fb1 = fb.consume();
float fb2 = fb.consume();
float fb3 = fb.consume();

float out0 = tick(x0, zm1, fb0);
float out1 = tick(x1, zm1, fb1);
Expand All @@ -104,7 +104,7 @@ struct FBAM:

loop(inNumSamples & 3, [&] {
const float x = ZXP(inSig);
float feedback = clip ? sc_clip(fb.consume(), 0, 1.9) : fb.consume();
float feedback = fb.consume();
ZXP(outSig) = tick(x, zm1, feedback);
});

Expand Down
116 changes: 116 additions & 0 deletions source/NovaUGens/NovaUGensCommon.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
/*
*
* Copyright (C) 2013 Tim Blechmann
*
* This program 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 2 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, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/

#include "SC_PlugIn.hpp"

#ifndef NOVAUGENSCOMMON_HPP
#define NOVAUGENSCOMMON_HPP

struct NovaUnit:
public SCUnit
{
template <typename FloatType, typename Functor>
struct ScalarSignal2
{
ScalarSignal2(FloatType value, Functor const & f):
value(f(value))
{}

FloatType consume() const
{
return value;
}

FloatType value;
};

template <typename FloatType, typename Functor>
struct SlopeSignal2:
Functor
{
SlopeSignal2(FloatType value, FloatType slope, Functor const & f):
Functor(f), value(value), slope(slope)
{}

FloatType consume()
{
FloatType ret = value;
value += slope;
return Functor::operator()(ret);
}

FloatType value, slope;
};

template <typename FloatType, typename Functor>
struct AudioSignal2:
Functor
{
AudioSignal2(const FloatType * pointer, Functor const & f):
Functor(f), pointer(pointer)
{}

FloatType consume()
{
return Functor::operator()(*pointer++);
}

const FloatType * pointer;
};

template <typename FloatType, typename Functor>
inline ScalarSignal2<FloatType, Functor> makeScalar(FloatType value, Functor const & f) const
{
return ScalarSignal2<FloatType, Functor>(value, f);
}

template <typename FloatType>
inline ScalarSignal<FloatType> makeScalar(FloatType value) const
{
return SCUnit::makeScalar(value);
}

template <typename FloatType, typename Functor>
inline SlopeSignal2<FloatType, Functor> makeSlope(FloatType next, FloatType last, Functor const & f) const
{
return SlopeSignal2<FloatType, Functor>(last, calcSlope(next, last), f);
}

template <typename FloatType>
inline SlopeSignal<FloatType> makeSlope(FloatType next, FloatType last) const
{
return SCUnit::makeSlope(next, last);
}

template <typename Functor>
inline AudioSignal2<float, Functor> makeSignal(int index, Functor const & f) const
{
const float * input = in(index);
return AudioSignal2<float, Functor>(input, f);
}

template <typename Functor>
inline AudioSignal<float> makeSignal(int index) const
{
return SCUnit::makeSignal(index);
}
};


#endif // NOVAUGENSCOMMON_HPP

0 comments on commit 68f55aa

Please sign in to comment.