r741: Add 'take from alpha' option for creating timefronts... pretty cool
[cinelerra_cv/mob.git] / plugins / normalize / normalize.C
blobf81f462289d0d42d3bebc49fbb7724fd9c5ccd8c
1 #include "bcdisplayinfo.h"
2 #include "defaults.h"
3 #include "mainprogress.h"
4 #include "normalize.h"
5 #include "normalizewindow.h"
6 #include "picon_png.h"
7 #include "units.h"
8 #include "vframe.h"
10 #include <stdio.h>
11 #include <string.h>
13 #include <libintl.h>
14 #define _(String) gettext(String)
15 #define gettext_noop(String) String
16 #define N_(String) gettext_noop (String)
18 REGISTER_PLUGIN(NormalizeMain)
25 NormalizeMain::NormalizeMain(PluginServer *server)
26  : PluginAClient(server)
28         load_defaults();
31 NormalizeMain::~NormalizeMain()
33         save_defaults();
34         delete defaults;
37 char* NormalizeMain::plugin_title() { return N_("Normalize"); }
38 int NormalizeMain::is_realtime() { return 0; }
39 int NormalizeMain::is_multichannel() { return 1; }
42 VFrame* NormalizeMain::new_picon()
44         return new VFrame(picon_png);
48 int NormalizeMain::load_defaults()
50         char directory[BCTEXTLEN];
52 // set the default directory
53         sprintf(directory, "%snormalize.rc", BCASTDIR);
54         
55 // load the defaults
57         defaults = new Defaults(directory);
59         defaults->load();
61         db_over = defaults->get("DBOVER", 0);
62         separate_tracks = defaults->get("SEPERATE_TRACKS", 1);
63         return 0;
66 int NormalizeMain::save_defaults()
68         defaults->update("DBOVER", db_over);
69         defaults->update("SEPERATE_TRACKS", separate_tracks);
70         defaults->save();
71         return 0;
74 int NormalizeMain::get_parameters()
76         BC_DisplayInfo info;
77         NormalizeWindow window(info.get_abs_cursor_x(), info.get_abs_cursor_y());
78         window.create_objects(&db_over, &separate_tracks);
79         int result = window.run_window();
80         return result;
83 int NormalizeMain::start_loop()
85         char string[BCTEXTLEN];
86         sprintf(string, "%s...", plugin_title());
87         progress = start_progress(string, (PluginClient::end - PluginClient::start) * 2);
89         writing = 0;
90         current_position = PluginClient::start;
91         peak = new double[PluginClient::total_in_buffers];
92         scale = new double[PluginClient::total_in_buffers];
93         bzero(peak, sizeof(double) * PluginClient::total_in_buffers);
94         return 0;
98 int NormalizeMain::process_loop(double **buffer, int64_t &write_length)
100         int result = 0;
101         int64_t fragment_len;
103 //printf("NormalizeMain::process_loop 1\n");
104         if(writing)
105         {
106                 fragment_len = PluginClient::in_buffer_size;
107                 if(current_position + fragment_len > PluginClient::end) fragment_len = PluginClient::end - current_position;
108 //printf("NormalizeMain::process_loop 2 %d %f\n", current_position, scale[0]);
110                 for(int i = 0; i < PluginClient::total_in_buffers; i++)
111                 {
112                         read_samples(buffer[i], i, current_position, fragment_len);
113                         for(int j = 0; j < fragment_len; j++)
114                                 buffer[i][j] *= scale[i];
115                 }
117 //printf("NormalizeMain::process_loop 1 %d %f\n", current_position, scale[0]);
118                 current_position += fragment_len;
119                 write_length = fragment_len;
120                 result = progress->update(PluginClient::end - 
121                         PluginClient::start + 
122                         current_position - 
123                         PluginClient::start);
124                 if(current_position >= PluginClient::end) result = 1;
125         }
126         else
127         {
128 // Get peak
129 //printf("NormalizeMain::process_loop 4\n");
130                 for(int i = PluginClient::start; 
131                         i < PluginClient::end && !result; 
132                         i += fragment_len)
133                 {
134                         fragment_len = PluginClient::in_buffer_size;
135                         if(i + fragment_len > PluginClient::end) fragment_len = PluginClient::end - i;
136 //printf("NormalizeMain::process_loop 5\n");
138                         for(int j = 0; j < PluginClient::total_in_buffers; j++)
139                         {
140 //printf("NormalizeMain::process_loop 6 %p\n", buffer);
141                                 read_samples(buffer[j], j, i, fragment_len);
142 //printf("NormalizeMain::process_loop 7\n");
143                                 
144                                 for(int k = 0; k < fragment_len; k++)
145                                 {
146                                         if(peak[j] < fabs(buffer[j][k])) peak[j] = fabs(buffer[j][k]);
147                                 }
148                         }
149 //printf("NormalizeMain::process_loop 8\n");
150                         result = progress->update(i - PluginClient::start);
151 //printf("NormalizeMain::process_loop 9\n");
152                 }
154 // Normalize all tracks
155                 double max = 0;
156                 for(int i = 0; i < PluginClient::total_in_buffers; i++)
157                 {
158                         if(peak[i] > max) max = peak[i];
159                 }
160                 if(!separate_tracks)
161                 {
162                         for(int i = 0; i < PluginClient::total_in_buffers; i++)
163                         {
164                                 peak[i] = max;
165                         }
166                 }
168                 for(int i = 0; i < PluginClient::total_in_buffers; i++)
169                 {
170                         scale[i] = DB::fromdb(db_over) / peak[i];
171                 }
172 //printf("NormalizeMain::process_loop 10\n");
174                 char string[BCTEXTLEN];
175                 sprintf(string, "%s %.0f%%...", plugin_title(), (DB::fromdb(db_over) / max) * 100);
176                 progress->update_title(string);
177 // Start writing on next iteration
178                 writing = 1;
179         }
181         return result;
184 int NormalizeMain::stop_loop()
186         progress->stop_progress();
187         delete [] peak;
188         delete [] scale;
189         delete progress;
190         return 0;