2 Copyright (C) 2006 Paul Davis
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2 of the License, or
7 (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18 #include "pbd/error.h"
20 #include "ardour/amp.h"
21 #include "ardour/audioengine.h"
22 #include "ardour/audiofilesource.h"
23 #include "ardour/audioregion.h"
24 #include "ardour/debug.h"
25 #include "ardour/delivery.h"
26 #include "ardour/disk_reader.h"
27 #include "ardour/disk_writer.h"
28 #include "ardour/event_type_map.h"
29 #include "ardour/io_processor.h"
30 #include "ardour/meter.h"
31 #include "ardour/midi_region.h"
32 #include "ardour/monitor_control.h"
33 #include "ardour/playlist.h"
34 #include "ardour/playlist_factory.h"
35 #include "ardour/port.h"
36 #include "ardour/processor.h"
37 #include "ardour/profile.h"
38 #include "ardour/region_factory.h"
39 #include "ardour/record_enable_control.h"
40 #include "ardour/record_safe_control.h"
41 #include "ardour/route_group_specialized.h"
42 #include "ardour/session.h"
43 #include "ardour/session_playlists.h"
44 #include "ardour/smf_source.h"
45 #include "ardour/track.h"
46 #include "ardour/types_convert.h"
47 #include "ardour/utils.h"
52 using namespace ARDOUR;
55 Track::Track (Session& sess, string name, PresentationInfo::Flag flag, TrackMode mode, DataType default_type)
56 : Route (sess, name, flag, default_type)
57 , _saved_meter_point (_meter_point)
59 , _alignment_choice (Automatic)
61 _freeze_record.state = NoFreeze;
68 DEBUG_TRACE (DEBUG::Destruction, string_compose ("track %1 destructor\n", _name));
71 _disk_reader->set_route (boost::shared_ptr<Route>());
72 _disk_reader.reset ();
76 _disk_writer->set_route (boost::shared_ptr<Route>());
77 _disk_writer.reset ();
88 DiskIOProcessor::Flag dflags = DiskIOProcessor::Recordable;
90 if (_mode == Destructive && !Profile->get_trx()) {
91 dflags = DiskIOProcessor::Flag (dflags | DiskIOProcessor::Destructive);
92 } else if (_mode == NonLayered){
93 dflags = DiskIOProcessor::Flag(dflags | DiskIOProcessor::NonLayered);
96 _disk_reader.reset (new DiskReader (_session, name(), dflags));
97 _disk_reader->set_block_size (_session.get_block_size ());
98 _disk_reader->set_route (boost::dynamic_pointer_cast<Route> (shared_from_this()));
100 _disk_writer.reset (new DiskWriter (_session, name(), dflags));
101 _disk_writer->set_block_size (_session.get_block_size ());
102 _disk_writer->set_route (boost::dynamic_pointer_cast<Route> (shared_from_this()));
104 set_align_choice_from_io ();
106 use_new_playlist (data_type());
108 boost::shared_ptr<Route> rp (boost::dynamic_pointer_cast<Route> (shared_from_this()));
109 boost::shared_ptr<Track> rt = boost::dynamic_pointer_cast<Track> (rp);
111 _record_enable_control.reset (new RecordEnableControl (_session, EventTypeMap::instance().to_symbol (RecEnableAutomation), *this));
112 add_control (_record_enable_control);
114 _record_safe_control.reset (new RecordSafeControl (_session, EventTypeMap::instance().to_symbol (RecSafeAutomation), *this));
115 add_control (_record_safe_control);
117 _monitoring_control.reset (new MonitorControl (_session, EventTypeMap::instance().to_symbol (MonitoringAutomation), *this));
118 add_control (_monitoring_control);
120 _session.config.ParameterChanged.connect_same_thread (*this, boost::bind (&Track::parameter_changed, this, _1));
122 _monitoring_control->Changed.connect_same_thread (*this, boost::bind (&Track::monitoring_changed, this, _1, _2));
123 _record_safe_control->Changed.connect_same_thread (*this, boost::bind (&Track::record_safe_changed, this, _1, _2));
124 _record_enable_control->Changed.connect_same_thread (*this, boost::bind (&Track::record_enable_changed, this, _1, _2));
126 _input->changed.connect_same_thread (*this, boost::bind (&Track::input_changed, this));
132 Track::input_changed ()
134 if (_disk_writer && _alignment_choice == Automatic) {
135 set_align_choice_from_io ();
146 Track::state (bool full)
148 XMLNode& root (Route::state (full));
150 if (_playlists[DataType::AUDIO]) {
151 root.set_property (X_("audio-playlist"), _playlists[DataType::AUDIO]->id().to_s());
154 if (_playlists[DataType::MIDI]) {
155 root.set_property (X_("midi-playlist"), _playlists[DataType::MIDI]->id().to_s());
158 root.add_child_nocopy (_monitoring_control->get_state ());
159 root.add_child_nocopy (_record_safe_control->get_state ());
160 root.add_child_nocopy (_record_enable_control->get_state ());
162 root.set_property (X_("saved-meter-point"), _saved_meter_point);
163 root.set_property (X_("alignment-choice"), _alignment_choice);
169 Track::set_state (const XMLNode& node, int version)
171 if (Route::set_state (node, version)) {
177 if (version >= 3000 && version < 4000) {
178 if ((child = find_named_node (node, X_("Diskstream"))) != 0) {
179 /* XXX if we remember anything from stored DiskStream
180 state (older Ardour versions) that is needed by a
181 DiskReader or DiskWriter, we should cook up a new
182 XMLNode here, populate it with that information
183 (child nodes, properties, etc.) and then call
184 ::set_state() on the writer/reader.
186 But at present (June 2017), there's no such state.
191 std::string playlist_id;
193 if (node.get_property (X_("audio-playlist"), playlist_id)) {
194 find_and_use_playlist (DataType::AUDIO, PBD::ID (playlist_id));
197 if (node.get_property (X_("midi-playlist"), playlist_id)) {
198 find_and_use_playlist (DataType::MIDI, PBD::ID (playlist_id));
201 XMLNodeList nlist = node.children();
202 for (XMLNodeConstIterator niter = nlist.begin(); niter != nlist.end(); ++niter) {
205 if (child->name() == Controllable::xml_node_name) {
207 if (!child->get_property ("name", name)) {
211 if (name == _record_enable_control->name()) {
212 _record_enable_control->set_state (*child, version);
213 } else if (name == _record_safe_control->name()) {
214 _record_safe_control->set_state (*child, version);
215 } else if (name == _monitoring_control->name()) {
216 _monitoring_control->set_state (*child, version);
221 if (!node.get_property (X_("saved-meter-point"), _saved_meter_point)) {
222 _saved_meter_point = _meter_point;
228 if (node.get_property (X_("alignment-choice"), ac)) {
229 set_align_choice (ac, true);
236 Track::get_template ()
238 return state (false);
241 Track::FreezeRecord::~FreezeRecord ()
243 for (vector<FreezeRecordProcessorInfo*>::iterator i = processor_info.begin(); i != processor_info.end(); ++i) {
249 Track::freeze_state() const
251 return _freeze_record.state;
257 bool will_record = true;
258 for (PortSet::iterator i = _input->ports().begin(); i != _input->ports().end() && will_record; ++i) {
267 Track::prep_record_enabled (bool yn)
269 if (yn && _record_safe_control->get_value()) {
273 if (!can_be_record_enabled()) {
277 /* keep track of the meter point as it was before we rec-enabled */
278 if (!_disk_writer->record_enabled()) {
279 _saved_meter_point = _meter_point;
285 will_follow = _disk_writer->prep_record_enable ();
287 will_follow = _disk_writer->prep_record_disable ();
292 if (_meter_point != MeterCustom) {
293 set_meter_point (MeterInput);
296 set_meter_point (_saved_meter_point);
304 Track::record_enable_changed (bool, Controllable::GroupControlDisposition)
306 _disk_writer->set_record_enabled (_record_enable_control->get_value());
310 Track::record_safe_changed (bool, Controllable::GroupControlDisposition)
312 _disk_writer->set_record_safe (_record_safe_control->get_value());
316 Track::can_be_record_safe ()
318 return !_record_enable_control->get_value() && _disk_writer && _session.writable() && (_freeze_record.state != Frozen);
322 Track::can_be_record_enabled ()
324 return !_record_safe_control->get_value() && _disk_writer && !_disk_writer->record_safe() && _session.writable() && (_freeze_record.state != Frozen);
328 Track::parameter_changed (string const & p)
330 if (p == "track-name-number") {
331 resync_track_name ();
333 else if (p == "track-name-take") {
334 resync_track_name ();
336 else if (p == "take-name") {
337 if (_session.config.get_track_name_take()) {
338 resync_track_name ();
344 Track::resync_track_name ()
350 Track::set_name (const string& str)
358 if (_record_enable_control->get_value()) {
359 /* when re-arm'ed the file (named after the track) is already ready to rolll */
363 string diskstream_name = "";
364 if (_session.config.get_track_name_take () && !_session.config.get_take_name ().empty()) {
365 // Note: any text is fine, legalize_for_path() fixes this later
366 diskstream_name += _session.config.get_take_name ();
367 diskstream_name += "_";
369 const int64_t tracknumber = track_number();
370 if (tracknumber > 0 && _session.config.get_track_name_number()) {
371 char num[64], fmt[10];
372 snprintf(fmt, sizeof(fmt), "%%0%d" PRId64, _session.track_number_decimals());
373 snprintf(num, sizeof(num), fmt, tracknumber);
374 diskstream_name += num;
375 diskstream_name += "_";
377 diskstream_name += str;
379 if (diskstream_name == _diskstream_name) {
382 _diskstream_name = diskstream_name;
384 _disk_writer->set_write_source_name (diskstream_name);
386 boost::shared_ptr<Track> me = boost::dynamic_pointer_cast<Track> (shared_from_this ());
388 if (_playlists[data_type()]->all_regions_empty () && _session.playlists->playlists_for_track (me).size() == 1) {
389 /* Only rename the diskstream (and therefore the playlist) if
390 a) the playlist has never had a region added to it and
391 b) there is only one playlist for this track.
393 If (a) is not followed, people can get confused if, say,
394 they have notes about a playlist with a given name and then
395 it changes (see mantis #4759).
397 If (b) is not followed, we rename the current playlist and not
398 the other ones, which is a bit confusing (see mantis #4977).
400 _disk_reader->set_name (str);
401 _disk_writer->set_name (str);
404 for (uint32_t n = 0; n < DataType::num_types; ++n) {
406 _playlists[n]->set_name (str);
410 /* save state so that the statefile fully reflects any filename changes */
412 if ((ret = Route::set_name (str)) == 0) {
413 _session.save_state ("");
420 Track::no_roll (pframes_t nframes, samplepos_t start_sample, samplepos_t end_sample, bool session_state_changing)
422 Glib::Threads::RWLock::ReaderLock lm (_processor_lock, Glib::Threads::TRY_LOCK);
428 /* no outputs? nothing to do ... what happens if we have sends etc. ? */
430 if (n_outputs().n_total() == 0 && !ARDOUR::Profile->get_mixbus()) {
431 //Note: Mixbus has its own output mechanism, so we should operate even if no explicit outputs are assigned
435 /* not active ... do the minimum possible by just outputting silence */
439 if (_meter_point == MeterInput && ((_monitoring_control->monitoring_choice() & MonitorInput) || _disk_writer->record_enabled())) {
445 if (session_state_changing) {
446 if (_session.transport_speed() != 0.0f) {
447 /* we're rolling but some state is changing (e.g. our
448 disk reader contents) so we cannot use them. Be
449 silent till this is over. Don't declick.
451 XXX note the absurdity of ::no_roll() being called when we ARE rolling!
453 passthru_silence (start_sample, end_sample, nframes, 0);
456 /* we're really not rolling, so we're either delivery silence or actually
457 monitoring, both of which are safe to do while session_state_changing is true.
462 MonitorState const s = monitoring_state ();
463 /* we are not rolling, so be silent even if we are monitoring disk, as there
464 will be no disk data coming in.
467 case MonitoringSilence:
473 case MonitoringInput:
481 //if we have an internal generator, let it play regardless of monitoring state
482 if (_have_internal_generator) {
486 /* if have_internal_generator, or .. */
490 if (_meter_point == MeterInput) {
491 /* still need input monitoring and metering */
493 bool const track_rec = _disk_writer->record_enabled ();
494 bool const auto_input = _session.config.get_auto_input ();
495 bool const software_monitor = Config->get_monitoring_model() == SoftwareMonitoring;
496 bool const tape_machine_mode = Config->get_tape_machine_mode ();
497 bool no_meter = false;
499 /* this needs a proper K-map
500 * and should be separated into a function similar to monitoring_state()
501 * that also handles roll() states in audio_track.cc, midi_track.cc and route.cc
503 * see http://www.oofus.co.uk/ardour/Ardour3MonitorModesV3.pdf
505 if (!auto_input && !track_rec) {
508 else if (tape_machine_mode && !track_rec && auto_input) {
511 else if (!software_monitor && tape_machine_mode && !track_rec) {
514 else if (!software_monitor && !tape_machine_mode && !track_rec && !auto_input) {
519 BufferSet& bufs (_session.get_silent_buffers (n_process_buffers()));
520 _meter->run (bufs, start_sample, end_sample, 1.0, nframes, true);
521 _input->process_input (boost::shared_ptr<Processor>(), start_sample, end_sample, _session.transport_speed(), nframes);
523 _input->process_input (_meter, start_sample, end_sample, _session.transport_speed(), nframes);
527 passthru_silence (start_sample, end_sample, nframes, 0);
531 BufferSet& bufs = _session.get_route_buffers (n_process_buffers());
533 fill_buffers_with_input (bufs, _input, nframes);
535 if (_meter_point == MeterInput) {
536 _meter->run (bufs, start_sample, end_sample, _session.transport_speed(), nframes, true);
539 passthru (bufs, start_sample, end_sample, nframes, false, true);
542 flush_processor_buffers_locked (nframes);
547 boost::shared_ptr<Playlist>
550 return _playlists[data_type()];
554 Track::request_input_monitoring (bool m)
556 for (PortSet::iterator i = _input->ports().begin(); i != _input->ports().end(); ++i) {
557 AudioEngine::instance()->request_input_monitoring ((*i)->name(), m);
562 Track::ensure_input_monitoring (bool m)
564 for (PortSet::iterator i = _input->ports().begin(); i != _input->ports().end(); ++i) {
565 AudioEngine::instance()->ensure_input_monitoring ((*i)->name(), m);
570 Track::destructive () const
572 return _disk_writer->destructive ();
575 list<boost::shared_ptr<Source> > &
576 Track::last_capture_sources ()
578 return _disk_writer->last_capture_sources ();
582 Track::update_latency_information ()
584 Glib::Threads::RWLock::ReaderLock lr (_processor_lock);
585 samplecnt_t chain_latency = _input->latency ();
587 for (ProcessorList::iterator p = _processors.begin(); p != _processors.end(); ++p) {
588 (*p)->set_input_latency (chain_latency);
589 chain_latency += (*p)->signal_latency ();
594 Track::steal_write_source_name()
596 return _disk_writer->steal_write_source_name ();
600 Track::reset_write_sources (bool r, bool force)
602 _disk_writer->reset_write_sources (r, force);
606 Track::playback_buffer_load () const
608 return _disk_reader->buffer_load ();
612 Track::capture_buffer_load () const
614 return _disk_writer->buffer_load ();
620 return _disk_reader->do_refill ();
624 Track::do_flush (RunContext c, bool force)
626 return _disk_writer->do_flush (c, force);
630 Track::set_pending_overwrite (bool o)
632 _disk_reader->set_pending_overwrite (o);
636 Track::seek (samplepos_t p, bool complete_refill)
638 if (_disk_reader->seek (p, complete_refill)) {
641 return _disk_writer->seek (p, complete_refill);
645 Track::can_internal_playback_seek (samplecnt_t p)
647 return _disk_reader->can_internal_playback_seek (p);
651 Track::internal_playback_seek (samplecnt_t p)
653 return _disk_reader->internal_playback_seek (p);
657 Track::non_realtime_locate (samplepos_t p)
659 Route::non_realtime_locate (p);
661 if (!is_private_route()) {
662 /* don't waste i/o cycles and butler calls
663 for private tracks (e.g.auditioner)
665 _disk_reader->non_realtime_locate (p);
666 _disk_writer->non_realtime_locate (p);
671 Track::non_realtime_speed_change ()
673 _disk_reader->non_realtime_speed_change ();
677 Track::overwrite_existing_buffers ()
679 return _disk_reader->overwrite_existing_buffers ();
683 Track::get_captured_samples (uint32_t n) const
685 return _disk_writer->get_captured_samples (n);
689 Track::set_loop (Location* l)
691 if (_disk_reader->set_loop (l)) {
694 return _disk_writer->set_loop (l);
698 Track::transport_looped (samplepos_t p)
700 return _disk_writer->transport_looped (p);
704 Track::realtime_speed_change ()
706 if (_disk_reader->realtime_speed_change ()) {
709 return _disk_writer->realtime_speed_change ();
713 Track::realtime_handle_transport_stopped ()
715 Glib::Threads::RWLock::ReaderLock lm (_processor_lock, Glib::Threads::TRY_LOCK);
721 for (ProcessorList::iterator i = _processors.begin(); i != _processors.end(); ++i) {
722 (*i)->realtime_handle_transport_stopped ();
727 Track::transport_stopped_wallclock (struct tm & n, time_t t, bool g)
729 _disk_writer->transport_stopped_wallclock (n, t, g);
733 Track::pending_overwrite () const
735 return _disk_reader->pending_overwrite ();
739 Track::set_slaved (bool s)
741 _disk_reader->set_slaved (s);
742 _disk_writer->set_slaved (s);
748 return _disk_reader->output_streams();
752 Track::get_capture_start_sample (uint32_t n) const
754 return _disk_writer->get_capture_start_sample (n);
758 Track::alignment_style () const
760 return _disk_writer->alignment_style ();
764 Track::alignment_choice () const
766 return _alignment_choice;
770 Track::current_capture_start () const
772 return _disk_writer->current_capture_start ();
776 Track::current_capture_end () const
778 return _disk_writer->current_capture_end ();
782 Track::playlist_modified ()
784 _disk_reader->playlist_modified ();
788 Track::find_and_use_playlist (DataType dt, PBD::ID const & id)
790 boost::shared_ptr<Playlist> playlist;
792 if ((playlist = _session.playlists->by_id (id)) == 0) {
797 error << string_compose(_("DiskIOProcessor: \"%1\" isn't an playlist"), id.to_s()) << endmsg;
801 return use_playlist (dt, playlist);
805 Track::use_playlist (DataType dt, boost::shared_ptr<Playlist> p)
809 if ((ret = _disk_reader->use_playlist (dt, p)) == 0) {
810 if ((ret = _disk_writer->use_playlist (dt, p)) == 0) {
811 p->set_orig_track_id (id());
819 _session.set_dirty ();
820 PlaylistChanged (); /* EMIT SIGNAL */
826 Track::use_copy_playlist ()
828 assert (_playlists[data_type()]);
830 if (_playlists[data_type()] == 0) {
831 error << string_compose(_("DiskIOProcessor %1: there is no existing playlist to make a copy of!"), _name) << endmsg;
836 boost::shared_ptr<Playlist> playlist;
838 newname = Playlist::bump_name (_playlists[data_type()]->name(), _session);
840 if ((playlist = PlaylistFactory::create (_playlists[data_type()], newname)) == 0) {
844 playlist->reset_shares();
846 return use_playlist (data_type(), playlist);
850 Track::use_new_playlist (DataType dt)
853 boost::shared_ptr<Playlist> playlist = _playlists[dt];
856 newname = Playlist::bump_name (playlist->name(), _session);
858 newname = Playlist::bump_name (_name, _session);
861 playlist = PlaylistFactory::create (dt, _session, newname, is_private_route());
867 return use_playlist (dt, playlist);
871 Track::set_align_choice (AlignChoice ac, bool force)
873 _alignment_choice = ac;
876 set_align_choice_from_io ();
879 _disk_writer->set_align_style (CaptureTime, force);
881 case UseExistingMaterial:
882 _disk_writer->set_align_style (ExistingMaterial, force);
888 Track::set_align_style (AlignStyle s, bool force)
890 _disk_writer->set_align_style (s, force);
894 Track::set_align_choice_from_io ()
896 bool have_physical = false;
900 vector<string> connections;
901 boost::shared_ptr<Port> p;
905 p = _input->nth (n++);
911 if (p->get_connections (connections) != 0) {
912 if (AudioEngine::instance()->port_is_physical (connections[0])) {
913 have_physical = true;
918 connections.clear ();
923 // compensate for latency when bouncing from master or mixbus.
924 // we need to use "ExistingMaterial" to pick up the master bus' latency
925 // see also Route::direct_feeds_according_to_reality
927 ios.push_back (_input);
928 if (_session.master_out() && ios.fed_by (_session.master_out()->output())) {
929 have_physical = true;
931 for (uint32_t n = 0; n < NUM_MIXBUSES && !have_physical; ++n) {
932 if (_session.get_mixbus (n) && ios.fed_by (_session.get_mixbus(n)->output())) {
933 have_physical = true;
939 _disk_writer->set_align_style (ExistingMaterial);
941 _disk_writer->set_align_style (CaptureTime);
946 Track::set_block_size (pframes_t n)
948 Route::set_block_size (n);
949 _disk_reader->set_block_size (n);
950 _disk_writer->set_block_size (n);
954 Track::adjust_playback_buffering ()
957 _disk_reader->adjust_buffering ();
962 Track::adjust_capture_buffering ()
965 _disk_writer->adjust_buffering ();
971 Track::maybe_declick (BufferSet& bufs, samplecnt_t nframes, int declick)
973 /* never declick if there is an internal generator - we just want it to
974 keep generating sound without interruption.
976 ditto if we are monitoring inputs.
979 if (_have_internal_generator || (_monitoring_control->monitoring_choice() == MonitorInput)) {
984 declick = _pending_declick;
988 Amp::declick (bufs, nframes, declick);
993 Track::monitoring_changed (bool, Controllable::GroupControlDisposition)
995 for (ProcessorList::iterator i = _processors.begin(); i != _processors.end(); ++i) {
996 (*i)->monitoring_changed ();
1001 Track::metering_state () const
1004 if (_session.transport_rolling ()) {
1005 // audio_track.cc || midi_track.cc roll() runs meter IFF:
1006 rv = _meter_point == MeterInput && ((_monitoring_control->monitoring_choice() & MonitorInput) || _disk_writer->record_enabled());
1008 // track no_roll() always metering if
1009 rv = _meter_point == MeterInput;
1011 return rv ? MeteringInput : MeteringRoute;
1015 Track::set_processor_state (XMLNode const & node, XMLProperty const* prop, ProcessorList& new_order, bool& must_configure)
1017 if (Route::set_processor_state (node, prop, new_order, must_configure)) {
1021 cerr << name() << " looking for state for track procs, DR = " << _disk_reader << endl;
1023 if (prop->value() == "diskreader") {
1025 _disk_reader->set_state (node, Stateful::current_state_version);
1026 new_order.push_back (_disk_reader);
1029 } else if (prop->value() == "diskwriter") {
1031 _disk_writer->set_state (node, Stateful::current_state_version);
1032 new_order.push_back (_disk_writer);
1037 error << string_compose(_("unknown Processor type \"%1\"; ignored"), prop->value()) << endmsg;
1042 Track::use_captured_sources (SourceList& srcs, CaptureInfos const & capture_info)
1048 boost::shared_ptr<AudioFileSource> afs = boost::dynamic_pointer_cast<AudioFileSource> (srcs.front());
1049 boost::shared_ptr<SMFSource> mfs = boost::dynamic_pointer_cast<SMFSource> (srcs.front());
1052 use_captured_audio_sources (srcs, capture_info);
1056 use_captured_midi_sources (srcs, capture_info);
1061 Track::use_captured_midi_sources (SourceList& srcs, CaptureInfos const & capture_info)
1063 if (srcs.empty() || data_type() != DataType::MIDI) {
1067 boost::shared_ptr<SMFSource> mfs = boost::dynamic_pointer_cast<SMFSource> (srcs.front());
1068 boost::shared_ptr<Playlist> pl = _playlists[DataType::MIDI];
1069 boost::shared_ptr<MidiRegion> midi_region;
1070 CaptureInfos::const_iterator ci;
1076 samplecnt_t total_capture = 0;
1078 for (total_capture = 0, ci = capture_info.begin(); ci != capture_info.end(); ++ci) {
1079 total_capture += (*ci)->samples;
1082 /* we will want to be able to keep (over)writing the source
1083 but we don't want it to be removable. this also differs
1084 from the audio situation, where the source at this point
1085 must be considered immutable. luckily, we can rely on
1086 MidiSource::mark_streaming_write_completed() to have
1087 already done the necessary work for that.
1090 string whole_file_region_name;
1091 whole_file_region_name = region_name_from_path (mfs->name(), true);
1093 /* Register a new region with the Session that
1094 describes the entire source. Do this first
1095 so that any sub-regions will obviously be
1096 children of this one (later!)
1102 plist.add (Properties::name, whole_file_region_name);
1103 plist.add (Properties::whole_file, true);
1104 plist.add (Properties::automatic, true);
1105 plist.add (Properties::start, 0);
1106 plist.add (Properties::length, total_capture);
1107 plist.add (Properties::layer, 0);
1109 boost::shared_ptr<Region> rx (RegionFactory::create (srcs, plist));
1111 midi_region = boost::dynamic_pointer_cast<MidiRegion> (rx);
1112 midi_region->special_set_position (capture_info.front()->start);
1115 catch (failed_constructor& err) {
1116 error << string_compose(_("%1: could not create region for complete midi file"), _name) << endmsg;
1120 pl->clear_changes ();
1123 /* Session sample time of the initial capture in this pass, which is where the source starts */
1124 samplepos_t initial_capture = 0;
1125 if (!capture_info.empty()) {
1126 initial_capture = capture_info.front()->start;
1129 BeatsSamplesConverter converter (_session.tempo_map(), capture_info.front()->start);
1130 const samplepos_t preroll_off = _session.preroll_record_trim_len ();
1132 for (ci = capture_info.begin(); ci != capture_info.end(); ++ci) {
1136 RegionFactory::region_name (region_name, mfs->name(), false);
1138 DEBUG_TRACE (DEBUG::CaptureAlignment, string_compose ("%1 capture start @ %2 length %3 add new region %4\n",
1139 _name, (*ci)->start, (*ci)->samples, region_name));
1142 // cerr << _name << ": based on ci of " << (*ci)->start << " for " << (*ci)->samples << " add a region\n";
1147 /* start of this region is the offset between the start of its capture and the start of the whole pass */
1148 plist.add (Properties::start, (*ci)->start - initial_capture);
1149 plist.add (Properties::length, (*ci)->samples);
1150 plist.add (Properties::length_beats, converter.from((*ci)->samples).to_double());
1151 plist.add (Properties::name, region_name);
1153 boost::shared_ptr<Region> rx (RegionFactory::create (srcs, plist));
1154 midi_region = boost::dynamic_pointer_cast<MidiRegion> (rx);
1155 if (preroll_off > 0) {
1156 midi_region->trim_front ((*ci)->start - initial_capture + preroll_off);
1160 catch (failed_constructor& err) {
1161 error << _("MidiDiskstream: could not create region for captured midi!") << endmsg;
1162 continue; /* XXX is this OK? */
1165 // cerr << "add new region, buffer position = " << buffer_position << " @ " << (*ci)->start << endl;
1167 pl->add_region (midi_region, (*ci)->start + preroll_off, _disk_writer->non_layered());
1171 _session.add_command (new StatefulDiffCommand (pl));
1175 Track::use_captured_audio_sources (SourceList& srcs, CaptureInfos const & capture_info)
1177 if (srcs.empty() || data_type() != DataType::AUDIO) {
1181 boost::shared_ptr<AudioFileSource> afs = boost::dynamic_pointer_cast<AudioFileSource> (srcs.front());
1182 boost::shared_ptr<Playlist> pl = _playlists[DataType::AUDIO];
1183 boost::shared_ptr<AudioRegion> region;
1189 /* destructive tracks have a single, never changing region */
1191 if (destructive()) {
1193 /* send a signal that any UI can pick up to do the right thing. there is
1194 a small problem here in that a UI may need the peak data to be ready
1195 for the data that was recorded and this isn't interlocked with that
1196 process. this problem is deferred to the UI.
1199 pl->LayeringChanged(); // XXX this may not get the UI to do the right thing
1203 string whole_file_region_name;
1204 whole_file_region_name = region_name_from_path (afs->name(), true);
1206 /* Register a new region with the Session that
1207 describes the entire source. Do this first
1208 so that any sub-regions will obviously be
1209 children of this one (later!)
1215 plist.add (Properties::start, afs->last_capture_start_sample());
1216 plist.add (Properties::length, afs->length(0));
1217 plist.add (Properties::name, whole_file_region_name);
1218 boost::shared_ptr<Region> rx (RegionFactory::create (srcs, plist));
1219 rx->set_automatic (true);
1220 rx->set_whole_file (true);
1222 region = boost::dynamic_pointer_cast<AudioRegion> (rx);
1223 region->special_set_position (afs->natural_position());
1227 catch (failed_constructor& err) {
1228 error << string_compose(_("%1: could not create region for complete audio file"), _name) << endmsg;
1232 pl->clear_changes ();
1233 pl->set_capture_insertion_in_progress (true);
1236 const samplepos_t preroll_off = _session.preroll_record_trim_len ();
1237 samplecnt_t buffer_position = afs->last_capture_start_sample ();
1238 CaptureInfos::const_iterator ci;
1240 for (ci = capture_info.begin(); ci != capture_info.end(); ++ci) {
1244 RegionFactory::region_name (region_name, whole_file_region_name, false);
1246 DEBUG_TRACE (DEBUG::CaptureAlignment, string_compose ("%1 capture bufpos %5 start @ %2 length %3 add new region %4\n",
1247 _name, (*ci)->start, (*ci)->samples, region_name, buffer_position));
1253 plist.add (Properties::start, buffer_position);
1254 plist.add (Properties::length, (*ci)->samples);
1255 plist.add (Properties::name, region_name);
1257 boost::shared_ptr<Region> rx (RegionFactory::create (srcs, plist));
1258 region = boost::dynamic_pointer_cast<AudioRegion> (rx);
1259 if (preroll_off > 0) {
1260 region->trim_front (buffer_position + preroll_off);
1264 catch (failed_constructor& err) {
1265 error << _("AudioDiskstream: could not create region for captured audio!") << endmsg;
1266 continue; /* XXX is this OK? */
1269 pl->add_region (region, (*ci)->start + preroll_off, 1, _disk_writer->non_layered());
1270 pl->set_layer (region, DBL_MAX);
1272 buffer_position += (*ci)->samples;
1276 pl->set_capture_insertion_in_progress (false);
1277 _session.add_command (new StatefulDiffCommand (pl));