-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathImpulseSynth.cpp
More file actions
209 lines (180 loc) · 5.04 KB
/
ImpulseSynth.cpp
File metadata and controls
209 lines (180 loc) · 5.04 KB
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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
/*
VocoderSynth is a DSP plugin.
Copyright 2024 Tim Krause
This file is part of VocoderSynth
VocoderSynth is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published
by the Free Software Foundation, either version 3 of the License,
or (at your option) any later version.
VocoderSynth is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
General Public License for more details.
You should have received a copy of the GNU General Public License
along with VocoderSynth. If not, see
<https://www.gnu.org/licenses/>.
Contact: tim.krause@twkrause.ca
*/
#include <cmath>
#include <iostream>
#include "ImpulseSynth.h"
#include "VocoderSynth.h"
Voice::Voice(double sample_rate, ImpulseSynth &synth):
sample_rate(sample_rate),
synth(synth)
{
}
void Voice::NoteOn(uint8_t note, uint8_t vel)
{
Voice::note = note;
Voice::vel = vel;
freq0 = 440.0f*powf(2.0f,((float)note-69.0f)/12.0f);
}
void Voice::NoteOff(uint8_t vel)
{
synth.DeactivateVoice(this);
}
float Voice::Evaluate(void)
{
float freq = freq0 * powf(2.0f,synth.pitch_bend*synth.plugin.synth_pitch_bend/12.0f);
gen.Tperiod = sample_rate / freq;
return gen.Evaluate()*synth.plugin.synth_gain;
}
ImpulseSynth::ImpulseSynth(
double sample_rate,
const LV2_Feature *const *features,
VocoderSynth &plugin)
:
sample_rate(sample_rate),
plugin(plugin),
pitch_bend(0.0f)
{
LV2_URID_Map* map;
const char* missing = lv2_features_query
(
features,
LV2_URID__map, &map, true,
NULL
);
if (missing){
//std::cout << "map feature is missing" << std::endl;
throw;
}
for(int i=0;i<N_VOICES;i++){
alloc_voices[i].reset(new Voice(sample_rate, *this));
avail_voices.push_back(alloc_voices[i].get());
}
for(int i=0;i<128;i++){
keys[i] = {nullptr, false};
}
MidiEvent_URID = map->map(map->handle, LV2_MIDI__MidiEvent);
}
Voice* ImpulseSynth::ActivateVoice(void)
{
Voice *voice;
if(avail_voices.empty()){
// take the oldest form the active list
voice = active_voices.back();
DeactivateVoice(voice);
}
// take from the available list
voice = avail_voices.front();
avail_voices.pop_front();
active_voices.push_front(voice);
voice->active_pos = active_voices.begin();
return voice;
}
void ImpulseSynth::DeactivateVoice(Voice* voice)
{
// remove from the active list
active_voices.erase(voice->active_pos);
// insert into the avail list
avail_voices.push_front(voice);
// remove from the key list
keys[voice->note].voice = nullptr;
}
void ImpulseSynth::NoteOn(uint8_t note, uint8_t vel)
{
Voice* voice;
if(keys[note].voice){
voice = keys[note].voice;
}else{
voice = ActivateVoice();
}
voice->NoteOn(note,vel);
keys[note].voice = voice;
keys[note].pressed = true;
}
void ImpulseSynth::NoteOff(uint8_t note, uint8_t vel)
{
if(keys[note].pressed){
if(keys[note].voice){
keys[note].voice->NoteOff(vel);
}
}
keys[note].pressed = false;
}
void ImpulseSynth::AllNotesOff(void)
{
for(std::list<Voice*>::iterator it=active_voices.begin();it!=active_voices.end();)
{
Voice *voice = *it;
it++;
DeactivateVoice(voice);
}
}
void ImpulseSynth::Bender(uint8_t lsb, uint8_t msb)
{
short x = (lsb&0x7F)|((short)(msb&0x7F)<<7);
pitch_bend = ((float)x-8192.0f)/8192.0f;
}
void ImpulseSynth::EvaluateStart(const LV2_Atom_Sequence *sequence)
{
ImpulseSynth::sequence = sequence;
event = lv2_atom_sequence_begin(&sequence->body);
if(lv2_atom_sequence_is_end(&sequence->body,sequence->atom.size,event))
event = nullptr;
frame = 0;
}
void ImpulseSynth::NextEvent(void)
{
if(!event)
return;
event = lv2_atom_sequence_next(event);
if(lv2_atom_sequence_is_end(&sequence->body,sequence->atom.size,event))
event = nullptr;
}
float ImpulseSynth::Evaluate(void)
{
while(event && event->time.frames==frame)
{
if(event->body.type == MidiEvent_URID){
const uint8_t* const msg = (const uint8_t*)(event + 1);
const uint8_t type = lv2_midi_message_type(msg);
switch(type){
case LV2_MIDI_MSG_NOTE_ON:
NoteOn(msg[1], msg[2]);
break;
case LV2_MIDI_MSG_NOTE_OFF:
NoteOff(msg[1], msg[2]);
break;
case LV2_MIDI_MSG_CONTROLLER:
if(msg[1]>=LV2_MIDI_CTL_ALL_SOUNDS_OFF)
AllNotesOff();
break;
case LV2_MIDI_MSG_BENDER:
Bender(msg[1], msg[2]);
break;
default:
break;
}
}
NextEvent();
}
frame++;
float y=0.0f;
for(auto &voice:active_voices){
y += voice->Evaluate();
}
return y;
}