More tinkering with State<>. Use some StateDiffCommands instead of
[ardour.git] / libs / ardour / audio_diskstream.cc
index e43cbd78917987a54530d0a0e7737acd30b299d8..ef209fad3a897228425e90b088c04cd52227c28d 100644 (file)
 #include "pbd/enumwriter.h"
 #include "pbd/stacktrace.h"
 
-#include "ardour/ardour.h"
-#include "ardour/audioengine.h"
 #include "ardour/analyser.h"
+#include "ardour/ardour.h"
+#include "ardour/audio_buffer.h"
 #include "ardour/audio_diskstream.h"
-#include "ardour/utils.h"
-#include "ardour/configuration.h"
+#include "ardour/audio_port.h"
+#include "ardour/audioengine.h"
 #include "ardour/audiofilesource.h"
-#include "ardour/send.h"
-#include "ardour/region_factory.h"
+
 #include "ardour/audioplaylist.h"
-#include "ardour/playlist_factory.h"
-#include "ardour/cycle_timer.h"
 #include "ardour/audioregion.h"
-#include "ardour/audio_port.h"
-#include "ardour/source_factory.h"
-#include "ardour/audio_buffer.h"
-#include "ardour/session.h"
+#include "ardour/butler.h"
+#include "ardour/configuration.h"
+#include "ardour/cycle_timer.h"
+#include "ardour/debug.h"
 #include "ardour/io.h"
+#include "ardour/playlist_factory.h"
+#include "ardour/region_factory.h"
+#include "ardour/send.h"
+#include "ardour/session.h"
+#include "ardour/source_factory.h"
+#include "ardour/utils.h"
+#include "ardour/session_playlists.h"
 
 #include "i18n.h"
 #include <locale.h>
@@ -121,7 +125,7 @@ AudioDiskstream::init (Diskstream::Flag f)
 
 AudioDiskstream::~AudioDiskstream ()
 {
-       notify_callbacks ();
+       DEBUG_TRACE (DEBUG::Destruction, string_compose ("Audio Diskstream %1 destructor\n", _name));
 
        {
                RCUWriter<ChannelList> writer (channels);
@@ -253,7 +257,7 @@ AudioDiskstream::find_and_use_playlist (const string& name)
 {
        boost::shared_ptr<AudioPlaylist> playlist;
 
-       if ((playlist = boost::dynamic_pointer_cast<AudioPlaylist> (_session.playlist_by_name (name))) == 0) {
+       if ((playlist = boost::dynamic_pointer_cast<AudioPlaylist> (_session.playlists->by_name (name))) == 0) {
                playlist = boost::dynamic_pointer_cast<AudioPlaylist> (PlaylistFactory::create (DataType::AUDIO, _session, name));
        }
 
@@ -388,128 +392,27 @@ AudioDiskstream::use_destructive_playlist ()
 }
 
 void
-AudioDiskstream::check_record_status (nframes_t transport_frame, nframes_t /*nframes*/, bool can_record)
+AudioDiskstream::prepare_record_status(nframes_t capture_start_frame)
 {
-       int possibly_recording;
-       int rolling;
-       int change;
-       const int transport_rolling = 0x4;
-       const int track_rec_enabled = 0x2;
-       const int global_rec_enabled = 0x1;
-
-       /* merge together the 3 factors that affect record status, and compute
-          what has changed.
-       */
-
-       rolling = _session.transport_speed() != 0.0f;
-       possibly_recording = (rolling << 2) | (record_enabled() << 1) | can_record;
-       change = possibly_recording ^ last_possibly_recording;
-
-       if (possibly_recording == last_possibly_recording) {
-               return;
-       }
-
-       /* change state */
-
-       /* if per-track or global rec-enable turned on while the other was already on, we've started recording */
-
-       if (((change & track_rec_enabled) && record_enabled() && (!(change & global_rec_enabled) && can_record)) ||
-           ((change & global_rec_enabled) && can_record && (!(change & track_rec_enabled) && record_enabled()))) {
-
-               /* starting to record: compute first+last frames */
-
-               first_recordable_frame = transport_frame + _capture_offset;
-               last_recordable_frame = max_frames;
-               capture_start_frame = transport_frame;
-
-               if (!(last_possibly_recording & transport_rolling) && (possibly_recording & transport_rolling)) {
-
-                       /* was stopped, now rolling (and recording) */
-
-                       if (_alignment_style == ExistingMaterial) {
-                               first_recordable_frame += _session.worst_output_latency();
-                       } else {
-                               first_recordable_frame += _roll_delay;
-                       }
-
-               } else {
-
-                       /* was rolling, but record state changed */
-
-                       if (_alignment_style == ExistingMaterial) {
-
-                               if (!_session.config.get_punch_in()) {
-
-                                       /* manual punch in happens at the correct transport frame
-                                          because the user hit a button. but to get alignment correct
-                                          we have to back up the position of the new region to the
-                                          appropriate spot given the roll delay.
-                                       */
-
-                                       capture_start_frame -= _roll_delay;
-
-                                       /* XXX paul notes (august 2005): i don't know why
-                                          this is needed.
-                                       */
-
-                                       first_recordable_frame += _capture_offset;
-
-                               } else {
-
-                                       /* autopunch toggles recording at the precise
-                                          transport frame, and then the DS waits
-                                          to start recording for a time that depends
-                                          on the output latency.
-                                       */
-
-                                       first_recordable_frame += _session.worst_output_latency();
-                               }
+       if (recordable() && destructive()) {
+               boost::shared_ptr<ChannelList> c = channels.reader();
+               for (ChannelList::iterator chan = c->begin(); chan != c->end(); ++chan) {
 
-                       } else {
+                       RingBufferNPT<CaptureTransition>::rw_vector transvec;
+                       (*chan)->capture_transition_buf->get_write_vector(&transvec);
 
-                               if (_session.config.get_punch_in()) {
-                                       first_recordable_frame += _roll_delay;
-                               } else {
-                                       capture_start_frame -= _roll_delay;
-                               }
+                       if (transvec.len[0] > 0) {
+                               transvec.buf[0]->type = CaptureStart;
+                               transvec.buf[0]->capture_val = capture_start_frame;
+                               (*chan)->capture_transition_buf->increment_write_ptr(1);
                        }
-
-               }
-
-               if (recordable() && destructive()) {
-                       boost::shared_ptr<ChannelList> c = channels.reader();
-                       for (ChannelList::iterator chan = c->begin(); chan != c->end(); ++chan) {
-
-                               RingBufferNPT<CaptureTransition>::rw_vector transvec;
-                               (*chan)->capture_transition_buf->get_write_vector(&transvec);
-
-                               if (transvec.len[0] > 0) {
-                                       transvec.buf[0]->type = CaptureStart;
-                                       transvec.buf[0]->capture_val = capture_start_frame;
-                                       (*chan)->capture_transition_buf->increment_write_ptr(1);
-                               }
-                               else {
-                                       // bad!
-                                       fatal << X_("programming error: capture_transition_buf is full on rec start!  inconceivable!")
-                                             << endmsg;
-                               }
+                       else {
+                               // bad!
+                               fatal << X_("programming error: capture_transition_buf is full on rec start!  inconceivable!")
+                                       << endmsg;
                        }
                }
-
-       } else if (!record_enabled() || !can_record) {
-
-               /* stop recording */
-
-               last_recordable_frame = transport_frame + _capture_offset;
-
-               if (_alignment_style == ExistingMaterial) {
-                       last_recordable_frame += _session.worst_output_latency();
-               } else {
-                       last_recordable_frame += _roll_delay;
-               }
        }
-
-       last_possibly_recording = possibly_recording;
 }
 
 int
@@ -571,54 +474,14 @@ AudioDiskstream::process (nframes_t transport_frame, nframes_t nframes, bool can
        }
 
        if (nominally_recording || (_session.get_record_enabled() && _session.config.get_punch_in())) {
-               OverlapType ot;
-
                // Safeguard against situations where process() goes haywire when autopunching and last_recordable_frame < first_recordable_frame
                if (last_recordable_frame < first_recordable_frame) {
                        last_recordable_frame = max_frames;
                }
 
-               ot = coverage (first_recordable_frame, last_recordable_frame, transport_frame, transport_frame + nframes);
-
-               switch (ot) {
-               case OverlapNone:
-                       rec_nframes = 0;
-                       break;
-
-               case OverlapInternal:
-               /*     ----------    recrange
-                         |---|       transrange
-               */
-                       rec_nframes = nframes;
-                       rec_offset = 0;
-                       break;
-
-               case OverlapStart:
-                       /*    |--------|    recrange
-                            -----|          transrange
-                       */
-                       rec_nframes = transport_frame + nframes - first_recordable_frame;
-                       if (rec_nframes) {
-                               rec_offset = first_recordable_frame - transport_frame;
-                       }
-                       break;
-
-               case OverlapEnd:
-                       /*    |--------|    recrange
-                                 |--------  transrange
-                       */
-                       rec_nframes = last_recordable_frame - transport_frame;
-                       rec_offset = 0;
-                       break;
+               OverlapType ot = coverage (first_recordable_frame, last_recordable_frame, transport_frame, transport_frame + nframes);
 
-               case OverlapExternal:
-                       /*    |--------|    recrange
-                            --------------  transrange
-                       */
-                       rec_nframes = last_recordable_frame - first_recordable_frame;
-                       rec_offset = first_recordable_frame - transport_frame;
-                       break;
-               }
+               calculate_record_range(ot, transport_frame, nframes, rec_nframes, rec_offset);
 
                if (rec_nframes && !was_recording) {
                        capture_captured = 0;
@@ -766,6 +629,7 @@ AudioDiskstream::process (nframes_t transport_frame, nframes_t nframes, bool can
                                nframes_t total = chaninfo->playback_vector.len[0] + chaninfo->playback_vector.len[1];
 
                                if (necessary_samples > total) {
+                                       cerr << _name << " Need " << necessary_samples << " total = " << total << endl;
                                        cerr << "underrun for " << _name << endl;
                                        DiskUnderrun ();
                                        goto out;
@@ -816,19 +680,19 @@ AudioDiskstream::process (nframes_t transport_frame, nframes_t nframes, bool can
 void
 AudioDiskstream::process_varispeed_playback(nframes_t nframes, boost::shared_ptr<ChannelList> c)
 {
-         ChannelList::iterator chan;
+       ChannelList::iterator chan;
 
-               interpolation.set_speed (_target_speed);
+       interpolation.set_speed (_target_speed);
 
-               int channel = 0;
-               for (chan = c->begin(); chan != c->end(); ++chan, ++channel) {
-                       ChannelInfo* chaninfo (*chan);
+       int channel = 0;
+       for (chan = c->begin(); chan != c->end(); ++chan, ++channel) {
+               ChannelInfo* chaninfo (*chan);
 
-                       playback_distance = interpolation.interpolate (
-                                       channel, nframes, chaninfo->current_playback_buffer, chaninfo->speed_buffer);
+               playback_distance = interpolation.interpolate (
+                               channel, nframes, chaninfo->current_playback_buffer, chaninfo->speed_buffer);
 
-                       chaninfo->current_playback_buffer = chaninfo->speed_buffer;
-               }
+               chaninfo->current_playback_buffer = chaninfo->speed_buffer;
+       }
 }
 
 bool
@@ -1569,7 +1433,7 @@ AudioDiskstream::transport_stopped (struct tm& when, time_t twhen, bool abort_ca
                if (s) {
                        srcs.push_back (s);
                        s->update_header (capture_info.front()->start, when, twhen);
-                       s->set_captured_for (_name);
+                       s->set_captured_for (_name.get());
                        s->mark_immutable ();
                        if (Config->get_auto_analyse_audio()) {
                                Analyser::queue_source_for_analysis (s, true);
@@ -1587,7 +1451,7 @@ AudioDiskstream::transport_stopped (struct tm& when, time_t twhen, bool abort_ca
                   process. this problem is deferred to the UI.
                 */
 
-               _playlist->Modified();
+               _playlist->LayeringChanged(); // XXX this may not get the UI to do the right thing
 
        } else {
 
@@ -1641,7 +1505,7 @@ AudioDiskstream::transport_stopped (struct tm& when, time_t twhen, bool abort_ca
                                continue; /* XXX is this OK? */
                        }
 
-                       region->GoingAway.connect (bind (mem_fun (*this, &Diskstream::remove_region_from_last_capture), boost::weak_ptr<Region>(region)));
+                       region->DropReferences.connect_same_thread (*this, boost::bind (&Diskstream::remove_region_from_last_capture, this, boost::weak_ptr<Region>(region)));
 
                        _last_capture_regions.push_back (region);
 
@@ -1787,15 +1651,6 @@ AudioDiskstream::set_record_enabled (bool yn)
                return;
        }
 
-       if (yn && channels.reader()->front()->source == 0) {
-
-               /* pick up connections not initiated *from* the IO object
-                  we're associated with.
-               */
-
-               get_input_sources ();
-       }
-
        /* yes, i know that this not proof against race conditions, but its
           good enough. i think.
        */
@@ -1894,7 +1749,7 @@ AudioDiskstream::get_state ()
                if (_session.config.get_punch_in() && ((pi = _session.locations()->auto_punch_location()) != 0)) {
                        snprintf (buf, sizeof (buf), "%" PRId64, pi->start());
                } else {
-                       snprintf (buf, sizeof (buf), "%" PRIu32, _session.transport_frame());
+                       snprintf (buf, sizeof (buf), "%" PRId64, _session.transport_frame());
                }
 
                cs_child->add_property (X_("at"), buf);
@@ -1909,7 +1764,7 @@ AudioDiskstream::get_state ()
 }
 
 int
-AudioDiskstream::set_state (const XMLNode& node, int version)
+AudioDiskstream::set_state (const XMLNode& node, int /*version*/)
 {
        const XMLProperty* prop;
        XMLNodeList nlist = node.children();
@@ -2054,6 +1909,10 @@ AudioDiskstream::use_new_write_source (uint32_t n)
        /* do not remove destructive files even if they are empty */
 
        chan->write_source->set_allow_remove_if_empty (!destructive());
+       
+       /* until we write, this file is considered removable */
+
+       chan->write_source->mark_for_remove ();
 
        return 0;
 }
@@ -2111,7 +1970,7 @@ AudioDiskstream::rename_write_sources ()
 
        for (chan = c->begin(), n = 0; chan != c->end(); ++chan, ++n) {
                if ((*chan)->write_source != 0) {
-                       (*chan)->write_source->set_source_name (_name, destructive());
+                       (*chan)->write_source->set_source_name (_name.get(), destructive());
                        /* XXX what to do if one of them fails ? */
                }
        }
@@ -2140,7 +1999,7 @@ AudioDiskstream::allocate_temporary_buffers ()
 {
        /* make sure the wrap buffer is at least large enough to deal
           with the speeds up to 1.2, to allow for micro-variation
-          when slaving to MTC, SMPTE etc.
+          when slaving to MTC, Timecode etc.
        */
 
        double sp = max (fabsf (_actual_speed), 1.2f);
@@ -2206,10 +2065,9 @@ AudioDiskstream::set_align_style_from_io ()
 int
 AudioDiskstream::add_channel_to (boost::shared_ptr<ChannelList> c, uint32_t how_many)
 {
-
        while (how_many--) {
-               c->push_back (new ChannelInfo(_session.audio_diskstream_buffer_size(), speed_buffer_size, wrap_buffer_size));
-               interpolation.add_channel_to (_session.audio_diskstream_buffer_size(), speed_buffer_size);
+               c->push_back (new ChannelInfo(_session.butler()->audio_diskstream_buffer_size(), speed_buffer_size, wrap_buffer_size));
+               interpolation.add_channel_to (_session.butler()->audio_diskstream_buffer_size(), speed_buffer_size);
        }
 
        _n_channels.set(DataType::AUDIO, c->size());
@@ -2304,8 +2162,7 @@ AudioDiskstream::use_pending_capture_data (XMLNode& node)
                        try {
                                fs = boost::dynamic_pointer_cast<AudioFileSource> (
                                                SourceFactory::createWritable (DataType::AUDIO, _session,
-                                                               prop->value(), true,
-                                                               false, _session.frame_rate()));
+                                                               prop->value(), false, _session.frame_rate()));
                        }
 
                        catch (failed_constructor& err) {
@@ -2321,7 +2178,7 @@ AudioDiskstream::use_pending_capture_data (XMLNode& node)
                                first_fs = fs;
                        }
 
-                       fs->set_captured_for (_name);
+                       fs->set_captured_for (_name.get());
                }
        }
 
@@ -2446,7 +2303,7 @@ AudioDiskstream::can_become_destructive (bool& requires_bounce) const
 
        assert (afirst);
 
-       if (afirst->source()->used() > 1) {
+       if (_session.playlists->source_use_count (afirst->source()) > 1) {
                requires_bounce = true;
                return false;
        }