4 * Copyright (C) 2008 Adam Williams <broadcast at earthling dot net>
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.
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.
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
22 #include "bcdisplayinfo.h"
25 #include "filesystem.h"
28 #include "leveleffect.h"
47 REGISTER_PLUGIN(SoundLevelEffect)
57 SoundLevelConfig::SoundLevelConfig()
62 void SoundLevelConfig::copy_from(SoundLevelConfig &that)
64 duration = that.duration;
67 int SoundLevelConfig::equivalent(SoundLevelConfig &that)
69 return EQUIV(duration, that.duration);
72 void SoundLevelConfig::interpolate(SoundLevelConfig &prev,
73 SoundLevelConfig &next,
76 int64_t current_frame)
78 duration = prev.duration;
95 SoundLevelDuration::SoundLevelDuration(SoundLevelEffect *plugin, int x, int y)
96 : BC_FSlider(x, y, 0, xS(180), yS(180), 0.0, 10.0, plugin->config.duration)
98 this->plugin = plugin;
102 int SoundLevelDuration::handle_event()
104 plugin->config.duration = get_value();
105 plugin->send_configure_change();
111 SoundLevelWindow::SoundLevelWindow(SoundLevelEffect *plugin)
112 : PluginClientWindow(plugin,
119 this->plugin = plugin;
122 void SoundLevelWindow::create_objects()
124 int xs10 = xS(10), xs150 = xS(150);
125 int ys10 = yS(10), ys35 = yS(35);
126 //printf("SoundLevelWindow::create_objects 1\n");
127 int x = xs10, y = ys10;
130 add_subwindow(new BC_Title(x, y, _("Duration (seconds):")));
131 add_subwindow(duration = new SoundLevelDuration(plugin, x + xs150, y));
133 add_subwindow(new BC_Title(x, y, _("Max soundlevel (dB):")));
134 add_subwindow(soundlevel_max = new BC_Title(x + xs150, y, "0.0"));
136 add_subwindow(new BC_Title(x, y, _("RMS soundlevel (dB):")));
137 add_subwindow(soundlevel_rms = new BC_Title(x + xs150, y, "0.0"));
141 //printf("SoundLevelWindow::create_objects 2\n");
172 SoundLevelEffect::SoundLevelEffect(PluginServer *server)
173 : PluginAClient(server)
179 SoundLevelEffect::~SoundLevelEffect()
185 LOAD_CONFIGURATION_MACRO(SoundLevelEffect, SoundLevelConfig)
187 NEW_WINDOW_MACRO(SoundLevelEffect, SoundLevelWindow)
191 void SoundLevelEffect::reset()
198 const char* SoundLevelEffect::plugin_title() { return N_("SoundLevel"); }
199 int SoundLevelEffect::is_realtime() { return 1; }
202 void SoundLevelEffect::read_data(KeyFrame *keyframe)
205 input.set_shared_input(keyframe->xbuf);
210 result = input.read_tag();
214 if(input.tag.title_is("SOUNDLEVEL"))
216 config.duration = input.tag.get_property("DURATION", config.duration);
222 void SoundLevelEffect::save_data(KeyFrame *keyframe)
225 output.set_shared_output(keyframe->xbuf);
227 output.tag.set_title("SOUNDLEVEL");
228 output.tag.set_property("DURATION", config.duration);
230 output.tag.set_title("/SOUNDLEVEL");
232 output.append_newline();
233 output.terminate_string();
237 void SoundLevelEffect::update_gui()
239 //printf("SoundLevelEffect::update_gui 1\n");
242 load_configuration();
243 thread->window->lock_window();
244 ((SoundLevelWindow*)thread->window)->duration->update(config.duration);
245 thread->window->unlock_window();
247 //printf("SoundLevelEffect::update_gui 2\n");
250 int SoundLevelEffect::process_realtime(int64_t size, Samples *input_ptr, Samples *output_ptr)
252 load_configuration();
255 double *input_samples = input_ptr->get_data();
256 for(int i = 0; i < size; i++)
258 double value = fabs(input_samples[i]);
259 if(value > max_accum) max_accum = value;
260 rms_accum += value * value;
263 if(accum_size > config.duration * PluginAClient::project_sample_rate)
265 //printf("SoundLevelEffect::process_realtime 1 %f %d\n", rms_accum, accum_size);
266 rms_accum = sqrt(rms_accum / accum_size);
270 send_render_gui(arg, 2);
278 void SoundLevelEffect::render_gui(void *data, int size)
282 thread->window->lock_window();
283 char string[BCTEXTLEN];
284 double *arg = (double*)data;
285 sprintf(string, "%.2f", DB::todb(arg[0]));
286 ((SoundLevelWindow*)thread->window)->soundlevel_max->update(string);
287 sprintf(string, "%.2f", DB::todb(arg[1]));
288 ((SoundLevelWindow*)thread->window)->soundlevel_rms->update(string);
289 thread->window->flush();
290 thread->window->unlock_window();