igor ru.po
[goodguy/history.git] / cinelerra-5.1 / plugins / reverseaudio / reverseaudio.C
1
2 /*
3  * CINELERRA
4  * Copyright (C) 2008 Adam Williams <broadcast at earthling dot net>
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19  *
20  */
21
22 #include "bcdisplayinfo.h"
23 #include "bchash.h"
24 #include "filexml.h"
25 #include "guicast.h"
26 #include "language.h"
27 #include "pluginaclient.h"
28 #include "samples.h"
29 #include "transportque.h"
30
31 #include <string.h>
32
33 class ReverseAudio;
34
35 class ReverseAudioConfig
36 {
37 public:
38         ReverseAudioConfig();
39         int enabled;
40 };
41
42
43 class ReverseAudioEnabled : public BC_CheckBox
44 {
45 public:
46         ReverseAudioEnabled(ReverseAudio *plugin,
47                 int x,
48                 int y);
49         int handle_event();
50         ReverseAudio *plugin;
51 };
52
53 class ReverseAudioWindow : public PluginClientWindow
54 {
55 public:
56         ReverseAudioWindow(ReverseAudio *plugin);
57         ~ReverseAudioWindow();
58         void create_objects();
59
60         ReverseAudio *plugin;
61         ReverseAudioEnabled *enabled;
62 };
63
64
65
66 class ReverseAudio : public PluginAClient
67 {
68 public:
69         ReverseAudio(PluginServer *server);
70         ~ReverseAudio();
71
72         PLUGIN_CLASS_MEMBERS(ReverseAudioConfig)
73
74         void save_data(KeyFrame *keyframe);
75         void read_data(KeyFrame *keyframe);
76         void update_gui();
77         int is_realtime();
78         int process_buffer(int64_t size,
79                 Samples *buffer,
80                 int64_t start_position,
81                 int sample_rate);
82
83         int64_t input_position;
84         int fragment_size;
85 };
86
87
88
89
90
91
92
93 REGISTER_PLUGIN(ReverseAudio);
94
95
96
97 ReverseAudioConfig::ReverseAudioConfig()
98 {
99         enabled = 1;
100 }
101
102
103
104
105
106 ReverseAudioWindow::ReverseAudioWindow(ReverseAudio *plugin)
107  : PluginClientWindow(plugin, 265, 60, 265, 60, 0)
108 {
109         this->plugin = plugin;
110 }
111
112 ReverseAudioWindow::~ReverseAudioWindow()
113 {
114 }
115
116 void ReverseAudioWindow::create_objects()
117 {
118         int x = 10, y = 10;
119
120         add_subwindow(enabled = new ReverseAudioEnabled(plugin,
121                 x,
122                 y));
123         show_window();
124         flush();
125 }
126
127
128
129
130
131
132 ReverseAudioEnabled::ReverseAudioEnabled(ReverseAudio *plugin,
133         int x,
134         int y)
135  : BC_CheckBox(x,
136         y,
137         plugin->config.enabled,
138         _("Enabled"))
139 {
140         this->plugin = plugin;
141 }
142
143 int ReverseAudioEnabled::handle_event()
144 {
145         plugin->config.enabled = get_value();
146         plugin->send_configure_change();
147         return 1;
148 }
149
150
151
152
153
154
155
156
157
158 ReverseAudio::ReverseAudio(PluginServer *server)
159  : PluginAClient(server)
160 {
161
162 }
163
164
165 ReverseAudio::~ReverseAudio()
166 {
167
168 }
169
170 const char* ReverseAudio::plugin_title() { return _("Reverse audio"); }
171 int ReverseAudio::is_realtime() { return 1; }
172
173
174 NEW_WINDOW_MACRO(ReverseAudio, ReverseAudioWindow)
175
176
177 int ReverseAudio::process_buffer(int64_t size,
178         Samples *buffer,
179         int64_t start_position,
180         int sample_rate)
181 {
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                         int offset = buffer->get_offset();
189                         buffer->set_offset(offset + i);
190                         read_samples(buffer,
191                                 0,
192                                 sample_rate,
193                                 input_position,
194                                 fragment_size);
195                         buffer->set_offset(offset);
196                         double *buffer_samples = buffer->get_data();
197
198                         for(int start = i, end = i + fragment_size - 1;
199                                 end > start;
200                                 start++, end--)
201                         {
202                                 double temp = buffer_samples[start];
203                                 buffer_samples[start] = buffer_samples[end];
204                                 buffer_samples[end] = temp;
205                         }
206                 }
207                 else
208                 {
209                         int offset = buffer->get_offset();
210                         buffer->set_offset(offset + i);
211                         read_samples(buffer,
212                                 0,
213                                 sample_rate,
214                                 start_position,
215                                 fragment_size);
216                         buffer->set_offset(offset);
217                 }
218
219                 if(get_direction() == PLAY_FORWARD)
220                         start_position += fragment_size;
221                 else
222                         start_position -= fragment_size;
223         }
224
225
226         return 0;
227 }
228
229
230
231
232 int ReverseAudio::load_configuration()
233 {
234         KeyFrame *prev_keyframe, *next_keyframe;
235         next_keyframe = get_next_keyframe(get_source_position());
236         prev_keyframe = get_prev_keyframe(get_source_position());
237         read_data(next_keyframe);
238 // Previous keyframe stays in config object.
239         read_data(prev_keyframe);
240
241         int64_t prev_position = edl_to_local(prev_keyframe->position);
242         int64_t next_position = edl_to_local(next_keyframe->position);
243
244 // printf("ReverseAudio::load_configuration 1 %lld %lld %lld %lld\n",
245 // prev_position,
246 // next_position,
247 // prev_keyframe->position,
248 // next_keyframe->position);
249 // Defeat default keyframe
250         if(prev_position == 0 && next_position == 0)
251         {
252                 next_position = prev_position = get_source_start();
253         }
254
255 // Get range to flip in requested rate
256         int64_t range_start = prev_position;
257         int64_t range_end = next_position;
258
259 // Between keyframe and edge of range or no keyframes
260         if(range_start == range_end)
261         {
262 // Between first keyframe and start of effect
263                 if(get_source_position() >= get_source_start() &&
264                         get_source_position() < range_start)
265                 {
266                         range_start = get_source_start();
267                 }
268                 else
269 // Between last keyframe and end of effect
270                 if(get_source_position() >= range_start &&
271                         get_source_position() < get_source_start() + get_total_len())
272                 {
273                         range_end = get_source_start() + get_total_len();
274                 }
275                 else
276                 {
277 // Should never get here
278                         ;
279                 }
280         }
281
282 // Convert start position to new direction
283         if(get_direction() == PLAY_FORWARD)
284         {
285 // Truncate next buffer to keyframe
286                 if(range_end - get_source_position() < fragment_size)
287                         fragment_size = range_end - get_source_position();
288                 input_position = get_source_position() - range_start;
289                 input_position = range_end - input_position - fragment_size;
290         }
291         else
292         {
293                 if(get_source_position() - range_start < fragment_size)
294                         fragment_size = get_source_position() - range_start;
295                 input_position = range_end - get_source_position();
296                 input_position = range_start + input_position + fragment_size;
297         }
298 // printf("ReverseAudio::load_configuration 20 start=%lld end=%lld current=%lld input=%lld\n",
299 // range_start,
300 // range_end,
301 // get_source_position(),
302 // input_position);
303
304         return 0;
305 }
306
307
308 void ReverseAudio::save_data(KeyFrame *keyframe)
309 {
310         FileXML output;
311
312 // cause data to be stored directly in text
313         output.set_shared_output(keyframe->get_data(), MESSAGESIZE);
314         output.tag.set_title("REVERSEAUDIO");
315         output.tag.set_property("ENABLED", config.enabled);
316         output.append_tag();
317         output.tag.set_title("/REVERSEAUDIO");
318         output.append_tag();
319         output.append_newline();
320         output.terminate_string();
321 }
322
323 void ReverseAudio::read_data(KeyFrame *keyframe)
324 {
325         FileXML input;
326
327         input.set_shared_input(keyframe->get_data(), strlen(keyframe->get_data()));
328
329         while(!input.read_tag())
330         {
331                 if(input.tag.title_is("REVERSEAUDIO"))
332                 {
333                         config.enabled = input.tag.get_property("ENABLED", config.enabled);
334                 }
335         }
336 }
337
338 void ReverseAudio::update_gui()
339 {
340         if(thread)
341         {
342                 load_configuration();
343                 thread->window->lock_window();
344                 ((ReverseAudioWindow*)thread->window)->enabled->update(config.enabled);
345                 thread->window->unlock_window();
346         }
347 }
348
349
350
351
352