forked from odevices/er-301
-
Notifications
You must be signed in to change notification settings - Fork 0
/
EuclideanSequencer.cpp
84 lines (77 loc) · 2.02 KB
/
EuclideanSequencer.cpp
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
#include <EuclideanSequencer.h>
#include <bjorklund.h>
#include <od/config.h>
#include <hal/ops.h>
//#define BUILDOPT_VERBOSE
//#define BUILDOPT_DEBUG_LEVEL 10
#include <hal/log.h>
EuclideanSequencer::EuclideanSequencer(int space)
{
// Reserve space for the Bjorklund algorithm.
mSpace.reserve(space);
mScratch.reserve(2 * space);
// Register inlets, outlets, and parameters.
addInput(mTrigger);
addInput(mReset);
addOutput(mOutput);
addParameter(mBoxes);
mBoxes.enableSerialization();
addParameter(mCats);
mCats.enableSerialization();
// Initialize the cats and boxes.
simulateCatsInBoxes(0, 1);
}
EuclideanSequencer::~EuclideanSequencer()
{
}
void EuclideanSequencer::simulateCatsInBoxes(int cats, int boxes)
{
if (cats != mCachedCats || boxes != mCachedBoxes)
{
// Recalculate and cache the results.
logDebug(1, "cats=%d, boxes=%d", cats, boxes);
bjorklund(cats, boxes, mSpace.data(), mScratch.data());
if (boxes != mCachedBoxes)
{
mPhase = 0;
}
mCachedBoxes = boxes;
mCachedCats = cats;
}
}
// This method is called every audio frame.
// You are expected to process one frame of input and produce one frame of output.
void EuclideanSequencer::process()
{
// Read the parameters to see if they have changed.
int N = (int)mSpace.capacity();
int boxes = CLAMP(1, N, mBoxes.roundTarget());
int cats = CLAMP(0, boxes, mCats.roundTarget());
simulateCatsInBoxes(cats, boxes);
// Each inlet's buffer is filled with 1 frame of samples.
float *trig = mTrigger.buffer();
float *reset = mReset.buffer();
// The output buffer that needs to be filled.
float *out = mOutput.buffer();
// FRAMELENGTH is a macro that signals to the compiler that it should try to vectorize the loop.
for (int i = 0; i < FRAMELENGTH; i++)
{
if (reset[i] > 0.0f)
{
mPhase = 0;
}
if (trig[i] > 0.0f)
{
mPhase++;
if (mPhase >= mCachedBoxes)
{
mPhase = 0;
}
out[i] = mSpace[mPhase];
}
else
{
out[i] = 0.0f;
}
}
}