4 * Copyright (C) 2009-2013 Adam Williams <broadcast at earthling dot net>
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.
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.
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
22 #include "aattachmentpoint.h"
29 #include "automation.h"
30 #include "bcsignals.h"
35 #include "edlsession.h"
38 #include "floatautos.h"
43 #include "pluginarray.h"
44 #include "preferences.h"
45 #include "renderengine.h"
46 #include "mainsession.h"
48 #include "sharedlocation.h"
50 #include "transition.h"
51 #include "transportque.h"
61 AModuleResample::AModuleResample(AModule *module)
64 this->module = module;
65 bzero(nested_output, sizeof(Samples*) * MAX_CHANNELS);
66 nested_allocation = 0;
69 AModuleResample::~AModuleResample()
71 for(int i = 0; i < MAX_CHANNELS; i++)
72 delete nested_output[i];
75 int AModuleResample::read_samples(Samples *buffer, int64_t start, int64_t len)
82 // Files only read going forward.
83 if(get_direction() == PLAY_REVERSE)
88 //printf("AModuleResample::read_samples start=%jd len=%jd\n", start, len);
89 module->file->set_audio_position(start);
90 module->file->set_channel(module->channel);
91 result = module->file->read_samples(buffer, len);
93 // Reverse buffer so resampling filter renders forward.
94 if(get_direction() == PLAY_REVERSE)
95 Resample::reverse_buffer(buffer->get_data(), len);
98 if(module->nested_edl)
102 // Nested EDL generates reversed buffer.
103 for(int i = 0; i < module->nested_edl->session->audio_channels; i++)
105 if(nested_allocation < len)
107 delete nested_output[i];
108 nested_output[i] = 0;
111 if(!nested_output[i])
113 nested_output[i] = new Samples(len);
118 result = module->nested_renderengine->arender->process_buffer(
122 // printf("AModuleResample::read_samples buffer=%p module=%p len=%d\n",
126 memcpy(buffer->get_data(),
127 nested_output[module->channel]->get_data(),
128 len * sizeof(double));
144 AModule::AModule(RenderEngine *renderengine,
145 CommonRender *commonrender,
146 PluginArray *plugin_array,
148 : Module(renderengine, commonrender, plugin_array, track)
150 data_type = TRACK_AUDIO;
155 bzero(nested_output, sizeof(Samples*) * MAX_CHANNELS);
156 bzero(prev_head, SPEED_OVERLAP * sizeof(double));
157 bzero(prev_tail, SPEED_OVERLAP * sizeof(double));
158 nested_allocation = 0;
169 if(transition_temp) delete transition_temp;
170 if(speed_temp) delete speed_temp;
173 delete [] level_history;
174 delete [] level_samples;
177 for(int i = 0; i < MAX_CHANNELS; i++)
181 delete nested_output[i];
188 AttachmentPoint* AModule::new_attachment(Plugin *plugin)
190 return new AAttachmentPoint(renderengine, plugin);
194 void AModule::create_objects()
196 Module::create_objects();
197 // Not needed in pluginarray
200 level_history = new double[((ARender*)commonrender)->total_peaks];
201 level_samples = new int64_t[((ARender*)commonrender)->total_peaks];
204 for(int i = 0; i < ((ARender*)commonrender)->total_peaks; i++)
206 level_history[i] = 0;
207 level_samples[i] = -1;
212 int AModule::get_buffer_size()
215 return renderengine->fragment_len;
217 return plugin_array->get_bufsize();
221 CICache* AModule::get_cache()
224 return renderengine->get_acache();
230 int AModule::import_samples(AEdit *edit,
231 int64_t start_project,
232 int64_t edit_startproject,
233 int64_t edit_startsource,
237 int64_t fragment_len)
240 // start in EDL samplerate
241 int64_t start_source = start_project -
244 // fragment size adjusted for speed curve
245 int64_t speed_fragment_len = fragment_len;
246 // boundaries of input fragment required for speed curve
247 double max_position = 0;
248 double min_position = 0;
250 double speed_position = edit_startsource;
251 // position in source where speed curve starts reading
252 double speed_position1 = speed_position;
253 // position in source where speed curve finishes
254 double speed_position2 = speed_position;
256 // Need speed curve processing
258 // Temporary buffer for rendering speed curve
259 Samples *speed_buffer = buffer;
262 if(debug) printf("AModule::import_samples %d edit=%p nested_edl=%p\n",
266 if(nested_edl && edit->channel >= nested_edl->session->audio_channels)
268 if(debug) printf("AModule::import_samples %d\n", __LINE__);
270 this->channel = edit->channel;
271 if(debug) printf("AModule::import_samples %d speed_fragment_len=%jd\n",
278 // apply speed curve to source position so the timeline agrees with the playback
279 if(track->has_speed())
281 // get speed adjusted position from start of edit.
282 FloatAuto *previous = 0;
284 FloatAutos *speed_autos = (FloatAutos*)track->automation->autos[AUTOMATION_SPEED];
285 for(int64_t i = edit_startproject; i < start_project; i++)
287 double speed = speed_autos->get_value(i,
291 speed_position += speed;
294 speed_position1 = speed_position;
297 // calculate boundaries of input fragment required for speed curve
298 max_position = speed_position;
299 min_position = speed_position;
300 for(int64_t i = start_project; i < start_project + fragment_len; i++)
302 double speed = speed_autos->get_value(i,
306 speed_position += speed;
307 if(speed_position > max_position) max_position = speed_position;
308 if(speed_position < min_position) min_position = speed_position;
311 speed_position2 = speed_position;
312 if(speed_position2 < speed_position1)
315 // min_position -= 1.0;
316 speed_fragment_len = (int64_t)(max_position - min_position);
321 speed_fragment_len = (int64_t)(max_position - min_position);
324 //printf("AModule::import_samples %d %f %f %f %f\n",
325 // __LINE__, min_position, max_position, speed_position1, speed_position2);
327 // new start of source to read from file
328 start_source = (int64_t)min_position;
333 // swap in the temp buffer
334 if(speed_temp && speed_temp->get_allocated() < speed_fragment_len)
342 speed_temp = new Samples(speed_fragment_len);
345 speed_buffer = speed_temp;
350 if(speed_fragment_len == 0)
355 // Source is a nested EDL
361 if(direction == PLAY_REVERSE)
362 command = NORMAL_REWIND;
364 command = NORMAL_FWD;
366 if(debug) printf("AModule::import_samples %d\n", __LINE__);
367 if(!nested_edl || nested_edl->id != edit->nested_edl->id)
369 nested_edl = edit->nested_edl;
370 if(nested_renderengine)
372 delete nested_renderengine;
373 nested_renderengine = 0;
378 nested_command = new TransportCommand;
382 if(!nested_renderengine)
384 nested_command->command = command;
385 nested_command->get_edl()->copy_all(nested_edl);
386 nested_command->change_type = CHANGE_ALL;
387 nested_command->realtime = renderengine->command->realtime;
388 nested_renderengine = new RenderEngine(0, get_preferences(), 0, 1);
389 nested_renderengine->set_acache(get_cache());
390 // Must use a private cache for the audio
393 // cache = new CICache(get_preferences());
394 // private_cache = 1;
396 // nested_renderengine->set_acache(cache);
397 nested_renderengine->arm_command(nested_command);
400 if(debug) printf("AModule::import_samples %d speed_fragment_len=%d\n", __LINE__, (int)speed_fragment_len);
402 // Allocate output buffers for all channels
403 for(int i = 0; i < nested_edl->session->audio_channels; i++)
405 if(nested_allocation < speed_fragment_len)
407 delete nested_output[i];
408 nested_output[i] = 0;
411 if(!nested_output[i])
413 nested_output[i] = new Samples(speed_fragment_len);
416 if(debug) printf("AModule::import_samples %d\n", __LINE__);
418 if(nested_allocation < speed_fragment_len)
419 nested_allocation = speed_fragment_len;
421 // Update direction command
422 nested_renderengine->command->command = command;
424 // Render the segment
425 if(!nested_renderengine->arender)
427 bzero(speed_buffer->get_data(), speed_fragment_len * sizeof(double));
430 if(sample_rate != nested_edl->session->sample_rate)
432 // Read through sample rate converter.
435 resample = new AModuleResample(this);
438 if(debug) printf("AModule::import_samples %d %d %d\n",
441 (int)nested_edl->session->sample_rate);
442 result = resample->resample(speed_buffer,
444 nested_edl->session->sample_rate,
448 // Resample reverses to keep it running forward.
449 if(debug) printf("AModule::import_samples %d\n", __LINE__);
453 // Render without resampling
454 if(debug) printf("AModule::import_samples %d\n", __LINE__);
455 result = nested_renderengine->arender->process_buffer(
459 if(debug) printf("AModule::import_samples %d\n", __LINE__);
460 memcpy(speed_buffer->get_data(),
461 nested_output[edit->channel]->get_data(),
462 speed_fragment_len * sizeof(double));
463 if(debug) printf("AModule::import_samples %d\n", __LINE__);
465 // Reverse fragment so ::render can apply transitions going forward.
466 if(direction == PLAY_REVERSE)
468 Resample::reverse_buffer(speed_buffer->get_data(), speed_fragment_len);
472 if(debug) printf("AModule::import_samples %d\n", __LINE__);
475 // Source is an asset
479 if(debug) printf("AModule::import_samples %d\n", __LINE__);
482 if(debug) printf("AModule::import_samples %d\n", __LINE__);
485 if(debug) printf("AModule::import_samples %d\n", __LINE__);
486 if(nested_renderengine)
488 delete nested_renderengine;
489 nested_renderengine = 0;
492 if(debug) printf("AModule::import_samples %d\n", __LINE__);
494 if(!(file = get_cache()->check_out(
498 // couldn't open source file / skip the edit
499 printf(_("AModule::import_samples Couldn't open %s.\n"), asset->path);
507 if(sample_rate != asset->sample_rate)
509 // Read through sample rate converter.
512 resample = new AModuleResample(this);
515 if(debug) printf("AModule::import_samples %d %d %d\n",
519 result = resample->resample(speed_buffer,
525 // Resample reverses to keep it running forward.
531 printf("AModule::import_samples %d channel=%d start_source=%jd len=%d\n", __LINE__, edit->channel, start_source, (int)speed_fragment_len);
532 file->set_audio_position(start_source);
533 file->set_channel(edit->channel);
534 result = file->read_samples(speed_buffer, speed_fragment_len);
535 // Reverse fragment so ::render can apply transitions going forward.
536 if(debug) printf("AModule::import_samples %d speed_buffer=%p data=%p speed_fragment_len=%d\n",
539 (void*)speed_buffer->get_data(),
540 (int)speed_fragment_len);
541 if(direction == PLAY_REVERSE)
543 Resample::reverse_buffer(speed_buffer->get_data(), speed_fragment_len);
545 if(debug) printf("AModule::import_samples %d\n", __LINE__);
548 if(debug) printf("AModule::import_samples %d\n", __LINE__);
549 get_cache()->check_in(asset);
550 if(debug) printf("AModule::import_samples %d\n", __LINE__);
563 if(debug) printf("AModule::import_samples %d %p %d\n", __LINE__, speed_buffer->get_data(), (int)speed_fragment_len);
564 if(speed_fragment_len > 0) bzero(speed_buffer->get_data(), speed_fragment_len * sizeof(double));
565 if(debug) printf("AModule::import_samples %d\n", __LINE__);
567 if(debug) printf("AModule::import_samples %d\n", __LINE__);
577 // Stretch it to fit the speed curve
578 // Need overlapping buffers to get the interpolation to work, but this
579 // screws up sequential effects.
582 FloatAuto *previous = 0;
584 FloatAutos *speed_autos = (FloatAutos*)track->automation->autos[AUTOMATION_SPEED];
585 double *buffer_samples = buffer->get_data();
586 double *speed_samples = speed_buffer->get_data();
588 //printf("AModule::import_samples %d %lld\n", __LINE__, speed_fragment_len);
590 if(speed_fragment_len == 0)
592 bzero(buffer_samples, fragment_len * sizeof(double));
593 bzero(prev_tail, SPEED_OVERLAP * sizeof(double));
594 bzero(prev_head, SPEED_OVERLAP * sizeof(double));
598 // buffer is now reversed
599 if(direction == PLAY_REVERSE)
602 speed_position = speed_position2;
603 //printf("AModule::import_samples %d %lld %lld\n", __LINE__, start_project, speed_fragment_len);
604 for(int64_t i = start_project + fragment_len;
608 // funky sample reordering, because the source is a reversed buffer
609 int in_offset = (int64_t)(speed_fragment_len - 1 - speed_position);
610 CLAMP(in_offset, 0, speed_fragment_len - 1);
611 buffer_samples[out_offset++] = speed_samples[in_offset];
612 double speed = speed_autos->get_value(i,
616 speed_position -= speed;
618 //printf("AModule::import_samples %d %f\n", __LINE__, speed_position);
623 // position in buffer to read
624 speed_position = speed_position1 - start_source;
626 //printf("AModule::import_samples %d %f\n", __LINE__, speed_position);
627 for(int64_t i = start_project; i < start_project + fragment_len; i++)
629 double speed = speed_autos->get_value(i,
633 double next_speed_position = speed_position + speed;
635 int in_offset = (int)(speed_position);
636 if(fabs(speed) >= 1.0)
638 int total = abs(speed);
640 for(int j = 0; j < total; j++)
642 int in_offset2 = in_offset + (speed > 0 ? j : -j);
644 CLAMP(in_offset2, 0, speed_fragment_len - 1);
645 accum += speed_samples[in_offset2];
649 buffer_samples[out_offset++] = accum / total;
655 // if(in_offset < 0 || in_offset >= speed_fragment_len)
656 // printf("AModule::import_samples %d %d %d\n",
659 // speed_fragment_len);
661 int in_offset1 = in_offset;
662 int in_offset2 = in_offset;
666 in_offset1 += SPEED_OVERLAP;
667 in_offset2 = in_offset1 - 1;
671 in_offset1 -= SPEED_OVERLAP;
672 in_offset2 = in_offset1 + 1;
675 CLAMP(in_offset1, -SPEED_OVERLAP, speed_fragment_len - 1 + SPEED_OVERLAP);
676 CLAMP(in_offset2, -SPEED_OVERLAP, speed_fragment_len - 1 + SPEED_OVERLAP);
679 if(in_offset1 >= speed_fragment_len)
681 value1 = prev_head[in_offset1 - speed_fragment_len];
686 value1 = speed_samples[in_offset1];
690 //printf("AModule::import_samples %d %d\n", __LINE__, in_offset1);
691 value1 = prev_tail[SPEED_OVERLAP + in_offset1];
695 if(in_offset2 >= speed_fragment_len)
697 value2 = prev_head[in_offset2 - speed_fragment_len];
702 value2 = speed_samples()[in_offset2];
706 value2 = prev_tail[SPEED_OVERLAP + in_offset2];
709 double fraction = speed_position - floor(speed_position);
710 buffer_samples[out_offset++] =
711 value1 * (1.0 - fraction) +
714 buffer_samples[out_offset++] = value1;
719 speed_position = next_speed_position;
723 for(int i = 0; i < SPEED_OVERLAP; i++)
725 int offset = speed_fragment_len -
728 CLAMP(offset, 0, speed_fragment_len - 1);
729 //printf("AModule::import_samples %d %d\n", __LINE__, offset, );
730 prev_tail[i] = speed_samples[offset];
732 CLAMP(offset, 0, speed_fragment_len - 1);
733 prev_head[i] = speed_samples[offset];
747 int AModule::render(Samples *buffer,
749 int64_t start_position,
754 int64_t edl_rate = get_edl()->session->sample_rate;
757 if(debug) printf("AModule::render %d\n", __LINE__);
760 start_position += track->nudge *
763 AEdit *playable_edit;
764 int64_t end_position;
765 if(direction == PLAY_FORWARD)
766 end_position = start_position + input_len;
768 end_position = start_position - input_len;
769 int buffer_offset = 0;
773 // // Flip range around so the source is always read forward.
774 // if(direction == PLAY_REVERSE)
776 // start_project -= input_len;
777 // end_position -= input_len;
782 bzero(buffer->get_data(), input_len * sizeof(double));
784 // The EDL is normalized to the requested sample rate because
785 // the requested rate may be the project sample rate and a sample rate
786 // might as well be directly from the source rate to the requested rate.
787 // Get first edit containing the range
788 if(direction == PLAY_FORWARD)
789 playable_edit = (AEdit*)track->edits->first;
791 playable_edit = (AEdit*)track->edits->last;
792 if(debug) printf("AModule::render %d\n", __LINE__);
796 int64_t edit_start = playable_edit->startproject;
797 int64_t edit_end = playable_edit->startproject + playable_edit->length;
799 // Normalize to requested rate
800 edit_start = edit_start * sample_rate / edl_rate;
801 edit_end = edit_end * sample_rate / edl_rate;
803 if(direction == PLAY_FORWARD)
805 if(start_position < edit_end && end_position > edit_start)
809 playable_edit = (AEdit*)playable_edit->next;
813 if(end_position < edit_end && start_position > edit_start)
817 playable_edit = (AEdit*)playable_edit->previous;
822 if(debug) printf("AModule::render %d\n", __LINE__);
828 // Fill output one fragment at a time
829 while(start_position != end_position)
831 int64_t fragment_len = input_len;
833 if(debug) printf("AModule::render %d %jd %jd\n", __LINE__, start_position, end_position);
834 // Clamp fragment to end of input
835 if(direction == PLAY_FORWARD &&
836 start_position + fragment_len > end_position)
837 fragment_len = end_position - start_position;
839 if(direction == PLAY_REVERSE &&
840 start_position - fragment_len < end_position)
841 fragment_len = start_position - end_position;
842 if(debug) printf("AModule::render %d %jd\n", __LINE__, fragment_len);
844 // Normalize position here since update_transition is a boolean operation.
845 update_transition(start_position *
852 AEdit *previous_edit = (AEdit*)playable_edit->previous;
854 // Normalize EDL positions to requested rate
855 int64_t edit_startproject = playable_edit->startproject;
856 int64_t edit_endproject = playable_edit->startproject + playable_edit->length;
857 int64_t edit_startsource = playable_edit->startsource;
858 if(debug) printf("AModule::render %d %jd\n", __LINE__, fragment_len);
860 edit_startproject = edit_startproject * sample_rate / edl_rate;
861 edit_endproject = edit_endproject * sample_rate / edl_rate;
862 edit_startsource = edit_startsource * sample_rate / edl_rate;
863 if(debug) printf("AModule::render %d %jd\n", __LINE__, fragment_len);
867 // Clamp fragment to end of edit
868 if(direction == PLAY_FORWARD &&
869 start_position + fragment_len > edit_endproject)
870 fragment_len = edit_endproject - start_position;
872 if(direction == PLAY_REVERSE &&
873 start_position - fragment_len < edit_startproject)
874 fragment_len = start_position - edit_startproject;
875 if(debug) printf("AModule::render %d %jd\n", __LINE__, fragment_len);
877 // Clamp to end of transition
878 int64_t transition_len = 0;
883 transition_len = transition->length *
886 if(direction == PLAY_FORWARD &&
887 start_position < edit_startproject + transition_len &&
888 start_position + fragment_len > edit_startproject + transition_len)
889 fragment_len = edit_startproject + transition_len - start_position;
891 if(direction == PLAY_REVERSE &&
892 start_position > edit_startproject + transition_len &&
893 start_position - fragment_len < edit_startproject + transition_len)
894 fragment_len = start_position - edit_startproject - transition_len;
896 if(debug) printf("AModule::render %d buffer_offset=%d fragment_len=%jd\n",
901 Samples output(buffer);
902 output.set_offset(output.get_offset() + buffer_offset);
903 if(import_samples(playable_edit,
910 fragment_len)) result = 1;
912 if(debug) printf("AModule::render %d\n", __LINE__);
915 // Read transition into temp and render
916 if(transition && previous_edit)
918 int64_t previous_startproject = previous_edit->startproject *
921 int64_t previous_startsource = previous_edit->startsource *
925 // Allocate transition temp size
926 int transition_fragment_len = fragment_len;
927 if(direction == PLAY_FORWARD &&
928 fragment_len + start_position > edit_startproject + transition_len)
929 fragment_len = edit_startproject + transition_len - start_position;
932 // Read into temp buffers
933 // Temp + master or temp + temp ? temp + master
934 if(transition_temp &&
935 transition_temp->get_allocated() < fragment_len)
937 delete transition_temp;
943 transition_temp = new Samples(fragment_len);
946 if(debug) printf("AModule::render %d %jd\n", __LINE__, fragment_len);
948 if(transition_fragment_len > 0)
950 // Previous_edit is always the outgoing segment, regardless of direction
951 import_samples(previous_edit,
953 previous_startproject,
954 previous_startsource,
958 transition_fragment_len);
959 int64_t current_position;
961 // Reverse buffers here so transitions always render forward.
962 if(direction == PLAY_REVERSE)
964 Resample::reverse_buffer(output.get_data(), transition_fragment_len);
965 Resample::reverse_buffer(transition_temp->get_data(), transition_fragment_len);
966 current_position = start_position -
967 transition_fragment_len -
972 current_position = start_position - edit_startproject;
975 transition_server->process_transition(
979 transition_fragment_len,
982 // Reverse output buffer here so transitions always render forward.
983 if(direction == PLAY_REVERSE)
984 Resample::reverse_buffer(output.get_data(),
985 transition_fragment_len);
988 if(debug) printf("AModule::render %d start_position=%jd end_position=%jd fragment_len=%jd\n",
994 if(direction == PLAY_REVERSE)
996 if(playable_edit && start_position - fragment_len <= edit_startproject)
997 playable_edit = (AEdit*)playable_edit->previous;
1001 if(playable_edit && start_position + fragment_len >= edit_endproject)
1002 playable_edit = (AEdit*)playable_edit->next;
1006 if(fragment_len > 0)
1008 buffer_offset += fragment_len;
1009 if(direction == PLAY_FORWARD)
1010 start_position += fragment_len;
1012 start_position -= fragment_len;
1016 if(debug) printf("AModule::render %d\n", __LINE__);