audioeffect.cpp (5918B)
1 /* 2 * Copyright 2024 Michael Fabian 'Xaymar' Dirks <info@xaymar.com> 3 * Copyright 2024 Steinberg Media Technologies GmbH 4 * 5 * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: 6 * 7 * 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. 8 * 9 * 2. 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. 10 * 11 * 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission. 12 * 13 * 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. 14 */ 15 16 #include "audioeffect.hpp" 17 #include <cstring> 18 #include "aeffect.h" 19 20 #define FOURCC(a, b, c, d) ((a << 24) | (b << 16) | (c << 8) | d) 21 22 AudioEffect::AudioEffect(audioMasterCallback p_audioMaster, VstInt32 __unk00, VstInt32 __unk01) 23 { 24 memset(&cEffect, 0, sizeof(AEffect)); 25 cEffect.magic = FOURCC('V', 's', 't', 'P'); 26 cEffect.effect_data = this; 27 #if (!defined VST_VERSION_SUPPORT) 28 cEffect.version = kVstVersion_2400; 29 #else 30 cEffect.version = VST_VERSION_SUPPORT; 31 #endif 32 cEffect.control = [](AEffect* self, VstInt32 opcode, VstInt32 param1, VstIntPtr param2, void* ptr, float value) { return reinterpret_cast<AudioEffect*>(self->effect_data)->control(opcode, param1, param2, ptr, value); }; 33 cEffect.process = [](AEffect* self, float** inputs, float** outputs, VstInt32 sampleFrames) { return reinterpret_cast<AudioEffect*>(self->effect_data)->process(inputs, outputs, sampleFrames); }; 34 cEffect.setParameter = [](AEffect* self, VstInt32 index, float value) { reinterpret_cast<AudioEffect*>(self->effect_data)->setParameter(index, value); }; 35 cEffect.getParameter = [](AEffect* self, VstInt32 index) { return reinterpret_cast<AudioEffect*>(self->effect_data)->getParameter(index); }; 36 cEffect.processReplacing = [](AEffect* self, float** inputs, float** outputs, VstInt32 sampleFrames) { return reinterpret_cast<AudioEffect*>(self->effect_data)->processReplacing(inputs, outputs, sampleFrames); }; 37 38 audioMaster = p_audioMaster; 39 numPrograms = cEffect.numPrograms; 40 curProgram = 0; 41 editor = nullptr; 42 } 43 44 AudioEffect::~AudioEffect() {} 45 46 VstIntPtr AudioEffect::control(VstInt32 opcode, VstInt32 param1, VstIntPtr param2, void* ptr, float value) 47 { 48 switch (opcode) { 49 case audioEffectOpen: 50 open(); 51 break; 52 case audioEffectClose: 53 close(); 54 break; 55 case audioEffectSetProgram: 56 setProgram(param1); 57 break; 58 case audioEffectGetProgram: 59 return getProgram(); 60 case audioEffectSetProgramName: 61 setProgramName((char*)ptr); 62 break; 63 case audioEffectGetProgramName: 64 getProgramName((char*)ptr); 65 break; 66 case audioEffectGetParameterLabel: 67 getParameterLabel(param1, (char*)ptr); 68 break; 69 case audioEffectGetParameterDisplay: 70 getParameterDisplay(param1, (char*)ptr); 71 break; 72 case audioEffectGetParameterName: 73 getParameterName(param1, (char*)ptr); 74 break; 75 case audioEffectSetSampleRate: 76 setSampleRate(value); 77 break; 78 case audioEffectSetBlockSize: 79 setBlockSize(static_cast<VstInt32>(value)); 80 break; 81 case audioEffectSuspendResume: 82 if (!param2) { 83 suspend(); 84 } else { 85 resume(); 86 } 87 break; 88 case audioEffectEditorGetSize: 89 if (editor) { 90 return editor->getRect((ERect**)ptr) ? 1 : 0; 91 } 92 break; 93 case audioEffectEditorOpen: 94 if (editor) { 95 return editor->open(ptr); 96 } 97 break; 98 case audioEffectEditorClose: 99 if (editor) { 100 editor->close(); 101 } 102 break; 103 case audioEffectEditorDoNothing: // Occasionally appears. Does nothing? Only called when editor is open. 104 if (editor) { 105 editor->do_nothing(); 106 } 107 break; 108 case audioEffectGetChunk: 109 return getChunk((void**)ptr, !!param1); 110 case audioEffectSetChunk: 111 return setChunk(ptr, static_cast<VstInt32>(value), !!param1); 112 default: 113 break; 114 } 115 return 0; 116 } 117 118 AEffect* AudioEffect::getAeffect() 119 { 120 return &cEffect; 121 } 122 123 void AudioEffect::setEditor(AEffEditor* p_editor) 124 { 125 editor = p_editor; 126 } 127 128 void AudioEffect::setUniqueID(VstInt32 uniqueID) 129 { 130 cEffect.uniqueID = uniqueID; 131 } 132 133 void AudioEffect::setNumInputs(VstInt32 numInputs) 134 { 135 cEffect.numInputs = numInputs; 136 } 137 138 void AudioEffect::setNumOutputs(VstInt32 numOutputs) 139 { 140 cEffect.numOutputs = numOutputs; 141 } 142 143 void AudioEffect::setInitialDelay(VstInt32 delay) 144 { 145 cEffect.delay = delay; 146 } 147 148 void AudioEffect::canProcessReplacing(bool value) 149 { 150 if (value) 151 cEffect.flags |= AEffectFlagCanReplacing; 152 else 153 cEffect.flags &= ~AEffectFlagCanReplacing; 154 } 155 156 void AudioEffect::isSynth(bool value) 157 { 158 if (value) 159 cEffect.flags |= AEffectFlagIsSynthesizer; 160 else 161 cEffect.flags &= ~AEffectFlagIsSynthesizer; 162 } 163 164 void AudioEffect::programsAreChunks(bool value) 165 { 166 if (value) 167 cEffect.flags |= AEffectFlagProgramsAreChunks; 168 else 169 cEffect.flags &= ~AEffectFlagProgramsAreChunks; 170 } 171 172 void AudioEffect::setParameterAutomated(VstInt32 index, float value) 173 { 174 setParameter(index, value); 175 if (audioMaster) 176 audioMaster(getAeffect(), audioMasterAutomate, index, 0, 0, value); 177 }