-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathMidiChannel.cpp
165 lines (142 loc) · 5.55 KB
/
MidiChannel.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
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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
// Copyright 2019 Josh Bailey ([email protected])
// Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
// The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
#include "MidiChannel.h"
#define DEFAULT_DETUNE 64
MidiChannel::MidiChannel() {
bzero(&_noteMap, sizeof(_noteMap));
Reset();
}
void MidiChannel::ResetCC() {
attack = 0;
decay = 0;
sustain = maxMidiVal;
release = 0;
tremoloRange = 0;
coarseModulation = 0;
lfoRestart = true;
detune = DEFAULT_DETUNE;
detune2 = DEFAULT_DETUNE;
detuneAbs = DEFAULT_DETUNE;
detune2Abs = DEFAULT_DETUNE;
_maxPitch = 0;
volume = 127;
SetBend(0, 0);
SetPitchBendRange(midiPitchBendRange);
}
void MidiChannel::Reset() {
_midiNotes.clear();
while (!_idleNotes.empty()) {
_idleNotes.pop();
}
ResetCC();
}
void MidiChannel::IdleAllNotes() {
for (MidiNoteDeque::const_iterator i = _midiNotes.begin(); i != _midiNotes.end(); ++i) {
MidiNote *midiNote = *i;
midiNote->envelope.Idle();
}
HandleControl();
}
void MidiChannel::SetMaxPitch(uint8_t maxPitch) {
if (maxPitch != _maxPitch) {
_maxPitch = maxPitch;
_pitchBender.SetMaxPitch(maxPitch);
}
}
inline MidiNote *MidiChannel::LookupNote(uint8_t note) {
if (note > maxMidiPitch) {
return NULL;
}
return _noteMap[note];
}
void MidiChannel::RetuneNotes(OscillatorController *oc) {
int periodOffset = int(detuneAbs) - int(DEFAULT_DETUNE);
for (MidiNoteDeque::const_iterator i = _midiNotes.begin(); i != _midiNotes.end(); ++i) {
MidiNote *midiNote = *i;
cr_fp_t hz = BendHz(midiNote, midiTuneCents[detune]);
for (OscillatorDeque::const_iterator o = midiNote->oscillators.begin(); o != midiNote->oscillators.end(); ++o) {
Oscillator *oscillator = *o;
if (o == midiNote->oscillators.begin()) {
// TODO: add another oscillator if detune2 changed from default after note scheduled.
oc->SetFreqLazy(oscillator, hz, midiNote->velocityScale, periodOffset);
} else {
int periodOffset2 = int(detune2Abs) - int(DEFAULT_DETUNE);
cr_fp_t hz2 = BendHz(midiNote, midiTuneCents[detune2]);
oc->SetFreqLazy(oscillator, hz2, midiNote->velocityScale, periodOffset2);
}
}
}
}
void MidiChannel::SetBend(int newBend, OscillatorController *oc) {
if (!_pitchBender.SetBend(newBend)) {
return;
}
RetuneNotes(oc);
}
cr_fp_t MidiChannel::BendHz(MidiNote *midiNote, cr_fp_t detuneCoeff) {
cr_fp_t hz = pitchToHz[midiNote->pitch] * detuneCoeff;
return _pitchBender.BendHz(midiNote, hz);
}
void MidiChannel::_AddOscillatorToNote(cr_fp_t hz, MidiNote *midiNote, OscillatorController *oc, int periodOffset) {
Oscillator *oscillator;
oscillator = oc->GetFreeOscillator();
oc->SetFreq(oscillator, hz, midiNote->velocityScale, periodOffset);
midiNote->oscillators.push_back(oscillator);
}
void MidiChannel::NoteOn(uint8_t note, uint8_t velocity, MidiNote *midiNote, OscillatorController *oc) {
midiNote->pitch = note;
midiNote->envelope.Reset(attack, decay, sustain, release);
midiNote->velocityScale = midiValMap[velocity];
_AddOscillatorToNote(BendHz(midiNote, midiTuneCents[detune]), midiNote, oc, int(detuneAbs) - int(DEFAULT_DETUNE));
if (detune2 != DEFAULT_DETUNE || detune2Abs != DEFAULT_DETUNE) {
_AddOscillatorToNote(BendHz(midiNote, midiTuneCents[detune2]), midiNote, oc, int(detune2Abs) - int(DEFAULT_DETUNE));
}
for (OscillatorDeque::const_iterator o = midiNote->oscillators.begin(); o != midiNote->oscillators.end(); ++o) {
Oscillator *oscillator = *o;
oscillator->audible = true;
oscillator->envelope = &(midiNote->envelope);
}
_midiNotes.push_back(midiNote);
_noteMap[note] = midiNote;
}
void MidiChannel::ReleaseNote(uint8_t note) {
MidiNote *midiNote = LookupNote(note);
if (midiNote) {
midiNote->envelope.Release();
}
}
bool MidiChannel::ResetNote(uint8_t note) {
MidiNote *midiNote = LookupNote(note);
if (midiNote) {
midiNote->pitch = note;
midiNote->envelope.Reset(attack, decay, sustain, release);
return true;
}
return false;
}
MidiNote *MidiChannel::NotesExpire(OscillatorController *oc) {
while (!_idleNotes.empty()) {
MidiNote *midiNote = _idleNotes.top();
_idleNotes.pop();
_noteMap[midiNote->pitch] = NULL;
midiNote->ResetOscillators(oc);
return midiNote;
}
return NULL;
}
void MidiChannel::HandleControl() {
for (MidiNoteDeque::iterator i = _midiNotes.begin(); i != _midiNotes.end(); ++i) {
MidiNote *midiNote = *i;
midiNote->HandleControl();
if (midiNote->envelope.IsIdle()) {
_idleNotes.push(midiNote);
_midiNotes.erase(i--);
continue;
}
}
}
void MidiChannel::SetPitchBendRange(uint8_t semitones) {
_pitchBender.SetPitchBendRange(semitones);
}