DPF

DISTRHO Plugin Framework
Log | Files | Refs | Submodules | README | LICENSE

FileHandlingPlugin.cpp (7738B)


      1 /*
      2  * DISTRHO Plugin Framework (DPF)
      3  * Copyright (C) 2012-2020 Filipe Coelho <falktx@falktx.com>
      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 START_NAMESPACE_DISTRHO
     20 
     21 // -----------------------------------------------------------------------------------------------------------
     22 
     23 /**
     24   Plugin to demonstrate File handling within DPF.
     25  */
     26 class FileHandlingExamplePlugin : public Plugin
     27 {
     28 public:
     29     FileHandlingExamplePlugin()
     30         : Plugin(kParameterCount, 0, kStateCount)
     31     {
     32         std::memset(fParameters, 0, sizeof(fParameters));
     33     }
     34 
     35 protected:
     36    /* --------------------------------------------------------------------------------------------------------
     37     * Information */
     38 
     39    /**
     40       Get the plugin label.
     41       This label is a short restricted name consisting of only _, a-z, A-Z and 0-9 characters.
     42     */
     43     const char* getLabel() const override
     44     {
     45         return "FileHandling";
     46     }
     47 
     48    /**
     49       Get an extensive comment/description about the plugin.
     50     */
     51     const char* getDescription() const override
     52     {
     53         return "Plugin to demonstrate File handling.";
     54     }
     55 
     56    /**
     57       Get the plugin author/maker.
     58     */
     59     const char* getMaker() const override
     60     {
     61         return "DISTRHO";
     62     }
     63 
     64    /**
     65       Get the plugin homepage.
     66     */
     67     const char* getHomePage() const override
     68     {
     69         return "https://github.com/DISTRHO/DPF";
     70     }
     71 
     72    /**
     73       Get the plugin license name (a single line of text).
     74       For commercial plugins this should return some short copyright information.
     75     */
     76     const char* getLicense() const override
     77     {
     78         return "ISC";
     79     }
     80 
     81    /**
     82       Get the plugin version, in hexadecimal.
     83     */
     84     uint32_t getVersion() const override
     85     {
     86         return d_version(0, 0, 0);
     87     }
     88 
     89    /**
     90       Get the plugin unique Id.
     91       This value is used by LADSPA, DSSI and VST plugin formats.
     92     */
     93     int64_t getUniqueId() const override
     94     {
     95         return d_cconst('d', 'F', 'i', 'H');
     96     }
     97 
     98    /* --------------------------------------------------------------------------------------------------------
     99     * Init */
    100 
    101    /**
    102       Initialize the audio port @a index.@n
    103       This function will be called once, shortly after the plugin is created.
    104     */
    105     void initAudioPort(bool input, uint32_t index, AudioPort& port) override
    106     {
    107         // treat meter audio ports as stereo
    108         port.groupId = kPortGroupMono;
    109 
    110         // everything else is as default
    111         Plugin::initAudioPort(input, index, port);
    112     }
    113 
    114    /**
    115       Initialize the parameter @a index.
    116       This function will be called once, shortly after the plugin is created.
    117     */
    118     void initParameter(uint32_t index, Parameter& param) override
    119     {
    120         param.hints = kParameterIsOutput|kParameterIsInteger;
    121 
    122         switch (index)
    123         {
    124         case kParameterFileSize1:
    125             param.name   = "Size #1";
    126             param.symbol = "size1";
    127             break;
    128         case kParameterFileSize2:
    129             param.name   = "Size #2";
    130             param.symbol = "size2";
    131             break;
    132         case kParameterFileSize3:
    133             param.name   = "Size #3";
    134             param.symbol = "size3";
    135             break;
    136         }
    137     }
    138 
    139    /**
    140       Initialize the state @a index.@n
    141       This function will be called once, shortly after the plugin is created.
    142     */
    143     void initState(uint32_t index, State& state) override
    144     {
    145         switch (index)
    146         {
    147         case kStateFile1:
    148             state.key = "file1";
    149             state.label = "File 1";
    150             break;
    151         case kStateFile2:
    152             state.key = "file2";
    153             state.label = "File 2";
    154             break;
    155         case kStateFile3:
    156             state.key = "file3";
    157             state.label = "File 3";
    158             break;
    159         }
    160 
    161         state.hints = kStateIsFilenamePath;
    162     }
    163 
    164    /* --------------------------------------------------------------------------------------------------------
    165     * Internal data */
    166 
    167    /**
    168       Get the current value of a parameter.
    169       The host may call this function from any context, including realtime processing.
    170       We have no parameters in this plugin example, so we do nothing with the function.
    171     */
    172     float getParameterValue(uint32_t index) const override
    173     {
    174         return fParameters[index];
    175     }
    176 
    177    /**
    178       Change a parameter value.@n
    179       The host may call this function from any context, including realtime processing.
    180 
    181       This function will only be called for parameter inputs.
    182       Since we have no parameters inputs in this example, so we do nothing with the function.
    183     */
    184     void setParameterValue(uint32_t, float) override {}
    185 
    186    /**
    187       Change an internal state @a key to @a value.
    188     */
    189     void setState(const char* key, const char* value) override
    190     {
    191         d_stdout("DSP setState %s %s", key, value);
    192         int fileId = -1;
    193 
    194         /**/ if (std::strcmp(key, "file1") == 0)
    195             fileId = 0;
    196         else if (std::strcmp(key, "file2") == 0)
    197             fileId = 1;
    198         else if (std::strcmp(key, "file3") == 0)
    199             fileId = 2;
    200 
    201         if (fileId == -1)
    202             return;
    203 
    204         if (FILE* const fh = fopen(value, "r"))
    205         {
    206             fseek(fh, 0, SEEK_END);
    207 
    208             // filesize
    209             const long int size = ftell(fh);
    210             d_stdout("size of %s is %li", value, size);
    211             fParameters[kParameterFileSize1 + fileId] = static_cast<float>(size) / 1000.0f;
    212 
    213             fclose(fh);
    214         }
    215     }
    216 
    217    /* --------------------------------------------------------------------------------------------------------
    218     * Audio/MIDI Processing */
    219 
    220    /**
    221       Run/process function for plugins without MIDI input.
    222       @note Some parameters might be null if there are no audio inputs or outputs.
    223     */
    224     void run(const float** inputs, float** outputs, uint32_t frames) override
    225     {
    226        /**
    227           This plugin doesn't do audio, it just demonstrates file handling usage.
    228           So here we directly copy inputs over outputs, leaving the audio untouched.
    229           We need to be careful in case the host re-uses the same buffer for both inputs and outputs.
    230         */
    231         if (outputs[0] != inputs[0])
    232             std::memcpy(outputs[0], inputs[0], sizeof(float)*frames);
    233     }
    234 
    235     // -------------------------------------------------------------------------------------------------------
    236 
    237 private:
    238     float fParameters[kParameterCount];
    239 
    240    /**
    241       Set our plugin class as non-copyable and add a leak detector just in case.
    242     */
    243     DISTRHO_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(FileHandlingExamplePlugin)
    244 };
    245 
    246 /* ------------------------------------------------------------------------------------------------------------
    247  * Plugin entry point, called by DPF to create a new plugin instance. */
    248 
    249 Plugin* createPlugin()
    250 {
    251     return new FileHandlingExamplePlugin();
    252 }
    253 
    254 // -----------------------------------------------------------------------------------------------------------
    255 
    256 END_NAMESPACE_DISTRHO