Advertisement
Guest User

Untitled

a guest
Aug 26th, 2016
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.76 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4.  
  5. #include "lilv-0/lilv/lilv.h"
  6. #include "lv2/lv2plug.in/ns/ext/urid/urid.h"
  7.  
  8. struct Ports {
  9.     float in;
  10.     float *out;
  11. };
  12.  
  13. LV2_URID map_put(LV2_URID_Map_Handle handle, const char *str)
  14. {
  15.     (void)handle;
  16.     (void)str;
  17.     return 0;
  18. }
  19.  
  20. void set_port(const char *port_symbol, void *user_data, const void *value, uint32_t size, uint32_t type)
  21. {
  22.     (void)size;
  23.     (void)type;
  24.     struct Ports *ports = user_data;
  25.  
  26.     printf("In set_port\n");
  27.     if (!strcmp(port_symbol, "frequency")) {
  28.         printf("setting frequency...\n");
  29.         ports->in = *(float*)value;
  30.     } else if (!strcmp(port_symbol, "out")) {
  31.         printf("setting out...\n");
  32.         ports->out = (float*)value;
  33.     }
  34. }
  35.  
  36. int main()
  37. {
  38.     LV2_URID_Map map;
  39.     map.handle = NULL;
  40.     map.map = map_put;
  41.  
  42.     /*
  43.      * Declare a sampling rate and an audio buffer size that this host
  44.      * will deal with. The buffer size SHOULD always be a power of two.
  45.      */
  46.     const double sample_rate = 44100;
  47.     const unsigned buffer_size = 4096;
  48.  
  49.     struct Ports ports;
  50.  
  51.     /*
  52.      * Declare an output buffer that our plugin can write to.
  53.      */
  54.     ports.out = malloc(buffer_size * sizeof(float));
  55.  
  56.     LilvWorld *lworld = lilv_world_new();
  57.     if (!lworld) {
  58.         fprintf(stderr, "Failed to create a lilv world.\n");
  59.         goto jmp_main_exit;
  60.     }
  61.  
  62.     lilv_world_load_all(lworld);
  63.     const LilvPlugins *plugs = lilv_world_get_all_plugins(lworld);
  64.    
  65.     /*
  66.      * Look for the LV2 plugin that identifies itself by the globally unique
  67.      * string "https://sine.synth.love"
  68.      *
  69.      * This does NOT connect to the internet or anything! It is simply a
  70.      * globally unique string identifier for a plugin! The URI scheme is
  71.      * merely convention! The plugin still has to be installed on your
  72.      * system for this to work!
  73.      */
  74.     LilvNode *pluguri = lilv_new_uri(lworld, "https://sine.synth.love/");
  75.     const LilvPlugin *plug = lilv_plugins_get_by_uri(plugs, pluguri);
  76.     if (!plug) {
  77.         fprintf(stderr, "Could not find the plugin.\n");
  78.         goto jmp_main_exit;
  79.     }
  80.  
  81.     LilvInstance *pluginst = lilv_plugin_instantiate(plug, sample_rate, NULL);
  82.     if (!pluginst) {
  83.         fprintf(stderr, "Failed to instantiate the plugin.\n");
  84.         goto jmp_main_exit;
  85.     }
  86.     lilv_node_free(pluguri);
  87.  
  88.     LilvState *initstate = lilv_state_new_from_world(lworld, &map, pluguri);
  89.     lilv_state_restore(initstate, pluginst, (LilvSetPortValueFunc)set_port, &ports, 0, NULL);
  90.     /*float freq = 0.0;*/
  91.  
  92.     lilv_instance_connect_port(pluginst, 0, &ports.in);
  93.     lilv_instance_connect_port(pluginst, 1, ports.out);
  94.  
  95.     lilv_instance_run(pluginst, buffer_size);
  96.     printf("Successfully ran the plugin.\n");
  97.  
  98.     int i;
  99.     for (i = 0; i < 32; ++i) {
  100.         printf("%f\n", ports.out[i]);
  101.     }
  102.  
  103.     lilv_instance_free(pluginst);
  104.  
  105. jmp_main_exit:
  106.     lilv_world_free(lworld);
  107.     free(ports.out);
  108.     return 0;
  109. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement