r1008: pt_BR translation update
[cinelerra_cv/mob.git] / plugins / interpolateaudio / interpolateaudio.C
blob8e864e5e22a80c024fc5eefb51c0a4120aaa9662
1 #include "bcdisplayinfo.h"
2 #include "clip.h"
3 #include "bchash.h"
4 #include "guicast.h"
5 #include "filexml.h"
6 #include "language.h"
7 #include "mainprogress.h"
8 #include "pluginaclient.h"
9 #include "transportque.h"
11 #include <string.h>
24 class InterpolateAudioEffect : public PluginAClient
26 public:
27         InterpolateAudioEffect(PluginServer *server);
28         ~InterpolateAudioEffect();
30         VFrame* new_picon();
31         char* plugin_title();
33         int process_buffer(int64_t size, 
34                 double *buffer,
35                 int64_t start_position,
36                 int sample_rate);
37         int is_realtime();
39 #define FRAGMENT_SIZE 4096
40         double *start_fragment;
41         double *end_fragment;
42         double start_sample;
43         double end_sample;
44         int64_t range_start;
45         int64_t range_end;
51 REGISTER_PLUGIN(InterpolateAudioEffect)
59 InterpolateAudioEffect::InterpolateAudioEffect(PluginServer *server)
60  : PluginAClient(server)
62         start_fragment = 0;
63         end_fragment = 0;
66 InterpolateAudioEffect::~InterpolateAudioEffect()
68         if(start_fragment) delete [] start_fragment;
69         if(end_fragment) delete [] end_fragment;
75 char* InterpolateAudioEffect::plugin_title()
77         return N_("Interpolate");
81 int InterpolateAudioEffect::is_realtime()
83         return 1;
87 #include "picon_png.h"
88 NEW_PICON_MACRO(InterpolateAudioEffect)
92 int InterpolateAudioEffect::process_buffer(int64_t size, 
93         double *buffer,
94         int64_t start_position,
95         int sample_rate)
97         double slope;
98         double intercept;
100         if(!start_fragment) start_fragment = new double[FRAGMENT_SIZE];
101         if(!end_fragment) end_fragment = new double[FRAGMENT_SIZE];
103         if(get_direction() == PLAY_FORWARD)
104         {
105 // On first sample of range.  Get boundary samples of effect.
106                 if(get_source_position() == get_source_start())
107                 {
108 // Need to read before desired sample to diffuse transients after audio
109 // seeks.
110                         range_start = get_source_start();
111                         range_end = get_source_start() + get_total_len();
112 //printf("InterpolateAudioEffect::process_buffer 1 %p\n", start_fragment);
113                         read_samples(start_fragment,
114                                 0,
115                                 sample_rate,
116                                 range_start - FRAGMENT_SIZE,
117                                 FRAGMENT_SIZE);
118                         start_sample = start_fragment[FRAGMENT_SIZE - 1];
119                         read_samples(end_fragment,
120                                 0,
121                                 sample_rate,
122                                 range_end - FRAGMENT_SIZE,
123                                 FRAGMENT_SIZE);
124                         end_sample = end_fragment[FRAGMENT_SIZE - 1];
125                 }
128                 for(int i = 0; i < size; i++)
129                 {
130                         double end_fraction = (double)(i + start_position - range_start) / 
131                                 (range_end - range_start);
132                         double start_fraction = 1.0 - end_fraction;
133                         double out_sample = start_sample * start_fraction + 
134                                 end_sample * end_fraction;
135                         buffer[i] = out_sample;
136                 }
137         }
138         else
139         {
140 // On first sample of range.  Get boundary samples of effect.
141                 if(get_source_position() == get_source_start() + get_total_len())
142                 {
143 // Need to read before desired sample to diffuse transients after audio
144 // seeks.
145                         range_start = get_source_start() + get_total_len();
146                         range_end = get_source_start();
147                         read_samples(start_fragment,
148                                 0,
149                                 sample_rate,
150                                 range_start,
151                                 FRAGMENT_SIZE);
152                         start_sample = start_fragment[0];
153                         read_samples(end_fragment,
154                                 0,
155                                 sample_rate,
156                                 range_end,
157                                 FRAGMENT_SIZE);
158                         end_sample = end_fragment[0];
159                 }
162                 for(int i = 0; i < size; i++)
163                 {
164                         double start_fraction = (double)(start_position - i - range_end) / 
165                                 (range_start - range_end);
166                         double end_fraction = 1.0 - start_fraction;
167                         double out_sample = start_sample * start_fraction + 
168                                 end_sample * end_fraction;
169                         buffer[i] = out_sample;
170                 }
171         }
172         return 0;