r125: This commit was manufactured by cvs2svn to create tag 'r1_1_7-last'.
[cinelerra_cv/mob.git] / hvirtual / plugins / normalize / normalize.C
blob9530042f887cb533b6cc111e2e04cf4d14805cc1
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 _("Normalize"); }
39 int NormalizeMain::is_realtime() { return 0; }
41 int NormalizeMain::is_multichannel() { return 1; }
43 VFrame* NormalizeMain::new_picon()
45         return new VFrame(picon_png);
49 int NormalizeMain::load_defaults()
51         char directory[BCTEXTLEN];
53 // set the default directory
54         sprintf(directory, "%snormalize.rc", BCASTDIR);
55         
56 // load the defaults
58         defaults = new Defaults(directory);
60         defaults->load();
62         db_over = defaults->get("DBOVER", 0);
63         separate_tracks = defaults->get("SEPERATE_TRACKS", 1);
64         return 0;
67 int NormalizeMain::save_defaults()
69         defaults->update("DBOVER", db_over);
70         defaults->update("SEPERATE_TRACKS", separate_tracks);
71         defaults->save();
72         return 0;
75 int NormalizeMain::get_parameters()
77         BC_DisplayInfo info;
78 //printf("NormalizeMain::get_parameters 1\n");
79         NormalizeWindow window(info.get_abs_cursor_x(), info.get_abs_cursor_y());
80 //printf("NormalizeMain::get_parameters 2\n");
81         window.create_objects(&db_over, &separate_tracks);
82 //printf("NormalizeMain::get_parameters 3\n");
83         int result = window.run_window();
84 //printf("NormalizeMain::get_parameters 4\n");
85         return result;
88 int NormalizeMain::start_loop()
90         char string[BCTEXTLEN];
91         sprintf(string, "%s...", plugin_title());
92         progress = start_progress(string, (PluginClient::end - PluginClient::start) * 2);
94         writing = 0;
95         current_position = PluginClient::start;
96         peak = new double[PluginClient::total_in_buffers];
97         scale = new double[PluginClient::total_in_buffers];
98         bzero(peak, sizeof(double) * PluginClient::total_in_buffers);
99         return 0;
103 int NormalizeMain::process_loop(double **buffer, int64_t &write_length)
105         int result = 0;
106         int64_t fragment_len;
108 //printf("NormalizeMain::process_loop 1\n");
109         if(writing)
110         {
111                 fragment_len = PluginClient::in_buffer_size;
112                 if(current_position + fragment_len > PluginClient::end) fragment_len = PluginClient::end - current_position;
113 //printf("NormalizeMain::process_loop 2 %d %f\n", current_position, scale[0]);
115                 for(int i = 0; i < PluginClient::total_in_buffers; i++)
116                 {
117                         read_samples(buffer[i], i, current_position, fragment_len);
118                         for(int j = 0; j < fragment_len; j++)
119                                 buffer[i][j] *= scale[i];
120                 }
122 //printf("NormalizeMain::process_loop 1 %d %f\n", current_position, scale[0]);
123                 current_position += fragment_len;
124                 write_length = fragment_len;
125                 result = progress->update(PluginClient::end - 
126                         PluginClient::start + 
127                         current_position - 
128                         PluginClient::start);
129                 if(current_position >= PluginClient::end) result = 1;
130         }
131         else
132         {
133 // Get peak
134 //printf("NormalizeMain::process_loop 4\n");
135                 for(int i = PluginClient::start; 
136                         i < PluginClient::end && !result; 
137                         i += fragment_len)
138                 {
139                         fragment_len = PluginClient::in_buffer_size;
140                         if(i + fragment_len > PluginClient::end) fragment_len = PluginClient::end - i;
141 //printf("NormalizeMain::process_loop 5\n");
143                         for(int j = 0; j < PluginClient::total_in_buffers; j++)
144                         {
145 //printf("NormalizeMain::process_loop 6 %p\n", buffer);
146                                 read_samples(buffer[j], j, i, fragment_len);
147 //printf("NormalizeMain::process_loop 7\n");
148                                 
149                                 for(int k = 0; k < fragment_len; k++)
150                                 {
151                                         if(peak[j] < fabs(buffer[j][k])) peak[j] = fabs(buffer[j][k]);
152                                 }
153                         }
154 //printf("NormalizeMain::process_loop 8\n");
155                         result = progress->update(i - PluginClient::start);
156 //printf("NormalizeMain::process_loop 9\n");
157                 }
159 // Normalize all tracks
160                 double max = 0;
161                 for(int i = 0; i < PluginClient::total_in_buffers; i++)
162                 {
163                         if(peak[i] > max) max = peak[i];
164                 }
165                 if(!separate_tracks)
166                 {
167                         for(int i = 0; i < PluginClient::total_in_buffers; i++)
168                         {
169                                 peak[i] = max;
170                         }
171                 }
173                 for(int i = 0; i < PluginClient::total_in_buffers; i++)
174                 {
175                         scale[i] = DB::fromdb(db_over) / peak[i];
176                 }
177 //printf("NormalizeMain::process_loop 10\n");
179                 char string[BCTEXTLEN];
180                 sprintf(string, "%s %.0f%%...", plugin_title(), (DB::fromdb(db_over) / max) * 100);
181                 progress->update_title(string);
182 // Start writing on next iteration
183                 writing = 1;
184         }
186         return result;
189 int NormalizeMain::stop_loop()
191         progress->stop_progress();
192         delete [] peak;
193         delete [] scale;
194         delete progress;
195         return 0;