Advertisement
Guest User

Untitled

a guest
Mar 30th, 2018
162
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 8.99 KB | None | 0 0
  1. #include <stdlib.h>
  2. #include <string.h>
  3. #ifndef WIN32
  4. #include "config.h"
  5. #endif
  6.  
  7. #ifdef ENABLE_NLS
  8. #include <libintl.h>
  9. #endif
  10.  
  11. #define         _ISOC9X_SOURCE  1
  12. #define         _ISOC99_SOURCE  1
  13. #define         __USE_ISOC99    1
  14. #define         __USE_ISOC9X    1
  15.  
  16. #include <math.h>
  17.  
  18. #include "ladspa.h"
  19.  
  20. #ifdef WIN32
  21. #define _WINDOWS_DLL_EXPORT_ __declspec(dllexport)
  22. int bIsFirstTime = 1;
  23. void __attribute__((constructor)) swh_init(); // forward declaration
  24. #else
  25. #define _WINDOWS_DLL_EXPORT_
  26. #endif
  27.  
  28. #line 9 "lowpass_iir_1891.xml"
  29.  
  30. #include "config.h"
  31. #include "util/iir.h"
  32. #include "ladspa-util.h"
  33.  
  34. #define LOWPASS_IIR_CUTOFF             0
  35. #define LOWPASS_IIR_STAGES             1
  36. #define LOWPASS_IIR_INPUT              2
  37. #define LOWPASS_IIR_OUTPUT             3
  38.  
  39. static LADSPA_Descriptor *lowpass_iirDescriptor = NULL;
  40.  
  41. typedef struct {
  42.     LADSPA_Data *cutoff;
  43.     LADSPA_Data *stages;
  44.     LADSPA_Data *input;
  45.     LADSPA_Data *output;
  46.     iir_stage_t* gt;
  47.     iirf_t*      iirf;
  48.     long         sample_rate;
  49.     LADSPA_Data run_adding_gain;
  50. } Lowpass_iir;
  51.  
  52. _WINDOWS_DLL_EXPORT_
  53. const LADSPA_Descriptor *ladspa_descriptor(unsigned long index) {
  54.  
  55. #ifdef WIN32
  56.     if (bIsFirstTime) {
  57.         swh_init();
  58.         bIsFirstTime = 0;
  59.     }
  60. #endif
  61.     switch (index) {
  62.     case 0:
  63.         return lowpass_iirDescriptor;
  64.     default:
  65.         return NULL;
  66.     }
  67. }
  68.  
  69. static void activateLowpass_iir(LADSPA_Handle instance) {
  70.     Lowpass_iir *plugin_data = (Lowpass_iir *)instance;
  71.     iir_stage_t*gt = plugin_data->gt;
  72.     iirf_t*iirf = plugin_data->iirf;
  73.     long sample_rate = plugin_data->sample_rate;
  74. #line 36 "lowpass_iir_1891.xml"
  75.    
  76.     gt = init_iir_stage(IIR_STAGE_LOWPASS,10,3,2);
  77.     iirf = init_iirf_t(gt);
  78.     chebyshev(iirf, gt, 2*CLAMP(f_round(*(plugin_data->stages)),1,10), IIR_STAGE_LOWPASS,
  79.               *(plugin_data->cutoff)/(float)sample_rate, 0.5f);
  80.     plugin_data->gt = gt;
  81.     plugin_data->iirf = iirf;
  82.     plugin_data->sample_rate = sample_rate;
  83.  
  84. }
  85.  
  86. static void cleanupLowpass_iir(LADSPA_Handle instance) {
  87. #line 43 "lowpass_iir_1891.xml"
  88.     Lowpass_iir *plugin_data = (Lowpass_iir *)instance;
  89.     free_iirf_t(plugin_data->iirf, plugin_data->gt);
  90.     free_iir_stage(plugin_data->gt);
  91.     free(instance);
  92. }
  93.  
  94. static void connectPortLowpass_iir(
  95.  LADSPA_Handle instance,
  96.  unsigned long port,
  97.  LADSPA_Data *data) {
  98.     Lowpass_iir *plugin;
  99.  
  100.     plugin = (Lowpass_iir *)instance;
  101.     switch (port) {
  102.     case LOWPASS_IIR_CUTOFF:
  103.         plugin->cutoff = data;
  104.         break;
  105.     case LOWPASS_IIR_STAGES:
  106.         plugin->stages = data;
  107.         break;
  108.     case LOWPASS_IIR_INPUT:
  109.         plugin->input = data;
  110.         break;
  111.     case LOWPASS_IIR_OUTPUT:
  112.         plugin->output = data;
  113.         break;
  114.     }
  115. }
  116.  
  117. static LADSPA_Handle instantiateLowpass_iir(
  118.  const LADSPA_Descriptor *descriptor,
  119.  unsigned long s_rate) {
  120.     Lowpass_iir *plugin_data = (Lowpass_iir *)calloc(1, sizeof(Lowpass_iir));
  121.     iir_stage_t*gt = NULL;
  122.     iirf_t*iirf = NULL;
  123.     long sample_rate;
  124.  
  125. #line 25 "lowpass_iir_1891.xml"
  126.     sample_rate = s_rate;
  127.  
  128.     plugin_data->gt = gt;
  129.     plugin_data->iirf = iirf;
  130.     plugin_data->sample_rate = sample_rate;
  131.  
  132.     return (LADSPA_Handle)plugin_data;
  133. }
  134.  
  135. #undef buffer_write
  136. #undef RUN_ADDING
  137. #undef RUN_REPLACING
  138.  
  139. #define buffer_write(b, v) (b = v)
  140. #define RUN_ADDING    0
  141. #define RUN_REPLACING 1
  142.  
  143. static void runLowpass_iir(LADSPA_Handle instance, unsigned long sample_count) {
  144.     Lowpass_iir *plugin_data = (Lowpass_iir *)instance;
  145.     LADSPA_Data run_adding_gain = plugin_data->run_adding_gain;
  146.  
  147.     /* Cutoff Frequency (float value) */
  148.     const LADSPA_Data cutoff = *(plugin_data->cutoff);
  149.  
  150.     /* Stages(2 poles per stage) (float value) */
  151.     const LADSPA_Data stages = *(plugin_data->stages);
  152.  
  153.     /* Input (array of floats of length sample_count) */
  154.     const LADSPA_Data * const input = plugin_data->input;
  155.  
  156.     /* Output (array of floats of length sample_count) */
  157.     LADSPA_Data * const output = plugin_data->output;
  158.     iir_stage_t* gt = plugin_data->gt;
  159.     iirf_t* iirf = plugin_data->iirf;
  160.     long sample_rate = plugin_data->sample_rate;
  161.  
  162. #line 28 "lowpass_iir_1891.xml"
  163.     chebyshev(iirf, gt, 2*CLAMP((int)stages,1,10), IIR_STAGE_LOWPASS, cutoff/(float)sample_rate, 0.5f);
  164.     iir_process_buffer_ns_5(iirf, gt, input, output, sample_count,RUN_ADDING);
  165.  
  166.     // Unused variable
  167.     (void)(run_adding_gain);
  168. }
  169. #undef buffer_write
  170. #undef RUN_ADDING
  171. #undef RUN_REPLACING
  172.  
  173. #define buffer_write(b, v) (b += (v) * run_adding_gain)
  174. #define RUN_ADDING    1
  175. #define RUN_REPLACING 0
  176.  
  177. static void setRunAddingGainLowpass_iir(LADSPA_Handle instance, LADSPA_Data gain) {
  178.     ((Lowpass_iir *)instance)->run_adding_gain = gain;
  179. }
  180.  
  181. static void runAddingLowpass_iir(LADSPA_Handle instance, unsigned long sample_count) {
  182.     Lowpass_iir *plugin_data = (Lowpass_iir *)instance;
  183.     LADSPA_Data run_adding_gain = plugin_data->run_adding_gain;
  184.  
  185.     /* Cutoff Frequency (float value) */
  186.     const LADSPA_Data cutoff = *(plugin_data->cutoff);
  187.  
  188.     /* Stages(2 poles per stage) (float value) */
  189.     const LADSPA_Data stages = *(plugin_data->stages);
  190.  
  191.     /* Input (array of floats of length sample_count) */
  192.     const LADSPA_Data * const input = plugin_data->input;
  193.  
  194.     /* Output (array of floats of length sample_count) */
  195.     LADSPA_Data * const output = plugin_data->output;
  196.     iir_stage_t* gt = plugin_data->gt;
  197.     iirf_t* iirf = plugin_data->iirf;
  198.     long sample_rate = plugin_data->sample_rate;
  199.  
  200. #line 28 "lowpass_iir_1891.xml"
  201.     chebyshev(iirf, gt, 2*CLAMP((int)stages,1,10), IIR_STAGE_LOWPASS, cutoff/(float)sample_rate, 0.5f);
  202.     iir_process_buffer_ns_5(iirf, gt, input, output, sample_count,RUN_ADDING);
  203.  
  204.     // Unused variable
  205.     (void)(run_adding_gain);
  206. }
  207.  
  208. void __attribute__((constructor)) swh_init() {
  209.     char **port_names;
  210.     LADSPA_PortDescriptor *port_descriptors;
  211.     LADSPA_PortRangeHint *port_range_hints;
  212.  
  213. #ifdef ENABLE_NLS
  214. #define D_(s) dgettext(PACKAGE, s)
  215.     bindtextdomain(PACKAGE, PACKAGE_LOCALE_DIR);
  216. #else
  217. #define D_(s) (s)
  218. #endif
  219.  
  220.  
  221.     lowpass_iirDescriptor =
  222.      (LADSPA_Descriptor *)malloc(sizeof(LADSPA_Descriptor));
  223.  
  224.     if (lowpass_iirDescriptor) {
  225.         lowpass_iirDescriptor->UniqueID = 1891;
  226.         lowpass_iirDescriptor->Label = "lowpass_iir";
  227.         lowpass_iirDescriptor->Properties =
  228.          LADSPA_PROPERTY_HARD_RT_CAPABLE;
  229.         lowpass_iirDescriptor->Name =
  230.          D_("Glame Lowpass Filter");
  231.         lowpass_iirDescriptor->Maker =
  232.          "Alexander Ehlert <[email protected]>";
  233.         lowpass_iirDescriptor->Copyright =
  234.          "GPL";
  235.         lowpass_iirDescriptor->PortCount = 4;
  236.  
  237.         port_descriptors = (LADSPA_PortDescriptor *)calloc(4,
  238.          sizeof(LADSPA_PortDescriptor));
  239.         lowpass_iirDescriptor->PortDescriptors =
  240.          (const LADSPA_PortDescriptor *)port_descriptors;
  241.  
  242.         port_range_hints = (LADSPA_PortRangeHint *)calloc(4,
  243.          sizeof(LADSPA_PortRangeHint));
  244.         lowpass_iirDescriptor->PortRangeHints =
  245.          (const LADSPA_PortRangeHint *)port_range_hints;
  246.  
  247.         port_names = (char **)calloc(4, sizeof(char*));
  248.         lowpass_iirDescriptor->PortNames =
  249.          (const char **)port_names;
  250.  
  251.         /* Parameters for Cutoff Frequency */
  252.         port_descriptors[LOWPASS_IIR_CUTOFF] =
  253.          LADSPA_PORT_INPUT | LADSPA_PORT_CONTROL;
  254.         port_names[LOWPASS_IIR_CUTOFF] =
  255.          D_("Cutoff Frequency");
  256.         port_range_hints[LOWPASS_IIR_CUTOFF].HintDescriptor =
  257.          LADSPA_HINT_BOUNDED_BELOW | LADSPA_HINT_BOUNDED_ABOVE | LADSPA_HINT_DEFAULT_HIGH | LADSPA_HINT_SAMPLE_RATE | LADSPA_HINT_LOGARITHMIC;
  258.         port_range_hints[LOWPASS_IIR_CUTOFF].LowerBound = 0.0001;
  259.         port_range_hints[LOWPASS_IIR_CUTOFF].UpperBound = 0.45;
  260.  
  261.         /* Parameters for Stages(2 poles per stage) */
  262.         port_descriptors[LOWPASS_IIR_STAGES] =
  263.          LADSPA_PORT_INPUT | LADSPA_PORT_CONTROL;
  264.         port_names[LOWPASS_IIR_STAGES] =
  265.          D_("Stages(2 poles per stage)");
  266.         port_range_hints[LOWPASS_IIR_STAGES].HintDescriptor =
  267.          LADSPA_HINT_BOUNDED_BELOW | LADSPA_HINT_BOUNDED_ABOVE | LADSPA_HINT_DEFAULT_1 | LADSPA_HINT_INTEGER;
  268.         port_range_hints[LOWPASS_IIR_STAGES].LowerBound = 1.0;
  269.         port_range_hints[LOWPASS_IIR_STAGES].UpperBound = 10.0;
  270.  
  271.         /* Parameters for Input */
  272.         port_descriptors[LOWPASS_IIR_INPUT] =
  273.          LADSPA_PORT_INPUT | LADSPA_PORT_AUDIO;
  274.         port_names[LOWPASS_IIR_INPUT] =
  275.          D_("Input");
  276.         port_range_hints[LOWPASS_IIR_INPUT].HintDescriptor = 0;
  277.  
  278.         /* Parameters for Output */
  279.         port_descriptors[LOWPASS_IIR_OUTPUT] =
  280.          LADSPA_PORT_OUTPUT | LADSPA_PORT_AUDIO;
  281.         port_names[LOWPASS_IIR_OUTPUT] =
  282.          D_("Output");
  283.         port_range_hints[LOWPASS_IIR_OUTPUT].HintDescriptor = 0;
  284.  
  285.         lowpass_iirDescriptor->activate = activateLowpass_iir;
  286.         lowpass_iirDescriptor->cleanup = cleanupLowpass_iir;
  287.         lowpass_iirDescriptor->connect_port = connectPortLowpass_iir;
  288.         lowpass_iirDescriptor->deactivate = NULL;
  289.         lowpass_iirDescriptor->instantiate = instantiateLowpass_iir;
  290.         lowpass_iirDescriptor->run = runLowpass_iir;
  291.         lowpass_iirDescriptor->run_adding = runAddingLowpass_iir;
  292.         lowpass_iirDescriptor->set_run_adding_gain = setRunAddingGainLowpass_iir;
  293.     }
  294. }
  295.  
  296. void __attribute__((destructor)) swh_fini() {
  297.     if (lowpass_iirDescriptor) {
  298.         free((LADSPA_PortDescriptor *)lowpass_iirDescriptor->PortDescriptors);
  299.         free((char **)lowpass_iirDescriptor->PortNames);
  300.         free((LADSPA_PortRangeHint *)lowpass_iirDescriptor->PortRangeHints);
  301.         free(lowpass_iirDescriptor);
  302.     }
  303.     lowpass_iirDescriptor = NULL;
  304.  
  305. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement