/
DualDrumPatch.hpp
executable file
·108 lines (103 loc) · 2.64 KB
/
DualDrumPatch.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
#ifndef __DualDrumPatch_hpp__
#define __DualDrumPatch_hpp__
#include "StompBox.h"
#include "VoltsPerOctave.h"
#include "Cymbal.hpp"
#include "BassDrum.hpp"
class SampleTrigger {
private:
VoltsPerOctave volts;
Envelope* env;
bool state;
const float threshold = 2.0f; // 2v trigger threshold
public:
SampleTrigger(Envelope* envelope) :
state(false){}
void trigger(FloatArray samples){
for(int i=0; i<samples.getSize(); ++i)
if(volts.sampleToVolts(samples[i]) > threshold && !state){
state = true;
env->trigger(i);
}else{
state = false;
}
}
void gate(FloatArray samples){
for(int i=0; i<samples.getSize(); ++i){
if(volts.sampleToVolts(samples[i]) > threshold){
if(!state){
env->gate(true, i);
state = true;
}
}else{
if(state){
env->gate(false, i);
state = false;
}
}
}
}
};
class DrumTrigger {
private:
VoltsPerOctave volts;
Drum* drum;
bool state;
const float threshold = 2.0f; // 2v trigger threshold
public:
DrumTrigger(Drum* drm) :
drum(drm), state(false){}
void getSamples(FloatArray samples){
for(int i=0; i<samples.getSize(); ++i){
if(volts.sampleToVolts(samples[i]) > threshold){
if(!state){
state = true;
drum->trigger();
}
}else{
state = false;
}
samples[i] = drum->getNextSample();
}
}
};
class DualDrumPatch : public Patch {
private:
CymbalVoice* hat;
BassDrumVoice* kick;
DrumTrigger* drum[2];
public:
DualDrumPatch(){
registerParameter(PARAMETER_A, "Hat Tone");
registerParameter(PARAMETER_B, "Hat Decay");
registerParameter(PARAMETER_C, "Kick Tone");
registerParameter(PARAMETER_D, "Kick Decay");
registerParameter(PARAMETER_E, "Accent");
hat = new CymbalVoice(getSampleRate());
kick = new BassDrumVoice(getSampleRate());
drum[0] = new DrumTrigger(hat);
drum[1] = new DrumTrigger(kick);
kick->setSnap(0.2);
}
~DualDrumPatch(){
}
void processAudio(AudioBuffer& buffer){
float tone = 120*powf(2, getParameterValue(PARAMETER_A)*4);
float decay = getParameterValue(PARAMETER_B);
float accent = getParameterValue(PARAMETER_E);
hat->setFrequency(tone);
hat->setFilter(getParameterValue(PARAMETER_A)*0.3 + 0.5);
hat->setDecay(decay);
hat->setAccent(accent);
tone = 20*powf(2, getParameterValue(PARAMETER_C)*4);
decay = getParameterValue(PARAMETER_D);
kick->setFrequency(tone);
kick->setDecay(decay);
kick->setAccent(accent);
FloatArray left = buffer.getSamples(LEFT_CHANNEL);
FloatArray right = buffer.getSamples(RIGHT_CHANNEL);
drum[0]->getSamples(left);
drum[1]->getSamples(right);
}
};
#endif // __DualDrumPatch_hpp__