Saving of edited MIDI data to disk (on session save).
[ardour.git] / libs / ardour / audio_diskstream.cc
index b372c14b57d2b5927a877f003d8649dcefccd063..7d95b7870636e716aa8368b31a1ac5204fc95dbb 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>
@@ -50,6 +49,7 @@
 #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 "i18n.h"
@@ -112,7 +112,7 @@ AudioDiskstream::init (Diskstream::Flag f)
        allocate_temporary_buffers ();
 
        add_channel (1);
-       assert(_n_channels == 1);
+       assert(_n_channels == ChanCount(DataType::AUDIO, 1));
 }
 
 AudioDiskstream::~AudioDiskstream ()
@@ -167,12 +167,12 @@ AudioDiskstream::non_realtime_input_change ()
                        RCUWriter<ChannelList> writer (channels);
                        boost::shared_ptr<ChannelList> c = writer.get_copy();
                        
-                       _n_channels = c->size();
+                       _n_channels.set(DataType::AUDIO, c->size());
                        
-                       if (_io->n_inputs() > _n_channels) {
-                               add_channel_to (c, _io->n_inputs() - _n_channels);
-                       } else if (_io->n_inputs() < _n_channels) {
-                               remove_channel_from (c, _n_channels - _io->n_inputs());
+                       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());
                        }
                }
                
@@ -211,7 +211,7 @@ AudioDiskstream::get_input_sources ()
 
        uint32_t n;
        ChannelList::iterator chan;
-       uint32_t ni = _io->n_inputs();
+       uint32_t ni = _io->n_inputs().n_audio();
 
        for (n = 0, chan = c->begin(); chan != c->end() && n < ni; ++chan, ++n) {
                
@@ -226,7 +226,8 @@ AudioDiskstream::get_input_sources ()
                        (*chan)->source = 0;
                        
                } else {
-                       (*chan)->source = _session.engine().get_port_by_name (connections[0]);
+                       (*chan)->source = dynamic_cast<AudioPort*>(
+                               _session.engine().get_port_by_name (connections[0]) );
                }
                
                if (connections) {
@@ -241,7 +242,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) {
@@ -278,7 +279,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);
@@ -463,7 +464,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) {
                                
@@ -549,12 +550,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) {
@@ -592,7 +598,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;
                }
@@ -610,7 +616,7 @@ AudioDiskstream::process (nframes_t transport_frame, nframes_t nframes, nframes_
 
        if (nominally_recording || rec_nframes) {
 
-               uint32_t limit = _io->n_inputs ();
+               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
@@ -633,7 +639,10 @@ AudioDiskstream::process (nframes_t transport_frame, nframes_t nframes, nframes_
                                   rec_offset
                                */
 
-                               memcpy (chaninfo->current_capture_buffer, _io->input(n)->get_buffer (rec_nframes) + offset + rec_offset, sizeof (Sample) * rec_nframes);
+                               AudioPort* const ap = _io->audio_input(n);
+                               assert(ap);
+                               assert(rec_nframes <= ap->get_audio_buffer().capacity());
+                               memcpy (chaninfo->current_capture_buffer, ap->get_audio_buffer().data(rec_nframes, offset + rec_offset), sizeof (Sample) * rec_nframes);
 
                        } else {
 
@@ -644,7 +653,10 @@ AudioDiskstream::process (nframes_t transport_frame, nframes_t nframes, nframes_
                                        goto out;
                                }
 
-                               Sample* buf = _io->input (n)->get_buffer (nframes) + offset;
+                               AudioPort* const ap = _io->audio_input(n);
+                               assert(ap);
+
+                               Sample* buf = ap->get_audio_buffer().data(nframes, offset);
                                nframes_t first = chaninfo->capture_vector.len[0];
 
                                memcpy (chaninfo->capture_wrap_buffer, buf, sizeof (Sample) * first);
@@ -932,7 +944,7 @@ int
 AudioDiskstream::seek (nframes_t frame, bool complete_refill)
 {
        uint32_t n;
-       int ret;
+       int ret = -1;
        ChannelList::iterator chan;
        boost::shared_ptr<ChannelList> c = channels.reader();
 
@@ -1579,7 +1591,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) {
@@ -1612,7 +1624,7 @@ AudioDiskstream::transport_stopped (struct tm& when, time_t twhen, bool abort_ca
                }
 
                _playlist->thaw ();
-                XMLNode &after = _playlist->get_state();
+               XMLNode &after = _playlist->get_state();
                _session.add_command (new MementoCommand<Playlist>(*_playlist, &before, &after));
        }
 
@@ -1631,6 +1643,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)
 {
@@ -1646,7 +1709,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;
@@ -1683,7 +1745,7 @@ AudioDiskstream::finish_capture (bool rec_monitors_input, boost::shared_ptr<Chan
 void
 AudioDiskstream::set_record_enabled (bool yn)
 {
-       if (!recordable() || !_session.record_enabling_legal() || _io->n_inputs() == 0) {
+       if (!recordable() || !_session.record_enabling_legal() || _io->n_inputs().n_audio() == 0) {
                return;
        }
 
@@ -1731,11 +1793,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 ();
                }
        }
 
@@ -1863,15 +1927,15 @@ AudioDiskstream::set_state (const XMLNode& node)
        // create necessary extra channels
        // we are always constructed with one and we always need one
 
-       _n_channels = channels.reader()->size();
+       _n_channels.set(DataType::AUDIO, channels.reader()->size());
        
-       if (nchans > _n_channels) {
+       if (nchans > _n_channels.n_audio()) {
 
-               add_channel (nchans - _n_channels);
+               add_channel (nchans - _n_channels.n_audio());
 
-       } else if (nchans < _n_channels) {
+       } else if (nchans < _n_channels.n_audio()) {
 
-               remove_channel (_n_channels - nchans);
+               remove_channel (_n_channels.n_audio() - nchans);
        }
 
        if ((prop = node.property ("playlist")) == 0) {
@@ -2016,7 +2080,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 ? */
                }
        }
@@ -2112,7 +2176,7 @@ AudioDiskstream::add_channel_to (boost::shared_ptr<ChannelList> c, uint32_t how_
                c->push_back (new ChannelInfo(_session.diskstream_buffer_size(), speed_buffer_size, wrap_buffer_size));
        }
 
-       _n_channels = c->size();
+       _n_channels.set(DataType::AUDIO, c->size());
 
        return 0;
 }
@@ -2134,7 +2198,7 @@ AudioDiskstream::remove_channel_from (boost::shared_ptr<ChannelList> c, uint32_t
                c->pop_back();
        }
 
-       _n_channels = c->size();
+       _n_channels.set(DataType::AUDIO, c->size());
 
        return 0;
 }
@@ -2192,8 +2256,15 @@ 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 (_session, prop->value(), false, _session.frame_rate()));
+                               fs = boost::dynamic_pointer_cast<AudioFileSource> (
+                                       SourceFactory::createWritable (DataType::AUDIO, _session, prop->value(), false, _session.frame_rate()));
                        }
 
                        catch (failed_constructor& err) {
@@ -2218,7 +2289,7 @@ AudioDiskstream::use_pending_capture_data (XMLNode& node)
                return 1;
        }
 
-       if (pending_sources.size() != _n_channels) {
+       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 +2410,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