X-Git-Url: https://main.carlh.net/gitweb/?a=blobdiff_plain;f=libs%2Fardour%2Ftrack.cc;h=72948b4a98a5ffc90c9bae6a99ec3cf78c523146;hb=9885f04fe6e69bce59271aaa98998b7331153bc0;hp=41769ae471f0cbd189d027b37d179a5de8f819af;hpb=53e6f13852f1cc831002cce9dc84d07e7e526515;p=ardour.git diff --git a/libs/ardour/track.cc b/libs/ardour/track.cc index 41769ae471..72948b4a98 100644 --- a/libs/ardour/track.cc +++ b/libs/ardour/track.cc @@ -19,6 +19,8 @@ #include "ardour/amp.h" #include "ardour/audioengine.h" +#include "ardour/audiofilesource.h" +#include "ardour/audioregion.h" #include "ardour/debug.h" #include "ardour/delivery.h" #include "ardour/disk_reader.h" @@ -26,17 +28,20 @@ #include "ardour/event_type_map.h" #include "ardour/io_processor.h" #include "ardour/meter.h" +#include "ardour/midi_region.h" #include "ardour/monitor_control.h" #include "ardour/playlist.h" #include "ardour/playlist_factory.h" #include "ardour/port.h" #include "ardour/processor.h" #include "ardour/profile.h" +#include "ardour/region_factory.h" #include "ardour/record_enable_control.h" #include "ardour/record_safe_control.h" #include "ardour/route_group_specialized.h" #include "ardour/session.h" #include "ardour/session_playlists.h" +#include "ardour/smf_source.h" #include "ardour/track.h" #include "ardour/types_convert.h" #include "ardour/utils.h" @@ -50,7 +55,9 @@ using namespace PBD; Track::Track (Session& sess, string name, PresentationInfo::Flag flag, TrackMode mode, DataType default_type) : Route (sess, name, flag, default_type) , _saved_meter_point (_meter_point) + , _disk_io_point (DiskIOPreFader) , _mode (mode) + , _alignment_choice (Automatic) { _freeze_record.state = NoFreeze; _declickable = true; @@ -59,6 +66,16 @@ Track::Track (Session& sess, string name, PresentationInfo::Flag flag, TrackMode Track::~Track () { DEBUG_TRACE (DEBUG::Destruction, string_compose ("track %1 destructor\n", _name)); + + if (_disk_reader) { + _disk_reader->set_route (boost::shared_ptr()); + _disk_reader.reset (); + } + + if (_disk_writer) { + _disk_writer->set_route (boost::shared_ptr()); + _disk_writer.reset (); + } } int @@ -77,18 +94,18 @@ Track::init () } _disk_reader.reset (new DiskReader (_session, name(), dflags)); - _disk_reader->set_block_size (_session.get_block_size ()); - _disk_reader->set_route (shared_from_this()); + _disk_reader->set_route (boost::dynamic_pointer_cast (shared_from_this())); _disk_writer.reset (new DiskWriter (_session, name(), dflags)); _disk_writer->set_block_size (_session.get_block_size ()); - _disk_writer->set_route (shared_from_this()); + _disk_writer->set_route (boost::dynamic_pointer_cast (shared_from_this())); use_new_playlist (); - add_processor (_disk_writer, PreFader); - add_processor (_disk_reader, PreFader); + /* disk writer and reader processors will be added when Route calls + * setup_invisible_processors_oh_children_of_mine (). + */ boost::shared_ptr rp (boost::dynamic_pointer_cast (shared_from_this())); boost::shared_ptr rt = boost::dynamic_pointer_cast (rp); @@ -108,9 +125,19 @@ Track::init () _record_safe_control->Changed.connect_same_thread (*this, boost::bind (&Track::record_safe_changed, this, _1, _2)); _record_enable_control->Changed.connect_same_thread (*this, boost::bind (&Track::record_enable_changed, this, _1, _2)); + _input->changed.connect_same_thread (*this, boost::bind (&Track::input_changed, this)); + return 0; } +void +Track::input_changed () +{ + if (_disk_writer && _alignment_choice == Automatic) { + set_align_choice_from_io (); + } +} + XMLNode& Track::get_state () { @@ -123,11 +150,11 @@ Track::state (bool full) XMLNode& root (Route::state (full)); if (_playlists[DataType::AUDIO]) { - root.add_property (X_("audio-playlist"), _playlists[DataType::AUDIO]->id().to_s()); + root.set_property (X_("audio-playlist"), _playlists[DataType::AUDIO]->id().to_s()); } if (_playlists[DataType::MIDI]) { - root.add_property (X_("midi-playlist"), _playlists[DataType::MIDI]->id().to_s()); + root.set_property (X_("midi-playlist"), _playlists[DataType::MIDI]->id().to_s()); } root.add_child_nocopy (_monitoring_control->get_state ()); @@ -135,6 +162,8 @@ Track::state (bool full) root.add_child_nocopy (_record_enable_control->get_state ()); root.set_property (X_("saved-meter-point"), _saved_meter_point); + root.set_property (X_("disk-io-point"), _disk_io_point); + root.set_property (X_("alignment-choice"), _alignment_choice); return root; } @@ -147,20 +176,29 @@ Track::set_state (const XMLNode& node, int version) } XMLNode* child; - XMLProperty const * prop; if (version >= 3000 && version < 4000) { if ((child = find_named_node (node, X_("Diskstream"))) != 0) { - /* XXX DISK ... setup reader/writer from XML */ + /* XXX if we remember anything from stored DiskStream + state (older Ardour versions) that is needed by a + DiskReader or DiskWriter, we should cook up a new + XMLNode here, populate it with that information + (child nodes, properties, etc.) and then call + ::set_state() on the writer/reader. + + But at present (June 2017), there's no such state. + */ } } - if ((prop = node.property (X_("audio-playlist")))) { - find_and_use_playlist (DataType::AUDIO, PBD::ID (prop->value())); + std::string playlist_id; + + if (node.get_property (X_("audio-playlist"), playlist_id)) { + find_and_use_playlist (DataType::AUDIO, PBD::ID (playlist_id)); } - if ((prop = node.property (X_("midi-playlist")))) { - find_and_use_playlist (DataType::MIDI, PBD::ID (prop->value())); + if (node.get_property (X_("midi-playlist"), playlist_id)) { + find_and_use_playlist (DataType::MIDI, PBD::ID (playlist_id)); } XMLNodeList nlist = node.children(); @@ -187,6 +225,16 @@ Track::set_state (const XMLNode& node, int version) _saved_meter_point = _meter_point; } + if (!node.get_property (X_("saved-meter-point"), _disk_io_point)) { + _disk_io_point = DiskIOPreFader; + } + + AlignChoice ac; + + if (node.get_property (X_("alignment-choice"), ac)) { + set_align_choice (ac, true); + } + return 0; } @@ -342,6 +390,7 @@ Track::set_name (const string& str) _disk_writer->set_write_source_name (diskstream_name); boost::shared_ptr me = boost::dynamic_pointer_cast (shared_from_this ()); + if (_playlists[data_type()]->all_regions_empty () && _session.playlists->playlists_for_track (me).size() == 1) { /* Only rename the diskstream (and therefore the playlist) if a) the playlist has never had a region added to it and @@ -358,6 +407,12 @@ Track::set_name (const string& str) _disk_writer->set_name (str); } + for (uint32_t n = 0; n < DataType::num_types; ++n) { + if (_playlists[n]) { + _playlists[n]->set_name (str); + } + } + /* save state so that the statefile fully reflects any filename changes */ if ((ret = Route::set_name (str)) == 0) { @@ -483,9 +538,9 @@ Track::no_roll (pframes_t nframes, framepos_t start_frame, framepos_t end_frame, if (no_meter) { BufferSet& bufs (_session.get_silent_buffers (n_process_buffers())); _meter->run (bufs, start_frame, end_frame, 1.0, nframes, true); - _input->process_input (boost::shared_ptr(), start_frame, end_frame, speed(), nframes); + _input->process_input (boost::shared_ptr(), start_frame, end_frame, _session.transport_speed(), nframes); } else { - _input->process_input (_meter, start_frame, end_frame, speed(), nframes); + _input->process_input (_meter, start_frame, end_frame, _session.transport_speed(), nframes); } } @@ -498,7 +553,7 @@ Track::no_roll (pframes_t nframes, framepos_t start_frame, framepos_t end_frame, fill_buffers_with_input (bufs, _input, nframes); if (_meter_point == MeterInput) { - _meter->run (bufs, start_frame, end_frame, 1.0 /*speed()*/, nframes, true); + _meter->run (bufs, start_frame, end_frame, _session.transport_speed(), nframes, true); } passthru (bufs, start_frame, end_frame, nframes, false); @@ -577,9 +632,15 @@ Track::last_capture_sources () } void -Track::set_capture_offset () +Track::update_latency_information () { - _disk_writer->set_capture_offset (); + Glib::Threads::RWLock::ReaderLock lr (_processor_lock); + framecnt_t chain_latency = _input->latency (); + + for (ProcessorList::iterator p = _processors.begin(); p != _processors.end(); ++p) { + (*p)->set_input_latency (chain_latency); + chain_latency += (*p)->signal_latency (); + } } std::string @@ -666,9 +727,9 @@ Track::non_realtime_locate (framepos_t p) } void -Track::non_realtime_set_speed () +Track::non_realtime_speed_change () { - _disk_reader->non_realtime_set_speed (); + _disk_reader->non_realtime_speed_change (); } int @@ -699,12 +760,26 @@ Track::transport_looped (framepos_t p) } bool -Track::realtime_set_speed (double s, bool g) +Track::realtime_speed_change () { - if (_disk_reader->realtime_set_speed (s, g)) { + if (_disk_reader->realtime_speed_change ()) { return -1; } - return _disk_writer->realtime_set_speed (s, g); + return _disk_writer->realtime_speed_change (); +} + +void +Track::realtime_handle_transport_stopped () +{ + Glib::Threads::RWLock::ReaderLock lm (_processor_lock, Glib::Threads::TRY_LOCK); + + if (!lm.locked ()) { + return; + } + + for (ProcessorList::iterator i = _processors.begin(); i != _processors.end(); ++i) { + (*i)->realtime_handle_transport_stopped (); + } } void @@ -719,12 +794,6 @@ Track::pending_overwrite () const return _disk_reader->pending_overwrite (); } -double -Track::speed () const -{ - return _disk_reader->speed (); -} - void Track::prepare_to_stop (framepos_t t, framepos_t a) { @@ -741,7 +810,7 @@ Track::set_slaved (bool s) ChanCount Track::n_channels () { - return _disk_reader->output_streams(); // XXX DISK + return _disk_reader->output_streams(); } framepos_t @@ -860,16 +929,78 @@ Track::use_new_playlist () return use_playlist (data_type(), playlist); } +void +Track::set_align_choice (AlignChoice ac, bool force) +{ + switch (ac) { + case Automatic: + _alignment_choice = Automatic; + set_align_choice_from_io (); + return; + default: + break; + } + + _disk_writer->set_align_choice (ac, force); + _alignment_choice = ac; +} + void Track::set_align_style (AlignStyle s, bool force) { - // XXX DISK + _disk_writer->set_align_style (s, force); } void -Track::set_align_choice (AlignChoice s, bool force) +Track::set_align_choice_from_io () { - // XXX DISK + bool have_physical = false; + + if (_input) { + uint32_t n = 0; + vector connections; + boost::shared_ptr p; + + while (true) { + + p = _input->nth (n++); + + if (!p) { + break; + } + + if (p->get_connections (connections) != 0) { + if (AudioEngine::instance()->port_is_physical (connections[0])) { + have_physical = true; + break; + } + } + + connections.clear (); + } + } + +#ifdef MIXBUS + // compensate for latency when bouncing from master or mixbus. + // we need to use "ExistingMaterial" to pick up the master bus' latency + // see also Route::direct_feeds_according_to_reality + IOVector ios; + ios.push_back (_input); + if (_session.master_out() && ios.fed_by (_session.master_out()->output())) { + have_physical = true; + } + for (uint32_t n = 0; n < NUM_MIXBUSES && !have_physical; ++n) { + if (_session.get_mixbus (n) && ios.fed_by (_session.get_mixbus(n)->output())) { + have_physical = true; + } + } +#endif + + if (have_physical) { + _disk_writer->set_align_style (ExistingMaterial); + } else { + _disk_writer->set_align_style (CaptureTime); + } } void @@ -909,7 +1040,7 @@ Track::monitoring_state () const { /* Explicit requests */ - if (_monitoring & MonitorInput) { + if (_monitoring != MonitorInput) { return MonitoringInput; } @@ -961,12 +1092,19 @@ Track::monitoring_state () const /* Explicit requests */ MonitorChoice m (_monitoring_control->monitoring_choice()); - if (m & MonitorInput) { - return MonitoringInput; - } + if (m != MonitorAuto) { - if (m & MonitorDisk) { - return MonitoringDisk; + MonitorState ms ((MonitorState) 0); + + if (m & MonitorInput) { + ms = MonitoringInput; + } + + if (m & MonitorDisk) { + ms = MonitorState (ms | MonitoringDisk); + } + + return ms; } switch (_session.config.get_session_monitoring ()) { @@ -1115,3 +1253,320 @@ Track::metering_state () const } return rv ? MeteringInput : MeteringRoute; } + +bool +Track::set_processor_state (XMLNode const & node, XMLProperty const* prop, ProcessorList& new_order, bool& must_configure) +{ + if (Route::set_processor_state (node, prop, new_order, must_configure)) { + return true; + } + + if (prop->value() == "diskreader") { + if (_disk_reader) { + _disk_reader->set_state (node, Stateful::current_state_version); + new_order.push_back (_disk_reader); + return true; + } + } else if (prop->value() == "diskwriter") { + if (_disk_writer) { + _disk_writer->set_state (node, Stateful::current_state_version); + new_order.push_back (_disk_writer); + return true; + } + } + + error << string_compose(_("unknown Processor type \"%1\"; ignored"), prop->value()) << endmsg; + return false; +} + +void +Track::use_captured_sources (SourceList& srcs, CaptureInfos const & capture_info) +{ + if (srcs.empty()) { + return; + } + + boost::shared_ptr afs = boost::dynamic_pointer_cast (srcs.front()); + boost::shared_ptr mfs = boost::dynamic_pointer_cast (srcs.front()); + + if (afs) { + use_captured_audio_sources (srcs, capture_info); + } + + if (mfs) { + use_captured_midi_sources (srcs, capture_info); + } +} + +void +Track::use_captured_midi_sources (SourceList& srcs, CaptureInfos const & capture_info) +{ + if (srcs.empty() || data_type() != DataType::MIDI) { + return; + } + + boost::shared_ptr mfs = boost::dynamic_pointer_cast (srcs.front()); + boost::shared_ptr pl = _playlists[DataType::MIDI]; + boost::shared_ptr midi_region; + CaptureInfos::const_iterator ci; + + if (!mfs || !pl) { + return; + } + + framecnt_t total_capture = 0; + + for (total_capture = 0, ci = capture_info.begin(); ci != capture_info.end(); ++ci) { + total_capture += (*ci)->frames; + } + + /* we will want to be able to keep (over)writing the source + but we don't want it to be removable. this also differs + from the audio situation, where the source at this point + must be considered immutable. luckily, we can rely on + MidiSource::mark_streaming_write_completed() to have + already done the necessary work for that. + */ + + string whole_file_region_name; + whole_file_region_name = region_name_from_path (mfs->name(), true); + + /* Register a new region with the Session that + describes the entire source. Do this first + so that any sub-regions will obviously be + children of this one (later!) + */ + + try { + PropertyList plist; + + plist.add (Properties::name, whole_file_region_name); + plist.add (Properties::whole_file, true); + plist.add (Properties::automatic, true); + plist.add (Properties::start, 0); + plist.add (Properties::length, total_capture); + plist.add (Properties::layer, 0); + + boost::shared_ptr rx (RegionFactory::create (srcs, plist)); + + midi_region = boost::dynamic_pointer_cast (rx); + midi_region->special_set_position (capture_info.front()->start); + } + + catch (failed_constructor& err) { + error << string_compose(_("%1: could not create region for complete midi file"), _name) << endmsg; + /* XXX what now? */ + } + + pl->clear_changes (); + pl->freeze (); + + /* Session frame time of the initial capture in this pass, which is where the source starts */ + framepos_t initial_capture = 0; + if (!capture_info.empty()) { + initial_capture = capture_info.front()->start; + } + + BeatsFramesConverter converter (_session.tempo_map(), capture_info.front()->start); + const framepos_t preroll_off = _session.preroll_record_trim_len (); + + for (ci = capture_info.begin(); ci != capture_info.end(); ++ci) { + + string region_name; + + RegionFactory::region_name (region_name, mfs->name(), false); + + DEBUG_TRACE (DEBUG::CaptureAlignment, string_compose ("%1 capture start @ %2 length %3 add new region %4\n", + _name, (*ci)->start, (*ci)->frames, region_name)); + + + // cerr << _name << ": based on ci of " << (*ci)->start << " for " << (*ci)->frames << " add a region\n"; + + try { + PropertyList plist; + + /* start of this region is the offset between the start of its capture and the start of the whole pass */ + plist.add (Properties::start, (*ci)->start - initial_capture); + plist.add (Properties::length, (*ci)->frames); + plist.add (Properties::length_beats, converter.from((*ci)->frames).to_double()); + plist.add (Properties::name, region_name); + + boost::shared_ptr rx (RegionFactory::create (srcs, plist)); + midi_region = boost::dynamic_pointer_cast (rx); + if (preroll_off > 0) { + midi_region->trim_front ((*ci)->start - initial_capture + preroll_off); + } + } + + catch (failed_constructor& err) { + error << _("MidiDiskstream: could not create region for captured midi!") << endmsg; + continue; /* XXX is this OK? */ + } + + // cerr << "add new region, buffer position = " << buffer_position << " @ " << (*ci)->start << endl; + + pl->add_region (midi_region, (*ci)->start + preroll_off); + } + + pl->thaw (); + _session.add_command (new StatefulDiffCommand (pl)); +} + +void +Track::use_captured_audio_sources (SourceList& srcs, CaptureInfos const & capture_info) +{ + if (srcs.empty() || data_type() != DataType::AUDIO) { + return; + } + + boost::shared_ptr afs = boost::dynamic_pointer_cast (srcs.front()); + boost::shared_ptr pl = _playlists[DataType::AUDIO]; + boost::shared_ptr region; + + if (!afs || !pl) { + return; + } + + /* destructive tracks have a single, never changing region */ + + if (destructive()) { + + /* send a signal that any UI can pick up to do the right thing. there is + a small problem here in that a UI may need the peak data to be ready + for the data that was recorded and this isn't interlocked with that + process. this problem is deferred to the UI. + */ + + pl->LayeringChanged(); // XXX this may not get the UI to do the right thing + return; + } + + string whole_file_region_name; + whole_file_region_name = region_name_from_path (afs->name(), true); + + /* Register a new region with the Session that + describes the entire source. Do this first + so that any sub-regions will obviously be + children of this one (later!) + */ + + try { + PropertyList plist; + + plist.add (Properties::start, afs->last_capture_start_frame()); + plist.add (Properties::length, afs->length(0)); + plist.add (Properties::name, whole_file_region_name); + boost::shared_ptr rx (RegionFactory::create (srcs, plist)); + rx->set_automatic (true); + rx->set_whole_file (true); + + region = boost::dynamic_pointer_cast (rx); + region->special_set_position (afs->natural_position()); + } + + + catch (failed_constructor& err) { + error << string_compose(_("%1: could not create region for complete audio file"), _name) << endmsg; + /* XXX what now? */ + } + + pl->clear_changes (); + pl->set_capture_insertion_in_progress (true); + pl->freeze (); + + const framepos_t preroll_off = _session.preroll_record_trim_len (); + framecnt_t buffer_position = afs->last_capture_start_frame (); + CaptureInfos::const_iterator ci; + + for (ci = capture_info.begin(); ci != capture_info.end(); ++ci) { + + string region_name; + + RegionFactory::region_name (region_name, whole_file_region_name, false); + + DEBUG_TRACE (DEBUG::CaptureAlignment, string_compose ("%1 capture bufpos %5 start @ %2 length %3 add new region %4\n", + _name, (*ci)->start, (*ci)->frames, region_name, buffer_position)); + + try { + + PropertyList plist; + + plist.add (Properties::start, buffer_position); + plist.add (Properties::length, (*ci)->frames); + plist.add (Properties::name, region_name); + + boost::shared_ptr rx (RegionFactory::create (srcs, plist)); + region = boost::dynamic_pointer_cast (rx); + if (preroll_off > 0) { + region->trim_front (buffer_position + preroll_off); + } + } + + catch (failed_constructor& err) { + error << _("AudioDiskstream: could not create region for captured audio!") << endmsg; + continue; /* XXX is this OK? */ + } + + pl->add_region (region, (*ci)->start + preroll_off, 1, _disk_writer->non_layered()); + pl->set_layer (region, DBL_MAX); + + buffer_position += (*ci)->frames; + } + + pl->thaw (); + pl->set_capture_insertion_in_progress (false); + _session.add_command (new StatefulDiffCommand (pl)); +} + +#ifdef __clang__ +__attribute__((annotate("realtime"))) +#endif +void +Track::setup_invisible_processors_oh_children_of_mine (ProcessorList& processors) +{ + ProcessorList::iterator insert_pos; + + switch (_disk_io_point) { + case DiskIOPreFader: + insert_pos = find (processors.begin(), processors.end(), _trim); + if (insert_pos != processors.end()) { + insert_pos = processors.insert (insert_pos, _disk_writer); + processors.insert (insert_pos, _disk_reader); + } + break; + case DiskIOPostFader: + insert_pos = find (processors.begin(), processors.end(), _main_outs); + if (insert_pos != processors.end()) { + insert_pos = processors.insert (insert_pos, _disk_writer); + processors.insert (insert_pos, _disk_reader); + } + case DiskIOCustom: + break; + } +} + +void +Track::set_disk_io_position (DiskIOPoint diop) +{ + bool display = false; + + switch (diop) { + case DiskIOCustom: + display = true; + break; + default: + display = false; + } + + _disk_writer->set_display_to_user (display); + _disk_reader->set_display_to_user (display); + + const bool changed = (diop != _disk_io_point); + + _disk_io_point = diop; + + if (changed) { + Glib::Threads::Mutex::Lock lx (AudioEngine::instance()->process_lock ()); + configure_processors (0); + } +}