Credit User for Gain plugin mods + Andrew for Debian 12 bld fix
[goodguy/cinelerra.git] / cinelerra-5.1 / plugins / gain / gain.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 "clip.h"
23 #include "confirmsave.h"
24 #include "bchash.h"
25 #include "errorbox.h"
26 #include "filexml.h"
27 #include "language.h"
28 #include "gain.h"
29 #include "gainwindow.h"
30 #include "samples.h"
31
32 #include "vframe.h"
33
34 #include <string.h>
35
36
37 REGISTER_PLUGIN(GainMain)
38
39
40 GainConfig::GainConfig()
41 {
42         reset();
43 }
44
45 void GainConfig::reset()
46 {
47         level = 0.0;
48 }
49
50 int GainConfig::equivalent(GainConfig &that)
51 {
52         return EQUIV(level, that.level);
53 }
54
55 void GainConfig::copy_from(GainConfig &that)
56 {
57         this->level = that.level;
58 }
59
60 void GainConfig::interpolate(GainConfig &prev,
61         GainConfig &next,
62         int64_t prev_frame,
63         int64_t next_frame,
64         int64_t current_frame)
65 {
66         double next_scale = (double)(current_frame - prev_frame) / (next_frame - prev_frame);
67         double prev_scale = (double)(next_frame - current_frame) / (next_frame - prev_frame);
68         level = prev.level * prev_scale + next.level * next_scale;
69 }
70
71
72
73
74
75
76
77
78
79 GainMain::GainMain(PluginServer *server)
80  : PluginAClient(server)
81 {
82
83 }
84
85 GainMain::~GainMain()
86 {
87
88 }
89
90 const char* GainMain::plugin_title() { return N_("Gain"); }
91 int GainMain::is_realtime() { return 1; }
92
93
94 NEW_WINDOW_MACRO(GainMain, GainWindow)
95 LOAD_CONFIGURATION_MACRO(GainMain, GainConfig)
96
97 int GainMain::process_realtime(int64_t size, Samples *input_ptr, Samples *output_ptr)
98 {
99         load_configuration();
100
101         double gain = db.fromdb(config.level);
102         double *output_samples = output_ptr->get_data();
103         double *input_samples = input_ptr->get_data();
104
105         for(int64_t i = 0; i < size; i++)
106         {
107                 output_samples[i] = input_samples[i] * gain;
108         }
109
110         return 0;
111 }
112
113
114
115
116 void GainMain::save_data(KeyFrame *keyframe)
117 {
118         FileXML output;
119
120 // cause xml file to store data directly in text
121         output.set_shared_output(keyframe->xbuf);
122
123         output.tag.set_title("GAIN");
124         output.tag.set_property("LEVEL", config.level);
125         output.append_tag();
126         output.tag.set_title("/GAIN");
127         output.append_tag();
128         output.append_newline();
129         output.terminate_string();
130 }
131
132 void GainMain::read_data(KeyFrame *keyframe)
133 {
134         FileXML input;
135 // cause xml file to read directly from text
136         input.set_shared_input(keyframe->xbuf);
137         int result = 0;
138
139         result = input.read_tag();
140
141         if(!result)
142         {
143                 if(input.tag.title_is("GAIN"))
144                 {
145                         config.level = input.tag.get_property("LEVEL", config.level);
146                 }
147         }
148 }
149
150 void GainMain::update_gui()
151 {
152         if(thread)
153         {
154                 load_configuration();
155                 thread->window->lock_window();
156                 ((GainWindow*)thread->window)->level_text->update((float)config.level);
157                 ((GainWindow*)thread->window)->level_slider->update(config.level);
158                 thread->window->unlock_window();
159         }
160 }
161
162