f90908d8f3e953248f0ab7ccbb2121f31e7b798d
[goodguy/history.git] / cinelerra-5.0 / 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 "mainprogress.h"
25 #include "normalize.h"
26 #include "normalizewindow.h"
27 #include "samples.h"
28 #include "units.h"
29 #include "vframe.h"
30
31 #include <stdio.h>
32 #include <string.h>
33
34 #include <libintl.h>
35 #define _(String) gettext(String)
36 #define gettext_noop(String) String
37 #define N_(String) gettext_noop (String)
38
39 REGISTER_PLUGIN(NormalizeMain)
40
41
42
43
44
45
46 NormalizeMain::NormalizeMain(PluginServer *server)
47  : PluginAClient(server)
48 {
49 }
50
51 NormalizeMain::~NormalizeMain()
52 {
53 }
54
55 const char* NormalizeMain::plugin_title() { return N_("Normalize"); }
56 int NormalizeMain::is_realtime() { return 0; }
57 int NormalizeMain::is_multichannel() { return 1; }
58
59
60 int NormalizeMain::load_defaults()
61 {
62         char directory[BCTEXTLEN];
63
64 // set the default directory
65         sprintf(directory, "%snormalize.rc", BCASTDIR);
66         
67 // load the defaults
68
69         defaults = new BC_Hash(directory);
70
71         defaults->load();
72
73         db_over = defaults->get("DBOVER", 0);
74         separate_tracks = defaults->get("SEPERATE_TRACKS", 1);
75         return 0;
76 }
77
78 int NormalizeMain::save_defaults()
79 {
80         defaults->update("DBOVER", db_over);
81         defaults->update("SEPERATE_TRACKS", separate_tracks);
82         defaults->save();
83         return 0;
84 }
85
86 int NormalizeMain::get_parameters()
87 {
88         BC_DisplayInfo info;
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();
92         return result;
93 }
94
95 int NormalizeMain::start_loop()
96 {
97         char string[BCTEXTLEN];
98         sprintf(string, "%s...", plugin_title());
99         progress = start_progress(string, (PluginClient::end - PluginClient::start) * 2);
100
101         writing = 0;
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);
106         return 0;
107 }
108
109
110 int NormalizeMain::process_loop(Samples **buffer, int64_t &write_length)
111 {
112         int result = 0;
113         int64_t fragment_len;
114
115 //printf("NormalizeMain::process_loop 1\n");
116         if(writing)
117         {
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]);
121
122                 for(int i = 0; i < PluginClient::total_in_buffers; i++)
123                 {
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];
128                 }
129
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 + 
135                         current_position - 
136                         PluginClient::start);
137                 if(current_position >= PluginClient::end) result = 1;
138         }
139         else
140         {
141 // Get peak
142 //printf("NormalizeMain::process_loop 4\n");
143                 for(int i = PluginClient::start; 
144                         i < PluginClient::end && !result; 
145                         i += fragment_len)
146                 {
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");
150
151                         for(int j = 0; j < PluginClient::total_in_buffers; j++)
152                         {
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();
157                                 
158                                 for(int k = 0; k < fragment_len; k++)
159                                 {
160                                         double sample = fabs(buffer_samples[k]);
161                                         if(peak[j] < sample) 
162                                                 peak[j] = sample;
163                                 }
164                         }
165 //printf("NormalizeMain::process_loop 8\n");
166                         result = progress->update(i - PluginClient::start);
167 //printf("NormalizeMain::process_loop 9\n");
168                 }
169
170 // Normalize all tracks
171                 double max = 0;
172                 for(int i = 0; i < PluginClient::total_in_buffers; i++)
173                 {
174                         if(peak[i] > max) max = peak[i];
175                 }
176                 if(!separate_tracks)
177                 {
178                         for(int i = 0; i < PluginClient::total_in_buffers; i++)
179                         {
180                                 peak[i] = max;
181                         }
182                 }
183
184                 for(int i = 0; i < PluginClient::total_in_buffers; i++)
185                 {
186                         scale[i] = DB::fromdb(db_over) / peak[i];
187                 }
188 //printf("NormalizeMain::process_loop 10\n");
189
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
194                 writing = 1;
195         }
196
197         return result;
198 }
199
200 int NormalizeMain::stop_loop()
201 {
202         progress->stop_progress();
203         delete [] peak;
204         delete [] scale;
205         delete progress;
206         return 0;
207 }