merged with 2.0-ongoing changes 2582-2605 (not thoroughly tested but it compiles...
[ardour.git] / libs / ardour / audio_diskstream.cc
index 2c878fbd4d99e0566b77d8f39263eec0de97391e..bc42cb0b5b5849c6c07c023ed6e8ac0a6fbf6541 100644 (file)
@@ -31,7 +31,6 @@
 #include <sys/mman.h>
 
 #include <pbd/error.h>
-#include <pbd/basename.h>
 #include <glibmm/thread.h>
 #include <pbd/xml++.h>
 #include <pbd/memento_command.h>
@@ -170,10 +169,10 @@ AudioDiskstream::non_realtime_input_change ()
                        
                        _n_channels.set(DataType::AUDIO, c->size());
                        
-                       if (_io->n_inputs().get(DataType::AUDIO) > _n_channels.get(DataType::AUDIO)) {
-                               add_channel_to (c, _io->n_inputs().get(DataType::AUDIO) - _n_channels.get(DataType::AUDIO));
-                       } else if (_io->n_inputs().get(DataType::AUDIO) < _n_channels.get(DataType::AUDIO)) {
-                               remove_channel_from (c, _n_channels.get(DataType::AUDIO) - _io->n_inputs().get(DataType::AUDIO));
+                       if (_io->n_inputs().n_audio() > _n_channels.n_audio()) {
+                               add_channel_to (c, _io->n_inputs().n_audio() - _n_channels.n_audio());
+                       } else if (_io->n_inputs().n_audio() < _n_channels.n_audio()) {
+                               remove_channel_from (c, _n_channels.n_audio() - _io->n_inputs().n_audio());
                        }
                }
                
@@ -212,14 +211,15 @@ AudioDiskstream::get_input_sources ()
 
        uint32_t n;
        ChannelList::iterator chan;
-       uint32_t ni = _io->n_inputs().get(DataType::AUDIO);
+       uint32_t ni = _io->n_inputs().n_audio();
+       vector<string> connections;
 
        for (n = 0, chan = c->begin(); chan != c->end() && n < ni; ++chan, ++n) {
                
-               const char **connections = _io->input(n)->get_connections ();
+               connections.clear ();
+
+               if (_io->input(n)->get_connections (connections) == 0) {
                
-               if (connections == 0 || connections[0] == 0) {
-                       
                        if ((*chan)->source) {
                                // _source->disable_metering ();
                        }
@@ -227,12 +227,7 @@ AudioDiskstream::get_input_sources ()
                        (*chan)->source = 0;
                        
                } else {
-                       (*chan)->source = dynamic_cast<AudioPort*>(
-                               _session.engine().get_port_by_name (connections[0]) );
-               }
-               
-               if (connections) {
-                       free (connections);
+                       (*chan)->source = dynamic_cast<AudioPort*>(_session.engine().get_port_by_name (connections[0]) );
                }
        }
 }              
@@ -243,7 +238,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) {
-               playlist = boost::dynamic_pointer_cast<AudioPlaylist> (PlaylistFactory::create (_session, name));
+               playlist = boost::dynamic_pointer_cast<AudioPlaylist> (PlaylistFactory::create (DataType::AUDIO, _session, name));
        }
 
        if (!playlist) {
@@ -280,7 +275,7 @@ AudioDiskstream::use_new_playlist ()
                newname = Playlist::bump_name (_name, _session);
        }
 
-       if ((playlist = boost::dynamic_pointer_cast<AudioPlaylist> (PlaylistFactory::create (_session, newname, hidden()))) != 0) {
+       if ((playlist = boost::dynamic_pointer_cast<AudioPlaylist> (PlaylistFactory::create (DataType::AUDIO, _session, newname, hidden()))) != 0) {
                
                playlist->set_orig_diskstream_id (id());
                return use_playlist (playlist);
@@ -465,7 +460,7 @@ AudioDiskstream::check_record_status (nframes_t transport_frame, nframes_t nfram
                        
                }
 
-               if (_flags & Recordable) {
+               if (recordable() && destructive()) {
                        boost::shared_ptr<ChannelList> c = channels.reader();
                        for (ChannelList::iterator chan = c->begin(); chan != c->end(); ++chan) {
                                
@@ -551,12 +546,17 @@ AudioDiskstream::process (nframes_t transport_frame, nframes_t nframes, nframes_
 
        for (chan = c->begin(); chan != c->end(); ++chan) {
                (*chan)->current_capture_buffer = 0;
-               (*chan)->current_playback_buffer  = 0;
+               (*chan)->current_playback_buffer = 0;
        }
 
        if (nominally_recording || (_session.get_record_enabled() && 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) {
@@ -594,7 +594,7 @@ AudioDiskstream::process (nframes_t transport_frame, nframes_t nframes, nframes_
                        /*    |--------|    recrange
                             --------------  transrange
                        */
-                       rec_nframes = last_recordable_frame - last_recordable_frame;
+                       rec_nframes = last_recordable_frame - first_recordable_frame;
                        rec_offset = first_recordable_frame - transport_frame;
                        break;
                }
@@ -612,7 +612,16 @@ AudioDiskstream::process (nframes_t transport_frame, nframes_t nframes, nframes_
 
        if (nominally_recording || rec_nframes) {
 
-               for (n = 0, chan = c->begin(); chan != c->end(); ++chan, ++n) {
+               uint32_t limit = _io->n_inputs ().n_audio();
+
+               /* one or more ports could already have been removed from _io, but our
+                  channel setup hasn't yet been updated. prevent us from trying to
+                  use channels that correspond to missing ports. note that the
+                  process callback (from which this is called) is always atomic
+                  with respect to port removal/addition.
+               */
+
+               for (n = 0, chan = c->begin(); chan != c->end() && n < limit; ++chan, ++n) {
                        
                        ChannelInfo* chaninfo (*chan);
 
@@ -753,11 +762,18 @@ AudioDiskstream::process (nframes_t transport_frame, nframes_t nframes, nframes_
                if (rec_nframes == 0 && _actual_speed != 1.0f && _actual_speed != -1.0f) {
                        
                        uint64_t phase = last_phase;
+                       int64_t phi_delta;
                        nframes_t i = 0;
 
                        // Linearly interpolate into the alt buffer
                        // using 40.24 fixp maths (swh)
 
+                       if (phi != target_phi) {
+                               phi_delta = ((int64_t)(target_phi - phi)) / nframes;
+                       } else {
+                               phi_delta = 0;
+                       }
+
                        for (chan = c->begin(); chan != c->end(); ++chan) {
 
                                float fr;
@@ -772,19 +788,21 @@ AudioDiskstream::process (nframes_t transport_frame, nframes_t nframes, nframes_
                                        chaninfo->speed_buffer[outsample] = 
                                                chaninfo->current_playback_buffer[i] * (1.0f - fr) +
                                                chaninfo->current_playback_buffer[i+1] * fr;
-                                       phase += phi;
+                                       phase += phi + phi_delta;
                                }
                                
                                chaninfo->current_playback_buffer = chaninfo->speed_buffer;
                        }
 
-                       playback_distance = i + 1;
+                       playback_distance = i; // + 1;
                        last_phase = (phase & 0xFFFFFF);
 
                } else {
                        playback_distance = nframes;
                }
 
+               phi = target_phi;
+
        }
 
        ret = 0;
@@ -1578,7 +1596,7 @@ AudioDiskstream::transport_stopped (struct tm& when, time_t twhen, bool abort_ca
 
                // cerr << _name << ": there are " << capture_info.size() << " capture_info records\n";
                
-                XMLNode &before = _playlist->get_state();
+               XMLNode &before = _playlist->get_state();
                _playlist->freeze ();
                
                for (buffer_position = c->front()->write_source->last_capture_start_frame(), ci = capture_info.begin(); ci != capture_info.end(); ++ci) {
@@ -1630,6 +1648,57 @@ AudioDiskstream::transport_stopped (struct tm& when, time_t twhen, bool abort_ca
        capture_start_frame = 0;
 }
 
+void
+AudioDiskstream::transport_looped (nframes_t transport_frame)
+{
+       if (was_recording) {
+               // all we need to do is finish this capture, with modified capture length
+               boost::shared_ptr<ChannelList> c = channels.reader();
+
+               // adjust the capture length knowing that the data will be recorded to disk
+               // only necessary after the first loop where we're recording
+               if (capture_info.size() == 0) {
+                       capture_captured += _capture_offset;
+
+                       if (_alignment_style == ExistingMaterial) {
+                               capture_captured += _session.worst_output_latency();
+                       } else {
+                               capture_captured += _roll_delay;
+                       }
+               }
+
+               finish_capture (true, c);
+
+               // the next region will start recording via the normal mechanism
+               // we'll set the start position to the current transport pos
+               // no latency adjustment or capture offset needs to be made, as that already happened the first time
+               capture_start_frame = transport_frame;
+               first_recordable_frame = transport_frame; // mild lie
+               last_recordable_frame = max_frames;
+               was_recording = true;
+
+               if (recordable() && destructive()) {
+                       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 loop!  inconceivable!") 
+                                             << endmsg;
+                               }
+                       }
+               }
+
+       }
+}
+
 void
 AudioDiskstream::finish_capture (bool rec_monitors_input, boost::shared_ptr<ChannelList> c)
 {
@@ -1645,7 +1714,6 @@ AudioDiskstream::finish_capture (bool rec_monitors_input, boost::shared_ptr<Chan
                        RingBufferNPT<CaptureTransition>::rw_vector transvec;
                        (*chan)->capture_transition_buf->get_write_vector(&transvec);
                        
-                       
                        if (transvec.len[0] > 0) {
                                transvec.buf[0]->type = CaptureEnd;
                                transvec.buf[0]->capture_val = capture_captured;
@@ -1677,12 +1745,15 @@ AudioDiskstream::finish_capture (bool rec_monitors_input, boost::shared_ptr<Chan
 
        capture_info.push_back (ci);
        capture_captured = 0;
+
+       /* now we've finished a capture, reset first_recordable_frame for next time */
+       first_recordable_frame = max_frames;
 }
 
 void
 AudioDiskstream::set_record_enabled (bool yn)
 {
-       if (!recordable() || !_session.record_enabling_legal() || _io->n_inputs().get(DataType::AUDIO) == 0) {
+       if (!recordable() || !_session.record_enabling_legal() || _io->n_inputs().n_audio() == 0) {
                return;
        }
 
@@ -1730,11 +1801,13 @@ AudioDiskstream::engage_record_enable ()
                                (*chan)->source->ensure_monitor_input (!(Config->get_auto_input() && rolling));
                        }
                        capturing_sources.push_back ((*chan)->write_source);
+                       (*chan)->write_source->mark_streaming_write_started ();
                }
                
        } else {
                for (ChannelList::iterator chan = c->begin(); chan != c->end(); ++chan) {
                        capturing_sources.push_back ((*chan)->write_source);
+                       (*chan)->write_source->mark_streaming_write_started ();
                }
        }
 
@@ -1864,13 +1937,13 @@ AudioDiskstream::set_state (const XMLNode& node)
 
        _n_channels.set(DataType::AUDIO, channels.reader()->size());
        
-       if (nchans > _n_channels.get(DataType::AUDIO)) {
+       if (nchans > _n_channels.n_audio()) {
 
-               add_channel (nchans - _n_channels.get(DataType::AUDIO));
+               add_channel (nchans - _n_channels.n_audio());
 
-       } else if (nchans < _n_channels.get(DataType::AUDIO)) {
+       } else if (nchans < _n_channels.n_audio()) {
 
-               remove_channel (_n_channels.get(DataType::AUDIO) - nchans);
+               remove_channel (_n_channels.n_audio() - nchans);
        }
 
        if ((prop = node.property ("playlist")) == 0) {
@@ -2015,7 +2088,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_name (_name, destructive());
+                       (*chan)->write_source->set_source_name (_name, destructive());
                        /* XXX what to do if one of them fails ? */
                }
        }
@@ -2191,6 +2264,12 @@ AudioDiskstream::use_pending_capture_data (XMLNode& node)
                                continue;
                        }
 
+                       // This protects sessions from errant CapturingSources in stored sessions
+                       struct stat sbuf;
+                       if (stat (prop->value().c_str(), &sbuf)) {
+                               continue;
+                       }
+
                        try {
                                fs = boost::dynamic_pointer_cast<AudioFileSource> (
                                        SourceFactory::createWritable (DataType::AUDIO, _session, prop->value(), false, _session.frame_rate()));
@@ -2218,7 +2297,7 @@ AudioDiskstream::use_pending_capture_data (XMLNode& node)
                return 1;
        }
 
-       if (pending_sources.size() != _n_channels.get(DataType::AUDIO)) {
+       if (pending_sources.size() != _n_channels.n_audio()) {
                error << string_compose (_("%1: incorrect number of pending sources listed - ignoring them all"), _name)
                      << endmsg;
                return -1;
@@ -2339,7 +2418,7 @@ AudioDiskstream::ChannelInfo::ChannelInfo (nframes_t bufsize, nframes_t speed_si
 
        playback_buf = new RingBufferNPT<Sample> (bufsize);
        capture_buf = new RingBufferNPT<Sample> (bufsize);
-       capture_transition_buf = new RingBufferNPT<CaptureTransition> (128);
+       capture_transition_buf = new RingBufferNPT<CaptureTransition> (256);
        
        /* touch the ringbuffer buffers, which will cause
           them to be mapped into locked physical RAM if