Skeleton tests for BBT_Time.
[ardour.git] / libs / ardour / audio_diskstream.cc
index bb0b715f3b5a79321c8c065083c319e522af307e..fbd10b92e35f9e216a518e67e368228e94362c7f 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/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 "i18n.h"
 #include <locale.h>
@@ -90,7 +91,7 @@ AudioDiskstream::AudioDiskstream (Session& sess, const XMLNode& node)
        in_set_state = true;
        init (Recordable);
 
-       if (set_state (node)) {
+       if (set_state (node, Stateful::loading_state_version)) {
                in_set_state = false;
                throw failed_constructor();
        }
@@ -388,128 +389,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 +471,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;
+               OverlapType ot = coverage (first_recordable_frame, last_recordable_frame, transport_frame, transport_frame + nframes);
 
-               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;
-               }
+               calculate_record_range(ot, transport_frame, nframes, rec_nframes, rec_offset);
 
                if (rec_nframes && !was_recording) {
                        capture_captured = 0;
@@ -816,19 +676,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
@@ -1909,7 +1769,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();
@@ -2140,7 +2000,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 +2066,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());
@@ -2446,7 +2305,7 @@ AudioDiskstream::can_become_destructive (bool& requires_bounce) const
 
        assert (afirst);
 
-       if (afirst->source()->used() > 1) {
+       if (_session.source_use_count (afirst->source()) > 1) {
                requires_bounce = true;
                return false;
        }