rework keyframe hide popup, keyframe auto render, textbox set_selection wide text
[goodguy/history.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 "language.h"
25 #include "mainprogress.h"
26 #include "normalize.h"
27 #include "normalizewindow.h"
28 #include "samples.h"
29 #include "units.h"
30 #include "vframe.h"
31
32 #include <stdio.h>
33 #include <string.h>
34
35
36 REGISTER_PLUGIN(NormalizeMain)
37
38
39
40
41
42
43 NormalizeMain::NormalizeMain(PluginServer *server)
44  : PluginAClient(server)
45 {
46 }
47
48 NormalizeMain::~NormalizeMain()
49 {
50 }
51
52 const char* NormalizeMain::plugin_title() { return _("Normalize"); }
53 int NormalizeMain::is_realtime() { return 0; }
54 int NormalizeMain::is_multichannel() { return 1; }
55
56
57 int NormalizeMain::load_defaults()
58 {
59         char directory[BCTEXTLEN];
60
61 // set the default directory
62         sprintf(directory, "%snormalize.rc", BCASTDIR);
63         
64 // load the defaults
65
66         defaults = new BC_Hash(directory);
67
68         defaults->load();
69
70         db_over = defaults->get("DBOVER", 0);
71         separate_tracks = defaults->get("SEPERATE_TRACKS", 1);
72         return 0;
73 }
74
75 int NormalizeMain::save_defaults()
76 {
77         defaults->update("DBOVER", db_over);
78         defaults->update("SEPERATE_TRACKS", separate_tracks);
79         defaults->save();
80         return 0;
81 }
82
83 int NormalizeMain::get_parameters()
84 {
85         BC_DisplayInfo info;
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();
89         return result;
90 }
91
92 int NormalizeMain::start_loop()
93 {
94         char string[BCTEXTLEN];
95         sprintf(string, "%s...", plugin_title());
96         progress = start_progress(string, (PluginClient::end - PluginClient::start) * 2);
97
98         writing = 0;
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);
103         return 0;
104 }
105
106
107 int NormalizeMain::process_loop(Samples **buffer, int64_t &write_length)
108 {
109         int result = 0;
110         int64_t fragment_len;
111
112 //printf("NormalizeMain::process_loop 1\n");
113         if(writing)
114         {
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]);
118
119                 for(int i = 0; i < PluginClient::total_in_buffers; i++)
120                 {
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];
125                 }
126
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 + 
132                         current_position - 
133                         PluginClient::start);
134                 if(current_position >= PluginClient::end) result = 1;
135         }
136         else
137         {
138 // Get peak
139 //printf("NormalizeMain::process_loop 4\n");
140                 for(int i = PluginClient::start; 
141                         i < PluginClient::end && !result; 
142                         i += fragment_len)
143                 {
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");
147
148                         for(int j = 0; j < PluginClient::total_in_buffers; j++)
149                         {
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();
154                                 
155                                 for(int k = 0; k < fragment_len; k++)
156                                 {
157                                         double sample = fabs(buffer_samples[k]);
158                                         if(peak[j] < sample) 
159                                                 peak[j] = sample;
160                                 }
161                         }
162 //printf("NormalizeMain::process_loop 8\n");
163                         result = progress->update(i - PluginClient::start);
164 //printf("NormalizeMain::process_loop 9\n");
165                 }
166
167 // Normalize all tracks
168                 double max = 0;
169                 for(int i = 0; i < PluginClient::total_in_buffers; i++)
170                 {
171                         if(peak[i] > max) max = peak[i];
172                 }
173                 if(!separate_tracks)
174                 {
175                         for(int i = 0; i < PluginClient::total_in_buffers; i++)
176                         {
177                                 peak[i] = max;
178                         }
179                 }
180
181                 for(int i = 0; i < PluginClient::total_in_buffers; i++)
182                 {
183                         scale[i] = DB::fromdb(db_over) / peak[i];
184                 }
185 //printf("NormalizeMain::process_loop 10\n");
186
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
191                 writing = 1;
192         }
193
194         return result;
195 }
196
197 int NormalizeMain::stop_loop()
198 {
199         progress->stop_progress();
200         delete [] peak;
201         delete [] scale;
202         delete progress;
203         return 0;
204 }