2 #include "confirmsave.h"
9 #include "reverbwindow.h"
20 PluginClient* new_plugin(PluginServer *server)
22 return new Reverb(server);
27 Reverb::Reverb(PluginServer *server)
28 : PluginAClient(server)
31 redo_buffers = 1; // set to redo buffers before the first render
41 PLUGIN_CONSTRUCTOR_MACRO
46 PLUGIN_DESTRUCTOR_MACRO
50 for(int i = 0; i < total_in_buffers; 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];
62 delete [] ref_channels;
63 delete [] ref_offsets;
65 delete [] ref_lowpass;
66 delete [] lowpass_in1;
67 delete [] lowpass_in2;
69 for(int i = 0; i < (smp + 1); i++)
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,
87 int64_t new_dsp_length, i, j;
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;
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++)
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];
118 engine = new ReverbEngine*[(smp + 1)];
119 for(i = 0; i < (smp + 1); i++)
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);
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)
137 for(i = 0; i < total_in_buffers; i++)
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;
148 dsp_in_length = new_dsp_length;
151 //printf("Reverb::process_realtime 1\n");
155 for(i = 0; i < total_in_buffers; i++)
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];
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];
172 ref_channels[i][0] = i; // primary noise
173 ref_channels[i][1] = i; // first reflection
175 ref_offsets[i][0] = 0;
176 ref_offsets[i][1] = config.delay_init * project_sample_rate / 1000;
178 ref_levels[i][0] = db.fromdb(config.level_init);
179 ref_levels[i][1] = db.fromdb(config.ref_level1);
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++)
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;
211 //printf("Reverb::process_realtime 1\n");
213 for(i = 0; i < total_in_buffers; )
215 for(j = 0; j < (smp + 1) && (i + j) < total_in_buffers; j++)
217 engine[j]->process_overlays(i + j, size);
220 for(j = 0; j < (smp + 1) && i < total_in_buffers; j++, i++)
222 engine[j]->wait_process_overlays();
225 //printf("Reverb::process_realtime 2 %d %d\n", total_in_buffers, size);
227 for(i = 0; i < total_in_buffers; i++)
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];
235 for(k = 0; j < dsp_in_length; j++, k++) current_in[k] = current_in[j];
237 for(; k < dsp_in_length; k++) current_in[k] = 0;
239 //printf("Reverb::process_realtime 2 %d %d\n", total_in_buffers, size);
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());
260 defaults = new BC_Hash(directory);
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);
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);
295 LOAD_CONFIGURATION_MACRO(Reverb, ReverbConfig)
298 void Reverb::save_data(KeyFrame *keyframe)
300 //printf("Reverb::save_data 1\n");
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);
320 output.append_newline();
321 //printf("Reverb::save_data 1\n");
325 output.terminate_string();
326 //printf("Reverb::save_data 2\n");
329 void Reverb::read_data(KeyFrame *keyframe)
332 // cause xml file to read directly from text
333 input.set_shared_string(keyframe->data, strlen(keyframe->data));
336 result = input.read_tag();
340 if(input.tag.title_is("REVERB"))
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);
354 void Reverb::update_gui()
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();
374 int Reverb::load_from_file(char *path)
381 if(in = fopen(path, "rb"))
383 fseek(in, 0, SEEK_END);
385 fseek(in, 0, SEEK_SET);
386 fread(string, length, 1, in);
388 // read_data(string);
394 ErrorBox errorbox("");
396 sprintf(string, _("Couldn't open %s."), path);
397 errorbox.create_objects(string);
398 errorbox.run_window();
405 int Reverb::save_to_file(char *path)
412 // ConfirmSave confirm;
413 // result = confirm.test_file("", path);
418 if(out = fopen(path, "wb"))
420 // save_data(string);
421 fwrite(string, strlen(string), 1, out);
428 ErrorBox errorbox("");
430 sprintf(string, _("Couldn't save %s."), path);
431 errorbox.create_objects(string);
432 errorbox.run_window();
440 ReverbEngine::ReverbEngine(Reverb *plugin)
443 this->plugin = plugin;
449 ReverbEngine::~ReverbEngine()
456 int ReverbEngine::process_overlays(int output_buffer, int64_t size)
458 this->output_buffer = output_buffer;
463 int ReverbEngine::wait_process_overlays()
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
472 if(lowpass == -1 || lowpass >= 20000)
475 for(int i = 0; i < size; i++) out[i] += in[i] * level;
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++)
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);
490 out[i] += out2 * level;
495 void ReverbEngine::run()
498 //printf("ReverbEngine::run 1 %d\n", calculate_realtime());
502 if(completed) return;
505 for(i = 0; i < plugin->total_in_buffers; i++)
507 for(j = 0; j < plugin->config.ref_total + 1; j++)
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,
521 output_lock.unlock();
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,
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",