edl plugin names eng, fix segv for opengl brender, renderfarm rework strategy, perf...
[goodguy/history.git] / cinelerra-5.1 / plugins / loopaudio / loopaudio.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 "clip.h"
24 #include "bchash.h"
25 #include "filexml.h"
26 #include "guicast.h"
27 #include "language.h"
28 #include "pluginaclient.h"
29 #include "samples.h"
30 #include "transportque.h"
31
32 #include <string.h>
33
34 class LoopAudio;
35
36 class LoopAudioConfig
37 {
38 public:
39         LoopAudioConfig();
40         int64_t samples;
41 };
42
43
44 class LoopAudioSamples : public BC_TextBox
45 {
46 public:
47         LoopAudioSamples(LoopAudio *plugin,
48                 int x,
49                 int y);
50         int handle_event();
51         LoopAudio *plugin;
52 };
53
54 class LoopAudioWindow : public PluginClientWindow
55 {
56 public:
57         LoopAudioWindow(LoopAudio *plugin);
58         ~LoopAudioWindow();
59         void create_objects();
60         LoopAudio *plugin;
61         LoopAudioSamples *samples;
62 };
63
64
65
66 class LoopAudio : public PluginAClient
67 {
68 public:
69         LoopAudio(PluginServer *server);
70         ~LoopAudio();
71
72         PLUGIN_CLASS_MEMBERS(LoopAudioConfig)
73
74         void save_data(KeyFrame *keyframe);
75         void read_data(KeyFrame *keyframe);
76         void update_gui();
77         int is_realtime();
78         int is_synthesis();
79         int process_buffer(int64_t size,
80                 Samples *buffer,
81                 int64_t start_position,
82                 int sample_rate);
83 };
84
85
86
87
88
89
90
91 REGISTER_PLUGIN(LoopAudio);
92
93
94
95 LoopAudioConfig::LoopAudioConfig()
96 {
97         samples = 48000;
98 }
99
100
101
102
103
104 LoopAudioWindow::LoopAudioWindow(LoopAudio *plugin)
105  : PluginClientWindow(plugin,
106         210,
107         160,
108         200,
109         160,
110         0)
111 {
112         this->plugin = plugin;
113 }
114
115 LoopAudioWindow::~LoopAudioWindow()
116 {
117 }
118
119 void LoopAudioWindow::create_objects()
120 {
121         int x = 10, y = 10;
122
123         add_subwindow(new BC_Title(x, y, _("Samples to loop:")));
124         y += 20;
125         add_subwindow(samples = new LoopAudioSamples(plugin,
126                 x,
127                 y));
128         show_window();
129         flush();
130 }
131
132
133
134
135
136
137
138
139
140 LoopAudioSamples::LoopAudioSamples(LoopAudio *plugin,
141         int x,
142         int y)
143  : BC_TextBox(x,
144         y,
145         100,
146         1,
147         plugin->config.samples)
148 {
149         this->plugin = plugin;
150         set_precision(2);
151 }
152
153 int LoopAudioSamples::handle_event()
154 {
155         plugin->config.samples = atol(get_text());
156         plugin->config.samples = MAX(1, plugin->config.samples);
157         plugin->send_configure_change();
158         return 1;
159 }
160
161
162
163
164
165
166
167
168
169 LoopAudio::LoopAudio(PluginServer *server)
170  : PluginAClient(server)
171 {
172
173 }
174
175
176 LoopAudio::~LoopAudio()
177 {
178
179 }
180
181 const char* LoopAudio::plugin_title() { return N_("Loop audio"); }
182 int LoopAudio::is_realtime() { return 1; }
183 int LoopAudio::is_synthesis() { return 1; }
184
185
186
187 NEW_WINDOW_MACRO(LoopAudio, LoopAudioWindow)
188
189
190 int LoopAudio::process_buffer(int64_t size,
191         Samples *buffer,
192         int64_t start_position,
193         int sample_rate)
194 {
195         int64_t current_position = start_position;
196         int step = (get_direction() == PLAY_FORWARD) ? 1 : -1;
197         int fragment_size;
198         int64_t current_loop_end;
199
200 //printf("LoopAudio::process_buffer 1 %lld %d\n", start_position, size);
201
202         current_position = start_position;
203
204         for(int i = 0; i < size; i += fragment_size)
205         {
206 // Truncate to end of buffer
207                 fragment_size = MIN(size - i, size);
208
209                 int64_t current_loop_position;
210
211 // Truncate to next keyframe
212                 if(get_direction() == PLAY_FORWARD)
213                 {
214                         KeyFrame *next_keyframe = get_next_keyframe(current_position);
215                         int64_t next_position = edl_to_local(next_keyframe->position);
216                         if(next_position > current_position)
217                                 fragment_size = MIN(fragment_size, next_position - current_position);
218
219 // Get start of current loop
220                         KeyFrame *prev_keyframe = get_prev_keyframe(current_position);
221                         int64_t prev_position = edl_to_local(prev_keyframe->position);
222                         if(prev_position == 0)
223                                 prev_position = get_source_start();
224                         read_data(prev_keyframe);
225
226 // Get start of fragment in current loop
227                         current_loop_position = prev_position +
228                                 ((current_position - prev_position) %
229                                         config.samples);
230                         while(current_loop_position < prev_position) current_loop_position += config.samples;
231                         while(current_loop_position >= prev_position + config.samples) current_loop_position -= config.samples;
232
233 // Truncate fragment to end of loop
234                         current_loop_end = current_position -
235                                 current_loop_position +
236                                 prev_position +
237                                 config.samples;
238                         fragment_size = MIN(current_loop_end - current_position,
239                                 fragment_size);
240                 }
241                 else
242                 {
243                         KeyFrame *next_keyframe = get_prev_keyframe(current_position);
244                         int64_t next_position = edl_to_local(next_keyframe->position);
245                         if(next_position < current_position)
246                                 fragment_size = MIN(fragment_size, current_position - next_position);
247
248                         KeyFrame *prev_keyframe = get_next_keyframe(current_position);
249                         int64_t prev_position = edl_to_local(prev_keyframe->position);
250                         if(prev_position == 0)
251                                 prev_position = get_source_start() + get_total_len();
252                         read_data(prev_keyframe);
253
254                         current_loop_position = prev_position -
255                                 ((prev_position - current_position) %
256                                           config.samples);
257                         while(current_loop_position <= prev_position - config.samples) current_loop_position += config.samples;
258                         while(current_loop_position > prev_position) current_loop_position -= config.samples;
259
260 // Truncate fragment to end of loop
261                         current_loop_end = current_position +
262                                 prev_position -
263                                 current_loop_position -
264                                 config.samples;
265                         fragment_size = MIN(current_position - current_loop_end,
266                                 fragment_size);
267                 }
268
269
270 // printf("LoopAudio::process_buffer 100 %lld %lld %lld %d\n",
271 // current_position, current_loop_position, current_loop_end, fragment_size);
272                 int offset = buffer->get_offset();
273                 buffer->set_offset(offset + i);
274                 read_samples(buffer,
275                         0,
276                         sample_rate,
277                         current_loop_position,
278                         fragment_size);
279                 buffer->set_offset(offset);
280
281
282                 current_position += step * fragment_size;
283         }
284
285
286         return 0;
287 }
288
289
290
291
292 int LoopAudio::load_configuration()
293 {
294         KeyFrame *prev_keyframe;
295         int64_t old_samples = config.samples;
296         prev_keyframe = get_prev_keyframe(get_source_position());
297         read_data(prev_keyframe);
298         return old_samples != config.samples;
299 }
300
301
302 void LoopAudio::save_data(KeyFrame *keyframe)
303 {
304         FileXML output;
305
306 // cause data to be stored directly in text
307         output.set_shared_output(keyframe->get_data(), MESSAGESIZE);
308         output.tag.set_title("LOOPAUDIO");
309         output.tag.set_property("SAMPLES", config.samples);
310         output.append_tag();
311         output.tag.set_title("/LOOPAUDIO");
312         output.append_tag();
313         output.append_newline();
314         output.terminate_string();
315 }
316
317 void LoopAudio::read_data(KeyFrame *keyframe)
318 {
319         FileXML input;
320
321         input.set_shared_input(keyframe->get_data(), strlen(keyframe->get_data()));
322
323         while(!input.read_tag())
324         {
325                 if(input.tag.title_is("LOOPAUDIO"))
326                 {
327                         config.samples = input.tag.get_property("SAMPLES", config.samples);
328                 }
329         }
330 }
331
332 void LoopAudio::update_gui()
333 {
334         if(thread)
335         {
336                 load_configuration();
337                 thread->window->lock_window();
338                 ((LoopAudioWindow*)thread->window)->samples->update(config.samples);
339                 thread->window->unlock_window();
340         }
341 }
342
343
344
345
346