-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathVocoderSynth.cpp
More file actions
172 lines (143 loc) · 4.24 KB
/
VocoderSynth.cpp
File metadata and controls
172 lines (143 loc) · 4.24 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
/*
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 "VocoderSynth.h"
VocoderSynth::VocoderSynth(const double sample_rate, const LV2_Feature *const *features)
:vocoder(sample_rate),
synth(sample_rate, features, *this),
voice(sample_rate, *this)
{
}
void VocoderSynth::UpdateControls(void)
{
raw_audio_enabled = *controls[CONTROL_RAW_ENABLE]>0.0f;
voice_enabled = *controls[CONTROL_VOICE_ENABLE]>0.0f;
voice_impulse_gain = powf(10.0f, *controls[CONTROL_VOICE_IMPULSE_GAIN]/20.0f);
voice_noise_gain = powf(10.0f, *controls[CONTROL_VOICE_NOISE_GAIN]/20.0f);
voice_pitch_factor = powf(2.0f, *controls[CONTROL_VOICE_PITCH_OFFSET]/12.0f);
synth_enabled = *controls[CONTROL_SYNTH_ENABLE]>0.0f;
synth_gain = powf(10.0f, *controls[CONTROL_SYNTH_GAIN]/20.0f);
synth_pitch_bend = *controls[CONTROL_SYNTH_BEND_RANGE];
}
void VocoderSynth::connectPort(const uint32_t port, void* data_location)
{
switch(port){
case PORT_MIDI_IN:
midi_in = (const LV2_Atom_Sequence*) data_location;
break;
case PORT_CTL_AUDIO:
ctl_audio = (float*)data_location;
break;
case PORT_RAW_AUDIO:
raw_audio = (float*)data_location;
break;
case PORT_AUDIO_OUT:
audio_out = (float*)data_location;
break;
default:
if(port < (int)PORT_CONTROL + (int)CONTROL_NCONTROLS){
controls[port - PORT_CONTROL] = (float*)data_location;
}
}
}
void VocoderSynth::run(const uint32_t sample_count)
{
UpdateControls();
if(synth_enabled){
synth.EvaluateStart(midi_in);
}
float *ctl_p = ctl_audio;
float *raw_p = raw_audio;
float *out_p = audio_out;
for(uint32_t s=0;s<sample_count;s++){
float ctl_audio = dcRemove.Evaluate(*ctl_p);
float vocoder_in = 0.0f;
if(raw_audio_enabled){
vocoder_in += *raw_p;
}
if(voice_enabled){
vocoder_in += voice.Evaluate(ctl_audio);
}
if(synth_enabled){
vocoder_in += synth.Evaluate();
}
*out_p = vocoder.Evaluate(ctl_audio, vocoder_in);
ctl_p++;
raw_p++;
out_p++;
}
}
/*
* LV2 Interface functions
*/
static LV2_Handle instantiate (const struct LV2_Descriptor *descriptor, double sample_rate, const char *bundle_path, const LV2_Feature *const *features)
{
VocoderSynth *m;
try {
m = new VocoderSynth (sample_rate, features);
}
catch(...){
m = nullptr;
}
return (LV2_Handle)m;
}
static void connect_port (LV2_Handle instance, uint32_t port, void *data_location)
{
VocoderSynth* m = (VocoderSynth*) instance;
if (m) m->connectPort (port, data_location);
}
static void activate (LV2_Handle instance)
{
/* not needed here */
}
static void run (LV2_Handle instance, uint32_t sample_count)
{
VocoderSynth* m = (VocoderSynth*) instance;
if (m) m->run (sample_count);
}
static void deactivate (LV2_Handle instance)
{
/* not needed here */
}
static void cleanup (LV2_Handle instance)
{
VocoderSynth* m = (VocoderSynth*) instance;
if (m) delete m;
}
static const void* extension_data (const char *uri)
{
return NULL;
}
/* descriptor */
static LV2_Descriptor const descriptor =
{
"https://twkrause.ca/plugins/VocoderSynth",
instantiate,
connect_port,
activate,
run,
deactivate,
cleanup,
extension_data
};
/* interface */
LV2_SYMBOL_EXPORT const LV2_Descriptor* lv2_descriptor (uint32_t index)
{
if (index == 0) return &descriptor;
else return NULL;
}