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"
24 #include "mainprogress.h"
25 #include "normalize.h"
26 #include "normalizewindow.h"
35 #define _(String) gettext(String)
36 #define gettext_noop(String) String
37 #define N_(String) gettext_noop (String)
39 REGISTER_PLUGIN(NormalizeMain)
46 NormalizeMain::NormalizeMain(PluginServer *server)
47 : PluginAClient(server)
51 NormalizeMain::~NormalizeMain()
55 const char* NormalizeMain::plugin_title() { return N_("Normalize"); }
56 int NormalizeMain::is_realtime() { return 0; }
57 int NormalizeMain::is_multichannel() { return 1; }
60 int NormalizeMain::load_defaults()
62 char directory[BCTEXTLEN];
64 // set the default directory
65 sprintf(directory, "%snormalize.rc", BCASTDIR);
69 defaults = new BC_Hash(directory);
73 db_over = defaults->get("DBOVER", 0);
74 separate_tracks = defaults->get("SEPERATE_TRACKS", 1);
78 int NormalizeMain::save_defaults()
80 defaults->update("DBOVER", db_over);
81 defaults->update("SEPERATE_TRACKS", separate_tracks);
86 int NormalizeMain::get_parameters()
89 NormalizeWindow window(info.get_abs_cursor_x(), info.get_abs_cursor_y());
90 window.create_objects(&db_over, &separate_tracks);
91 int result = window.run_window();
95 int NormalizeMain::start_loop()
97 char string[BCTEXTLEN];
98 sprintf(string, "%s...", plugin_title());
99 progress = start_progress(string, (PluginClient::end - PluginClient::start) * 2);
102 current_position = PluginClient::start;
103 peak = new double[PluginClient::total_in_buffers];
104 scale = new double[PluginClient::total_in_buffers];
105 bzero(peak, sizeof(double) * PluginClient::total_in_buffers);
110 int NormalizeMain::process_loop(Samples **buffer, int64_t &write_length)
113 int64_t fragment_len;
115 //printf("NormalizeMain::process_loop 1\n");
118 fragment_len = PluginClient::in_buffer_size;
119 if(current_position + fragment_len > PluginClient::end) fragment_len = PluginClient::end - current_position;
120 //printf("NormalizeMain::process_loop 2 %d %f\n", current_position, scale[0]);
122 for(int i = 0; i < PluginClient::total_in_buffers; i++)
124 read_samples(buffer[i], i, current_position, fragment_len);
125 double *buffer_samples = buffer[i]->get_data();
126 for(int j = 0; j < fragment_len; j++)
127 buffer_samples[j] *= scale[i];
130 //printf("NormalizeMain::process_loop 1 %d %f\n", current_position, scale[0]);
131 current_position += fragment_len;
132 write_length = fragment_len;
133 result = progress->update(PluginClient::end -
134 PluginClient::start +
136 PluginClient::start);
137 if(current_position >= PluginClient::end) result = 1;
142 //printf("NormalizeMain::process_loop 4\n");
143 for(int i = PluginClient::start;
144 i < PluginClient::end && !result;
147 fragment_len = PluginClient::in_buffer_size;
148 if(i + fragment_len > PluginClient::end) fragment_len = PluginClient::end - i;
149 //printf("NormalizeMain::process_loop 5\n");
151 for(int j = 0; j < PluginClient::total_in_buffers; j++)
153 //printf("NormalizeMain::process_loop 6 %p\n", buffer);
154 read_samples(buffer[j], j, i, fragment_len);
155 //printf("NormalizeMain::process_loop 7\n");
156 double *buffer_samples = buffer[j]->get_data();
158 for(int k = 0; k < fragment_len; k++)
160 double sample = fabs(buffer_samples[k]);
165 //printf("NormalizeMain::process_loop 8\n");
166 result = progress->update(i - PluginClient::start);
167 //printf("NormalizeMain::process_loop 9\n");
170 // Normalize all tracks
172 for(int i = 0; i < PluginClient::total_in_buffers; i++)
174 if(peak[i] > max) max = peak[i];
178 for(int i = 0; i < PluginClient::total_in_buffers; i++)
184 for(int i = 0; i < PluginClient::total_in_buffers; i++)
186 scale[i] = DB::fromdb(db_over) / peak[i];
188 //printf("NormalizeMain::process_loop 10\n");
190 char string[BCTEXTLEN];
191 sprintf(string, "%s %.0f%%...", plugin_title(), (DB::fromdb(db_over) / max) * 100);
192 progress->update_title(string);
193 // Start writing on next iteration
200 int NormalizeMain::stop_loop()
202 progress->stop_progress();