-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSnoozequencerPatch.hpp
135 lines (109 loc) · 3.11 KB
/
SnoozequencerPatch.hpp
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
#ifndef SNOOZEQUENCERPATCH_HPP
#define SNOOZEQUENCERPATCH_HPP
#define USE_MIDI_CALLBACK
#include "BassDrum.hpp"
#include "SnareDrum.hpp"
#include "Hat.hpp"
#include "AdsrEnvelope.h"
#include "Patch.h"
class SnoozequencerPatch : public Patch {
private:
int blockSize;
float sampleRate;
SineOscillator* sin;
AdsrEnvelope<true>* env;
FloatArray sinFloat;
FloatArray drumOutFloat;
FloatArray drum2OutFloat;
FloatArray drum3OutFloat;
bool bdTriggers[16*4];
bool sdTriggers[16*4];
bool hdTriggers[16*4];
int pos;
BassDrum* bd;
SnareDrum* sd;
Hat* hd;
bool recording;
public:
SnoozequencerPatch() :
sampleRate(getSampleRate()),
blockSize(getBlockSize()),
pos(0),
recording(false) {
registerParameter(PARAMETER_A, "Metronome");
sinFloat = FloatArray::create(blockSize);
drumOutFloat = FloatArray::create(blockSize);
drum2OutFloat = FloatArray::create(blockSize);
drum3OutFloat = FloatArray::create(blockSize);
sin = SineOscillator::create(sampleRate);
sin->setFrequency(800);
env = AdsrEnvelope<true>::create(sampleRate);
env->setAttack(.01);
env->setDecay(.05);
env->setSustain(0);
bd = new BassDrum(sampleRate);
sd = new SnareDrum(sampleRate);
hd = new Hat(sampleRate);
}
void buttonChanged(PatchButtonId bid, uint16_t value, uint16_t memes) override {
if (bid == BUTTON_2 && value == ON) {
recording = !recording;
}
if (bid == BUTTON_1 && value == ON) {
tick();
}
}
void processMidi(MidiMessage msg) override {
if (msg.isNoteOn() && msg.getNote() == 36) {
bd->trigger();
if (recording)
bdTriggers[pos] = true;
}
if (msg.isNoteOn() && msg.getNote() == 37) {
sd->trigger();
if (recording)
sdTriggers[pos] = true;
}
if (msg.isNoteOn() && msg.getNote() == 38) {
hd->trigger();
if (recording)
hdTriggers[pos] = true;
}
}
void tick() {
pos = (pos + 1) % 64;
if (pos % (4*4) == 0) {
sin->setFrequency(1000);
}
else {
sin->setFrequency(800);
}
if (pos == 0) {
sin->setFrequency(1200);
}
if (pos % 4 == 0)
env->trigger();
if (bdTriggers[pos]) {
bd->trigger();
}
if (sdTriggers[pos]) {
sd->trigger();
}
if (hdTriggers[pos]) {
hd->trigger();
}
}
void processAudio(AudioBuffer &buffer) override {
FloatArray inOutLeft = buffer.getSamples(0);
sin->generate(sinFloat);
sinFloat.multiply(getParameterValue(PARAMETER_A));
bd->generate(drumOutFloat);
sd->generate(drum2OutFloat);
hd->generate(drum3OutFloat);
env->process(sinFloat, inOutLeft);
inOutLeft.add(drumOutFloat);
inOutLeft.add(drum2OutFloat);
inOutLeft.add(drum3OutFloat);
}
};
#endif