Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <math.h>
- #include <stdio.h>
- #include <stdlib.h>
- #include "lv2/lv2plug.in/ns/lv2core/lv2.h"
- #define REVERB_URI "http://localhost/plugins/reverb"
- typedef enum {
- REVERB_DELAY = 0,
- REVERB_DECAY = 1,
- AUDIO_INPUT_LEFT = 2,
- AUDIO_INPUT_RIGHT = 3,
- AUDIO_OUTPUT_LEFT = 4,
- AUDIO_OUTPUT_RIGHT = 5
- } PortIndex;
- typedef struct {
- float* left_delay_buffer; // Cache of previous samples
- float* right_delay_buffer;
- const float* delay;
- const float* decay;
- const float* input_left;
- const float* input_right;
- float* output_left;
- float* output_right;
- float sample_rate;
- int delay_pos;
- /*struct {
- int dt;
- } CombFilter;*/
- } Reverb;
- static LV2_Handle
- instantiate( const LV2_Descriptor* descriptor,
- double rate,
- const char* bundle_path,
- const LV2_Feature* const* features)
- {
- // calloc() zero-initializes allocated memory, malloc() does not
- Reverb* reverb = (Reverb*)malloc(sizeof(reverb));
- // Set up reverb cache depending on sample rate
- // Max delay is half a second, so must maintain a cache of
- // half the size of the sample rate
- reverb->left_delay_buffer = (float*)calloc((int)(rate * 0.5), sizeof(float));
- reverb->right_delay_buffer = (float*)calloc((int)(rate * 0.5), sizeof(float));
- reverb->sample_rate = rate;
- reverb->delay_pos = 0;
- return (LV2_Handle)reverb;
- }
- static void
- connect_port( LV2_Handle instance,
- uint32_t port,
- void* data)
- {
- Reverb* reverb = (Reverb*)instance;
- switch ((PortIndex)port){
- case REVERB_DELAY:
- reverb->delay = (const float*)data;
- break;
- case REVERB_DECAY:
- reverb->decay = (const float*)data;
- break;
- case AUDIO_INPUT_LEFT:
- reverb->input_left = (const float*)data;
- break;
- case AUDIO_INPUT_RIGHT:
- reverb->input_right = (const float*)data;
- break;
- case AUDIO_OUTPUT_LEFT:
- reverb->output_left = (float*)data;
- break;
- case AUDIO_OUTPUT_RIGHT:
- reverb->output_right = (float*)data;
- break;
- }
- }
- static void
- activate( LV2_Handle instance)
- {
- // Reset reverb's internal state
- }
- static void
- run(LV2_Handle instance,
- uint32_t n_samples)
- {
- Reverb* reverb = (Reverb*)instance;
- float outL, outR;
- const float* const input_left = reverb->input_left;
- const float* const input_right = reverb->input_right;
- float* const output_left = reverb->output_left;
- float* const output_right = reverb->output_right;
- // dt is how big the delay_buffer should be. Even though it's
- // 22050 in size, that's only how much is used when there's a
- // 500ms delay. Otherwise, it should be less.
- int dt = (int) (reverb->sample_rate * *(reverb->delay));
- int *delay_pos = &(reverb->delay_pos);
- for(int i = 0; i < (n_samples); i ++){
- outL = reverb->left_delay_buffer[*delay_pos];
- outR = reverb->right_delay_buffer[*delay_pos];
- reverb->left_delay_buffer[*delay_pos] = input_left[i];
- reverb->right_delay_buffer[*delay_pos] = input_right[i];
- output_left[i] = outL;
- output_right[i] = outR;
- *delay_pos = (*delay_pos != dt ? *delay_pos + 1 : 0);
- }
- }
- static void deactivate(LV2_Handle instance){
- }
- static void cleanup(LV2_Handle instance){
- free(instance);
- }
- static const void* extension_data(const char* uri){
- return NULL;
- }
- static const LV2_Descriptor descriptor = {
- REVERB_URI,
- instantiate,
- connect_port,
- activate,
- run,
- deactivate,
- cleanup,
- extension_data
- };
- LV2_SYMBOL_EXPORT
- const LV2_Descriptor*
- lv2_descriptor(uint32_t index)
- {
- switch(index){
- case 0: return &descriptor;
- default: return NULL;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment