|
| 1 | +/* |
| 2 | + * DISTRHO Plugin Framework (DPF) |
| 3 | + * Copyright (C) 2012-2021 Filipe Coelho <[email protected]> |
| 4 | + * |
| 5 | + * Permission to use, copy, modify, and/or distribute this software for any purpose with |
| 6 | + * or without fee is hereby granted, provided that the above copyright notice and this |
| 7 | + * permission notice appear in all copies. |
| 8 | + * |
| 9 | + * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD |
| 10 | + * TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN |
| 11 | + * NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL |
| 12 | + * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER |
| 13 | + * IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN |
| 14 | + * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. |
| 15 | + */ |
| 16 | + |
| 17 | +#include "DistrhoPlugin.hpp" |
| 18 | + |
| 19 | +#include <cmath> |
| 20 | +#include <cstring> |
| 21 | + |
| 22 | +START_NAMESPACE_DISTRHO |
| 23 | + |
| 24 | +// ----------------------------------------------------------------------------------------------------------- |
| 25 | + |
| 26 | +/** |
| 27 | + Plugin that demonstrates sending notes from the editor in DPF. |
| 28 | + */ |
| 29 | +class SendNoteExamplePlugin : public Plugin |
| 30 | +{ |
| 31 | +public: |
| 32 | + SendNoteExamplePlugin() |
| 33 | + : Plugin(0, 0, 0) |
| 34 | + { |
| 35 | + std::memset(fNotesPlayed, 0, sizeof(fNotesPlayed)); |
| 36 | + std::memset(fOscillatorPhases, 0, sizeof(fOscillatorPhases)); |
| 37 | + } |
| 38 | + |
| 39 | +protected: |
| 40 | + /* -------------------------------------------------------------------------------------------------------- |
| 41 | + * Information */ |
| 42 | + |
| 43 | + /** |
| 44 | + Get the plugin label. |
| 45 | + This label is a short restricted name consisting of only _, a-z, A-Z and 0-9 characters. |
| 46 | + */ |
| 47 | + const char* getLabel() const override |
| 48 | + { |
| 49 | + return "SendNote"; |
| 50 | + } |
| 51 | + |
| 52 | + /** |
| 53 | + Get an extensive comment/description about the plugin. |
| 54 | + */ |
| 55 | + const char* getDescription() const override |
| 56 | + { |
| 57 | + return "Plugin that demonstrates sending notes from the editor in DPF."; |
| 58 | + } |
| 59 | + |
| 60 | + /** |
| 61 | + Get the plugin author/maker. |
| 62 | + */ |
| 63 | + const char* getMaker() const override |
| 64 | + { |
| 65 | + return "DISTRHO"; |
| 66 | + } |
| 67 | + |
| 68 | + /** |
| 69 | + Get the plugin homepage. |
| 70 | + */ |
| 71 | + const char* getHomePage() const override |
| 72 | + { |
| 73 | + return "https://github.com/DISTRHO/DPF"; |
| 74 | + } |
| 75 | + |
| 76 | + /** |
| 77 | + Get the plugin license name (a single line of text). |
| 78 | + For commercial plugins this should return some short copyright information. |
| 79 | + */ |
| 80 | + const char* getLicense() const override |
| 81 | + { |
| 82 | + return "ISC"; |
| 83 | + } |
| 84 | + |
| 85 | + /** |
| 86 | + Get the plugin version, in hexadecimal. |
| 87 | + */ |
| 88 | + uint32_t getVersion() const override |
| 89 | + { |
| 90 | + return d_version(1, 0, 0); |
| 91 | + } |
| 92 | + |
| 93 | + /** |
| 94 | + Get the plugin unique Id. |
| 95 | + This value is used by LADSPA, DSSI and VST plugin formats. |
| 96 | + */ |
| 97 | + int64_t getUniqueId() const override |
| 98 | + { |
| 99 | + return d_cconst('d', 'S', 'N', 'o'); |
| 100 | + } |
| 101 | + |
| 102 | + /* -------------------------------------------------------------------------------------------------------- |
| 103 | + * Init and Internal data, unused in this plugin */ |
| 104 | + |
| 105 | + void initParameter(uint32_t, Parameter&) override {} |
| 106 | + float getParameterValue(uint32_t) const override { return 0.0f;} |
| 107 | + void setParameterValue(uint32_t, float) override {} |
| 108 | + |
| 109 | + /* -------------------------------------------------------------------------------------------------------- |
| 110 | + * Audio/MIDI Processing */ |
| 111 | + |
| 112 | + /** |
| 113 | + Run/process function for plugins with MIDI input. |
| 114 | + This synthesizes the MIDI voices with a sum of sine waves. |
| 115 | + */ |
| 116 | + void run(const float**, float** outputs, uint32_t frames, |
| 117 | + const MidiEvent* midiEvents, uint32_t midiEventCount) override |
| 118 | + { |
| 119 | + for (uint32_t i = 0; i < midiEventCount; ++i) |
| 120 | + { |
| 121 | + if (midiEvents[i].size <= 3) |
| 122 | + { |
| 123 | + uint8_t status = midiEvents[i].data[0]; |
| 124 | + uint8_t note = midiEvents[i].data[1] & 127; |
| 125 | + uint8_t velocity = midiEvents[i].data[2] & 127; |
| 126 | + |
| 127 | + switch (status & 0xf0) |
| 128 | + { |
| 129 | + case 0x90: |
| 130 | + if (velocity != 0) |
| 131 | + { |
| 132 | + fNotesPlayed[note] = velocity; |
| 133 | + break; |
| 134 | + } |
| 135 | + /* fall through */ |
| 136 | + case 0x80: |
| 137 | + fNotesPlayed[note] = 0; |
| 138 | + fOscillatorPhases[note] = 0; |
| 139 | + break; |
| 140 | + } |
| 141 | + } |
| 142 | + } |
| 143 | + |
| 144 | + float* const output = outputs[0]; |
| 145 | + std::memset(output, 0, frames * sizeof(float)); |
| 146 | + |
| 147 | + for (uint32_t noteNumber = 0; noteNumber < 128; ++noteNumber) |
| 148 | + { |
| 149 | + if (fNotesPlayed[noteNumber] == 0) |
| 150 | + continue; |
| 151 | + |
| 152 | + float notePitch = 8.17579891564 * std::exp(0.0577622650 * noteNumber); |
| 153 | + |
| 154 | + float phase = fOscillatorPhases[noteNumber]; |
| 155 | + float timeStep = notePitch / getSampleRate(); |
| 156 | + float k2pi = 2.0 * M_PI; |
| 157 | + float gain = 0.1; |
| 158 | + |
| 159 | + for (uint32_t i = 0; i < frames; ++i) |
| 160 | + { |
| 161 | + output[i] += gain * std::sin(k2pi * phase); |
| 162 | + phase += timeStep; |
| 163 | + phase -= (int)phase; |
| 164 | + } |
| 165 | + |
| 166 | + fOscillatorPhases[noteNumber] = phase; |
| 167 | + } |
| 168 | + } |
| 169 | + |
| 170 | + // ------------------------------------------------------------------------------------------------------- |
| 171 | + |
| 172 | +private: |
| 173 | + uint8_t fNotesPlayed[128]; |
| 174 | + float fOscillatorPhases[128]; |
| 175 | + |
| 176 | + /** |
| 177 | + Set our plugin class as non-copyable and add a leak detector just in case. |
| 178 | + */ |
| 179 | + DISTRHO_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(SendNoteExamplePlugin) |
| 180 | +}; |
| 181 | + |
| 182 | +/* ------------------------------------------------------------------------------------------------------------ |
| 183 | + * Plugin entry point, called by DPF to create a new plugin instance. */ |
| 184 | + |
| 185 | +Plugin* createPlugin() |
| 186 | +{ |
| 187 | + return new SendNoteExamplePlugin(); |
| 188 | +} |
| 189 | + |
| 190 | +// ----------------------------------------------------------------------------------------------------------- |
| 191 | + |
| 192 | +END_NAMESPACE_DISTRHO |
0 commit comments