Fixed initialisation of tf in file_open(). Without setting the memory to 0,
[cinelerra_cv/mob.git] / plugins / reverb / reverb.C
blob00304335a6654fa1a2ab82943098232222764843
1 #include "clip.h"
2 #include "confirmsave.h"
3 #include "bchash.h"
4 #include "errorbox.h"
5 #include "filexml.h"
6 #include "language.h"
7 #include "picon_png.h"
8 #include "reverb.h"
9 #include "reverbwindow.h"
11 #include "vframe.h"
13 #include <math.h>
14 #include <string.h>
15 #include <time.h>
16 #include <unistd.h>
20 PluginClient* new_plugin(PluginServer *server)
22         return new Reverb(server);
27 Reverb::Reverb(PluginServer *server)
28  : PluginAClient(server)
30         srand(time(0));
31         redo_buffers = 1;       // set to redo buffers before the first render
32         dsp_in_length = 0;
33         ref_channels = 0;
34         ref_offsets = 0;
35         ref_levels = 0;
36         ref_lowpass = 0;
37         dsp_in = 0;
38         lowpass_in1 = 0;
39         lowpass_in2 = 0;
40         initialized = 0;
41         PLUGIN_CONSTRUCTOR_MACRO
44 Reverb::~Reverb()
46         PLUGIN_DESTRUCTOR_MACRO
48         if(initialized)
49         {
50                 for(int i = 0; i < total_in_buffers; i++)
51                 {
52                         delete [] dsp_in[i];
53                         delete [] ref_channels[i];
54                         delete [] ref_offsets[i];
55                         delete [] ref_levels[i];
56                         delete [] ref_lowpass[i];
57                         delete [] lowpass_in1[i];
58                         delete [] lowpass_in2[i];
59                 }
61                 delete [] dsp_in;
62                 delete [] ref_channels;
63                 delete [] ref_offsets;
64                 delete [] ref_levels;
65                 delete [] ref_lowpass;
66                 delete [] lowpass_in1;
67                 delete [] lowpass_in2;
69                 for(int i = 0; i < (smp + 1); i++)
70                 {
71                         delete engine[i];
72                 }
73                 delete [] engine;
74                 initialized = 0;
75         }
78 char* Reverb::plugin_title() { return N_("Heroine College Concert Hall"); }
79 int Reverb::is_realtime() { return 1; }
80 int Reverb::is_multichannel() { return 1; }
81 int Reverb::is_synthesis() { return 1; }
83 int Reverb::process_realtime(int64_t size, 
84         double **input_ptr, 
85         double **output_ptr)
87         int64_t new_dsp_length, i, j;
88         main_in = input_ptr;
89         main_out = output_ptr;
90 //printf("Reverb::process_realtime 1\n");
91         redo_buffers |= load_configuration();
93 //printf("Reverb::process_realtime 1\n");
94         if(!config.ref_total) return 0;
97         if(!initialized)
98         {
99                 dsp_in = new double*[total_in_buffers];
100                 ref_channels = new int64_t*[total_in_buffers];
101                 ref_offsets = new int64_t*[total_in_buffers];
102                 ref_levels = new double*[total_in_buffers];
103                 ref_lowpass = new int64_t*[total_in_buffers];
104                 lowpass_in1 = new double*[total_in_buffers];
105                 lowpass_in2 = new double*[total_in_buffers];
107                 for(i = 0; i < total_in_buffers; i++)
108                 {
109                         dsp_in[i] = new double[1];
110                         ref_channels[i] = new int64_t[1];
111                         ref_offsets[i] = new int64_t[1];
112                         ref_levels[i] = new double[1];
113                         ref_lowpass[i] = new int64_t[1];
114                         lowpass_in1[i] = new double[1];
115                         lowpass_in2[i] = new double[1];
116                 }
118                 engine = new ReverbEngine*[(smp + 1)];
119                 for(i = 0; i < (smp + 1); i++)
120                 {
121                         engine[i] = new ReverbEngine(this);
122 //printf("Reverb::start_realtime %d\n", Thread::calculate_realtime());
123 // Realtime priority moved to sound driver
124 //              engine[i]->set_realtime(realtime_priority);
125                         engine[i]->start();
126                 }
127                 initialized = 1;
128                 redo_buffers = 1;
129         }
131         new_dsp_length = size + 
132                 (config.delay_init + config.ref_length) * project_sample_rate / 1000 + 1;
133 //printf("Reverb::process_realtime 1 %d %d\n", in_buffer_size, size);
135         if(redo_buffers || new_dsp_length != dsp_in_length)
136         {
137                 for(i = 0; i < total_in_buffers; i++)
138                 {
139                         double *old_dsp = dsp_in[i];
140                         double *new_dsp = new double[new_dsp_length];
141                         for(j = 0; j < dsp_in_length && j < new_dsp_length; j++) 
142                                 new_dsp[j] = old_dsp[j];
143                         for( ; j < new_dsp_length; j++) new_dsp[j] = 0;
144                         delete [] old_dsp;
145                         dsp_in[i] = new_dsp;
146                 }
148                 dsp_in_length = new_dsp_length;
149                 redo_buffers = 1;
150         }
151 //printf("Reverb::process_realtime 1\n");
153         if(redo_buffers)
154         {
155                 for(i = 0; i < total_in_buffers; i++)
156                 {
157                         delete [] ref_channels[i];
158                         delete [] ref_offsets[i];
159                         delete [] ref_lowpass[i];
160                         delete [] ref_levels[i];
161                         delete [] lowpass_in1[i];
162                         delete [] lowpass_in2[i];
163                         
164                         ref_channels[i] = new int64_t[config.ref_total + 1];
165                         ref_offsets[i] = new int64_t[config.ref_total + 1];
166                         ref_lowpass[i] = new int64_t[config.ref_total + 1];
167                         ref_levels[i] = new double[config.ref_total + 1];
168                         lowpass_in1[i] = new double[config.ref_total + 1];
169                         lowpass_in2[i] = new double[config.ref_total + 1];
171 // set channels                 
172                         ref_channels[i][0] = i;         // primary noise
173                         ref_channels[i][1] = i;         // first reflection
174 // set offsets
175                         ref_offsets[i][0] = 0;
176                         ref_offsets[i][1] = config.delay_init * project_sample_rate / 1000;
177 // set levels
178                         ref_levels[i][0] = db.fromdb(config.level_init);
179                         ref_levels[i][1] = db.fromdb(config.ref_level1);
180 // set lowpass
181                         ref_lowpass[i][0] = -1;     // ignore first noise
182                         ref_lowpass[i][1] = config.lowpass1;
183                         lowpass_in1[i][0] = 0;
184                         lowpass_in2[i][0] = 0;
185                         lowpass_in1[i][1] = 0;
186                         lowpass_in2[i][1] = 0;
188                         int64_t ref_division = config.ref_length * project_sample_rate / 1000 / (config.ref_total + 1);
189                         for(j = 2; j < config.ref_total + 1; j++)
190                         {
191 // set random channels for remaining reflections
192                                 ref_channels[i][j] = rand() % total_in_buffers;
194 // set random offsets after first reflection
195                                 ref_offsets[i][j] = ref_offsets[i][1];
196                                 ref_offsets[i][j] += ref_division * j - (rand() % ref_division) / 2;
198 // set changing levels
199                                 ref_levels[i][j] = db.fromdb(config.ref_level1 + (config.ref_level2 - config.ref_level1) / (config.ref_total - 1) * (j - 2));
200                                 //ref_levels[i][j] /= 100;
202 // set changing lowpass as linear
203                                 ref_lowpass[i][j] = (int64_t)(config.lowpass1 + (double)(config.lowpass2 - config.lowpass1) / (config.ref_total - 1) * (j - 2));
204                                 lowpass_in1[i][j] = 0;
205                                 lowpass_in2[i][j] = 0;
206                         }
207                 }
208                 
209                 redo_buffers = 0;
210         }
211 //printf("Reverb::process_realtime 1\n");
213         for(i = 0; i < total_in_buffers; )
214         {
215                 for(j = 0; j < (smp + 1) && (i + j) < total_in_buffers; j++)
216                 {
217                         engine[j]->process_overlays(i + j, size);
218                 }
219                 
220                 for(j = 0; j < (smp + 1) && i < total_in_buffers; j++, i++)
221                 {
222                         engine[j]->wait_process_overlays();
223                 }
224         }
225 //printf("Reverb::process_realtime 2 %d %d\n", total_in_buffers, size);
227         for(i = 0; i < total_in_buffers; i++)
228         {
229                 double *current_out = main_out[i];
230                 double *current_in = dsp_in[i];
232                 for(j = 0; j < size; j++) current_out[j] = current_in[j];
234                 int64_t k;
235                 for(k = 0; j < dsp_in_length; j++, k++) current_in[k] = current_in[j];
236                 
237                 for(; k < dsp_in_length; k++) current_in[k] = 0;
238         }
239 //printf("Reverb::process_realtime 2 %d %d\n", total_in_buffers, size);
240         return 0;
243 NEW_PICON_MACRO(Reverb)
245 SHOW_GUI_MACRO(Reverb, ReverbThread)
247 SET_STRING_MACRO(Reverb)
249 RAISE_WINDOW_MACRO(Reverb)
251 int Reverb::load_defaults()
253         char directory[1024];
255 // set the default directory
256         sprintf(directory, "%sreverb.rc", get_defaultdir());
258 // load the defaults
260         defaults = new BC_Hash(directory);
262         defaults->load();
264         config.level_init = defaults->get("LEVEL_INIT", (double)0);
265         config.delay_init = defaults->get("DELAY_INIT", 100);
266         config.ref_level1 = defaults->get("REF_LEVEL1", (double)-6);
267         config.ref_level2 = defaults->get("REF_LEVEL2", (double)INFINITYGAIN);
268         config.ref_total = defaults->get("REF_TOTAL", 12);
269         config.ref_length = defaults->get("REF_LENGTH", 1000);
270         config.lowpass1 = defaults->get("LOWPASS1", 20000);
271         config.lowpass2 = defaults->get("LOWPASS2", 20000);
273         sprintf(config_directory, "~");
274         defaults->get("CONFIG_DIRECTORY", config_directory);
276 //printf("Reverb::load_defaults config.ref_level2 %f\n", config.ref_level2);
277         return 0;
280 int Reverb::save_defaults()
282         defaults->update("LEVEL_INIT", config.level_init);
283         defaults->update("DELAY_INIT", config.delay_init);
284         defaults->update("REF_LEVEL1", config.ref_level1);
285         defaults->update("REF_LEVEL2", config.ref_level2);
286         defaults->update("REF_TOTAL", config.ref_total);
287         defaults->update("REF_LENGTH", config.ref_length);
288         defaults->update("LOWPASS1", config.lowpass1);
289         defaults->update("LOWPASS2", config.lowpass2);
290         defaults->update("CONFIG_DIRECTORY", config_directory);
291         defaults->save();
292         return 0;
295 LOAD_CONFIGURATION_MACRO(Reverb, ReverbConfig)
298 void Reverb::save_data(KeyFrame *keyframe)
300 //printf("Reverb::save_data 1\n");
301         FileXML output;
302 //printf("Reverb::save_data 1\n");
304 // cause xml file to store data directly in text
305         output.set_shared_string(keyframe->data, MESSAGESIZE);
306 //printf("Reverb::save_data 1\n");
308         output.tag.set_title("REVERB");
309         output.tag.set_property("LEVELINIT", config.level_init);
310         output.tag.set_property("DELAY_INIT", config.delay_init);
311         output.tag.set_property("REF_LEVEL1", config.ref_level1);
312         output.tag.set_property("REF_LEVEL2", config.ref_level2);
313         output.tag.set_property("REF_TOTAL", config.ref_total);
314 //printf("Reverb::save_data 1\n");
315         output.tag.set_property("REF_LENGTH", config.ref_length);
316         output.tag.set_property("LOWPASS1", config.lowpass1);
317         output.tag.set_property("LOWPASS2", config.lowpass2);
318 //printf("Reverb::save_data config.ref_level2 %f\n", config.ref_level2);
319         output.append_tag();
320         output.append_newline();
321 //printf("Reverb::save_data 1\n");
322         
323         
324         
325         output.terminate_string();
326 //printf("Reverb::save_data 2\n");
329 void Reverb::read_data(KeyFrame *keyframe)
331         FileXML input;
332 // cause xml file to read directly from text
333         input.set_shared_string(keyframe->data, strlen(keyframe->data));
334         int result = 0;
336         result = input.read_tag();
338         if(!result)
339         {
340                 if(input.tag.title_is("REVERB"))
341                 {
342                         config.level_init = input.tag.get_property("LEVELINIT", config.level_init);
343                         config.delay_init = input.tag.get_property("DELAY_INIT", config.delay_init);
344                         config.ref_level1 = input.tag.get_property("REF_LEVEL1", config.ref_level1);
345                         config.ref_level2 = input.tag.get_property("REF_LEVEL2", config.ref_level2);
346                         config.ref_total = input.tag.get_property("REF_TOTAL", config.ref_total);
347                         config.ref_length = input.tag.get_property("REF_LENGTH", config.ref_length);
348                         config.lowpass1 = input.tag.get_property("LOWPASS1", config.lowpass1);
349                         config.lowpass2 = input.tag.get_property("LOWPASS2", config.lowpass2);
350                 }
351         }
354 void Reverb::update_gui()
356         if(thread)
357         {
358                 thread->window->lock_window();
359                 thread->window->level_init->update(config.level_init);
360                 thread->window->delay_init->update(config.delay_init);
361                 thread->window->ref_level1->update(config.ref_level1);
362                 thread->window->ref_level2->update(config.ref_level2);
363                 thread->window->ref_total->update(config.ref_total);
364                 thread->window->ref_length->update(config.ref_length);
365                 thread->window->lowpass1->update(config.lowpass1);
366                 thread->window->lowpass2->update(config.lowpass2);
367                 thread->window->unlock_window();
368         }
374 int Reverb::load_from_file(char *path)
376         FILE *in;
377         int result = 0;
378         int length;
379         char string[1024];
380         
381         if(in = fopen(path, "rb"))
382         {
383                 fseek(in, 0, SEEK_END);
384                 length = ftell(in);
385                 fseek(in, 0, SEEK_SET);
386                 fread(string, length, 1, in);
387                 fclose(in);
388 //              read_data(string);
389         }
390         else
391         {
392                 perror("fopen:");
393 // failed
394                 ErrorBox errorbox("");
395                 char string[1024];
396                 sprintf(string, _("Couldn't open %s."), path);
397                 errorbox.create_objects(string);
398                 errorbox.run_window();
399                 result = 1;
400         }
401         
402         return result;
405 int Reverb::save_to_file(char *path)
407         FILE *out;
408         int result = 0;
409         char string[1024];
410         
411         {
412 //              ConfirmSave confirm;
413 //              result = confirm.test_file("", path);
414         }
415         
416         if(!result)
417         {
418                 if(out = fopen(path, "wb"))
419                 {
420 //                      save_data(string);
421                         fwrite(string, strlen(string), 1, out);
422                         fclose(out);
423                 }
424                 else
425                 {
426                         result = 1;
427 // failed
428                         ErrorBox errorbox("");
429                         char string[1024];
430                         sprintf(string, _("Couldn't save %s."), path);
431                         errorbox.create_objects(string);
432                         errorbox.run_window();
433                         result = 1;
434                 }
435         }
436         
437         return result;
440 ReverbEngine::ReverbEngine(Reverb *plugin)
441  : Thread()
443         this->plugin = plugin;
444         completed = 0;
445         input_lock.lock();
446         output_lock.lock();
449 ReverbEngine::~ReverbEngine()
451         completed = 1;
452         input_lock.unlock();
453         join();
456 int ReverbEngine::process_overlays(int output_buffer, int64_t size)
458         this->output_buffer = output_buffer;
459         this->size = size;
460         input_lock.unlock();
463 int ReverbEngine::wait_process_overlays()
465         output_lock.lock();
467         
468 int ReverbEngine::process_overlay(double *in, double *out, double &out1, double &out2, double level, int64_t lowpass, int64_t samplerate, int64_t size)
470 // Modern niquist frequency is 44khz but pot limit is 20khz so can't use
471 // niquist
472         if(lowpass == -1 || lowpass >= 20000)
473         {
474 // no lowpass filter
475                 for(int i = 0; i < size; i++) out[i] += in[i] * level;
476         }
477         else
478         {
479                 double coef = 0.25 * 2.0 * M_PI * (double)lowpass / (double)plugin->project_sample_rate;
480                 double a = coef * 0.25;
481                 double b = coef * 0.50;
483                 for(int i = 0; i < size; i++)
484                 {
485                         out2 += a * (3 * out1 + in[i] - out2);
486                         out2 += b * (out1 + in[i] - out2);
487                         out2 += a * (out1 + 3 * in[i] - out2);
488                         out2 += coef * (in[i] - out2);
489                         out1 = in[i];
490                         out[i] += out2 * level;
491                 }
492         }
495 void ReverbEngine::run()
497         int j, i;
498 //printf("ReverbEngine::run 1 %d\n", calculate_realtime());
499         while(1)
500         {
501                 input_lock.lock();
502                 if(completed) return;
504 // Process reverb
505                 for(i = 0; i < plugin->total_in_buffers; i++)
506                 {
507                         for(j = 0; j < plugin->config.ref_total + 1; j++)
508                         {
509                                 if(plugin->ref_channels[i][j] == output_buffer)
510                                         process_overlay(plugin->main_in[i], 
511                                                                 &(plugin->dsp_in[plugin->ref_channels[i][j]][plugin->ref_offsets[i][j]]), 
512                                                                 plugin->lowpass_in1[i][j], 
513                                                                 plugin->lowpass_in2[i][j], 
514                                                                 plugin->ref_levels[i][j], 
515                                                                 plugin->ref_lowpass[i][j], 
516                                                                 plugin->project_sample_rate, 
517                                                                 size);
518                         }
519                 }
521                 output_lock.unlock();
522         }
531 ReverbConfig::ReverbConfig()
535 int ReverbConfig::equivalent(ReverbConfig &that)
537         return (EQUIV(level_init, that.level_init) &&
538                 delay_init == that.delay_init &&
539                 EQUIV(ref_level1, that.ref_level1) &&
540                 EQUIV(ref_level2, that.ref_level2) &&
541                 ref_total == that.ref_total &&
542                 ref_length == that.ref_length &&
543                 lowpass1 == that.lowpass1 &&
544                 lowpass2 == that.lowpass2);
547 void ReverbConfig::copy_from(ReverbConfig &that)
549         level_init = that.level_init;
550         delay_init = that.delay_init;
551         ref_level1 = that.ref_level1;
552         ref_level2 = that.ref_level2;
553         ref_total = that.ref_total;
554         ref_length = that.ref_length;
555         lowpass1 = that.lowpass1;
556         lowpass2 = that.lowpass2;
559 void ReverbConfig::interpolate(ReverbConfig &prev, 
560         ReverbConfig &next, 
561         int64_t prev_frame, 
562         int64_t next_frame, 
563         int64_t current_frame)
565         level_init = prev.level_init;
566         delay_init = prev.delay_init;
567         ref_level1 = prev.ref_level1;
568         ref_level2 = prev.ref_level2;
569         ref_total = prev.ref_total;
570         ref_length = prev.ref_length;
571         lowpass1 = prev.lowpass1;
572         lowpass2 = prev.lowpass2;
575 void ReverbConfig::dump()
577         printf("ReverbConfig::dump %f %d %f %f %d %d %d %d\n", 
578         level_init,
579         delay_init, 
580         ref_level1, 
581         ref_level2, 
582         ref_total, 
583         ref_length, 
584         lowpass1, 
585         lowpass2);