minor changes; mostly for new Context Help feature
[goodguy/cinelerra.git] / cinelerra-5.1 / plugins / normalize / normalize.C
1
2 /*
3  * CINELERRA
4  * Copyright (C) 2008 Adam Williams <broadcast at earthling dot net>
5  *
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.
10  *
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.
15  *
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
19  *
20  */
21
22 #include "bcdisplayinfo.h"
23 #include "bchash.h"
24 #include "file.h"
25 #include "language.h"
26 #include "mainprogress.h"
27 #include "normalize.h"
28 #include "normalizewindow.h"
29 #include "samples.h"
30 #include "units.h"
31 #include "vframe.h"
32
33 #include <stdio.h>
34 #include <string.h>
35
36
37 REGISTER_PLUGIN(NormalizeMain)
38
39
40
41
42
43
44 NormalizeMain::NormalizeMain(PluginServer *server)
45  : PluginAClient(server)
46 {
47 }
48
49 NormalizeMain::~NormalizeMain()
50 {
51 }
52
53 const char* NormalizeMain::plugin_title() { return N_("Normalize"); }
54 int NormalizeMain::is_realtime() { return 0; }
55 int NormalizeMain::is_multichannel() { return 1; }
56
57
58 int NormalizeMain::load_defaults()
59 {
60         char directory[BCTEXTLEN];
61
62 // set the default directory
63         sprintf(directory, "%s/normalize.rc", File::get_config_path());
64
65 // load the defaults
66
67         defaults = new BC_Hash(directory);
68
69         defaults->load();
70
71         db_over = defaults->get("DBOVER", 0);
72         separate_tracks = defaults->get("SEPERATE_TRACKS", 1);
73         return 0;
74 }
75
76 int NormalizeMain::save_defaults()
77 {
78         defaults->update("DBOVER", db_over);
79         defaults->update("SEPERATE_TRACKS", separate_tracks);
80         defaults->save();
81         return 0;
82 }
83
84 int NormalizeMain::get_parameters()
85 {
86         BC_DisplayInfo info;
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();
90         return result;
91 }
92
93 int NormalizeMain::start_loop()
94 {
95         char string[BCTEXTLEN];
96         sprintf(string, "%s...", plugin_title());
97         progress = start_progress(string, (PluginClient::end - PluginClient::start) * 2);
98
99         writing = 0;
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);
104         return 0;
105 }
106
107
108 int NormalizeMain::process_loop(Samples **buffer, int64_t &write_length)
109 {
110         int result = 0;
111         int64_t fragment_len;
112
113 //printf("NormalizeMain::process_loop 1\n");
114         if(writing)
115         {
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]);
119
120                 for(int i = 0; i < PluginClient::total_in_buffers; i++)
121                 {
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];
126                 }
127
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 +
133                         current_position -
134                         PluginClient::start);
135                 if(current_position >= PluginClient::end) result = 1;
136         }
137         else
138         {
139 // Get peak
140 //printf("NormalizeMain::process_loop 4\n");
141                 for(int i = PluginClient::start;
142                         i < PluginClient::end && !result;
143                         i += fragment_len)
144                 {
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");
148
149                         for(int j = 0; j < PluginClient::total_in_buffers; j++)
150                         {
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();
155
156                                 for(int k = 0; k < fragment_len; k++)
157                                 {
158                                         double sample = fabs(buffer_samples[k]);
159                                         if(peak[j] < sample)
160                                                 peak[j] = sample;
161                                 }
162                         }
163 //printf("NormalizeMain::process_loop 8\n");
164                         result = progress->update(i - PluginClient::start);
165 //printf("NormalizeMain::process_loop 9\n");
166                 }
167
168 // Normalize all tracks
169                 double max = 0;
170                 for(int i = 0; i < PluginClient::total_in_buffers; i++)
171                 {
172                         if(peak[i] > max) max = peak[i];
173                 }
174                 if(!separate_tracks)
175                 {
176                         for(int i = 0; i < PluginClient::total_in_buffers; i++)
177                         {
178                                 peak[i] = max;
179                         }
180                 }
181
182                 for(int i = 0; i < PluginClient::total_in_buffers; i++)
183                 {
184                         scale[i] = DB::fromdb(db_over) / peak[i];
185                 }
186 //printf("NormalizeMain::process_loop 10\n");
187
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
192                 writing = 1;
193         }
194
195         return result;
196 }
197
198 int NormalizeMain::stop_loop()
199 {
200         progress->stop_progress();
201         delete [] peak;
202         delete [] scale;
203         delete progress;
204         return 0;
205 }