-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathOrcaDSP.h
147 lines (133 loc) · 3.86 KB
/
OrcaDSP.h
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
136
137
138
139
140
141
142
143
144
145
146
147
#pragma once
#include "OrcaChannel.h"
#include "LFO.h"
#include "ADSR.h"
#define NUM_CHANNELS (8)
class OrcaDSP {
public:
const OrcaConfig* config;
private:
//components
OrcaChannel* channels[8];
LFO* lfo[4];
double modWheel = 0.0;
double bendWheel = 0.0;
// state
double lfoValue[4];
double currentNote = -1;
public:
// methods
OrcaDSP(const OrcaConfig* config) {
this->config = config;
for (int i = 0; i < 4; i++) {
lfo[i] = new LFO(i / 4.0, &config->lfoRate, &config->samplerate, &config->lfoWaveform);
}
for (int i = 0; i< 8; i++) {
channels[i] = new OrcaChannel(config, &lfoValue[i % 4], &modWheel, &bendWheel);
}
}
void ModWheel(double value) {
modWheel = value;
}
void BendWheel(double value) {
bendWheel = value;
}
void NoteOn(int note, int velocity) {
if (currentNote == -1) {
currentNote = note;
}
if (config->portamentoType == 1) { // off
currentNote = note;
}
if (config->portamentoType == 0) { // auto
if (!anyChannelGateOn()) {
currentNote = note;
}
}
if (config->poly == 1) {
channels[0]->Trigger(note, (double)velocity / 127.0, currentNote);
currentNote = note;
} else {
OrcaChannel *existingChannel = findNote(note);
if (existingChannel != NULL) {
existingChannel->Trigger(note, (double)velocity/127.0, currentNote);
currentNote = note;
return;
}
OrcaChannel *channel = findChannel(idle);
if (channel == NULL) {
channel = findChannel(release);
}
if (channel == NULL) {
channel = findChannel(sustain);
}
if (channel == NULL) {
channel = findChannel(decay);
}
if (channel == NULL) {
channel = findChannel(attack);
}
if (channel != NULL) {
channel->Trigger(note, (double)velocity / 127.0, currentNote);
currentNote = note;
}
return;
}
};
void NoteOff(int note) {
if (config->poly == 1) {
if (channels[0]->getNote() == note) {
channels[0]->Release();
}
} else {
OrcaChannel *channel = findNote(note);
if (channel != NULL) {
channel->Release();
}
}
}
iplug::sample Tick() {
if (config->lfoLinked == 0) { // LINKED
double lfo1 = lfo[0]->Tick();
for (int i = 0 ; i < 4; i++) {
lfoValue[i] = lfo1;
}
} else {
for (int i = 0 ; i < 4; i++) {
lfoValue[i] = lfo[i]->Tick();
}
}
iplug::sample output = 0.0;
int numChannels = (config->poly == 1) ? 1:8;
for (int i=0;i<numChannels;i++) {
output += channels[i]->Tick();
}
return output;
};
private:
OrcaChannel* findChannel(State state) {
for (int i = 0 ; i<NUM_CHANNELS;i++) {
if (channels[i]->getState() == state) {
return channels[i];
}
}
return NULL;
}
OrcaChannel* findNote(int note) {
for (int i = 0 ; i<NUM_CHANNELS;i++) {
if (channels[i]->getNote() == note) {
return channels[i];
}
}
return NULL;
}
bool anyChannelGateOn() {
for (int i = 0 ; i<NUM_CHANNELS;i++) {
int state = channels[i]->getState();
if (state != idle && state != release) {
return true;
}
}
return false;
}
};