Fix mismatched initialisation order.
[ardour.git] / libs / ardour / audio_diskstream.cc
index d8f2ba9d746072d9423cacaac0dee5ebd9b4611f..60b21bbc22c1fa5c24a2acc1f9ca96d851a1ae0c 100644 (file)
@@ -27,8 +27,6 @@
 #include <fcntl.h>
 #include <cstdlib>
 #include <ctime>
-#include <sys/stat.h>
-#include <sys/mman.h>
 
 #include "pbd/error.h"
 #include "pbd/xml++.h"
@@ -150,6 +148,8 @@ AudioDiskstream::free_working_buffers()
 void
 AudioDiskstream::non_realtime_input_change ()
 {
+       bool need_write_sources = false;
+
        {
                Glib::Threads::Mutex::Lock lm (state_lock);
 
@@ -157,6 +157,11 @@ AudioDiskstream::non_realtime_input_change ()
                        return;
                }
 
+               boost::shared_ptr<ChannelList> cr = channels.reader();
+               if (!cr->empty() && !cr->front()->write_source) {
+                       need_write_sources = true;
+               }
+
                if (input_change_pending.type == IOChange::ConfigurationChanged) {
                        RCUWriter<ChannelList> writer (channels);
                        boost::shared_ptr<ChannelList> c = writer.get_copy();
@@ -168,6 +173,8 @@ AudioDiskstream::non_realtime_input_change ()
                        } else if (_io->n_ports().n_audio() < _n_channels.n_audio()) {
                                remove_channel_from (c, _n_channels.n_audio() - _io->n_ports().n_audio());
                        }
+
+                       need_write_sources = true;
                }
 
                if (input_change_pending.type & IOChange::ConnectionsChanged) {
@@ -181,9 +188,9 @@ AudioDiskstream::non_realtime_input_change ()
                /* implicit unlock */
        }
 
-       /* reset capture files */
-
-       reset_write_sources (false);
+       if (need_write_sources) {
+               reset_write_sources (false);
+       }
 
        /* now refill channel buffers */
 
@@ -408,7 +415,7 @@ AudioDiskstream::prepare_record_status(framepos_t capture_start_frame)
  *      that someone can read playback_distance worth of data from.
  */
 int
-AudioDiskstream::process (framepos_t transport_frame, pframes_t nframes, framecnt_t& playback_distance)
+AudioDiskstream::process (BufferSet& bufs, framepos_t transport_frame, pframes_t nframes, framecnt_t& playback_distance, bool need_disk_signal)
 {
        uint32_t n;
        boost::shared_ptr<ChannelList> c = channels.reader();
@@ -494,9 +501,9 @@ AudioDiskstream::process (framepos_t transport_frame, pframes_t nframes, framecn
                                assert(ap);
                                assert(rec_nframes <= (framecnt_t) ap->get_audio_buffer(nframes).capacity());
 
-                               Sample *buf = ap->get_audio_buffer (nframes).data (rec_offset);
+                               Sample *buf = bufs.get_audio (n).data(rec_offset);
                                memcpy (chaninfo->current_capture_buffer, buf, sizeof (Sample) * rec_nframes);
-
+                               
                        } else {
 
                                framecnt_t total = chaninfo->capture_vector.len[0] + chaninfo->capture_vector.len[1];
@@ -509,7 +516,7 @@ AudioDiskstream::process (framepos_t transport_frame, pframes_t nframes, framecn
                                boost::shared_ptr<AudioPort> const ap = _io->audio (n);
                                assert(ap);
 
-                               Sample* buf = ap->get_audio_buffer(nframes).data (rec_offset);
+                               Sample *buf = bufs.get_audio (n).data(rec_offset);
                                framecnt_t first = chaninfo->capture_vector.len[0];
 
                                memcpy (chaninfo->capture_wrap_buffer, buf, sizeof (Sample) * first);
@@ -618,7 +625,7 @@ AudioDiskstream::process (framepos_t transport_frame, pframes_t nframes, framecn
                                           as our current_playback_buffer.
                                        */
 
-                                       assert(wrap_buffer_size >= chaninfo->playback_vector.len[0] + chaninfo->playback_vector.len[1]);
+                                       assert(wrap_buffer_size >= necessary_samples);
 
                                        /* Copy buf[0] from playback_buf */
                                        memcpy ((char *) chaninfo->playback_wrap_buffer,
@@ -657,9 +664,74 @@ AudioDiskstream::process (framepos_t transport_frame, pframes_t nframes, framecn
                _speed = _target_speed;
        }
 
+       if (need_disk_signal) {
+
+               /* copy data over to buffer set */
+               
+               size_t n_buffers = bufs.count().n_audio();
+               size_t n_chans = c->size();
+               gain_t scaling = 1.0f;
+               
+               if (n_chans > n_buffers) {
+                       scaling = ((float) n_buffers)/n_chans;
+               }
+
+               for (n = 0, chan = c->begin(); chan != c->end(); ++chan, ++n) {
+                       
+                       AudioBuffer& buf (bufs.get_audio (n%n_buffers));
+                       ChannelInfo* chaninfo (*chan);
+                       
+                       if (n < n_chans) {
+                               if (scaling != 1.0f) {
+                                       buf.read_from_with_gain (chaninfo->current_playback_buffer, nframes, scaling);
+                               } else {
+                                       buf.read_from (chaninfo->current_playback_buffer, nframes);
+                               }
+                       } else {
+                               if (scaling != 1.0f) {
+                                       buf.accumulate_with_gain_from (chaninfo->current_playback_buffer, nframes, scaling);
+                               } else {
+                                       buf.accumulate_from (chaninfo->current_playback_buffer, nframes);
+                               }
+                       }
+               }
+
+               /* leave the MIDI count alone */
+               ChanCount cnt (DataType::AUDIO, n_chans);
+               cnt.set (DataType::MIDI, bufs.count().n_midi());
+               bufs.set_count (cnt);
+       
+               /* extra buffers will already be silent, so leave them alone */
+       }
+
        return 0;
 }
 
+frameoffset_t
+AudioDiskstream::calculate_playback_distance (pframes_t nframes)
+{
+       frameoffset_t playback_distance = nframes;
+
+       if (record_enabled()) {
+               playback_distance = nframes;
+       } else if (_actual_speed != 1.0f && _actual_speed != -1.0f) {
+               interpolation.set_speed (_target_speed);
+               boost::shared_ptr<ChannelList> c = channels.reader();
+               int channel = 0;
+               for (ChannelList::iterator chan = c->begin(); chan != c->end(); ++chan, ++channel) {
+                       playback_distance = interpolation.interpolate (channel, nframes, NULL, NULL);
+               }
+       } else {
+               playback_distance = nframes;
+       }
+
+       if (_actual_speed < 0.0) {
+               return -playback_distance;
+       } else {
+               return playback_distance;
+       }
+}
+
 /** Update various things including playback_sample, read pointer on each channel's playback_buf
  *  and write pointer on each channel's capture_buf.  Also wout whether the butler is needed.
  *  @return true if the butler is required.
@@ -860,7 +932,7 @@ AudioDiskstream::internal_playback_seek (framecnt_t distance)
        boost::shared_ptr<ChannelList> c = channels.reader();
 
        for (chan = c->begin(); chan != c->end(); ++chan) {
-               (*chan)->playback_buf->increment_read_ptr (distance);
+               (*chan)->playback_buf->increment_read_ptr (llabs(distance));
        }
 
        if (first_recordable_frame < max_framepos) {
@@ -1689,7 +1761,7 @@ AudioDiskstream::prep_record_enable ()
        if (Config->get_monitoring_model() == HardwareMonitoring) {
 
                for (ChannelList::iterator chan = c->begin(); chan != c->end(); ++chan) {
-                       (*chan)->source.request_jack_monitors_input (!(_session.config.get_auto_input() && rolling));
+                       (*chan)->source.request_input_monitoring (!(_session.config.get_auto_input() && rolling));
                        capturing_sources.push_back ((*chan)->write_source);
                        (*chan)->write_source->mark_streaming_write_started ();
                }
@@ -1710,7 +1782,7 @@ AudioDiskstream::prep_record_disable ()
        boost::shared_ptr<ChannelList> c = channels.reader();
        if (Config->get_monitoring_model() == HardwareMonitoring) {
                for (ChannelList::iterator chan = c->begin(); chan != c->end(); ++chan) {
-                       (*chan)->source.request_jack_monitors_input (false);
+                       (*chan)->source.request_input_monitoring (false);
                }
        }
        capturing_sources.clear ();
@@ -1726,7 +1798,7 @@ AudioDiskstream::get_state ()
        LocaleGuard lg (X_("POSIX"));
 
        boost::shared_ptr<ChannelList> c = channels.reader();
-       snprintf (buf, sizeof(buf), "%zd", c->size());
+       snprintf (buf, sizeof(buf), "%u", (unsigned int) c->size());
        node.add_property ("channels", buf);
 
        if (!capturing_sources.empty() && _session.get_record_enabled()) {
@@ -1845,7 +1917,7 @@ AudioDiskstream::use_new_write_source (uint32_t n)
 
        try {
                if ((chan->write_source = _session.create_audio_source_for_session (
-                            n_channels().n_audio(), name(), n, destructive())) == 0) {
+                            n_channels().n_audio(), write_source_name(), n, destructive())) == 0) {
                        throw failed_constructor();
                }
        }
@@ -1863,14 +1935,6 @@ AudioDiskstream::use_new_write_source (uint32_t n)
        return 0;
 }
 
-list<boost::shared_ptr<Source> >
-AudioDiskstream::steal_write_sources()
-{
-       /* not possible to steal audio write sources */
-       list<boost::shared_ptr<Source> > ret;
-       return ret;
-}
-
 void
 AudioDiskstream::reset_write_sources (bool mark_write_complete, bool /*force*/)
 {
@@ -1954,7 +2018,7 @@ AudioDiskstream::allocate_temporary_buffers ()
        */
 
        double const sp = max (fabsf (_actual_speed), 1.2f);
-       framecnt_t required_wrap_size = (framecnt_t) floor (_session.get_block_size() * sp) + 1;
+       framecnt_t required_wrap_size = (framecnt_t) ceil (_session.get_block_size() * sp) + 2;
 
        if (required_wrap_size > wrap_buffer_size) {
 
@@ -1976,12 +2040,12 @@ AudioDiskstream::allocate_temporary_buffers ()
 }
 
 void
-AudioDiskstream::request_jack_monitors_input (bool yn)
+AudioDiskstream::request_input_monitoring (bool yn)
 {
        boost::shared_ptr<ChannelList> c = channels.reader();
 
        for (ChannelList::iterator chan = c->begin(); chan != c->end(); ++chan) {
-               (*chan)->source.request_jack_monitors_input (yn);
+               (*chan)->source.request_input_monitoring (yn);
        }
 }
 
@@ -2124,11 +2188,16 @@ AudioDiskstream::use_pending_capture_data (XMLNode& node)
                                continue;
                        }
 
+                       /* XXX as of June 2014, we always record to mono
+                          files. Since this Source is being created as part of
+                          crash recovery, we know that we need the first
+                          channel (the final argument to the SourceFactory
+                          call below). If we ever support non-mono files for
+                          capture, this will need rethinking.
+                       */
+
                        try {
-                               fs = boost::dynamic_pointer_cast<AudioFileSource> (
-                                       SourceFactory::createWritable (
-                                               DataType::AUDIO, _session,
-                                               prop->value(), string(), false, _session.frame_rate()));
+                               fs = boost::dynamic_pointer_cast<AudioFileSource> (SourceFactory::createForRecovery (DataType::AUDIO, _session, prop->value(), 0));
                        }
 
                        catch (failed_constructor& err) {
@@ -2159,21 +2228,31 @@ AudioDiskstream::use_pending_capture_data (XMLNode& node)
                return -1;
        }
 
-       boost::shared_ptr<AudioRegion> region;
-
        try {
 
-               PropertyList plist;
+               boost::shared_ptr<AudioRegion> wf_region;
+               boost::shared_ptr<AudioRegion> region;
+               
+               /* First create the whole file region */
 
+               PropertyList plist;
+               
                plist.add (Properties::start, 0);
                plist.add (Properties::length, first_fs->length (first_fs->timeline_position()));
                plist.add (Properties::name, region_name_from_path (first_fs->name(), true));
 
-               region = boost::dynamic_pointer_cast<AudioRegion> (RegionFactory::create (pending_sources, plist));
+               wf_region = boost::dynamic_pointer_cast<AudioRegion> (RegionFactory::create (pending_sources, plist));
 
-               region->set_automatic (true);
-               region->set_whole_file (true);
-               region->special_set_position (0);
+               wf_region->set_automatic (true);
+               wf_region->set_whole_file (true);
+               wf_region->special_set_position (position);
+
+               /* Now create a region that isn't the whole file for adding to
+                * the playlist */
+
+               region = boost::dynamic_pointer_cast<AudioRegion> (RegionFactory::create (pending_sources, plist));
+               
+               _playlist->add_region (region, position);
        }
 
        catch (failed_constructor& err) {
@@ -2184,7 +2263,6 @@ AudioDiskstream::use_pending_capture_data (XMLNode& node)
                return -1;
        }
 
-       _playlist->add_region (region, position);
 
        return 0;
 }
@@ -2304,13 +2382,13 @@ AudioDiskstream::ChannelSource::is_physical () const
 }
 
 void
-AudioDiskstream::ChannelSource::request_jack_monitors_input (bool yn) const
+AudioDiskstream::ChannelSource::request_input_monitoring (bool yn) const
 {
        if (name.empty()) {
                return;
        }
 
-       return AudioEngine::instance()->request_jack_monitors_input (name, yn);
+       return AudioEngine::instance()->request_input_monitoring (name, yn);
 }
 
 AudioDiskstream::ChannelInfo::ChannelInfo (framecnt_t playback_bufsize, framecnt_t capture_bufsize, framecnt_t speed_size, framecnt_t wrap_size)
@@ -2382,6 +2460,9 @@ AudioDiskstream::ChannelInfo::~ChannelInfo ()
 bool
 AudioDiskstream::set_name (string const & name)
 {
+       if (_name == name) {
+               return true;
+       }
        Diskstream::set_name (name);
 
        /* get a new write source so that its name reflects the new diskstream name */
@@ -2396,3 +2477,24 @@ AudioDiskstream::set_name (string const & name)
 
        return true;
 }
+
+bool
+AudioDiskstream::set_write_source_name (const std::string& str) {
+       if (_write_source_name == str) {
+               return true;
+       }
+
+       Diskstream::set_write_source_name (str);
+
+       if (_write_source_name == name()) {
+               return true;
+       }
+       boost::shared_ptr<ChannelList> c = channels.reader();
+       ChannelList::iterator i;
+       int n = 0;
+
+       for (n = 0, i = c->begin(); i != c->end(); ++i, ++n) {
+               use_new_write_source (n);
+       }
+       return true;
+}