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;
153 bzero(nested_output, sizeof(Samples*) * MAX_CHANNELS);
154 bzero(prev_head, SPEED_OVERLAP * sizeof(double));
155 bzero(prev_tail, SPEED_OVERLAP * sizeof(double));
156 meter_history = new MeterHistory();
157 nested_allocation = 0;
168 delete transition_temp;
170 delete meter_history;
171 for(int i = 0; i < MAX_CHANNELS; i++)
172 delete nested_output[i];
176 AttachmentPoint* AModule::new_attachment(Plugin *plugin)
178 return new AAttachmentPoint(renderengine, plugin);
182 void AModule::create_objects()
184 Module::create_objects();
185 // Not needed in pluginarray
187 meter_history->init(1, ((ARender*)commonrender)->total_peaks);
188 meter_history->reset_channel(0);
192 int AModule::get_buffer_size()
195 return renderengine->fragment_len;
197 return plugin_array->get_bufsize();
201 CICache* AModule::get_cache()
204 return renderengine->get_acache();
210 int AModule::import_samples(AEdit *edit,
211 int64_t start_project,
212 int64_t edit_startproject,
213 int64_t edit_startsource,
217 int64_t fragment_len)
220 // start in EDL samplerate
221 int64_t start_source = start_project -
224 // fragment size adjusted for speed curve
225 int64_t speed_fragment_len = fragment_len;
226 // boundaries of input fragment required for speed curve
227 double max_position = 0;
228 double min_position = 0;
230 double speed_position = edit_startsource;
231 // position in source where speed curve starts reading
232 double speed_position1 = speed_position;
233 // position in source where speed curve finishes
234 double speed_position2 = speed_position;
236 // Need speed curve processing
238 // Temporary buffer for rendering speed curve
239 Samples *speed_buffer = buffer;
242 if(debug) printf("AModule::import_samples %d edit=%p nested_edl=%p\n",
246 if(nested_edl && edit->channel >= nested_edl->session->audio_channels)
248 if(debug) printf("AModule::import_samples %d\n", __LINE__);
250 this->channel = edit->channel;
251 if(debug) printf("AModule::import_samples %d speed_fragment_len=%jd\n",
258 // apply speed curve to source position so the timeline agrees with the playback
259 if(track->has_speed())
261 // get speed adjusted position from start of edit.
262 FloatAuto *previous = 0;
264 FloatAutos *speed_autos = (FloatAutos*)track->automation->autos[AUTOMATION_SPEED];
265 speed_position += speed_autos->automation_integral(edit_startproject,
266 start_project-edit_startproject, PLAY_FORWARD);
267 speed_position1 = speed_position;
270 // calculate boundaries of input fragment required for speed curve
271 max_position = speed_position;
272 min_position = speed_position;
273 for(int64_t i = start_project; i < start_project + fragment_len; i++)
275 double speed = speed_autos->get_value(i,
279 speed_position += speed;
280 if(speed_position > max_position) max_position = speed_position;
281 if(speed_position < min_position) min_position = speed_position;
284 speed_position2 = speed_position;
285 if(speed_position2 < speed_position1)
288 // min_position -= 1.0;
289 speed_fragment_len = (int64_t)(max_position - min_position);
294 speed_fragment_len = (int64_t)(max_position - min_position);
297 //printf("AModule::import_samples %d %f %f %f %f\n",
298 // __LINE__, min_position, max_position, speed_position1, speed_position2);
300 // new start of source to read from file
301 start_source = (int64_t)min_position;
306 // swap in the temp buffer
307 if(speed_temp && speed_temp->get_allocated() < speed_fragment_len)
315 speed_temp = new Samples(speed_fragment_len);
318 speed_buffer = speed_temp;
323 if(speed_fragment_len == 0)
328 // Source is a nested EDL
334 if(direction == PLAY_REVERSE)
335 command = NORMAL_REWIND;
337 command = NORMAL_FWD;
339 if(debug) printf("AModule::import_samples %d\n", __LINE__);
340 if(!nested_edl || nested_edl->id != edit->nested_edl->id)
342 nested_edl = edit->nested_edl;
343 if(nested_renderengine)
345 delete nested_renderengine;
346 nested_renderengine = 0;
351 nested_command = new TransportCommand;
355 if(!nested_renderengine)
357 nested_command->command = command;
358 nested_command->get_edl()->copy_all(nested_edl);
359 nested_command->change_type = CHANGE_ALL;
360 nested_command->realtime = renderengine->command->realtime;
361 nested_renderengine = new RenderEngine(0, get_preferences(), 0, 1);
362 nested_renderengine->set_acache(get_cache());
363 // Must use a private cache for the audio
366 // cache = new CICache(get_preferences());
367 // private_cache = 1;
369 // nested_renderengine->set_acache(cache);
370 nested_renderengine->arm_command(nested_command);
373 if(debug) printf("AModule::import_samples %d speed_fragment_len=%d\n", __LINE__, (int)speed_fragment_len);
375 // Allocate output buffers for all channels
376 for(int i = 0; i < nested_edl->session->audio_channels; i++)
378 if(nested_allocation < speed_fragment_len)
380 delete nested_output[i];
381 nested_output[i] = 0;
384 if(!nested_output[i])
386 nested_output[i] = new Samples(speed_fragment_len);
389 if(debug) printf("AModule::import_samples %d\n", __LINE__);
391 if(nested_allocation < speed_fragment_len)
392 nested_allocation = speed_fragment_len;
394 // Update direction command
395 nested_renderengine->command->command = command;
397 // Render the segment
398 if(!nested_renderengine->arender)
400 bzero(speed_buffer->get_data(), speed_fragment_len * sizeof(double));
403 if(sample_rate != nested_edl->session->sample_rate)
405 // Read through sample rate converter.
408 resample = new AModuleResample(this);
411 if(debug) printf("AModule::import_samples %d %d %d\n",
414 (int)nested_edl->session->sample_rate);
415 result = resample->resample(speed_buffer,
417 nested_edl->session->sample_rate,
421 // Resample reverses to keep it running forward.
422 if(debug) printf("AModule::import_samples %d\n", __LINE__);
426 // Render without resampling
427 if(debug) printf("AModule::import_samples %d\n", __LINE__);
428 result = nested_renderengine->arender->process_buffer(
432 if(debug) printf("AModule::import_samples %d\n", __LINE__);
433 memcpy(speed_buffer->get_data(),
434 nested_output[edit->channel]->get_data(),
435 speed_fragment_len * sizeof(double));
436 if(debug) printf("AModule::import_samples %d\n", __LINE__);
438 // Reverse fragment so ::render can apply transitions going forward.
439 if(direction == PLAY_REVERSE)
441 Resample::reverse_buffer(speed_buffer->get_data(), speed_fragment_len);
445 if(debug) printf("AModule::import_samples %d\n", __LINE__);
448 // Source is an asset
452 if(debug) printf("AModule::import_samples %d\n", __LINE__);
455 if(debug) printf("AModule::import_samples %d\n", __LINE__);
458 if(debug) printf("AModule::import_samples %d\n", __LINE__);
459 if(nested_renderengine)
461 delete nested_renderengine;
462 nested_renderengine = 0;
465 if(debug) printf("AModule::import_samples %d\n", __LINE__);
467 if(!(file = get_cache()->check_out(
471 // couldn't open source file / skip the edit
472 printf(_("AModule::import_samples Couldn't open %s.\n"), asset->path);
480 if(sample_rate != asset->sample_rate)
482 // Read through sample rate converter.
485 resample = new AModuleResample(this);
488 if(debug) printf("AModule::import_samples %d %d %d\n",
492 result = resample->resample(speed_buffer,
498 // Resample reverses to keep it running forward.
504 printf("AModule::import_samples %d channel=%d start_source=%jd len=%d\n", __LINE__, edit->channel, start_source, (int)speed_fragment_len);
505 file->set_audio_position(start_source);
506 file->set_channel(edit->channel);
507 result = file->read_samples(speed_buffer, speed_fragment_len);
508 // Reverse fragment so ::render can apply transitions going forward.
509 if(debug) printf("AModule::import_samples %d speed_buffer=%p data=%p speed_fragment_len=%d\n",
512 (void*)speed_buffer->get_data(),
513 (int)speed_fragment_len);
514 if(direction == PLAY_REVERSE)
516 Resample::reverse_buffer(speed_buffer->get_data(), speed_fragment_len);
518 if(debug) printf("AModule::import_samples %d\n", __LINE__);
521 if(debug) printf("AModule::import_samples %d\n", __LINE__);
522 get_cache()->check_in(asset);
523 if(debug) printf("AModule::import_samples %d\n", __LINE__);
536 if(debug) printf("AModule::import_samples %d %p %d\n", __LINE__, speed_buffer->get_data(), (int)speed_fragment_len);
537 if(speed_fragment_len > 0) bzero(speed_buffer->get_data(), speed_fragment_len * sizeof(double));
538 if(debug) printf("AModule::import_samples %d\n", __LINE__);
540 if(debug) printf("AModule::import_samples %d\n", __LINE__);
550 // Stretch it to fit the speed curve
551 // Need overlapping buffers to get the interpolation to work, but this
552 // screws up sequential effects.
555 FloatAuto *previous = 0;
557 FloatAutos *speed_autos = (FloatAutos*)track->automation->autos[AUTOMATION_SPEED];
558 double *buffer_samples = buffer->get_data();
559 double *speed_samples = speed_buffer->get_data();
561 //printf("AModule::import_samples %d %lld\n", __LINE__, speed_fragment_len);
563 if(speed_fragment_len == 0)
565 bzero(buffer_samples, fragment_len * sizeof(double));
566 bzero(prev_tail, SPEED_OVERLAP * sizeof(double));
567 bzero(prev_head, SPEED_OVERLAP * sizeof(double));
571 // buffer is now reversed
572 if(direction == PLAY_REVERSE)
575 speed_position = speed_position2;
576 //printf("AModule::import_samples %d %lld %lld\n", __LINE__, start_project, speed_fragment_len);
577 for(int64_t i = start_project + fragment_len;
581 // funky sample reordering, because the source is a reversed buffer
582 int in_offset = (int64_t)(speed_fragment_len - 1 - speed_position);
583 CLAMP(in_offset, 0, speed_fragment_len - 1);
584 buffer_samples[out_offset++] = speed_samples[in_offset];
585 double speed = speed_autos->get_value(i,
589 speed_position -= speed;
591 //printf("AModule::import_samples %d %f\n", __LINE__, speed_position);
596 // position in buffer to read
597 speed_position = speed_position1 - start_source;
599 //printf("AModule::import_samples %d %f\n", __LINE__, speed_position);
600 for(int64_t i = start_project; i < start_project + fragment_len; i++)
602 double speed = speed_autos->get_value(i,
606 double next_speed_position = speed_position + speed;
608 int in_offset = (int)(speed_position);
609 if(fabs(speed) >= 1.0)
611 int total = abs(speed);
613 for(int j = 0; j < total; j++)
615 int in_offset2 = in_offset + (speed > 0 ? j : -j);
617 CLAMP(in_offset2, 0, speed_fragment_len - 1);
618 accum += speed_samples[in_offset2];
622 buffer_samples[out_offset++] = accum / total;
628 // if(in_offset < 0 || in_offset >= speed_fragment_len)
629 // printf("AModule::import_samples %d %d %d\n",
632 // speed_fragment_len);
634 int in_offset1 = in_offset;
635 int in_offset2 = in_offset;
639 in_offset1 += SPEED_OVERLAP;
640 in_offset2 = in_offset1 - 1;
644 in_offset1 -= SPEED_OVERLAP;
645 in_offset2 = in_offset1 + 1;
648 CLAMP(in_offset1, -SPEED_OVERLAP, speed_fragment_len - 1 + SPEED_OVERLAP);
649 CLAMP(in_offset2, -SPEED_OVERLAP, speed_fragment_len - 1 + SPEED_OVERLAP);
652 if(in_offset1 >= speed_fragment_len)
654 value1 = prev_head[in_offset1 - speed_fragment_len];
659 value1 = speed_samples[in_offset1];
663 //printf("AModule::import_samples %d %d\n", __LINE__, in_offset1);
664 value1 = prev_tail[SPEED_OVERLAP + in_offset1];
668 if(in_offset2 >= speed_fragment_len)
670 value2 = prev_head[in_offset2 - speed_fragment_len];
675 value2 = speed_samples()[in_offset2];
679 value2 = prev_tail[SPEED_OVERLAP + in_offset2];
682 double fraction = speed_position - floor(speed_position);
683 buffer_samples[out_offset++] =
684 value1 * (1.0 - fraction) +
687 buffer_samples[out_offset++] = value1;
692 speed_position = next_speed_position;
696 for(int i = 0; i < SPEED_OVERLAP; i++)
698 int offset = speed_fragment_len -
701 CLAMP(offset, 0, speed_fragment_len - 1);
702 //printf("AModule::import_samples %d %d\n", __LINE__, offset, );
703 prev_tail[i] = speed_samples[offset];
705 CLAMP(offset, 0, speed_fragment_len - 1);
706 prev_head[i] = speed_samples[offset];
720 int AModule::render(Samples *buffer,
722 int64_t start_position,
727 int64_t edl_rate = get_edl()->session->sample_rate;
730 if(debug) printf("AModule::render %d\n", __LINE__);
733 start_position += track->nudge *
736 AEdit *playable_edit;
737 int64_t end_position;
738 if(direction == PLAY_FORWARD)
739 end_position = start_position + input_len;
741 end_position = start_position - input_len;
742 int buffer_offset = 0;
746 // // Flip range around so the source is always read forward.
747 // if(direction == PLAY_REVERSE)
749 // start_project -= input_len;
750 // end_position -= input_len;
755 bzero(buffer->get_data(), input_len * sizeof(double));
757 // The EDL is normalized to the requested sample rate because
758 // the requested rate may be the project sample rate and a sample rate
759 // might as well be directly from the source rate to the requested rate.
760 // Get first edit containing the range
761 if(direction == PLAY_FORWARD)
762 playable_edit = (AEdit*)track->edits->first;
764 playable_edit = (AEdit*)track->edits->last;
765 if(debug) printf("AModule::render %d\n", __LINE__);
769 int64_t edit_start = playable_edit->startproject;
770 int64_t edit_end = playable_edit->startproject + playable_edit->length;
772 // Normalize to requested rate
773 edit_start = edit_start * sample_rate / edl_rate;
774 edit_end = edit_end * sample_rate / edl_rate;
776 if(direction == PLAY_FORWARD)
778 if(start_position < edit_end && end_position > edit_start)
782 playable_edit = (AEdit*)playable_edit->next;
786 if(end_position < edit_end && start_position > edit_start)
790 playable_edit = (AEdit*)playable_edit->previous;
795 if(debug) printf("AModule::render %d\n", __LINE__);
801 // Fill output one fragment at a time
802 while(start_position != end_position)
804 int64_t fragment_len = input_len;
806 if(debug) printf("AModule::render %d %jd %jd\n", __LINE__, start_position, end_position);
807 // Clamp fragment to end of input
808 if(direction == PLAY_FORWARD &&
809 start_position + fragment_len > end_position)
810 fragment_len = end_position - start_position;
812 if(direction == PLAY_REVERSE &&
813 start_position - fragment_len < end_position)
814 fragment_len = start_position - end_position;
815 if(debug) printf("AModule::render %d %jd\n", __LINE__, fragment_len);
817 // Normalize position here since update_transition is a boolean operation.
818 update_transition(start_position *
825 AEdit *previous_edit = (AEdit*)playable_edit->previous;
827 // Normalize EDL positions to requested rate
828 int64_t edit_startproject = playable_edit->startproject;
829 int64_t edit_endproject = playable_edit->startproject + playable_edit->length;
830 int64_t edit_startsource = playable_edit->startsource;
831 if(debug) printf("AModule::render %d %jd\n", __LINE__, fragment_len);
833 edit_startproject = edit_startproject * sample_rate / edl_rate;
834 edit_endproject = edit_endproject * sample_rate / edl_rate;
835 edit_startsource = edit_startsource * sample_rate / edl_rate;
836 if(debug) printf("AModule::render %d %jd\n", __LINE__, fragment_len);
840 // Clamp fragment to end of edit
841 if(direction == PLAY_FORWARD &&
842 start_position + fragment_len > edit_endproject)
843 fragment_len = edit_endproject - start_position;
845 if(direction == PLAY_REVERSE &&
846 start_position - fragment_len < edit_startproject)
847 fragment_len = start_position - edit_startproject;
848 if(debug) printf("AModule::render %d %jd\n", __LINE__, fragment_len);
850 // Clamp to end of transition
851 int64_t transition_len = 0;
856 transition_len = transition->length *
859 if(direction == PLAY_FORWARD &&
860 start_position < edit_startproject + transition_len &&
861 start_position + fragment_len > edit_startproject + transition_len)
862 fragment_len = edit_startproject + transition_len - start_position;
864 if(direction == PLAY_REVERSE &&
865 start_position > edit_startproject + transition_len &&
866 start_position - fragment_len < edit_startproject + transition_len)
867 fragment_len = start_position - edit_startproject - transition_len;
869 if(debug) printf("AModule::render %d buffer_offset=%d fragment_len=%jd\n",
874 Samples output(buffer);
875 output.set_offset(output.get_offset() + buffer_offset);
876 if(import_samples(playable_edit,
883 fragment_len)) result = 1;
885 if(debug) printf("AModule::render %d\n", __LINE__);
888 // Read transition into temp and render
889 if(transition && previous_edit)
891 int64_t previous_startproject = previous_edit->startproject *
894 int64_t previous_startsource = previous_edit->startsource *
898 // Allocate transition temp size
899 int transition_fragment_len = fragment_len;
900 if(direction == PLAY_FORWARD &&
901 fragment_len + start_position > edit_startproject + transition_len)
902 fragment_len = edit_startproject + transition_len - start_position;
905 // Read into temp buffers
906 // Temp + master or temp + temp ? temp + master
907 if(transition_temp &&
908 transition_temp->get_allocated() < fragment_len)
910 delete transition_temp;
916 transition_temp = new Samples(fragment_len);
919 if(debug) printf("AModule::render %d %jd\n", __LINE__, fragment_len);
921 if(transition_fragment_len > 0)
923 // Previous_edit is always the outgoing segment, regardless of direction
924 import_samples(previous_edit,
926 previous_startproject,
927 previous_startsource,
931 transition_fragment_len);
932 int64_t current_position;
934 // Reverse buffers here so transitions always render forward.
935 if(direction == PLAY_REVERSE)
937 Resample::reverse_buffer(output.get_data(), transition_fragment_len);
938 Resample::reverse_buffer(transition_temp->get_data(), transition_fragment_len);
939 current_position = start_position -
940 transition_fragment_len -
945 current_position = start_position - edit_startproject;
948 transition_server->process_transition(
952 transition_fragment_len,
955 // Reverse output buffer here so transitions always render forward.
956 if(direction == PLAY_REVERSE)
957 Resample::reverse_buffer(output.get_data(),
958 transition_fragment_len);
961 if(debug) printf("AModule::render %d start_position=%jd end_position=%jd fragment_len=%jd\n",
967 if(direction == PLAY_REVERSE)
969 if(playable_edit && start_position - fragment_len <= edit_startproject)
970 playable_edit = (AEdit*)playable_edit->previous;
974 if(playable_edit && start_position + fragment_len >= edit_endproject)
975 playable_edit = (AEdit*)playable_edit->next;
981 buffer_offset += fragment_len;
982 if(direction == PLAY_FORWARD)
983 start_position += fragment_len;
985 start_position -= fragment_len;
989 if(debug) printf("AModule::render %d\n", __LINE__);