port 7.2 mods: align_edits foreground plugin refresh_frame tweak, rework soundlevel...
[goodguy/cinelerra.git] / cinelerra-5.1 / plugins / level / leveleffect.C
index a81b357b981b3b75a507518ec8de9f3cdab056a1..45b47f1f2b15655798af3682cd1fa39b7a2a07f2 100644 (file)
@@ -27,6 +27,7 @@
 #include "language.h"
 #include "leveleffect.h"
 #include "samples.h"
+#include "transportque.inc"
 #include "units.h"
 #include "vframe.h"
 
 #include <string.h>
 #include <unistd.h>
 
-
-
-
-
-
-
-
-
-
 REGISTER_PLUGIN(SoundLevelEffect)
 
 
-
-
-
-
-
-
-
 SoundLevelConfig::SoundLevelConfig()
 {
        duration = 1.0;
@@ -69,29 +54,13 @@ int SoundLevelConfig::equivalent(SoundLevelConfig &that)
        return EQUIV(duration, that.duration);
 }
 
-void SoundLevelConfig::interpolate(SoundLevelConfig &prev,
-       SoundLevelConfig &next,
-       int64_t prev_frame,
-       int64_t next_frame,
-       int64_t current_frame)
+void SoundLevelConfig::interpolate(SoundLevelConfig &prev, SoundLevelConfig &next,
+       int64_t prev_frame, int64_t next_frame, int64_t current_frame)
 {
        duration = prev.duration;
 }
 
 
-
-
-
-
-
-
-
-
-
-
-
-
-
 SoundLevelDuration::SoundLevelDuration(SoundLevelEffect *plugin, int x, int y)
  : BC_FSlider(x, y, 0, xS(180), yS(180), 0.0, 10.0, plugin->config.duration)
 {
@@ -107,14 +76,8 @@ int SoundLevelDuration::handle_event()
 }
 
 
-
 SoundLevelWindow::SoundLevelWindow(SoundLevelEffect *plugin)
- : PluginClientWindow(plugin,
-       xS(350),
-       yS(120),
-       xS(350),
-       yS(120),
-       0)
+ : PluginClientWindow(plugin, xS(350), yS(120), xS(350), yS(120), 0)
 {
        this->plugin = plugin;
 }
@@ -142,33 +105,6 @@ void SoundLevelWindow::create_objects()
 }
 
 
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
 SoundLevelEffect::SoundLevelEffect(PluginServer *server)
  : PluginAClient(server)
 {
@@ -205,16 +141,9 @@ void SoundLevelEffect::read_data(KeyFrame *keyframe)
        input.set_shared_input(keyframe->xbuf);
 
        int result = 0;
-       while(!result)
-       {
-               result = input.read_tag();
-
-               if(!result)
-               {
-                       if(input.tag.title_is("SOUNDLEVEL"))
-                       {
-                               config.duration = input.tag.get_property("DURATION", config.duration);
-                       }
+       while( !(result = input.read_tag()) ) {
+               if( input.tag.title_is("SOUNDLEVEL") ) {
+                       config.duration = input.tag.get_property("DURATION", config.duration);
                }
        }
 }
@@ -236,57 +165,78 @@ void SoundLevelEffect::save_data(KeyFrame *keyframe)
 
 void SoundLevelEffect::update_gui()
 {
-//printf("SoundLevelEffect::update_gui 1\n");
-       if(thread)
-       {
-               load_configuration();
-               thread->window->lock_window();
-               ((SoundLevelWindow*)thread->window)->duration->update(config.duration);
-               thread->window->unlock_window();
+       if( !thread ) return;
+       SoundLevelWindow *window = (SoundLevelWindow*)thread->window;
+       if( !window ) return;
+       int reconfigure = load_configuration();
+       int pending = pending_gui_frame();
+       if( !reconfigure && !pending ) return;
+       window->lock_window();
+       if( reconfigure ) {
+               window->duration->update(config.duration);
        }
-//printf("SoundLevelEffect::update_gui 2\n");
+       if( pending ) {
+               double pos = get_tracking_position();
+               double dir = get_tracking_direction() == PLAY_REVERSE ? -1 : 1;
+               SoundLevelClientFrame *frame =
+                       (SoundLevelClientFrame *)get_gui_frame(pos, dir);
+               char string[BCSTRLEN];
+               sprintf(string, "%.2f", DB::todb(frame->max));
+               window->soundlevel_max->update(string);
+               sprintf(string, "%.2f", DB::todb(frame->rms));
+               window->soundlevel_rms->update(string);
+       }
+       window->unlock_window();
 }
 
 int SoundLevelEffect::process_realtime(int64_t size, Samples *input_ptr, Samples *output_ptr)
 {
        load_configuration();
 
+       int sample_rate = get_project_samplerate();
+       int fragment = config.duration * sample_rate;
+       int64_t position = get_source_position();
        accum_size += size;
        double *input_samples = input_ptr->get_data();
-       for(int i = 0; i < size; i++)
-       {
+       for( int i=0; i<size; ++i ) {
                double value = fabs(input_samples[i]);
                if(value > max_accum) max_accum = value;
                rms_accum += value * value;
-       }
-
-       if(accum_size > config.duration * PluginAClient::project_sample_rate)
-       {
-//printf("SoundLevelEffect::process_realtime 1 %f %d\n", rms_accum, accum_size);
-               rms_accum = sqrt(rms_accum / accum_size);
-               double arg[2];
-               arg[0] = max_accum;
-               arg[1] = rms_accum;
-               send_render_gui(arg, 2);
-               rms_accum = 0;
-               max_accum = 0;
-               accum_size = 0;
+               if( ++accum_size >= fragment ) {
+                       rms_accum = sqrt(rms_accum / accum_size);
+                       SoundLevelClientFrame *level_frame = new SoundLevelClientFrame();
+                       level_frame->max = max_accum;
+                       level_frame->rms = rms_accum;
+                       level_frame->position = (double)(position+i)/get_project_samplerate();
+                       add_gui_frame(level_frame);
+                       rms_accum = 0;
+                       max_accum = 0;
+                       accum_size = 0;
+               }
        }
        return 0;
 }
 
 void SoundLevelEffect::render_gui(void *data, int size)
 {
-       if(thread)
-       {
-               thread->window->lock_window();
-               char string[BCTEXTLEN];
-               double *arg = (double*)data;
-               sprintf(string, "%.2f", DB::todb(arg[0]));
-               ((SoundLevelWindow*)thread->window)->soundlevel_max->update(string);
-               sprintf(string, "%.2f", DB::todb(arg[1]));
-               ((SoundLevelWindow*)thread->window)->soundlevel_rms->update(string);
-               thread->window->flush();
-               thread->window->unlock_window();
-       }
+       if( !thread ) return;
+       SoundLevelWindow *window = (SoundLevelWindow*)thread->window;
+       if( !window ) return;
+       window->lock_window();
+       char string[BCTEXTLEN];
+       double *arg = (double*)data;
+       sprintf(string, "%.2f", DB::todb(arg[0]));
+       window->soundlevel_max->update(string);
+       sprintf(string, "%.2f", DB::todb(arg[1]));
+       window->soundlevel_rms->update(string);
+       window->flush();
+       window->unlock_window();
+}
+
+void SoundLevelEffect::render_stop()
+{
+       rms_accum = 0;
+       max_accum = 0;
+       accum_size = 0;
 }
+