always check_dependents() after a relayer() (useful for LaterHigher model), don't...
[ardour.git] / libs / ardour / audio_diskstream.cc
index 580ce492314872fe318c1920854e944cf176916b..5f682789ef1bbf9def5fb3afba8b1cd4ff68734c 100644 (file)
@@ -381,7 +381,7 @@ AudioDiskstream::setup_destructive_playlist ()
 void
 AudioDiskstream::use_destructive_playlist ()
 {
-       /* this is called from the XML-based constructor. when its done,
+       /* this is called from the XML-based constructor or ::set_destructive. when called,
           we already have a playlist and a region, but we need to
           set up our sources for write. we use the sources associated 
           with the (presumed single, full-extent) region.
@@ -400,6 +400,10 @@ AudioDiskstream::use_destructive_playlist ()
                throw failed_constructor();
        }
 
+       /* be sure to stretch the region out to the maximum length */
+
+       region->set_length (max_frames - region->position(), this);
+
        uint32_t n;
        ChannelList::iterator chan;
 
@@ -407,6 +411,10 @@ AudioDiskstream::use_destructive_playlist ()
                (*chan).write_source = boost::dynamic_pointer_cast<AudioFileSource>(region->source (n));
                assert((*chan).write_source);
                (*chan).write_source->set_allow_remove_if_empty (false);
+
+               /* this might be false if we switched modes, so force it */
+
+               (*chan).write_source->set_destructive (true);
        }
 
        /* the source list will never be reset for a destructive track */
@@ -1484,29 +1492,21 @@ AudioDiskstream::transport_stopped (struct tm& when, time_t twhen, bool abort_ca
 
        if (abort_capture) {
                
-               ChannelList::iterator chan;
-               
-               list<boost::shared_ptr<Source> >* deletion_list = new list<boost::shared_ptr<Source> >;
+               if (destructive()) {
+                       goto outout;
+               }
 
-               for ( chan = channels.begin(); chan != channels.end(); ++chan) {
+               for (ChannelList::iterator chan = channels.begin(); chan != channels.end(); ++chan) {
 
                        if ((*chan).write_source) {
                                
                                (*chan).write_source->mark_for_remove ();
-                               
-                               deletion_list->push_back ((*chan).write_source);
-
+                               (*chan).write_source->drop_references ();
                                (*chan).write_source.reset ();
                        }
                        
                        /* new source set up in "out" below */
                }
-               
-               if (!deletion_list->empty()) {
-                       DeleteSources (deletion_list);
-               } else {
-                       delete deletion_list;
-               }
 
                goto out;
        } 
@@ -1543,6 +1543,9 @@ AudioDiskstream::transport_stopped (struct tm& when, time_t twhen, bool abort_ca
 
        } else {
 
+               string whole_file_region_name;
+               whole_file_region_name = region_name_from_path (channels[0].write_source->name());
+
                /* Register a new region with the Session that
                   describes the entire source. Do this first
                   so that any sub-regions will obviously be
@@ -1551,7 +1554,7 @@ AudioDiskstream::transport_stopped (struct tm& when, time_t twhen, bool abort_ca
                
                try {
                        boost::shared_ptr<Region> rx (RegionFactory::create (srcs, channels[0].write_source->last_capture_start_frame(), total_capture, 
-                                                                            region_name_from_path (channels[0].write_source->name()), 
+                                                                            whole_file_region_name,
                                                                             0, AudioRegion::Flag (AudioRegion::DefaultFlags|AudioRegion::Automatic|AudioRegion::WholeFile)));
 
                        region = boost::dynamic_pointer_cast<AudioRegion> (rx);
@@ -1574,7 +1577,8 @@ AudioDiskstream::transport_stopped (struct tm& when, time_t twhen, bool abort_ca
                for (buffer_position = channels[0].write_source->last_capture_start_frame(), ci = capture_info.begin(); ci != capture_info.end(); ++ci) {
                        
                        string region_name;
-                       _session.region_name (region_name, channels[0].write_source->name(), false);
+
+                       _session.region_name (region_name, whole_file_region_name, false);
                        
                        // cerr << _name << ": based on ci of " << (*ci)->start << " for " << (*ci)->frames << " add region " << region_name << endl;
                        
@@ -1588,9 +1592,9 @@ AudioDiskstream::transport_stopped (struct tm& when, time_t twhen, bool abort_ca
                                continue; /* XXX is this OK? */
                        }
                        
-                       _last_capture_regions.push_back (region);
+                       region->GoingAway.connect (bind (mem_fun (*this, &Diskstream::remove_region_from_last_capture), boost::weak_ptr<Region>(region)));
                        
-                       // cerr << "add new region, buffer position = " << buffer_position << " @ " << (*ci)->start << endl;
+                       _last_capture_regions.push_back (region);
                        
                        i_am_the_modifier++;
                        _playlist->add_region (region, (*ci)->start);
@@ -1606,9 +1610,11 @@ AudioDiskstream::transport_stopped (struct tm& when, time_t twhen, bool abort_ca
 
        mark_write_completed = true;
 
+  out:
        reset_write_sources (mark_write_completed);
 
-  out:
+  outout:
+
        for (ci = capture_info.begin(); ci != capture_info.end(); ++ci) {
                delete *ci;
        }
@@ -1708,7 +1714,7 @@ AudioDiskstream::engage_record_enable ()
 
        g_atomic_int_set (&_record_enabled, 1);
        capturing_sources.clear ();
-       if (Config->get_use_hardware_monitoring())  {
+       if (Config->get_monitoring_model() == HardwareMonitoring) {
                for (ChannelList::iterator chan = channels.begin(); chan != channels.end(); ++chan) {
                        if ((*chan).source) {
                                (*chan).source->ensure_monitor_input (!(Config->get_auto_input() && rolling));
@@ -1728,7 +1734,7 @@ void
 AudioDiskstream::disengage_record_enable ()
 {
        g_atomic_int_set (&_record_enabled, 0);
-       if (Config->get_use_hardware_monitoring()) {
+       if (Config->get_monitoring_model() == HardwareMonitoring) {
                for (ChannelList::iterator chan = channels.begin(); chan != channels.end(); ++chan) {
                        if ((*chan).source) {
                                (*chan).source->ensure_monitor_input (false);
@@ -1755,11 +1761,11 @@ AudioDiskstream::get_state ()
 
        node->add_property ("playlist", _playlist->name());
        
-       snprintf (buf, sizeof(buf), "%f", _visible_speed);
+       snprintf (buf, sizeof(buf), "%.12g", _visible_speed);
        node->add_property ("speed", buf);
 
        node->add_property("name", _name);
-       id().print (buf);
+       id().print (buf, sizeof (buf));
        node->add_property("id", buf);
 
        if (!capturing_sources.empty() && _session.get_record_enabled()) {
@@ -1933,13 +1939,8 @@ AudioDiskstream::use_new_write_source (uint32_t n)
        ChannelInfo &chan = channels[n];
        
        if (chan.write_source) {
-
-               if (AudioFileSource::is_empty (_session, chan.write_source->path())) {
-                       chan.write_source->mark_for_remove ();
-                       chan.write_source.reset ();
-               } else {
-                       chan.write_source.reset ();
-               }
+               chan.write_source->set_allow_remove_if_empty (true);
+               chan.write_source.reset ();
        }
 
        try {
@@ -2235,3 +2236,70 @@ AudioDiskstream::use_pending_capture_data (XMLNode& node)
 
        return 0;
 }
+
+int
+AudioDiskstream::set_destructive (bool yn)
+{
+       bool bounce_ignored;
+
+       if (yn != destructive()) {
+               
+               if (yn) {
+                       /* requestor should already have checked this and
+                          bounced if necessary and desired 
+                       */
+                       if (!can_become_destructive (bounce_ignored)) {
+                               return -1;
+                       }
+                       _flags |= Destructive;
+                       use_destructive_playlist ();
+               } else {
+                       _flags &= ~Destructive;
+                       reset_write_sources (true, true);
+               }
+       }
+
+       return 0;
+}
+
+bool
+AudioDiskstream::can_become_destructive (bool& requires_bounce) const
+{
+       if (!_playlist) {
+               requires_bounce = false;
+               return false;
+       }
+
+       /* is there only one region ? */
+
+       if (_playlist->n_regions() != 1) {
+               requires_bounce = true;
+               return false;
+       }
+
+       boost::shared_ptr<Region> first = _playlist->find_next_region (_session.current_start_frame(), Start, 1);
+       assert (first);
+
+       /* do the source(s) for the region cover the session start position ? */
+       
+       if (first->position() != _session.current_start_frame()) {
+               if (first->start() > _session.current_start_frame()) {
+                       requires_bounce = true;
+                       return false;
+               }
+       }
+
+       /* is the source used by only 1 playlist ? */
+
+       boost::shared_ptr<AudioRegion> afirst = boost::dynamic_pointer_cast<AudioRegion> (first);
+
+       assert (afirst);
+
+       if (afirst->source()->used() > 1) {
+               requires_bounce = true; 
+               return false;
+       }
+
+       requires_bounce = false;
+       return true;
+}