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 "mainprogress.h"
26 #include "normalize.h"
27 #include "normalizewindow.h"
36 REGISTER_PLUGIN(NormalizeMain)
43 NormalizeMain::NormalizeMain(PluginServer *server)
44 : PluginAClient(server)
48 NormalizeMain::~NormalizeMain()
52 const char* NormalizeMain::plugin_title() { return _("Normalize"); }
53 int NormalizeMain::is_realtime() { return 0; }
54 int NormalizeMain::is_multichannel() { return 1; }
57 int NormalizeMain::load_defaults()
59 char directory[BCTEXTLEN];
61 // set the default directory
62 sprintf(directory, "%snormalize.rc", BCASTDIR);
66 defaults = new BC_Hash(directory);
70 db_over = defaults->get("DBOVER", 0);
71 separate_tracks = defaults->get("SEPERATE_TRACKS", 1);
75 int NormalizeMain::save_defaults()
77 defaults->update("DBOVER", db_over);
78 defaults->update("SEPERATE_TRACKS", separate_tracks);
83 int NormalizeMain::get_parameters()
86 NormalizeWindow window(info.get_abs_cursor_x(), info.get_abs_cursor_y());
87 window.create_objects(&db_over, &separate_tracks);
88 int result = window.run_window();
92 int NormalizeMain::start_loop()
94 char string[BCTEXTLEN];
95 sprintf(string, "%s...", plugin_title());
96 progress = start_progress(string, (PluginClient::end - PluginClient::start) * 2);
99 current_position = PluginClient::start;
100 peak = new double[PluginClient::total_in_buffers];
101 scale = new double[PluginClient::total_in_buffers];
102 bzero(peak, sizeof(double) * PluginClient::total_in_buffers);
107 int NormalizeMain::process_loop(Samples **buffer, int64_t &write_length)
110 int64_t fragment_len;
112 //printf("NormalizeMain::process_loop 1\n");
115 fragment_len = PluginClient::in_buffer_size;
116 if(current_position + fragment_len > PluginClient::end) fragment_len = PluginClient::end - current_position;
117 //printf("NormalizeMain::process_loop 2 %d %f\n", current_position, scale[0]);
119 for(int i = 0; i < PluginClient::total_in_buffers; i++)
121 read_samples(buffer[i], i, current_position, fragment_len);
122 double *buffer_samples = buffer[i]->get_data();
123 for(int j = 0; j < fragment_len; j++)
124 buffer_samples[j] *= scale[i];
127 //printf("NormalizeMain::process_loop 1 %d %f\n", current_position, scale[0]);
128 current_position += fragment_len;
129 write_length = fragment_len;
130 result = progress->update(PluginClient::end -
131 PluginClient::start +
133 PluginClient::start);
134 if(current_position >= PluginClient::end) result = 1;
139 //printf("NormalizeMain::process_loop 4\n");
140 for(int i = PluginClient::start;
141 i < PluginClient::end && !result;
144 fragment_len = PluginClient::in_buffer_size;
145 if(i + fragment_len > PluginClient::end) fragment_len = PluginClient::end - i;
146 //printf("NormalizeMain::process_loop 5\n");
148 for(int j = 0; j < PluginClient::total_in_buffers; j++)
150 //printf("NormalizeMain::process_loop 6 %p\n", buffer);
151 read_samples(buffer[j], j, i, fragment_len);
152 //printf("NormalizeMain::process_loop 7\n");
153 double *buffer_samples = buffer[j]->get_data();
155 for(int k = 0; k < fragment_len; k++)
157 double sample = fabs(buffer_samples[k]);
162 //printf("NormalizeMain::process_loop 8\n");
163 result = progress->update(i - PluginClient::start);
164 //printf("NormalizeMain::process_loop 9\n");
167 // Normalize all tracks
169 for(int i = 0; i < PluginClient::total_in_buffers; i++)
171 if(peak[i] > max) max = peak[i];
175 for(int i = 0; i < PluginClient::total_in_buffers; i++)
181 for(int i = 0; i < PluginClient::total_in_buffers; i++)
183 scale[i] = DB::fromdb(db_over) / peak[i];
185 //printf("NormalizeMain::process_loop 10\n");
187 char string[BCTEXTLEN];
188 sprintf(string, "%s %.0f%%...", plugin_title(), (DB::fromdb(db_over) / max) * 100);
189 progress->update_title(string);
190 // Start writing on next iteration
197 int NormalizeMain::stop_loop()
199 progress->stop_progress();