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, 180, 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 //printf("SoundLevelWindow::create_objects 1\n");
128 add_subwindow(new BC_Title(x, y, _("Duration (seconds):")));
129 add_subwindow(duration = new SoundLevelDuration(plugin, x + 150, y));
131 add_subwindow(new BC_Title(x, y, _("Max soundlevel (dB):")));
132 add_subwindow(soundlevel_max = new BC_Title(x + 150, y, "0.0"));
134 add_subwindow(new BC_Title(x, y, _("RMS soundlevel (dB):")));
135 add_subwindow(soundlevel_rms = new BC_Title(x + 150, y, "0.0"));
139 //printf("SoundLevelWindow::create_objects 2\n");
170 SoundLevelEffect::SoundLevelEffect(PluginServer *server)
171 : PluginAClient(server)
177 SoundLevelEffect::~SoundLevelEffect()
183 LOAD_CONFIGURATION_MACRO(SoundLevelEffect, SoundLevelConfig)
185 NEW_WINDOW_MACRO(SoundLevelEffect, SoundLevelWindow)
189 void SoundLevelEffect::reset()
196 const char* SoundLevelEffect::plugin_title() { return N_("SoundLevel"); }
197 int SoundLevelEffect::is_realtime() { return 1; }
200 void SoundLevelEffect::read_data(KeyFrame *keyframe)
203 input.set_shared_input(keyframe->xbuf);
208 result = input.read_tag();
212 if(input.tag.title_is("SOUNDLEVEL"))
214 config.duration = input.tag.get_property("DURATION", config.duration);
220 void SoundLevelEffect::save_data(KeyFrame *keyframe)
223 output.set_shared_output(keyframe->xbuf);
225 output.tag.set_title("SOUNDLEVEL");
226 output.tag.set_property("DURATION", config.duration);
228 output.tag.set_title("/SOUNDLEVEL");
230 output.append_newline();
231 output.terminate_string();
235 void SoundLevelEffect::update_gui()
237 //printf("SoundLevelEffect::update_gui 1\n");
240 load_configuration();
241 thread->window->lock_window();
242 ((SoundLevelWindow*)thread->window)->duration->update(config.duration);
243 thread->window->unlock_window();
245 //printf("SoundLevelEffect::update_gui 2\n");
248 int SoundLevelEffect::process_realtime(int64_t size, Samples *input_ptr, Samples *output_ptr)
250 load_configuration();
253 double *input_samples = input_ptr->get_data();
254 for(int i = 0; i < size; i++)
256 double value = fabs(input_samples[i]);
257 if(value > max_accum) max_accum = value;
258 rms_accum += value * value;
261 if(accum_size > config.duration * PluginAClient::project_sample_rate)
263 //printf("SoundLevelEffect::process_realtime 1 %f %d\n", rms_accum, accum_size);
264 rms_accum = sqrt(rms_accum / accum_size);
268 send_render_gui(arg, 2);
276 void SoundLevelEffect::render_gui(void *data, int size)
280 thread->window->lock_window();
281 char string[BCTEXTLEN];
282 double *arg = (double*)data;
283 sprintf(string, "%.2f", DB::todb(arg[0]));
284 ((SoundLevelWindow*)thread->window)->soundlevel_max->update(string);
285 sprintf(string, "%.2f", DB::todb(arg[1]));
286 ((SoundLevelWindow*)thread->window)->soundlevel_rms->update(string);
287 thread->window->flush();
288 thread->window->unlock_window();