compressors: added mkup_gain reset, fixed smooth_only
[goodguy/cinelerra.git] / cinelerra-5.1 / plugins / audioscope / audioscope.C
1
2 /*
3  * CINELERRA
4  * Copyright (C) 1997-2011 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 "audioscope.h"
23 #include "bcdisplayinfo.h"
24 #include "bcsignals.h"
25 #include "clip.h"
26 #include "cursors.h"
27 #include "bchash.h"
28 #include "filexml.h"
29 #include "language.h"
30 #include "bccolors.h"
31 #include "samples.h"
32 #include "theme.h"
33 #include "transportque.inc"
34 #include "units.h"
35 #include "vframe.h"
36
37
38 #include <string.h>
39
40
41
42 REGISTER_PLUGIN(AudioScope)
43
44
45 AudioScopeConfig::AudioScopeConfig()
46 {
47         window_size = MAX_WINDOW;
48         history_size = 4;
49         mode = XY_MODE;
50         trigger_level = 0;
51 }
52
53 int AudioScopeConfig::equivalent(AudioScopeConfig &that)
54 {
55         return window_size == that.window_size &&
56                 history_size == that.history_size &&
57                 mode == that.mode &&
58                 EQUIV(trigger_level, that.trigger_level);
59 }
60
61 void AudioScopeConfig::copy_from(AudioScopeConfig &that)
62 {
63         window_size = that.window_size;
64         history_size = that.history_size;
65         mode = that.mode;
66         trigger_level = that.trigger_level;
67 }
68
69 void AudioScopeConfig::interpolate(AudioScopeConfig &prev,
70         AudioScopeConfig &next,
71         int64_t prev_frame,
72         int64_t next_frame,
73         int64_t current_frame)
74 {
75         copy_from(prev);
76
77         CLAMP(history_size, MIN_HISTORY, MAX_HISTORY - 1);
78 }
79
80
81
82 AudioScopeFrame::AudioScopeFrame(int data_size, int channels)
83 {
84         this->size = data_size;
85         this->channels = channels;
86         for(int i = 0; i < CHANNELS; i++)
87                 data[i] = new float[data_size];
88         force = 0;
89 }
90
91 AudioScopeFrame::~AudioScopeFrame()
92 {
93         for(int i = 0; i < CHANNELS; i++)
94                 delete [] data[i];
95 }
96
97
98
99
100
101
102
103 AudioScopeHistory::AudioScopeHistory(AudioScope *plugin,
104         int x,
105         int y)
106  : BC_IPot(x,
107         y,
108         plugin->config.history_size,
109         MIN_HISTORY,
110         MAX_HISTORY)
111 {
112         this->plugin = plugin;
113 }
114
115 int AudioScopeHistory::handle_event()
116 {
117         plugin->config.history_size = get_value();
118         plugin->send_configure_change();
119         return 1;
120 }
121
122
123
124
125
126
127
128
129 AudioScopeWindowSize::AudioScopeWindowSize(AudioScope *plugin,
130         int x,
131         int y,
132         char *text)
133  : BC_PopupMenu(x,
134         y,
135         xS(110),
136         text)
137 {
138         this->plugin = plugin;
139 }
140
141 int AudioScopeWindowSize::handle_event()
142 {
143         plugin->config.window_size = atoi(get_text());
144         plugin->send_configure_change();
145         return 1;
146 }
147
148
149 AudioScopeWindowSizeTumbler::AudioScopeWindowSizeTumbler(AudioScope *plugin, int x, int y)
150  : BC_Tumbler(x,
151         y)
152 {
153         this->plugin = plugin;
154 }
155
156 int AudioScopeWindowSizeTumbler::handle_up_event()
157 {
158         plugin->config.window_size *= 2;
159         if(plugin->config.window_size > MAX_WINDOW)
160                 plugin->config.window_size = MAX_WINDOW;
161         char string[BCTEXTLEN];
162         sprintf(string, "%d", plugin->config.window_size);
163         ((AudioScopeWindow*)plugin->get_thread()->get_window())->window_size->set_text(string);
164         plugin->send_configure_change();
165         return 1;
166 }
167
168 int AudioScopeWindowSizeTumbler::handle_down_event()
169 {
170         plugin->config.window_size /= 2;
171         if(plugin->config.window_size < MIN_WINDOW)
172                 plugin->config.window_size = MIN_WINDOW;
173         char string[BCTEXTLEN];
174         sprintf(string, "%d", plugin->config.window_size);
175         ((AudioScopeWindow*)plugin->get_thread()->get_window())->window_size->set_text(string);
176         plugin->send_configure_change();
177         return 1;
178 }
179
180
181
182
183
184 AudioScopeCanvas::AudioScopeCanvas(AudioScope *plugin,
185         int x,
186         int y,
187         int w,
188         int h)
189  : BC_SubWindow(x, y, w, h, BLACK)
190 {
191         this->plugin = plugin;
192         current_operation = NONE;
193 }
194
195 int AudioScopeCanvas::button_press_event()
196 {
197         if(is_event_win() && cursor_inside())
198         {
199                 calculate_point();
200                 current_operation = DRAG;
201                 plugin->send_configure_change();
202                 return 1;
203         }
204         return 0;
205 }
206
207 int AudioScopeCanvas::button_release_event()
208 {
209         if(current_operation == DRAG)
210         {
211                 current_operation = NONE;
212                 return 1;
213         }
214         return 0;
215 }
216
217 int AudioScopeCanvas::cursor_motion_event()
218 {
219         if(current_operation == DRAG)
220         {
221                 calculate_point();
222                 return 1;
223         }
224         return 0;
225 }
226
227
228 void AudioScopeCanvas::calculate_point()
229 {
230         int x = get_cursor_x();
231         int y = get_cursor_y();
232         CLAMP(x, 0, get_w() - 1);
233         CLAMP(y, 0, get_h() - 1);
234
235         ((AudioScopeWindow*)plugin->thread->window)->calculate_probe(x, y, 1);
236
237 //printf("AudioScopeCanvas::calculate_point %d %d\n", __LINE__, Freq::tofreq(freq_index));
238 }
239
240
241
242
243
244
245
246 AudioScopeTriggerLevel::AudioScopeTriggerLevel(AudioScope *plugin, int x, int y)
247  : BC_FPot(x, y, plugin->config.trigger_level, (float)-1.0, (float)1.0)
248 {
249         this->plugin = plugin;
250         set_precision(0.01);
251 }
252
253 int AudioScopeTriggerLevel::handle_event()
254 {
255         AudioScopeWindow *window = (AudioScopeWindow*)plugin->thread->window;
256         window->draw_trigger();
257         plugin->config.trigger_level = get_value();
258         plugin->send_configure_change();
259         window->draw_trigger();
260         window->canvas->flash();
261         return 1;
262 }
263
264
265
266
267
268 AudioScopeMode::AudioScopeMode(AudioScope *plugin,
269         int x,
270         int y)
271  : BC_PopupMenu(x,
272         y,
273         xS(180),
274         mode_to_text(plugin->config.mode))
275 {
276         this->plugin = plugin;
277 }
278
279 void AudioScopeMode::create_objects()
280 {
281         add_item(new BC_MenuItem(mode_to_text(XY_MODE)));
282         add_item(new BC_MenuItem(mode_to_text(WAVEFORM_NO_TRIGGER)));
283         add_item(new BC_MenuItem(mode_to_text(WAVEFORM_RISING_TRIGGER)));
284         add_item(new BC_MenuItem(mode_to_text(WAVEFORM_FALLING_TRIGGER)));
285 }
286
287 int AudioScopeMode::handle_event()
288 {
289         if(plugin->config.mode != text_to_mode(get_text()))
290         {
291                 AudioScopeWindow *window = (AudioScopeWindow*)plugin->thread->window;
292                 window->probe_x = -1;
293                 window->probe_y = -1;
294                 plugin->config.mode = text_to_mode(get_text());
295                 plugin->send_configure_change();
296
297                 window->canvas->clear_box(0, 0, window->canvas->get_w(), window->canvas->get_h());
298                 window->draw_overlay();
299                 window->canvas->flash();
300         }
301         return 1;
302 }
303
304 const char* AudioScopeMode::mode_to_text(int mode)
305 {
306         switch(mode)
307         {
308                 case XY_MODE:
309                         return _("XY Mode");
310                 case WAVEFORM_NO_TRIGGER:
311                         return _("Waveform");
312                 case WAVEFORM_RISING_TRIGGER:
313                         return _("Rising Trigger");
314                 case WAVEFORM_FALLING_TRIGGER:
315                 default:
316                         return _("Falling Trigger");
317         }
318 }
319
320 int AudioScopeMode::text_to_mode(const char *text)
321 {
322         if(!strcmp(mode_to_text(XY_MODE), text)) return XY_MODE;
323         if(!strcmp(mode_to_text(WAVEFORM_NO_TRIGGER), text)) return WAVEFORM_NO_TRIGGER;
324         if(!strcmp(mode_to_text(WAVEFORM_RISING_TRIGGER), text)) return WAVEFORM_RISING_TRIGGER;
325         if(!strcmp(mode_to_text(WAVEFORM_FALLING_TRIGGER), text)) return WAVEFORM_FALLING_TRIGGER;
326         return XY_MODE;
327 }
328
329
330
331
332
333
334
335
336
337 AudioScopeWindow::AudioScopeWindow(AudioScope *plugin)
338  : PluginClientWindow(plugin, plugin->w, plugin->h, xS(320), yS(320), 1)
339 {
340         this->plugin = plugin; 
341         probe_x = -1;
342         probe_y = -1;
343 }
344
345 AudioScopeWindow::~AudioScopeWindow()
346 {
347 }
348
349 void AudioScopeWindow::create_objects()
350 {
351         int x = plugin->get_theme()->widget_border;
352         int y = 0;
353         char string[BCTEXTLEN];
354
355         add_subwindow(canvas = new AudioScopeCanvas(plugin,
356                 0,
357                 0,
358                 get_w(),
359                 get_h() -
360                         plugin->get_theme()->widget_border * 4 -
361                         BC_Title::calculate_h(this, "X") * 3));
362         canvas->set_cursor(CROSS_CURSOR, 0, 0);
363         draw_overlay();
364
365         y += canvas->get_h() + plugin->get_theme()->widget_border;
366
367         add_subwindow(history_size_title = new BC_Title(x, y, _("History Size:")));
368         x += history_size_title->get_w() + plugin->get_theme()->widget_border;
369         add_subwindow(history_size = new AudioScopeHistory(plugin,
370                 x,
371                 y));
372         x += history_size->get_w() + plugin->get_theme()->widget_border;
373
374
375         int x1 = x;
376         sprintf(string, "%d", plugin->config.window_size);
377         add_subwindow(window_size_title = new BC_Title(x, y, _("Window Size:")));
378
379         x += window_size_title->get_w() + plugin->get_theme()->widget_border;
380         add_subwindow(window_size = new AudioScopeWindowSize(plugin, x, y, string));
381         x += window_size->get_w();
382         add_subwindow(window_size_tumbler = new AudioScopeWindowSizeTumbler(plugin, x, y));
383
384         for(int i = MIN_WINDOW; i <= MAX_WINDOW; i *= 2)
385         {
386                 sprintf(string, "%d", i);
387                 window_size->add_item(new BC_MenuItem(string));
388         }
389
390         x += window_size_tumbler->get_w() + plugin->get_theme()->widget_border;
391
392         int y1 = y;
393         x = x1;
394         y += window_size->get_h() + plugin->get_theme()->widget_border;
395         add_subwindow(mode_title = new BC_Title(x, y, _("Mode:")));
396
397         x += mode_title->get_w() + plugin->get_theme()->widget_border;
398         add_subwindow(mode = new AudioScopeMode(plugin, x, y));
399         mode->create_objects();
400
401         x += mode->get_w() + plugin->get_theme()->widget_border;
402         y = y1;
403         add_subwindow(trigger_level_title = new BC_Title(x, y, _("Trigger level:")));
404
405         x += trigger_level_title->get_w() + plugin->get_theme()->widget_border;
406         add_subwindow(trigger_level = new AudioScopeTriggerLevel(plugin, x, y));
407
408
409         x += trigger_level->get_w() + plugin->get_theme()->widget_border;
410         y = y1;
411         add_subwindow(probe_sample = new BC_Title(x, y, _("Sample: 0")));
412         y += probe_sample->get_h() + plugin->get_theme()->widget_border;
413         add_subwindow(probe_level0 = new BC_Title(x, y, _("Level 0: 0"), MEDIUMFONT, CHANNEL0_COLOR));
414         y += probe_level0->get_h() + plugin->get_theme()->widget_border;
415         add_subwindow(probe_level1 = new BC_Title(x, y, _("Level 1: 0"), MEDIUMFONT, CHANNEL1_COLOR));
416         y += probe_level1->get_h() + plugin->get_theme()->widget_border;
417
418
419
420         show_window();
421 }
422
423 int AudioScopeWindow::resize_event(int w, int h)
424 {
425         int y_diff = h - get_h();
426
427         int canvas_factor = get_h() - canvas->get_h();
428         canvas->reposition_window(0,
429                 0,
430                 w,
431                 h - canvas_factor);
432         canvas->clear_box(0, 0, canvas->get_w(), canvas->get_h());
433         draw_overlay();
434         canvas->flash(0);
435
436
437 // Remove all columns which may be a different size.
438         plugin->frame_buffer.remove_all_objects();
439
440         window_size_title->reposition_window(window_size_title->get_x(),
441                 window_size_title->get_y() + y_diff);
442         window_size->reposition_window(window_size->get_x(),
443                 window_size->get_y() + y_diff);
444         window_size_tumbler->reposition_window(window_size_tumbler->get_x(),
445                 window_size_tumbler->get_y() + y_diff);
446
447
448
449         history_size_title->reposition_window(history_size_title->get_x(),
450                 history_size_title->get_y() + y_diff);
451         history_size->reposition_window(history_size->get_x(),
452                 history_size->get_y() + y_diff);
453
454         mode_title->reposition_window(mode_title->get_x(),
455                 mode_title->get_y() + y_diff);
456         mode->reposition_window(mode->get_x(),
457                 mode->get_y() + y_diff);
458
459
460         trigger_level_title->reposition_window(trigger_level_title->get_x(),
461                 trigger_level_title->get_y() + y_diff);
462         trigger_level->reposition_window(trigger_level->get_x(),
463                 trigger_level->get_y() + y_diff);
464
465
466         probe_sample->reposition_window(probe_sample->get_x(),
467                 probe_sample->get_y() + y_diff);
468         probe_level0->reposition_window(probe_level0->get_x(),
469                 probe_level0->get_y() + y_diff);
470         probe_level1->reposition_window(probe_level1->get_x(),
471                 probe_level1->get_y() + y_diff);
472
473         flush();
474
475         plugin->w = w;
476         plugin->h = h;
477         return 0;
478 }
479
480 void AudioScopeWindow::draw_overlay()
481 {
482         canvas->set_color(GREEN);
483
484         canvas->draw_line(0,
485                 canvas->get_h() / 2,
486                 canvas->get_w(),
487                 canvas->get_h() / 2);
488         if(plugin->config.mode == XY_MODE)
489         {
490                 canvas->draw_line(canvas->get_w() / 2,
491                         0,
492                         canvas->get_w() / 2,
493                         canvas->get_h());
494         }
495
496         draw_trigger();
497         draw_probe();
498 //printf("AudioScopeWindow::draw_overlay %d\n", __LINE__);
499 }
500
501 void AudioScopeWindow::draw_trigger()
502 {
503         if(plugin->config.mode == WAVEFORM_RISING_TRIGGER ||
504                 plugin->config.mode == WAVEFORM_FALLING_TRIGGER)
505         {
506                 int y = (int)(-plugin->config.trigger_level *
507                         canvas->get_h() / 2 +
508                         canvas->get_h() / 2);
509                 CLAMP(y, 0, canvas->get_h() - 1);
510                 canvas->set_inverse();
511                 canvas->set_color(RED);
512                 canvas->draw_line(0, y, canvas->get_w(), y);
513                 canvas->set_opaque();
514         }
515 }
516
517 void AudioScopeWindow::draw_probe()
518 {
519         if(probe_x >= 0 || probe_y >= 0)
520         {
521                 canvas->set_color(GREEN);
522                 canvas->set_inverse();
523
524                 if(plugin->config.mode == XY_MODE)
525                 {
526                         canvas->draw_line(0, probe_y, get_w(), probe_y);
527                         canvas->draw_line(probe_x, 0, probe_x, get_h());
528                 }
529                 else
530                 {
531                         canvas->draw_line(probe_x, 0, probe_x, get_h());
532                 }
533
534                 canvas->set_opaque();
535         }
536 }
537
538
539
540
541 void AudioScopeWindow::calculate_probe(int x, int y, int do_overlay)
542 {
543         if(x < 0 && y < 0) return;
544
545 // Clear previous overlay
546         if(do_overlay) draw_probe();
547
548 // New probe position
549         probe_x = x;
550         probe_y = y;
551
552         int sample = 0;
553         double channel0 = 0;
554         double channel1 = 0;
555         if(plugin->config.mode == XY_MODE)
556         {
557                 channel0 = (float)(probe_x - canvas->get_w() / 2) / (canvas->get_w() / 2);
558                 channel1 = (float)(canvas->get_h() / 2 - probe_y) / (canvas->get_h() / 2);
559         }
560         else
561         if(plugin->current_frame)
562         {
563                 sample = probe_x * plugin->current_frame->size / canvas->get_w();
564                 CLAMP(sample, 0, plugin->current_frame->size - 1);
565                 channel0 = plugin->current_frame->data[0][sample];
566                 channel1 = plugin->current_frame->data[1][sample];
567         }
568
569
570         char string[BCTEXTLEN];
571         sprintf(string, _("Sample: %d"), sample);
572         probe_sample->update(string);
573
574         sprintf(string, _("Level 0: %.2f"), channel0);
575         probe_level0->update(string);
576
577         sprintf(string, _("Level 1: %.2f"), channel1);
578         probe_level1->update(string);
579
580
581         if(do_overlay)
582         {
583                 draw_probe();
584                 canvas->flash();
585         }
586 }
587
588
589
590
591
592 void AudioScopeWindow::update_gui()
593 {
594         char string[BCTEXTLEN];
595         sprintf(string, "%d", plugin->config.window_size);
596         window_size->set_text(string);
597         history_size->update(plugin->config.history_size);
598 }
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624 AudioScope::AudioScope(PluginServer *server)
625  : PluginAClient(server)
626 {
627         reset();
628         timer = new Timer;
629         w = xS(640);
630         h = yS(480);
631 }
632
633 AudioScope::~AudioScope()
634 {
635         delete [] data;
636         for(int i = 0; i < CHANNELS; i++)
637                 delete audio_buffer[i];
638         delete timer;
639         frame_buffer.remove_all_objects();
640         frame_history.remove_all_objects();
641 }
642
643
644 void AudioScope::reset()
645 {
646         thread = 0;
647         data = 0;
648         for(int i = 0; i < CHANNELS; i++)
649                 audio_buffer[i] = 0;
650         allocated_data = 0;
651         total_windows = 0;
652         buffer_size = 0;
653         bzero(&header, sizeof(data_header_t));
654         current_frame = 0;
655 }
656
657
658 const char* AudioScope::plugin_title() { return N_("AudioScope"); }
659 int AudioScope::is_realtime() { return 1; }
660 int AudioScope::is_multichannel() { return 1; }
661
662 int AudioScope::process_buffer(int64_t size,
663                 Samples **buffer,
664                 int64_t start_position,
665                 int sample_rate)
666 {
667         int channels = MIN(get_total_buffers(), CHANNELS);
668
669 // Pass through
670         for(int i = 0; i < get_total_buffers(); i++)
671         {
672                 read_samples(buffer[i],
673                         i,
674                         sample_rate,
675                         start_position,
676                         size);
677         }
678
679         load_configuration();
680
681 // Reset audio buffer
682         if(window_size != config.window_size)
683         {
684                 render_stop();
685                 window_size = config.window_size;
686         }
687
688         if(!data)
689         {
690                 data = new unsigned char[sizeof(data_header_t)];
691                 allocated_data = 0;
692         }
693
694         if(!audio_buffer[0])
695         {
696                 for(int i = 0; i < CHANNELS; i++)
697                 {
698                         audio_buffer[i] = new Samples(MAX_WINDOW);
699                 }
700         }
701
702 // Accumulate audio
703         int needed = buffer_size + size;
704         if(audio_buffer[0]->get_allocated() < needed)
705         {
706                 for(int i = 0; i < CHANNELS; i++)
707                 {
708                         Samples *new_samples = new Samples(needed);
709                         memcpy(new_samples->get_data(),
710                                 audio_buffer[i]->get_data(),
711                                 sizeof(double) * buffer_size);
712                         delete audio_buffer[i];
713                         audio_buffer[i] = new_samples;
714                 }
715         }
716
717         for(int i = 0; i < CHANNELS; i++)
718         {
719                 memcpy(audio_buffer[i]->get_data() + buffer_size,
720                         buffer[MIN(i, channels - 1)]->get_data(),
721                         sizeof(double) * size);
722         }
723         buffer_size += size;
724
725
726 // Append a windows to end of GUI buffer
727         total_windows = 0;
728         while(buffer_size >= window_size)
729         {
730                 if(allocated_data < (total_windows + 1) * window_size * CHANNELS)
731                 {
732                         int new_allocation = (total_windows + 1) *
733                                 window_size *
734                                 CHANNELS;
735                         unsigned char *new_data = new unsigned char[sizeof(data_header_t) +
736                                 sizeof(float) * new_allocation];
737                         data_header_t *new_header = (data_header_t*)new_data;
738                         data_header_t *old_header = (data_header_t*)data;
739                         memcpy(new_header->samples,
740                                 old_header->samples,
741                                 sizeof(float) * allocated_data);
742                         delete [] data;
743                         data = new_data;
744                         allocated_data = new_allocation;
745                 }
746
747 // Search for trigger
748                 int need_trigger = 0;
749                 int got_trigger = 0;
750                 int trigger_sample = 0;
751 //printf("AudioScope::process_buffer %d\n", __LINE__);
752                 if(config.mode == WAVEFORM_RISING_TRIGGER)
753                 {
754                         need_trigger = 1;
755                         double *trigger_data = audio_buffer[0]->get_data();
756                         for(int i = 1; i < buffer_size - window_size; i++)
757                         {
758                                 if(trigger_data[i - 1] < config.trigger_level &&
759                                         trigger_data[i] >= config.trigger_level)
760                                 {
761                                         got_trigger = 1;
762                                         trigger_sample = i;
763                                         break;
764                                 }
765                         }
766                 }
767                 else
768                 if(config.mode == WAVEFORM_FALLING_TRIGGER)
769                 {
770                         need_trigger = 1;
771                         double *trigger_data = audio_buffer[0]->get_data();
772                         for(int i = 1; i < buffer_size - window_size; i++)
773                         {
774                                 if(trigger_data[i - 1] > config.trigger_level &&
775                                         trigger_data[i] <= config.trigger_level)
776                                 {
777                                         got_trigger = 1;
778                                         trigger_sample = i;
779                                         break;
780                                 }
781                         }
782                 }
783
784 //printf("AudioScope::process_buffer %d\n", __LINE__);
785 // Flush buffer
786                 if(need_trigger && !got_trigger)
787                 {
788 //printf("AudioScope::process_buffer %d\n", __LINE__);
789                         for(int j = 0; j < CHANNELS; j++)
790                         {
791                                 double *sample_input = audio_buffer[j]->get_data();
792                                 memcpy(sample_input,
793                                         sample_input + buffer_size - window_size,
794                                         sizeof(double) * window_size);
795                         }
796
797                         buffer_size = window_size;
798                         if(buffer_size == window_size) break;
799 //printf("AudioScope::process_buffer %d\n", __LINE__);
800                 }
801                 else
802                 {
803                         data_header_t *header = (data_header_t*)data;
804 //printf("AudioScope::process_buffer %d\n", __LINE__);
805                         for(int j = 0; j < CHANNELS; j++)
806                         {
807                                 float *sample_output = header->samples +
808                                         total_windows * CHANNELS * window_size +
809                                         window_size * j;
810                                 double *sample_input = audio_buffer[j]->get_data();
811                                 for(int i = 0; i < window_size; i++)
812                                 {
813                                         sample_output[i] = sample_input[i + trigger_sample];
814                                 }
815
816 // Shift accumulation buffer
817 //printf("AudioScope::process_buffer %d\n", __LINE__);
818                                 memcpy(sample_input,
819                                         sample_input + trigger_sample + window_size,
820                                         sizeof(double) * (buffer_size - trigger_sample - window_size));
821 //printf("AudioScope::process_buffer %d\n", __LINE__);
822                         }
823
824 //printf("AudioScope::process_buffer %d\n", __LINE__);
825
826                         total_windows++;
827                         buffer_size -= window_size + trigger_sample;
828 //printf("AudioScope::process_buffer %d\n", __LINE__);
829                 }
830         }
831
832         data_header_t *header = (data_header_t*)data;
833         header->window_size = window_size;
834         header->sample_rate = sample_rate;
835         header->channels = channels;
836         header->total_windows = total_windows;
837
838         send_render_gui(data,
839                 sizeof(data_header_t) +
840                         sizeof(float) * total_windows * window_size * CHANNELS);
841
842         return 0;
843 }
844
845 void AudioScope::render_stop()
846 {
847         buffer_size = 0;
848         frame_buffer.remove_all_objects();
849 }
850
851
852
853
854 NEW_WINDOW_MACRO(AudioScope, AudioScopeWindow)
855
856 void AudioScope::update_gui()
857 {
858         if(thread)
859         {
860                 int result = load_configuration();
861                 AudioScopeWindow *window = (AudioScopeWindow*)thread->get_window();
862                 window->lock_window("AudioScope::update_gui");
863                 if(result) window->update_gui();
864
865 // Shift in accumulated frames
866                 if(frame_buffer.size())
867                 {
868                         BC_SubWindow *canvas = window->canvas;
869 // Frames to draw in this iteration
870                         int total_frames = timer->get_difference() *
871                                 header.sample_rate /
872                                 header.window_size /
873                                 1000;
874                         if(total_frames) timer->subtract(total_frames *
875                                 header.window_size *
876                                 1000 /
877                                 header.sample_rate);
878
879 // Add forced frame drawing
880                         for(int i = 0; i < frame_buffer.size(); i++)
881                                 if(frame_buffer.get(i)->force) total_frames++;
882                         total_frames = MIN(frame_buffer.size(), total_frames);
883 //printf("AudioScope::update_gui %d %d\n", __LINE__, total_frames);
884
885
886 // expire old history frames
887                         int new_frames = frame_history.size() + total_frames;
888                         int expired = new_frames - config.history_size;
889                         if( expired > 0 )
890                                 frame_history.remove_object_block(0, expired);
891 // Shift frames into history
892                         for(int frame = 0; frame < total_frames; frame++)
893                                 frame_history.append(frame_buffer.get(frame));
894                         frame_buffer.remove_block(0, total_frames);
895
896 // Point probe data at history
897                         current_frame = frame_history.get(frame_history.size() - 1);
898
899 // Draw frames from history
900                         canvas->clear_box(0, 0, canvas->get_w(), canvas->get_h());
901                         for(int frame = 0; frame < frame_history.size(); frame++)
902                         {
903                                 AudioScopeFrame *ptr = frame_history.get(frame);
904
905                                 int luma = (frame + 1) * 0x80 / frame_history.size();
906                                 if(frame == frame_history.size() - 1)
907                                 {
908                                         canvas->set_color(WHITE);
909                                         canvas->set_line_width(2);
910                                 }
911                                 else
912                                 {
913                                         canvas->set_color((luma << 16) |
914                                                 (luma << 8) |
915                                                 luma);
916                                 }
917
918                                 int x1 = 0;
919                                 int y1 = 0;
920                                 int w = canvas->get_w();
921                                 int h = canvas->get_h();
922                                 float *channel0 = ptr->data[0];
923                                 float *channel1 = ptr->data[1];
924
925                                 if(config.mode == XY_MODE)
926                                 {
927
928                                         int number = 0;
929                                         for(int point = 0; point < ptr->size; point++)
930                                         {
931                                                 int x2 = (int)(channel0[point] * w / 2 + w / 2);
932                                                 int y2 = (int)(-channel1[point] * h / 2 + h / 2);
933                                                 CLAMP(x2, 0, w - 1);
934                                                 CLAMP(y2, 0, h - 1);
935
936                                                 if(number)
937                                                 {
938                                                         canvas->draw_line(x1, y1, x2, y2);
939                                                 }
940                                                 else
941                                                 {
942                                                         number++;
943                                                 }
944
945                                                 x1 = x2;
946                                                 y1 = y2;
947                                         }
948                                 }
949                                 else
950                                 {
951                                         for(int channel = ptr->channels - 1; channel >= 0; channel--)
952                                         {
953                                                 if(channel > 0)
954                                                 {
955                                                         if(frame == frame_history.size() - 1)
956                                                         {
957                                                                 canvas->set_color(PINK);
958                                                                 canvas->set_line_width(2);
959                                                         }
960                                                         else
961                                                                 canvas->set_color(((luma * 0xff / 0xff) << 16) |
962                                                                         ((luma * 0x80 / 0xff) << 8) |
963                                                                         ((luma * 0x80 / 0xff)));
964                                                 }
965                                                 else
966                                                 {
967                                                         if(frame == frame_history.size() - 1)
968                                                                 canvas->set_color(WHITE);
969                                                         else
970                                                                 canvas->set_color((luma << 16) |
971                                                                         (luma << 8) |
972                                                                         luma);
973                                                 }
974
975                                                 if(ptr->size < w)
976                                                 {
977                                                         for(int point = 0; point < ptr->size; point++)
978                                                         {
979                                                                 int x2 = point * w / ptr->size;
980                                                                 int y2 = (int)(-ptr->data[channel][point] * h / 2 + h / 2);
981                                                                 CLAMP(y2, 0, h - 1);
982                                                                 if(point > 0)
983                                                                 {
984                                                                         canvas->draw_line(x1, y1, x2, y2);
985                                                                 }
986
987                                                                 x1 = x2;
988                                                                 y1 = y2;
989                                                         }
990                                                 }
991                                                 else
992                                                 {
993                                                         for(int x2 = 0; x2 < w; x2++)
994                                                         {
995                                                                 int sample1 = x2 * ptr->size / w;
996                                                                 int sample2 = (x2 + 1) * ptr->size / w;
997                                                                 double min = ptr->data[channel][sample1];
998                                                                 double max = ptr->data[channel][sample1];
999                                                                 for(int i = sample1 + 1; i < sample2; i++)
1000                                                                 {
1001                                                                         double value = ptr->data[channel][i];
1002                                                                         if(value < min) min = value;
1003                                                                         if(value > max) max = value;
1004                                                                 }
1005
1006                                                                 int min2 = (int)(-min * h / 2 + h / 2);
1007                                                                 int max2 = (int)(-max * h / 2 + h / 2);
1008                                                                 CLAMP(min2, 0, h - 1);
1009                                                                 CLAMP(max2, 0, h - 1);
1010                                                                 int y2 = (min2 + max2) / 2;
1011                                                                 canvas->draw_line(x2, min2, x2, max2);
1012                                                                 if(x2 > 0)
1013                                                                 {
1014                                                                         canvas->draw_line(x2, y1, x2, y2);
1015                                                                 }
1016
1017                                                                 y1 = y2;
1018                                                         }
1019                                                 }
1020                                         }
1021                                 }
1022
1023                                 canvas->set_line_width(1);
1024                         }
1025
1026 // Recompute probe level
1027                         window->calculate_probe(window->probe_x, window->probe_y, 0);
1028
1029
1030 // Draw overlay
1031                         window->draw_overlay();
1032                         canvas->flash();
1033                 }
1034
1035
1036
1037                 while(frame_buffer.size() > MAX_COLUMNS)
1038                         frame_buffer.remove_object_number(0);
1039
1040                 thread->get_window()->unlock_window();
1041         }
1042 }
1043
1044 void AudioScope::render_gui(void *data, int size)
1045 {
1046         if(thread)
1047         {
1048                 thread->get_window()->lock_window("AudioScope::render_gui");
1049                 data_header_t *header = (data_header_t*)data;
1050                 memcpy(&this->header, header, sizeof(data_header_t));
1051                 //BC_SubWindow *canvas = ((AudioScopeWindow*)thread->get_window())->canvas;
1052                 //int h = canvas->get_h();
1053
1054 // Set all previous frames to draw immediately
1055                 for(int i = 0; i < frame_buffer.size(); i++)
1056                         frame_buffer.get(i)->force = 1;
1057
1058                 for(int current_fragment = 0;
1059                         current_fragment < header->total_windows;
1060                         current_fragment++)
1061                 {
1062                         float *in_frame = header->samples +
1063                                 current_fragment * header->window_size * CHANNELS;
1064                         AudioScopeFrame *out_frame = new AudioScopeFrame(
1065                                 header->window_size,
1066                                 header->channels);
1067
1068 // Copy the window to the frame
1069                         for(int j = 0; j < CHANNELS; j++)
1070                         {
1071                                 for(int i = 0; i < header->window_size; i++)
1072                                 {
1073                                         out_frame->data[j][i] = in_frame[header->window_size * j + i];
1074                                 }
1075                         }
1076
1077                         frame_buffer.append(out_frame);
1078                         total_windows++;
1079                 }
1080
1081                 timer->update();
1082                 thread->get_window()->unlock_window();
1083         }
1084 }
1085
1086
1087
1088
1089
1090
1091
1092
1093 LOAD_CONFIGURATION_MACRO(AudioScope, AudioScopeConfig)
1094
1095 void AudioScope::read_data(KeyFrame *keyframe)
1096 {
1097         FileXML input;
1098         input.set_shared_input(keyframe->xbuf);
1099
1100         int result = 0;
1101         while(!result)
1102         {
1103                 result = input.read_tag();
1104
1105                 if(!result)
1106                 {
1107                         if(input.tag.title_is("AUDIOSCOPE"))
1108                         {
1109                                 if(is_defaults())
1110                                 {
1111                                         w = input.tag.get_property("W", w);
1112                                         h = input.tag.get_property("H", h);
1113                                 }
1114
1115                                 config.history_size = input.tag.get_property("HISTORY_SIZE", config.history_size);
1116                                 config.window_size = input.tag.get_property("WINDOW_SIZE", config.window_size);
1117                                 config.mode = input.tag.get_property("MODE", config.mode);
1118                                 config.trigger_level = input.tag.get_property("TRIGGER_LEVEL", config.trigger_level);
1119                         }
1120                 }
1121         }
1122 }
1123
1124 void AudioScope::save_data(KeyFrame *keyframe)
1125 {
1126         FileXML output;
1127         output.set_shared_output(keyframe->xbuf);
1128
1129 //printf("AudioScope::save_data %d %d %d\n", __LINE__, config.w, config.h);
1130         output.tag.set_title("AUDIOSCOPE");
1131         output.tag.set_property("W", w);
1132         output.tag.set_property("H", h);
1133         output.tag.set_property("HISTORY_SIZE", config.history_size);
1134         output.tag.set_property("WINDOW_SIZE", (int)config.window_size);
1135         output.tag.set_property("MODE", (int)config.mode);
1136         output.tag.set_property("TRIGGER_LEVEL", (float)config.trigger_level);
1137         output.append_tag();
1138         output.tag.set_title("/AUDIOSCOPE");
1139         output.append_tag();
1140         output.append_newline();
1141         output.terminate_string();
1142 }
1143
1144
1145
1146
1147