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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
|
//
// PluginChain.c - MrsWatson
// Created by Nik Reiman on 1/3/12.
// Copyright (c) 2012 Teragon Audio. All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are met:
//
// * Redistributions of source code must retain the above copyright notice,
// this list of conditions and the following disclaimer.
// * Redistributions in binary form must reproduce the above copyright notice,
// this list of conditions and the following disclaimer in the documentation
// and/or other materials provided with the distribution.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
// ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
// LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
// SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
// CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
// POSSIBILITY OF SUCH DAMAGE.
//
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "logging/EventLogger.h"
#include "plugin/PluginChain.h"
#include "audio/AudioSettings.h"
PluginChain pluginChainInstance = NULL;
PluginChain getPluginChain(void)
{
return pluginChainInstance;
}
void initPluginChain(void)
{
pluginChainInstance = (PluginChain)malloc(sizeof(PluginChainMembers));
pluginChainInstance->numPlugins = 0;
pluginChainInstance->plugins = (Plugin *)malloc(sizeof(Plugin) * MAX_PLUGINS);
pluginChainInstance->presets = (PluginPreset *)malloc(sizeof(PluginPreset) * MAX_PLUGINS);
pluginChainInstance->audioTimers = (TaskTimer *)malloc(sizeof(TaskTimer) * MAX_PLUGINS);
pluginChainInstance->midiTimers = (TaskTimer *)malloc(sizeof(TaskTimer) * MAX_PLUGINS);
pluginChainInstance->_realtime = false;
pluginChainInstance->_realtimeTimer = NULL;
}
boolByte pluginChainAppend(PluginChain self, Plugin plugin, PluginPreset preset)
{
if (plugin == NULL) {
return false;
} else if (self->numPlugins + 1 >= MAX_PLUGINS) {
logError("Could not add plugin '%s', maximum number reached", plugin->pluginName->data);
return false;
} else if (!openPlugin(plugin)) {
return false;
} else {
self->plugins[self->numPlugins] = plugin;
self->presets[self->numPlugins] = preset;
self->audioTimers[self->numPlugins] = newTaskTimer(plugin->pluginName, "Audio Processing");
self->midiTimers[self->numPlugins] = newTaskTimer(plugin->pluginName, "MIDI Processing");
self->numPlugins++;
return true;
}
}
boolByte pluginChainAddFromArgumentString(PluginChain pluginChain, const CharString argumentString, const CharString userSearchPath)
{
// Expect a semicolon-separated string of plugins with comma separators for preset names
// Example: plugin1,preset1name;plugin2,preset2name
char *substringStart;
char *pluginSeparator;
char *endChar;
CharString pluginNameBuffer = NULL;
CharString presetNameBuffer = NULL;
char *presetSeparator;
PluginPreset preset;
Plugin plugin;
size_t substringLength;
if (charStringIsEmpty(argumentString)) {
logWarn("Plugin chain string is empty");
return false;
}
substringStart = argumentString->data;
pluginSeparator = strchr(argumentString->data, CHAIN_STRING_PLUGIN_SEPARATOR);
endChar = argumentString->data + strlen(argumentString->data);
do {
if (pluginSeparator == NULL) {
substringLength = strlen(argumentString->data);
} else {
substringLength = pluginSeparator - substringStart;
}
pluginNameBuffer = newCharString();
strncpy(pluginNameBuffer->data, substringStart, substringLength);
// Look for the separator for presets to load into these plugins
presetNameBuffer = newCharString();
presetSeparator = strchr(pluginNameBuffer->data, CHAIN_STRING_PROGRAM_SEPARATOR);
if (presetSeparator != NULL) {
// Null-terminate this string to force it to end, then extract preset name from next char
*presetSeparator = '\0';
strncpy(presetNameBuffer->data, presetSeparator + 1, strlen(presetSeparator + 1));
}
// Find preset for this plugin (if given)
preset = NULL;
if (strlen(presetNameBuffer->data) > 0) {
logInfo("Opening preset '%s' for plugin", presetNameBuffer->data);
preset = pluginPresetFactory(presetNameBuffer);
}
// Guess the plugin type from the file extension, search root, etc.
plugin = pluginFactory(pluginNameBuffer, userSearchPath);
if (plugin != NULL) {
if (!pluginChainAppend(pluginChain, plugin, preset)) {
logError("Plugin '%s' could not be added to the chain", pluginNameBuffer->data);
free(pluginNameBuffer);
free(presetNameBuffer);
return false;
}
}
if (pluginSeparator == NULL) {
break;
} else {
substringStart = pluginSeparator + 1;
pluginSeparator = strchr(pluginSeparator + 1, CHAIN_STRING_PLUGIN_SEPARATOR);
}
} while (substringStart < endChar);
freeCharString(pluginNameBuffer);
freeCharString(presetNameBuffer);
return true;
}
static boolByte _loadPresetForPlugin(Plugin plugin, PluginPreset preset)
{
if (pluginPresetIsCompatibleWith(preset, plugin)) {
if (!preset->openPreset(preset)) {
logError("Could not open preset '%s'", preset->presetName->data);
return false;
}
if (!preset->loadPreset(preset, plugin)) {
logError("Could not load preset '%s' in plugin '%s'", preset->presetName->data, plugin->pluginName->data);
return false;
}
logInfo("Loaded preset '%s' in plugin '%s'", preset->presetName->data, plugin->pluginName->data);
return true;
} else {
logError("Preset '%s' is not a compatible format for plugin", preset->presetName->data);
return false;
}
}
ReturnCodes pluginChainInitialize(PluginChain pluginChain)
{
Plugin plugin;
PluginPreset preset;
unsigned int i;
for (i = 0; i < pluginChain->numPlugins; i++) {
plugin = pluginChain->plugins[i];
if (!openPlugin(plugin)) {
return RETURN_CODE_PLUGIN_ERROR;
} else {
if (i > 0 && plugin->pluginType == PLUGIN_TYPE_INSTRUMENT) {
logError("Instrument plugin '%s' must be first in the chain", plugin->pluginName->data);
return RETURN_CODE_INVALID_PLUGIN_CHAIN;
} else if (plugin->pluginType == PLUGIN_TYPE_UNKNOWN) {
logError("Plugin '%s' has unknown type; It was probably not loaded correctly", plugin->pluginName->data);
return RETURN_CODE_PLUGIN_ERROR;
} else if (plugin->pluginType == PLUGIN_TYPE_UNSUPPORTED) {
logError("Plugin '%s' is of unsupported type", plugin->pluginName->data);
return RETURN_CODE_PLUGIN_ERROR;
}
preset = pluginChain->presets[i];
if (preset != NULL) {
if (!_loadPresetForPlugin(plugin, preset)) {
return RETURN_CODE_INVALID_ARGUMENT;
}
}
}
}
return RETURN_CODE_SUCCESS;
}
void pluginChainInspect(PluginChain pluginChain)
{
Plugin plugin;
unsigned int i;
for (i = 0; i < pluginChain->numPlugins; i++) {
plugin = pluginChain->plugins[i];
plugin->displayInfo(plugin);
}
}
void pluginChainPrepareForProcessing(PluginChain self)
{
Plugin plugin;
unsigned int i;
for (i = 0; i < self->numPlugins; i++) {
plugin = self->plugins[i];
plugin->prepareForProcessing(plugin);
}
}
int pluginChainGetMaximumTailTimeInMs(PluginChain pluginChain)
{
Plugin plugin;
int tailTime;
int maxTailTime = 0;
unsigned int i;
for (i = 0; i < pluginChain->numPlugins; i++) {
plugin = pluginChain->plugins[i];
tailTime = plugin->getSetting(plugin, PLUGIN_SETTING_TAIL_TIME_IN_MS);
if (tailTime > maxTailTime) {
maxTailTime = tailTime;
}
}
return maxTailTime;
}
unsigned long pluginChainGetProcessingDelay(PluginChain self)
{
unsigned long processingDelay = 0;
unsigned int i;
for (i = 0; i < self->numPlugins; i++) {
Plugin plugin = self->plugins[i];
processingDelay += plugin->getSetting(plugin, PLUGIN_INITIAL_DELAY);
}
return processingDelay;
}
typedef struct {
Plugin plugin;
boolByte success;
} _PluginChainSetParameterPassData;
void _pluginChainSetParameter(void *item, void *userData)
{
// Expect that the linked list contains CharStrings, single that is what is
// being given from the command line.
char *parameterValue = (char *)item;
_PluginChainSetParameterPassData *passData = (_PluginChainSetParameterPassData *)userData;
Plugin plugin = passData->plugin;
char *comma = NULL;
int index;
float value;
// If a previous attempt to set a parameter failed, then return right away
// since this method will return false anyways.
if (!passData->success) {
return;
}
// TODO: Need a "pair" type, this string parsing is done several times in the codebase
comma = strchr(parameterValue, ',');
if (comma == NULL) {
logError("Malformed parameter string, see --help parameter for usage");
return;
}
*comma = '\0';
index = (int)strtod(parameterValue, NULL);
value = (float)strtod(comma + 1, NULL);
logDebug("Set parameter %d to %f", index, value);
passData->success = plugin->setParameter(plugin, (unsigned int)index, value);
}
boolByte pluginChainSetParameters(PluginChain self, const LinkedList parameters)
{
_PluginChainSetParameterPassData passData;
passData.plugin = self->plugins[0];
passData.success = true;
logDebug("Setting parameters on head plugin in chain");
linkedListForeach(parameters, _pluginChainSetParameter, &passData);
return passData.success;
}
void pluginChainSetRealtime(PluginChain self, boolByte realtime)
{
self->_realtime = realtime;
if (realtime) {
self->_realtimeTimer = newTaskTimerWithCString("PluginChain", "Realtime");
} else if (self->_realtimeTimer) {
freeTaskTimer(self->_realtimeTimer);
}
}
void pluginChainProcessAudio(PluginChain pluginChain, SampleBuffer inBuffer, SampleBuffer outBuffer)
{
Plugin plugin;
unsigned int i;
double processingTimeInMs;
double totalProcessingTimeInMs;
const double maxProcessingTimeInMs = inBuffer->blocksize * 1000.0 / getSampleRate();
if (pluginChain->_realtime) {
taskTimerStart(pluginChain->_realtimeTimer);
}
SampleBuffer formerOutputBuffer = inBuffer;
SampleBuffer nextInputBuffer = NULL;
for (i = 0; i < pluginChain->numPlugins; i++) {
plugin = pluginChain->plugins[i];
logDebug("Processing audio with plugin '%s'", plugin->pluginName->data);
nextInputBuffer = plugin->inputBuffer;
nextInputBuffer->blocksize = formerOutputBuffer->blocksize;
sampleBufferCopyAndMapChannels(nextInputBuffer, formerOutputBuffer);
plugin->outputBuffer->blocksize = plugin->inputBuffer->blocksize;
taskTimerStart(pluginChain->audioTimers[i]);
plugin->processAudio(plugin, plugin->inputBuffer, plugin->outputBuffer);
processingTimeInMs = taskTimerStop(pluginChain->audioTimers[i]);
if (processingTimeInMs > maxProcessingTimeInMs && pluginChain->_realtime) {
logWarn("Possible dropout! Plugin '%s' spent %dms processing time (%dms max)",
plugin->pluginName->data, (int)processingTimeInMs, (int)maxProcessingTimeInMs);
} else {
logDebug("Plugin '%s' spent %dms processing (%d%% effective CPU usage)",
plugin->pluginName->data, (int)processingTimeInMs,
(int)(processingTimeInMs / maxProcessingTimeInMs));
}
formerOutputBuffer = plugin->outputBuffer;
}
nextInputBuffer = outBuffer;
nextInputBuffer->blocksize = formerOutputBuffer->blocksize;
sampleBufferCopyAndMapChannels(nextInputBuffer, formerOutputBuffer);
if (pluginChain->_realtime) {
totalProcessingTimeInMs = taskTimerStop(pluginChain->_realtimeTimer);
if (totalProcessingTimeInMs < maxProcessingTimeInMs) {
taskTimerSleep(maxProcessingTimeInMs - totalProcessingTimeInMs);
}
}
}
void pluginChainProcessMidi(PluginChain pluginChain, LinkedList midiEvents)
{
Plugin plugin;
if (midiEvents->item != NULL) {
logDebug("Processing plugin chain MIDI events");
// Right now, we only process MIDI in the first plugin in the chain
// TODO: Is this really the correct behavior? How do other sequencers do it?
plugin = pluginChain->plugins[0];
taskTimerStart(pluginChain->midiTimers[0]);
plugin->processMidiEvents(plugin, midiEvents);
taskTimerStop(pluginChain->midiTimers[0]);
}
}
void pluginChainShutdown(PluginChain pluginChain)
{
Plugin plugin;
unsigned int i;
for (i = 0; i < pluginChain->numPlugins; i++) {
plugin = pluginChain->plugins[i];
logInfo("Closing plugin '%s'", plugin->pluginName->data);
closePlugin(plugin);
}
}
void freePluginChain(PluginChain pluginChain)
{
unsigned int i;
for (i = 0; i < pluginChain->numPlugins; i++) {
freePluginPreset(pluginChain->presets[i]);
freePlugin(pluginChain->plugins[i]);
freeTaskTimer(pluginChain->audioTimers[i]);
freeTaskTimer(pluginChain->midiTimers[i]);
}
free(pluginChain->presets);
free(pluginChain->plugins);
free(pluginChain->audioTimers);
free(pluginChain->midiTimers);
if (pluginChain->_realtime) {
freeTaskTimer(pluginChain->_realtimeTimer);
}
free(pluginChain);
}
|