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"
26 #include "mainprogress.h"
27 #include "normalize.h"
28 #include "normalizewindow.h"
37 REGISTER_PLUGIN(NormalizeMain)
44 NormalizeMain::NormalizeMain(PluginServer *server)
45 : PluginAClient(server)
49 NormalizeMain::~NormalizeMain()
53 const char* NormalizeMain::plugin_title() { return N_("Normalize"); }
54 int NormalizeMain::is_realtime() { return 0; }
55 int NormalizeMain::is_multichannel() { return 1; }
58 int NormalizeMain::load_defaults()
60 char directory[BCTEXTLEN];
62 // set the default directory
63 sprintf(directory, "%s/normalize.rc", File::get_config_path());
67 defaults = new BC_Hash(directory);
71 db_over = defaults->get("DBOVER", 0);
72 separate_tracks = defaults->get("SEPERATE_TRACKS", 1);
76 int NormalizeMain::save_defaults()
78 defaults->update("DBOVER", db_over);
79 defaults->update("SEPERATE_TRACKS", separate_tracks);
84 int NormalizeMain::get_parameters()
87 NormalizeWindow window(info.get_abs_cursor_x(), info.get_abs_cursor_y());
88 window.create_objects(&db_over, &separate_tracks);
89 int result = window.run_window();
93 int NormalizeMain::start_loop()
95 char string[BCTEXTLEN];
96 sprintf(string, "%s...", plugin_title());
97 progress = start_progress(string, (PluginClient::end - PluginClient::start) * 2);
100 current_position = PluginClient::start;
101 peak = new double[PluginClient::total_in_buffers];
102 scale = new double[PluginClient::total_in_buffers];
103 bzero(peak, sizeof(double) * PluginClient::total_in_buffers);
108 int NormalizeMain::process_loop(Samples **buffer, int64_t &write_length)
111 int64_t fragment_len;
113 //printf("NormalizeMain::process_loop 1\n");
116 fragment_len = PluginClient::in_buffer_size;
117 if(current_position + fragment_len > PluginClient::end) fragment_len = PluginClient::end - current_position;
118 //printf("NormalizeMain::process_loop 2 %d %f\n", current_position, scale[0]);
120 for(int i = 0; i < PluginClient::total_in_buffers; i++)
122 read_samples(buffer[i], i, current_position, fragment_len);
123 double *buffer_samples = buffer[i]->get_data();
124 for(int j = 0; j < fragment_len; j++)
125 buffer_samples[j] *= scale[i];
128 //printf("NormalizeMain::process_loop 1 %d %f\n", current_position, scale[0]);
129 current_position += fragment_len;
130 write_length = fragment_len;
131 result = progress->update(PluginClient::end -
132 PluginClient::start +
134 PluginClient::start);
135 if(current_position >= PluginClient::end) result = 1;
140 //printf("NormalizeMain::process_loop 4\n");
141 for(int i = PluginClient::start;
142 i < PluginClient::end && !result;
145 fragment_len = PluginClient::in_buffer_size;
146 if(i + fragment_len > PluginClient::end) fragment_len = PluginClient::end - i;
147 //printf("NormalizeMain::process_loop 5\n");
149 for(int j = 0; j < PluginClient::total_in_buffers; j++)
151 //printf("NormalizeMain::process_loop 6 %p\n", buffer);
152 read_samples(buffer[j], j, i, fragment_len);
153 //printf("NormalizeMain::process_loop 7\n");
154 double *buffer_samples = buffer[j]->get_data();
156 for(int k = 0; k < fragment_len; k++)
158 double sample = fabs(buffer_samples[k]);
163 //printf("NormalizeMain::process_loop 8\n");
164 result = progress->update(i - PluginClient::start);
165 //printf("NormalizeMain::process_loop 9\n");
168 // Normalize all tracks
170 for(int i = 0; i < PluginClient::total_in_buffers; i++)
172 if(peak[i] > max) max = peak[i];
176 for(int i = 0; i < PluginClient::total_in_buffers; i++)
182 for(int i = 0; i < PluginClient::total_in_buffers; i++)
184 scale[i] = DB::fromdb(db_over) / peak[i];
186 //printf("NormalizeMain::process_loop 10\n");
188 char string[BCTEXTLEN];
189 sprintf(string, "%s %.0f%%...", plugin_title(), (DB::fromdb(db_over) / max) * 100);
190 progress->update_title(string);
191 // Start writing on next iteration
198 int NormalizeMain::stop_loop()
200 progress->stop_progress();