r969: Render format selector: Do not change the path name when the format is changed.
[cinelerra_cv/ct.git] / plugins / reverseaudio / reverseaudio.C
blobfb4f55d0bb009bfafe22be0e7437bf90fab43965
1 #include "bcdisplayinfo.h"
2 #include "bchash.h"
3 #include "filexml.h"
4 #include "guicast.h"
5 #include "language.h"
6 #include "pluginaclient.h"
7 #include "transportque.h"
9 #include <string.h>
11 class ReverseAudio;
13 class ReverseAudioConfig
15 public:
16         ReverseAudioConfig();
17         int enabled;
21 class ReverseAudioEnabled : public BC_CheckBox
23 public:
24         ReverseAudioEnabled(ReverseAudio *plugin,
25                 int x,
26                 int y);
27         int handle_event();
28         ReverseAudio *plugin;
31 class ReverseAudioWindow : public BC_Window
33 public:
34         ReverseAudioWindow(ReverseAudio *plugin, int x, int y);
35         ~ReverseAudioWindow();
36         void create_objects();
37         int close_event();
38         ReverseAudio *plugin;
39         ReverseAudioEnabled *enabled;
42 PLUGIN_THREAD_HEADER(ReverseAudio, ReverseAudioThread, ReverseAudioWindow)
44 class ReverseAudio : public PluginAClient
46 public:
47         ReverseAudio(PluginServer *server);
48         ~ReverseAudio();
50         PLUGIN_CLASS_MEMBERS(ReverseAudioConfig, ReverseAudioThread)
52         int load_defaults();
53         int save_defaults();
54         void save_data(KeyFrame *keyframe);
55         void read_data(KeyFrame *keyframe);
56         void update_gui();
57         int is_realtime();
58         int process_buffer(int64_t size, 
59                 double *buffer,
60                 int64_t start_position,
61                 int sample_rate);
63         int64_t input_position;
64         int fragment_size;
73 REGISTER_PLUGIN(ReverseAudio);
77 ReverseAudioConfig::ReverseAudioConfig()
79         enabled = 1;
86 ReverseAudioWindow::ReverseAudioWindow(ReverseAudio *plugin, int x, int y)
87  : BC_Window(plugin->gui_string, 
88         x, 
89         y, 
90         210, 
91         160, 
92         200, 
93         160, 
94         0, 
95         0,
96         1)
98         this->plugin = plugin;
101 ReverseAudioWindow::~ReverseAudioWindow()
105 void ReverseAudioWindow::create_objects()
107         int x = 10, y = 10;
109         add_subwindow(enabled = new ReverseAudioEnabled(plugin, 
110                 x, 
111                 y));
112         show_window();
113         flush();
116 WINDOW_CLOSE_EVENT(ReverseAudioWindow)
119 PLUGIN_THREAD_OBJECT(ReverseAudio, ReverseAudioThread, ReverseAudioWindow)
126 ReverseAudioEnabled::ReverseAudioEnabled(ReverseAudio *plugin, 
127         int x, 
128         int y)
129  : BC_CheckBox(x, 
130         y, 
131         plugin->config.enabled,
132         _("Enabled"))
134         this->plugin = plugin;
137 int ReverseAudioEnabled::handle_event()
139         plugin->config.enabled = get_value();
140         plugin->send_configure_change();
141         return 1;
152 ReverseAudio::ReverseAudio(PluginServer *server)
153  : PluginAClient(server)
155         PLUGIN_CONSTRUCTOR_MACRO
159 ReverseAudio::~ReverseAudio()
161         PLUGIN_DESTRUCTOR_MACRO
164 char* ReverseAudio::plugin_title() { return N_("Reverse audio"); }
165 int ReverseAudio::is_realtime() { return 1; }
167 #include "picon_png.h"
168 NEW_PICON_MACRO(ReverseAudio)
170 SHOW_GUI_MACRO(ReverseAudio, ReverseAudioThread)
172 RAISE_WINDOW_MACRO(ReverseAudio)
174 SET_STRING_MACRO(ReverseAudio);
177 int ReverseAudio::process_buffer(int64_t size, 
178         double *buffer,
179         int64_t start_position,
180         int sample_rate)
182         for(int i = 0; i < size; i += fragment_size)
183         {
184                 fragment_size = size - i;
185                 load_configuration();
186                 if(config.enabled)
187                 {
188                         read_samples(buffer + i,
189                                 0,
190                                 sample_rate,
191                                 input_position,
192                                 fragment_size);
193                         for(int start = i, end = i + fragment_size - 1;
194                                 end > start; 
195                                 start++, end--)
196                         {
197                                 double temp = buffer[start];
198                                 buffer[start] = buffer[end];
199                                 buffer[end] = temp;
200                         }
201                 }
202                 else
203                         read_samples(buffer + i,
204                                 0,
205                                 sample_rate,
206                                 start_position,
207                                 fragment_size);
208                 if(get_direction() == PLAY_FORWARD)
209                         start_position += fragment_size;
210                 else
211                         start_position -= fragment_size;
212         }
213         
215         return 0;
221 int ReverseAudio::load_configuration()
223         KeyFrame *prev_keyframe, *next_keyframe;
224         next_keyframe = get_next_keyframe(get_source_position());
225         prev_keyframe = get_prev_keyframe(get_source_position());
226         read_data(next_keyframe);
227 // Previous keyframe stays in config object.
228         read_data(prev_keyframe);
230         int64_t prev_position = edl_to_local(prev_keyframe->position);
231         int64_t next_position = edl_to_local(next_keyframe->position);
233 // printf("ReverseAudio::load_configuration 1 %lld %lld %lld %lld\n", 
234 // prev_position,
235 // next_position,
236 // prev_keyframe->position, 
237 // next_keyframe->position);
238 // Defeat default keyframe
239         if(prev_position == 0 && next_position == 0) 
240         {
241                 next_position = prev_position = get_source_start();
242         }
244 // Get range to flip in requested rate
245         int64_t range_start = prev_position;
246         int64_t range_end = next_position;
248 // Between keyframe and edge of range or no keyframes
249         if(range_start == range_end)
250         {
251 // Between first keyframe and start of effect
252                 if(get_source_position() >= get_source_start() &&
253                         get_source_position() < range_start)
254                 {
255                         range_start = get_source_start();
256                 }
257                 else
258 // Between last keyframe and end of effect
259                 if(get_source_position() >= range_start &&
260                         get_source_position() < get_source_start() + get_total_len())
261                 {
262                         range_end = get_source_start() + get_total_len();
263                 }
264                 else
265                 {
266 // Should never get here
267                         ;
268                 }
269         }
271 // Convert start position to new direction
272         if(get_direction() == PLAY_FORWARD)
273         {
274 // Truncate next buffer to keyframe
275                 if(range_end - get_source_position() < fragment_size)
276                         fragment_size = range_end - get_source_position();
277                 input_position = get_source_position() - range_start;
278                 input_position = range_end - input_position - fragment_size;
279         }
280         else
281         {
282                 if(get_source_position() - range_start < fragment_size)
283                         fragment_size = get_source_position() - range_start;
284                 input_position = range_end - get_source_position();
285                 input_position = range_start + input_position + fragment_size;
286         }
287 // printf("ReverseAudio::load_configuration 20 start=%lld end=%lld current=%lld input=%lld\n", 
288 // range_start, 
289 // range_end, 
290 // get_source_position(),
291 // input_position);
293         return 0;
296 int ReverseAudio::load_defaults()
298         char directory[BCTEXTLEN];
299 // set the default directory
300         sprintf(directory, "%sreverseaudio.rc", BCASTDIR);
302 // load the defaults
303         defaults = new BC_Hash(directory);
304         defaults->load();
306         config.enabled = defaults->get("ENABLED", config.enabled);
307         return 0;
310 int ReverseAudio::save_defaults()
312         defaults->update("ENABLED", config.enabled);
313         defaults->save();
314         return 0;
317 void ReverseAudio::save_data(KeyFrame *keyframe)
319         FileXML output;
321 // cause data to be stored directly in text
322         output.set_shared_string(keyframe->data, MESSAGESIZE);
323         output.tag.set_title("REVERSEAUDIO");
324         output.tag.set_property("ENABLED", config.enabled);
325         output.append_tag();
326         output.terminate_string();
329 void ReverseAudio::read_data(KeyFrame *keyframe)
331         FileXML input;
333         input.set_shared_string(keyframe->data, strlen(keyframe->data));
335         int result = 0;
337         while(!input.read_tag())
338         {
339                 if(input.tag.title_is("REVERSEAUDIO"))
340                 {
341                         config.enabled = input.tag.get_property("ENABLED", config.enabled);
342                 }
343         }
346 void ReverseAudio::update_gui()
348         if(thread)
349         {
350                 load_configuration();
351                 thread->window->lock_window();
352                 thread->window->enabled->update(config.enabled);
353                 thread->window->unlock_window();
354         }