More tinkering with State<>. Use some StateDiffCommands instead of
[ardour.git] / libs / ardour / diskstream.cc
index 0b4c98eaa39ceccf8dd4df98511a8de24631256e..264dd3e79f044479243397ae559d1e0e8c48f0d9 100644 (file)
@@ -31,7 +31,6 @@
 #include <sys/stat.h>
 #include <sys/mman.h>
 
-#include <sigc++/bind.h>
 
 #include "pbd/error.h"
 #include "pbd/basename.h"
@@ -41,6 +40,7 @@
 
 #include "ardour/ardour.h"
 #include "ardour/audioengine.h"
+#include "ardour/debug.h"
 #include "ardour/diskstream.h"
 #include "ardour/utils.h"
 #include "ardour/configuration.h"
@@ -68,8 +68,8 @@ using namespace PBD;
  */
 ARDOUR::nframes_t Diskstream::disk_io_chunk_frames = 1024 * 256;
 
-sigc::signal<void>                Diskstream::DiskOverrun;
-sigc::signal<void>                Diskstream::DiskUnderrun;
+PBD::Signal0<void>                Diskstream::DiskOverrun;
+PBD::Signal0<void>                Diskstream::DiskUnderrun;
 
 Diskstream::Diskstream (Session &sess, const string &name, Flag flag)
        : SessionObject(sess, name)
@@ -128,8 +128,11 @@ Diskstream::init (Flag f)
 
 Diskstream::~Diskstream ()
 {
-       if (_playlist)
+       DEBUG_TRACE (DEBUG::Destruction, string_compose ("Diskstream %1 deleted\n", _name));
+
+       if (_playlist) {
                _playlist->release ();
+       }
 }
 
 void
@@ -137,11 +140,15 @@ Diskstream::set_route (Route& r)
 {
        _route = &r;
        _io = _route->input();
+
+       ic_connection.disconnect();
+       _io->changed.connect_same_thread (ic_connection, boost::bind (&Diskstream::handle_input_change, this, _1, _2));
+
        input_change_pending = ConfigurationChanged;
        non_realtime_input_change ();
        set_align_style_from_io ();
 
-       _route->GoingAway.connect (mem_fun (*this, &Diskstream::route_going_away));
+       _route->Destroyed.connect_same_thread (*this, boost::bind (&Diskstream::route_going_away, this));
 }
 
 void
@@ -289,7 +296,7 @@ Diskstream::get_captured_frames (uint32_t n)
        if (capture_info.size() > n) {
                return capture_info[n]->frames;
        }
-       else {
+       else {  
                return capture_captured;
        }
 }
@@ -319,9 +326,7 @@ Diskstream::use_playlist (boost::shared_ptr<Playlist> playlist)
                        return 0;
                }
 
-               plmod_connection.disconnect ();
-               plgone_connection.disconnect ();
-               plregion_connection.disconnect ();
+               playlist_connections.drop_connections ();
 
                if (_playlist) {
                        _playlist->release();
@@ -334,9 +339,9 @@ Diskstream::use_playlist (boost::shared_ptr<Playlist> playlist)
                        reset_write_sources (false);
                }
 
-               plmod_connection = _playlist->Modified.connect (mem_fun (*this, &Diskstream::playlist_modified));
-               plgone_connection = _playlist->GoingAway.connect (bind (mem_fun (*this, &Diskstream::playlist_deleted), boost::weak_ptr<Playlist>(_playlist)));
-               plregion_connection = _playlist->RangesMoved.connect (mem_fun (*this, &Diskstream::playlist_ranges_moved));
+               _playlist->ContentsChanged.connect_same_thread (playlist_connections, boost::bind (&Diskstream::playlist_modified, this));
+               _playlist->DropReferences.connect_same_thread (playlist_connections, boost::bind (&Diskstream::playlist_deleted, this, boost::weak_ptr<Playlist>(_playlist)));
+               _playlist->RangesMoved.connect_same_thread (playlist_connections, boost::bind (&Diskstream::playlist_ranges_moved, this, _1));
        }
 
        /* don't do this if we've already asked for it *or* if we are setting up
@@ -391,7 +396,7 @@ Diskstream::playlist_deleted (boost::weak_ptr<Playlist> wpl)
 bool
 Diskstream::set_name (const string& str)
 {
-       if (str != _name) {
+       if (_name != str) {
                assert(playlist());
                playlist()->set_name (str);
 
@@ -449,12 +454,11 @@ Diskstream::playlist_ranges_moved (list< Evoral::RangeMove<nframes_t> > const &
        }
 
        /* move processor automation */
-       _route->foreach_processor (sigc::bind (sigc::mem_fun (*this, &Diskstream::move_processor_automation), movements_frames));
+       _route->foreach_processor (boost::bind (&Diskstream::move_processor_automation, this, _1, movements_frames));
 }
 
 void
-Diskstream::move_processor_automation (boost::weak_ptr<Processor> p,
-                                      list< Evoral::RangeMove<nframes_t> > const & movements_frames)
+Diskstream::move_processor_automation (boost::weak_ptr<Processor> p, list< Evoral::RangeMove<nframes_t> > const & movements_frames)
 {
        boost::shared_ptr<Processor> processor (p.lock ());
        if (!processor) {
@@ -521,7 +525,11 @@ Diskstream::check_record_status (nframes_t transport_frame, nframes_t /*nframes*
                        /* was stopped, now rolling (and recording) */
 
                        if (_alignment_style == ExistingMaterial) {
+                         
                                first_recordable_frame += _session.worst_output_latency();
+                               
+                               DEBUG_TRACE (DEBUG::Latency, string_compose ("Offset rec from stop. Capture offset: %1 Worst O/P Latency: %2 Roll Delay: %3 First Recordable Frame: %4 Transport Frame: %5\n",
+                                                                            _capture_offset, _session.worst_output_latency(), _roll_delay, first_recordable_frame, transport_frame));
                        } else {
                                first_recordable_frame += _roll_delay;
                        }
@@ -532,34 +540,23 @@ Diskstream::check_record_status (nframes_t transport_frame, nframes_t /*nframes*
 
                        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();
-                               }
-
+                               /* 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.
+                               */
+                               
+                               
+                               /* 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();
+                               
+                               DEBUG_TRACE (DEBUG::Latency, string_compose ("Punch in manual/auto. Capture offset: %1 Worst O/P Latency: %2 Roll Delay: %3 First Recordable Frame: %4 Transport Frame: %5\n",
+                                                                            _capture_offset, _session.worst_output_latency(), _roll_delay, first_recordable_frame, transport_frame));
                        } else {
 
                                if (_session.config.get_punch_in()) {
@@ -584,6 +581,12 @@ Diskstream::check_record_status (nframes_t transport_frame, nframes_t /*nframes*
                } else {
                        last_recordable_frame += _roll_delay;
                }
+               
+               //first_recordable_frame = max_frames;
+               
+               DEBUG_TRACE (DEBUG::Latency, string_compose ("Stop record - %6 | %7. Capture offset: %1 Worst O/P Latency: %2 Roll Delay: %3 First Recordable Frame: %4 Transport Frame: %5\n",
+                                                            _capture_offset, _session.worst_output_latency(), _roll_delay, first_recordable_frame, transport_frame,
+                                                            can_record, record_enabled()));
        }
 
        last_possibly_recording = possibly_recording;
@@ -594,3 +597,48 @@ Diskstream::route_going_away ()
 {
        _io.reset ();
 }
+
+void
+Diskstream::calculate_record_range(OverlapType ot, sframes_t transport_frame, nframes_t nframes,
+                                  nframes_t& rec_nframes, nframes_t& rec_offset)
+{
+       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;
+
+       case OverlapExternal:
+               /*    |--------|    recrange
+                   --------------  transrange
+               */
+               rec_nframes = last_recordable_frame - first_recordable_frame;
+               rec_offset = first_recordable_frame - transport_frame;
+               break;
+       }
+}