| 1 |
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ |
| 2 |
/*************************************************************************** |
| 3 |
* pluginvst.cc |
| 4 |
* |
| 5 |
* Mon Feb 8 19:24:40 CET 2016 |
| 6 |
* Copyright 2016 Bent Bisballe Nyeng |
| 7 |
* [email protected] |
| 8 |
****************************************************************************/ |
| 9 |
|
| 10 |
/* |
| 11 |
* This file is part of PluginGizmo. |
| 12 |
* |
| 13 |
* PluginGizmo is free software; you can redistribute it and/or modify |
| 14 |
* it under the terms of the GNU Lesser General Public License as published by |
| 15 |
* the Free Software Foundation; either version 3 of the License, or |
| 16 |
* (at your option) any later version. |
| 17 |
* |
| 18 |
* PluginGizmo is distributed in the hope that it will be useful, |
| 19 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 20 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 21 |
* GNU Lesser General Public License for more details. |
| 22 |
* |
| 23 |
* You should have received a copy of the GNU Lesser General Public License |
| 24 |
* along with PluginGizmo; if not, write to the Free Software |
| 25 |
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA. |
| 26 |
*/ |
| 27 |
#include "pluginvst.h" |
| 28 |
|
| 29 |
#include "midievent.h" |
| 30 |
|
| 31 |
#include <public.sdk/source/vst2.x/audioeffectx.h> |
| 32 |
#include <public.sdk/source/vst2.x/aeffeditor.h> |
| 33 |
|
| 34 |
bool PluginVST::getFreeWheel() const |
| 35 |
{ |
| 36 |
return free_wheel; |
| 37 |
} |
| 38 |
|
| 39 |
float PluginVST::getSamplerate() |
| 40 |
{ |
| 41 |
return AudioEffectX::getSampleRate(); |
| 42 |
} |
| 43 |
|
| 44 |
|
| 45 |
std::size_t PluginVST::getFramesize() |
| 46 |
{ |
| 47 |
return AudioEffectX::getBlockSize(); |
| 48 |
} |
| 49 |
|
| 50 |
bool PluginVST::getActive() |
| 51 |
{ |
| 52 |
return active; |
| 53 |
} |
| 54 |
|
| 55 |
float PluginVST::getLatency() |
| 56 |
{ |
| 57 |
return current_latency; |
| 58 |
} |
| 59 |
|
| 60 |
void PluginVST::setLatency(float latency) |
| 61 |
{ |
| 62 |
update_latency = latency; |
| 63 |
} |
| 64 |
|
| 65 |
void PluginVST::updateLatency() |
| 66 |
{ |
| 67 |
if(update_latency != current_latency) |
| 68 |
{ |
| 69 |
AudioEffectX::setInitialDelay(update_latency); |
| 70 |
AudioEffectX::ioChanged(); |
| 71 |
current_latency = update_latency; |
| 72 |
} |
| 73 |
} |
| 74 |
|
| 75 |
static uint32_t sdbm_hash(std::string input) |
| 76 |
{ |
| 77 |
unsigned long hash = 0; |
| 78 |
|
| 79 |
for(auto& cha : input) |
| 80 |
{ |
| 81 |
hash = (unsigned char)cha + (hash << 6) + (hash << 16) - hash; |
| 82 |
} |
| 83 |
|
| 84 |
return hash; |
| 85 |
} |
| 86 |
|
| 87 |
// |
| 88 |
// VST AudioEffectX implementation: |
| 89 |
// |
| 90 |
PluginVST::PluginVST(audioMasterCallback audioMaster) |
| 91 |
: AudioEffectX(audioMaster, 0, 0) |
| 92 |
{ |
| 93 |
} |
| 94 |
|
| 95 |
PluginVST::~PluginVST() |
| 96 |
{ |
| 97 |
} |
| 98 |
|
| 99 |
void PluginVST::init() |
| 100 |
{ |
| 101 |
// virtual void setUniqueID (VstInt32 iD) |
| 102 |
// Must be called to set the plug-ins unique ID! |
| 103 |
uint32_t hash = sdbm_hash(getId()); |
| 104 |
AudioEffect::setUniqueID(hash); |
| 105 |
|
| 106 |
// virtual void setNumInputs (VstInt32 inputs) |
| 107 |
// Set the number of inputs the plug-in will handle. For a plug-in which |
| 108 |
// could change its IO configuration, this number is the maximun available |
| 109 |
// inputs. |
| 110 |
AudioEffect::setNumInputs(getNumberOfAudioInputs()); |
| 111 |
|
| 112 |
// virtual void setNumOutputs (VstInt32 outputs) |
| 113 |
// Set the number of outputs the plug-in will handle. For a plug-in which |
| 114 |
// could change its IO configuration, this number is the maximun available |
| 115 |
// ouputs. |
| 116 |
AudioEffect::setNumOutputs(getNumberOfAudioOutputs()); |
| 117 |
|
| 118 |
// virtual void canProcessReplacing (bool state=true) |
| 119 |
// Tells that processReplacing() could be used. Mandatory in VST 2.4! |
| 120 |
AudioEffect::canProcessReplacing(true); |
| 121 |
|
| 122 |
// virtual void canDoubleReplacing (bool state=true) |
| 123 |
// Tells that processDoubleReplacing() is implemented. |
| 124 |
AudioEffect::canDoubleReplacing(false); |
| 125 |
|
| 126 |
// virtual void programsAreChunks (bool state=true) |
| 127 |
// Program data is handled in formatless chunks (using getChunk-setChunks). |
| 128 |
AudioEffect::programsAreChunks(true); // for generic config string support. |
| 129 |
|
| 130 |
// virtual void setInitialDelay (VstInt32 delay) |
| 131 |
// Use to report the plug-in's latency (Group Delay). |
| 132 |
AudioEffect::setInitialDelay(0); |
| 133 |
|
| 134 |
AudioEffectX::isSynth(getNumberOfMidiInputs() > 0); |
| 135 |
|
| 136 |
// We might produce output when there is no input. |
| 137 |
AudioEffectX::noTail(false); |
| 138 |
|
| 139 |
if(hasGUI()) |
| 140 |
{ |
| 141 |
editor = std::make_shared<UI>(*this); |
| 142 |
setEditor(editor.get()); |
| 143 |
} |
| 144 |
} |
| 145 |
|
| 146 |
void PluginVST::open() |
| 147 |
{ |
| 148 |
// Called when plug-in is initialized. |
| 149 |
} |
| 150 |
|
| 151 |
void PluginVST::close() |
| 152 |
{ |
| 153 |
// Called when plug-in will be released. |
| 154 |
setEditor(nullptr); |
| 155 |
editor = nullptr; |
| 156 |
} |
| 157 |
|
| 158 |
void PluginVST::suspend() |
| 159 |
{ |
| 160 |
// Called when plug-in is switched to off. |
| 161 |
active = false; |
| 162 |
onActiveChange(active); |
| 163 |
updateLatency(); |
| 164 |
} |
| 165 |
|
| 166 |
void PluginVST::resume() |
| 167 |
{ |
| 168 |
updateLatency(); |
| 169 |
|
| 170 |
// Called when plug-in is switched to on. |
| 171 |
active = true; |
| 172 |
onActiveChange(active); |
| 173 |
} |
| 174 |
|
| 175 |
bool PluginVST::getInputProperties(VstInt32 index, VstPinProperties* props) |
| 176 |
{ |
| 177 |
if(index < (VstInt32)getNumberOfAudioInputs()) |
| 178 |
{ |
| 179 |
vst_strncpy(props->label, "Channel ", 63); |
| 180 |
char temp[11] = {0}; |
| 181 |
int2string(index + 1, temp, 10); |
| 182 |
vst_strncat(props->label, temp, 63); |
| 183 |
|
| 184 |
props->flags = kVstPinIsActive; |
| 185 |
|
| 186 |
return true; |
| 187 |
} |
| 188 |
|
| 189 |
return false; |
| 190 |
} |
| 191 |
|
| 192 |
bool PluginVST::getOutputProperties(VstInt32 index, VstPinProperties* props) |
| 193 |
{ |
| 194 |
if(index < (VstInt32)getNumberOfAudioOutputs()) |
| 195 |
{ |
| 196 |
vst_strncpy(props->label, "Channel ", 63); |
| 197 |
char temp[11] = {0}; |
| 198 |
int2string(index + 1, temp, 10); |
| 199 |
vst_strncat(props->label, temp, 63); |
| 200 |
|
| 201 |
props->flags = kVstPinIsActive; |
| 202 |
|
| 203 |
return true; |
| 204 |
} |
| 205 |
|
| 206 |
return false; |
| 207 |
} |
| 208 |
|
| 209 |
void PluginVST::setSampleRate(float sampleRate) |
| 210 |
{ |
| 211 |
// Called when the sample rate changes (always in a suspend state). |
| 212 |
onSamplerateChange(sampleRate); |
| 213 |
} |
| 214 |
|
| 215 |
void PluginVST::setBlockSize(VstInt32 blockSize) |
| 216 |
{ |
| 217 |
// Called when the Maximun block size changes (always in a suspend state). |
| 218 |
// Note that the sampleFrames in Process Calls could be smaller than this |
| 219 |
// block size, but NOT bigger. |
| 220 |
onFramesizeChange(blockSize); |
| 221 |
} |
| 222 |
|
| 223 |
bool PluginVST::getEffectName(char* name) |
| 224 |
{ |
| 225 |
vst_strncpy (name, this->getEffectName().c_str(), kVstMaxEffectNameLen); |
| 226 |
return true; |
| 227 |
} |
| 228 |
|
| 229 |
bool PluginVST::getVendorString(char* text) |
| 230 |
{ |
| 231 |
vst_strncpy (text, this->getVendorString().c_str(), kVstMaxVendorStrLen); |
| 232 |
return true; |
| 233 |
} |
| 234 |
|
| 235 |
bool PluginVST::getProductString(char* text) |
| 236 |
{ |
| 237 |
vst_strncpy (text, this->getProductString().c_str(), kVstMaxProductStrLen); |
| 238 |
return true; |
| 239 |
} |
| 240 |
|
| 241 |
VstPlugCategory PluginVST::getPlugCategory() |
| 242 |
{ |
| 243 |
switch(this->getPluginCategory()) |
| 244 |
{ |
| 245 |
case PluginCategory::Unknown: |
| 246 |
return kPlugCategUnknown; |
| 247 |
case PluginCategory::Effect: |
| 248 |
return kPlugCategEffect; |
| 249 |
case PluginCategory::Synth: |
| 250 |
return kPlugCategSynth; |
| 251 |
case PluginCategory::Analysis: |
| 252 |
return kPlugCategAnalysis; |
| 253 |
case PluginCategory::Mastering: |
| 254 |
return kPlugCategMastering; |
| 255 |
case PluginCategory::Spacializer: |
| 256 |
return kPlugCategSpacializer; |
| 257 |
case PluginCategory::RoomFx: |
| 258 |
return kPlugCategRoomFx; |
| 259 |
case PluginCategory::SurroundFx: |
| 260 |
return kPlugSurroundFx; |
| 261 |
case PluginCategory::Restoration: |
| 262 |
return kPlugCategRestoration; |
| 263 |
case PluginCategory::OfflineProcess: |
| 264 |
return kPlugCategOfflineProcess; |
| 265 |
case PluginCategory::Shell: |
| 266 |
return kPlugCategShell; |
| 267 |
case PluginCategory::Generator: |
| 268 |
return kPlugCategGenerator; |
| 269 |
} |
| 270 |
|
| 271 |
return kPlugCategUnknown; |
| 272 |
} |
| 273 |
|
| 274 |
VstInt32 PluginVST::processEvents(VstEvents* events) |
| 275 |
{ |
| 276 |
// For each process cycle, processEvents() is called once before a |
| 277 |
// processReplacing() call. |
| 278 |
|
| 279 |
for(VstInt32 i = 0; i < events->numEvents; ++i) |
| 280 |
{ |
| 281 |
auto event = events->events[i]; |
| 282 |
if(event->type != kVstMidiType) |
| 283 |
{ |
| 284 |
continue; |
| 285 |
} |
| 286 |
|
| 287 |
auto midi_event = (VstMidiEvent*)event; |
| 288 |
input_events.emplace_back(midi_event->deltaFrames, |
| 289 |
midi_event->midiData, |
| 290 |
midi_event->byteSize); |
| 291 |
} |
| 292 |
|
| 293 |
return 0; |
| 294 |
} |
| 295 |
|
| 296 |
void PluginVST::processReplacing(float** inputs, float** outputs, |
| 297 |
VstInt32 sampleFrames) |
| 298 |
{ |
| 299 |
updateLatency(); |
| 300 |
|
| 301 |
// Process 32 bit (single precision) floats (always in a resume state). |
| 302 |
|
| 303 |
// 0 = realtime/normal |
| 304 |
// 1 = non-realtime/rendering |
| 305 |
// 2 = offline processing |
| 306 |
long lvl = AudioEffectX::getCurrentProcessLevel(); |
| 307 |
bool last_free_wheel = free_wheel; |
| 308 |
free_wheel = (lvl != 0); |
| 309 |
if(last_free_wheel != free_wheel) |
| 310 |
{ |
| 311 |
onFreeWheelChange(free_wheel); |
| 312 |
} |
| 313 |
|
| 314 |
std::vector<const float*> input_audio_ports; |
| 315 |
for(std::size_t i = 0; i < getNumberOfAudioInputs(); ++i) |
| 316 |
{ |
| 317 |
input_audio_ports.emplace_back(inputs[i]); |
| 318 |
} |
| 319 |
|
| 320 |
std::vector<float*> output_audio_ports; |
| 321 |
output_audio_ports.resize(getNumberOfAudioOutputs()); |
| 322 |
|
| 323 |
for(std::size_t i = 0; i < getNumberOfAudioOutputs(); ++i) |
| 324 |
{ |
| 325 |
output_audio_ports[i] = outputs[i]; |
| 326 |
} |
| 327 |
|
| 328 |
std::vector<MidiEvent> output_events; |
| 329 |
|
| 330 |
// Process events and audio |
| 331 |
process(pos, |
| 332 |
input_events, |
| 333 |
output_events, |
| 334 |
input_audio_ports, |
| 335 |
output_audio_ports, |
| 336 |
(std::size_t)sampleFrames); |
| 337 |
|
| 338 |
input_events.clear(); |
| 339 |
|
| 340 |
if(getNumberOfMidiOutputs()) |
| 341 |
{ |
| 342 |
// Translate output_events to VST midi events. |
| 343 |
std::vector<VstMidiEvent> vst_output_event_list; |
| 344 |
vst_output_event_list.resize(output_events.size()); |
| 345 |
for(std::size_t i = 0; i < output_events.size(); ++i) |
| 346 |
{ |
| 347 |
vst_output_event_list[i].deltaFrames = output_events[i].getTime(); |
| 348 |
vst_output_event_list[i].type = kVstMidiType; |
| 349 |
const char* data = output_events[i].getData(); |
| 350 |
for(std::size_t j = 0; j < output_events[i].getSize(); ++j) |
| 351 |
{ |
| 352 |
vst_output_event_list[i].midiData[j] = data[j]; |
| 353 |
} |
| 354 |
vst_output_event_list[i].byteSize = output_events[i].getSize(); |
| 355 |
} |
| 356 |
|
| 357 |
if(!vst_output_event_list.empty()) |
| 358 |
{ |
| 359 |
// Dispatch output events to host |
| 360 |
VstEvents vst_output_events; |
| 361 |
vst_output_events.numEvents = vst_output_event_list.size(); |
| 362 |
vst_output_events.events[0] = (VstEvent*)vst_output_event_list.data(); |
| 363 |
sendVstEventsToHost(&vst_output_events); |
| 364 |
} |
| 365 |
} |
| 366 |
|
| 367 |
pos += sampleFrames; |
| 368 |
} |
| 369 |
|
| 370 |
VstInt32 PluginVST::getChunk(void **data, bool isPreset) |
| 371 |
{ |
| 372 |
std::string state = onStateSave(); |
| 373 |
char* chunk = (char*)malloc(state.size() + 1); |
| 374 |
memcpy(chunk, state.data(), state.size()); |
| 375 |
*data = chunk; |
| 376 |
return state.size(); |
| 377 |
} |
| 378 |
|
| 379 |
VstInt32 PluginVST::setChunk(void *data, VstInt32 byteSize, bool isPreset) |
| 380 |
{ |
| 381 |
std::string state; |
| 382 |
state.append((const char*)data, (std::size_t)byteSize); |
| 383 |
onStateRestore(state); |
| 384 |
return 0; |
| 385 |
} |
| 386 |
|
| 387 |
VstInt32 PluginVST::canDo(char* text) |
| 388 |
{ |
| 389 |
std::string feature = text; |
| 390 |
|
| 391 |
// Midi input |
| 392 |
if((feature == "receiveVstMidiEvent") &&//PlugCanDos::canDoReceiveVstMidiEvent |
| 393 |
(getNumberOfMidiInputs() > 0)) |
| 394 |
{ |
| 395 |
return 1; |
| 396 |
} |
| 397 |
|
| 398 |
// Midi output |
| 399 |
if((feature == "sendVstMidiEvent") && // PlugCanDos::canDoSendVstMidiEvent |
| 400 |
(getNumberOfMidiOutputs() > 0)) |
| 401 |
{ |
| 402 |
return 1; |
| 403 |
} |
| 404 |
|
| 405 |
// For FreeWheel functionality. |
| 406 |
if(feature == "offline") // PlugCanDos::canDoOffline |
| 407 |
{ |
| 408 |
return 1; |
| 409 |
} |
| 410 |
|
| 411 |
// TODO: For soft-bypass state. |
| 412 |
//if(feature == "bypass") // PlugCanDos::canDoBypass) |
| 413 |
//{ |
| 414 |
// return 1; |
| 415 |
//} |
| 416 |
|
| 417 |
// TODO: For receiving metronome ticks? |
| 418 |
//if(feature == "receiveVstTimeInfo") // PlugCanDos::canDoReceiveVstTimeInfo) |
| 419 |
//{ |
| 420 |
// return 1; |
| 421 |
//} |
| 422 |
|
| 423 |
return 0; |
| 424 |
} |
| 425 |
|
| 426 |
void PluginVST::resizeWindow(std::size_t width, std::size_t height) |
| 427 |
{ |
| 428 |
if(editor) |
| 429 |
{ |
| 430 |
editor->rect.top = 0; |
| 431 |
editor->rect.left = 0; |
| 432 |
editor->rect.right = width; |
| 433 |
editor->rect.bottom = height; |
| 434 |
} |
| 435 |
} |
| 436 |
|
| 437 |
void PluginVST::closeWindow() |
| 438 |
{ |
| 439 |
} |
| 440 |
|
| 441 |
PluginVST::UI::UI(PluginVST& plugin_vst) |
| 442 |
: AEffEditor(&plugin_vst) |
| 443 |
, plugin_vst(plugin_vst) |
| 444 |
{ |
| 445 |
} |
| 446 |
|
| 447 |
bool PluginVST::UI::open(void* ptr) |
| 448 |
{ |
| 449 |
plugin_vst.createWindow(ptr); |
| 450 |
|
| 451 |
AEffEditor::open(ptr); |
| 452 |
|
| 453 |
is_open = true; |
| 454 |
return true; |
| 455 |
} |
| 456 |
|
| 457 |
void PluginVST::UI::close() |
| 458 |
{ |
| 459 |
is_open = false; |
| 460 |
plugin_vst.onDestroyWindow(); |
| 461 |
|
| 462 |
AEffEditor::close(); |
| 463 |
} |
| 464 |
|
| 465 |
bool PluginVST::UI::isOpen() |
| 466 |
{ |
| 467 |
return is_open; |
| 468 |
} |
| 469 |
|
| 470 |
void PluginVST::UI::idle() |
| 471 |
{ |
| 472 |
if(is_open) |
| 473 |
{ |
| 474 |
plugin_vst.onIdle(); |
| 475 |
} |
| 476 |
|
| 477 |
AEffEditor::idle(); |
| 478 |
} |
| 479 |
|
| 480 |
bool PluginVST::UI::getRect(ERect** rect) |
| 481 |
{ |
| 482 |
*rect = &this->rect; |
| 483 |
return true; |
| 484 |
} |