Guest User

Untitled

a guest
Feb 12th, 2015
245
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.74 KB | None | 0 0
  1. #include <math.h>
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include "lv2/lv2plug.in/ns/lv2core/lv2.h"
  5.  
  6. #define REVERB_URI "http://localhost/plugins/reverb"
  7.  
  8. typedef enum {
  9. REVERB_DELAY = 0,
  10. REVERB_DECAY = 1,
  11. AUDIO_INPUT_LEFT = 2,
  12. AUDIO_INPUT_RIGHT = 3,
  13. AUDIO_OUTPUT_LEFT = 4,
  14. AUDIO_OUTPUT_RIGHT = 5
  15. } PortIndex;
  16.  
  17. typedef struct {
  18. float* left_delay_buffer; // Cache of previous samples
  19. float* right_delay_buffer;
  20. const float* delay;
  21. const float* decay;
  22. const float* input_left;
  23. const float* input_right;
  24. float* output_left;
  25. float* output_right;
  26. float sample_rate;
  27.  
  28. int delay_pos;
  29. /*struct {
  30. int dt;
  31.  
  32. } CombFilter;*/
  33.  
  34. } Reverb;
  35.  
  36. static LV2_Handle
  37. instantiate( const LV2_Descriptor* descriptor,
  38. double rate,
  39. const char* bundle_path,
  40. const LV2_Feature* const* features)
  41. {
  42. // calloc() zero-initializes allocated memory, malloc() does not
  43. Reverb* reverb = (Reverb*)malloc(sizeof(reverb));
  44.  
  45. // Set up reverb cache depending on sample rate
  46. // Max delay is half a second, so must maintain a cache of
  47. // half the size of the sample rate
  48. reverb->left_delay_buffer = (float*)calloc((int)(rate * 0.5), sizeof(float));
  49. reverb->right_delay_buffer = (float*)calloc((int)(rate * 0.5), sizeof(float));
  50. reverb->sample_rate = rate;
  51. reverb->delay_pos = 0;
  52. return (LV2_Handle)reverb;
  53. }
  54.  
  55. static void
  56. connect_port( LV2_Handle instance,
  57. uint32_t port,
  58. void* data)
  59. {
  60. Reverb* reverb = (Reverb*)instance;
  61.  
  62. switch ((PortIndex)port){
  63. case REVERB_DELAY:
  64. reverb->delay = (const float*)data;
  65. break;
  66. case REVERB_DECAY:
  67. reverb->decay = (const float*)data;
  68. break;
  69. case AUDIO_INPUT_LEFT:
  70. reverb->input_left = (const float*)data;
  71. break;
  72. case AUDIO_INPUT_RIGHT:
  73. reverb->input_right = (const float*)data;
  74. break;
  75. case AUDIO_OUTPUT_LEFT:
  76. reverb->output_left = (float*)data;
  77. break;
  78. case AUDIO_OUTPUT_RIGHT:
  79. reverb->output_right = (float*)data;
  80. break;
  81. }
  82. }
  83.  
  84. static void
  85. activate( LV2_Handle instance)
  86. {
  87. // Reset reverb's internal state
  88.  
  89. }
  90.  
  91. static void
  92. run(LV2_Handle instance,
  93. uint32_t n_samples)
  94. {
  95.  
  96. Reverb* reverb = (Reverb*)instance;
  97. float outL, outR;
  98. const float* const input_left = reverb->input_left;
  99. const float* const input_right = reverb->input_right;
  100.  
  101. float* const output_left = reverb->output_left;
  102. float* const output_right = reverb->output_right;
  103.  
  104.  
  105. // dt is how big the delay_buffer should be. Even though it's
  106. // 22050 in size, that's only how much is used when there's a
  107. // 500ms delay. Otherwise, it should be less.
  108. int dt = (int) (reverb->sample_rate * *(reverb->delay));
  109. int *delay_pos = &(reverb->delay_pos);
  110. for(int i = 0; i < (n_samples); i ++){
  111. outL = reverb->left_delay_buffer[*delay_pos];
  112. outR = reverb->right_delay_buffer[*delay_pos];
  113.  
  114. reverb->left_delay_buffer[*delay_pos] = input_left[i];
  115. reverb->right_delay_buffer[*delay_pos] = input_right[i];
  116.  
  117. output_left[i] = outL;
  118. output_right[i] = outR;
  119.  
  120. *delay_pos = (*delay_pos != dt ? *delay_pos + 1 : 0);
  121. }
  122.  
  123. }
  124.  
  125. static void deactivate(LV2_Handle instance){
  126.  
  127. }
  128.  
  129. static void cleanup(LV2_Handle instance){
  130. free(instance);
  131. }
  132.  
  133. static const void* extension_data(const char* uri){
  134. return NULL;
  135. }
  136.  
  137. static const LV2_Descriptor descriptor = {
  138. REVERB_URI,
  139. instantiate,
  140. connect_port,
  141. activate,
  142. run,
  143. deactivate,
  144. cleanup,
  145. extension_data
  146. };
  147.  
  148. LV2_SYMBOL_EXPORT
  149. const LV2_Descriptor*
  150. lv2_descriptor(uint32_t index)
  151. {
  152. switch(index){
  153. case 0: return &descriptor;
  154. default: return NULL;
  155. }
  156. }
Advertisement
Add Comment
Please, Sign In to add comment