Merged with trunk R846
[ardour.git] / libs / ardour / audio_track.cc
index 409b0e560fe86b68bd76678073bc6c85fafc1d25..7a44be9b54f7767dde9ca5593822ef14ab5ccb94 100644 (file)
@@ -33,7 +33,7 @@
 #include <ardour/audioplaylist.h>
 #include <ardour/panner.h>
 #include <ardour/utils.h>
-
+#include <ardour/buffer_set.h>
 #include "i18n.h"
 
 using namespace std;
@@ -55,9 +55,10 @@ AudioTrack::AudioTrack (Session& sess, string name, Route::Flag flag, TrackMode
                dflags = AudioDiskstream::Flag (dflags | AudioDiskstream::Destructive);
        }
 
-       AudioDiskstream* ds = new AudioDiskstream (_session, name, dflags);
-       
-       set_diskstream (*ds, this);
+       boost::shared_ptr<AudioDiskstream> ds (new AudioDiskstream (_session, name, dflags));
+       _session.add_diskstream (ds);
+
+       set_diskstream (boost::dynamic_pointer_cast<AudioDiskstream> (ds), this);
 }
 
 AudioTrack::AudioTrack (Session& sess, const XMLNode& node)
@@ -73,18 +74,18 @@ AudioTrack::~AudioTrack ()
 int
 AudioTrack::deprecated_use_diskstream_connections ()
 {
-       AudioDiskstream& diskstream = audio_diskstream();
+       boost::shared_ptr<AudioDiskstream> diskstream = audio_diskstream();
 
-       if (diskstream.deprecated_io_node == 0) {
+       if (diskstream->deprecated_io_node == 0) {
                return 0;
        }
 
        const XMLProperty* prop;
-       XMLNode& node (*diskstream.deprecated_io_node);
+       XMLNode& node (*diskstream->deprecated_io_node);
 
        /* don't do this more than once. */
 
-       diskstream.deprecated_io_node = 0;
+       diskstream->deprecated_io_node = 0;
 
        set_input_minimum (-1);
        set_input_maximum (-1);
@@ -125,17 +126,13 @@ AudioTrack::deprecated_use_diskstream_connections ()
 }
 
 int
-AudioTrack::set_diskstream (AudioDiskstream& ds, void *src)
+AudioTrack::set_diskstream (boost::shared_ptr<AudioDiskstream> ds, void *src)
 {
-       if (_diskstream) {
-               _diskstream->unref();
-       }
-
-       _diskstream = &ds.ref();
+       _diskstream = ds;
        _diskstream->set_io (*this);
        _diskstream->set_destructive (_mode == Destructive);
 
-       if (audio_diskstream().deprecated_io_node) {
+       if (audio_diskstream()->deprecated_io_node) {
 
                if (!connecting_legal) {
                        ConnectingLegal.connect (mem_fun (*this, &AudioTrack::deprecated_use_diskstream_connections));
@@ -158,33 +155,33 @@ AudioTrack::set_diskstream (AudioDiskstream& ds, void *src)
 int 
 AudioTrack::use_diskstream (string name)
 {
-       AudioDiskstream *dstream;
+       boost::shared_ptr<AudioDiskstream> dstream;
 
-       if ((dstream = dynamic_cast<AudioDiskstream*>(_session.diskstream_by_name (name))) == 0) {
-         error << string_compose(_("AudioTrack: audio diskstream \"%1\" not known by session"), name) << endmsg;
+       if ((dstream = boost::dynamic_pointer_cast<AudioDiskstream>(_session.diskstream_by_name (name))) == 0) {
+               error << string_compose(_("AudioTrack: audio diskstream \"%1\" not known by session"), name) << endmsg;
                return -1;
        }
        
-       return set_diskstream (*dstream, this);
+       return set_diskstream (dstream, this);
 }
 
 int 
 AudioTrack::use_diskstream (const PBD::ID& id)
 {
-       AudioDiskstream *dstream;
+       boost::shared_ptr<AudioDiskstream> dstream;
 
-       if ((dstream = dynamic_cast<AudioDiskstream*>(_session.diskstream_by_id (id))) == 0) {
+       if ((dstream = boost::dynamic_pointer_cast<AudioDiskstream> (_session.diskstream_by_id (id))) == 0) {
                error << string_compose(_("AudioTrack: audio diskstream \"%1\" not known by session"), id) << endmsg;
                return -1;
        }
        
-       return set_diskstream (*dstream, this);
+       return set_diskstream (dstream, this);
 }
 
-AudioDiskstream&
+boost::shared_ptr<AudioDiskstream>
 AudioTrack::audio_diskstream() const
 {
-       return *dynamic_cast<AudioDiskstream*>(_diskstream);
+       return boost::dynamic_pointer_cast<AudioDiskstream>(_diskstream);
 }
 
 int
@@ -400,24 +397,17 @@ AudioTrack::set_state_part_two ()
        return;
 }      
 
-uint32_t
+ChanCount
 AudioTrack::n_process_buffers ()
 {
-       return max ((uint32_t) _diskstream->n_channels(), redirect_max_outs);
-}
-
-void
-AudioTrack::passthru_silence (jack_nframes_t start_frame, jack_nframes_t end_frame, jack_nframes_t nframes, jack_nframes_t offset, int declick, bool meter)
-{
-       uint32_t nbufs = n_process_buffers ();
-       process_output_buffers (_session.get_silent_buffers (nbufs), nbufs, start_frame, end_frame, nframes, offset, true, declick, meter);
+       return max (_diskstream->n_channels(), redirect_max_outs);
 }
 
 int 
 AudioTrack::no_roll (jack_nframes_t nframes, jack_nframes_t start_frame, jack_nframes_t end_frame, jack_nframes_t offset, 
                     bool session_state_changing, bool can_record, bool rec_monitors_input)
 {
-       if (n_outputs() == 0) {
+       if (n_outputs().get_total() == 0) {
                return 0;
        }
 
@@ -434,7 +424,7 @@ AudioTrack::no_roll (jack_nframes_t nframes, jack_nframes_t start_frame, jack_nf
                return 0;
        }
 
-       audio_diskstream().check_record_status (start_frame, nframes, can_record);
+       audio_diskstream()->check_record_status (start_frame, nframes, can_record);
 
        bool send_silence;
        
@@ -501,7 +491,7 @@ AudioTrack::roll (jack_nframes_t nframes, jack_nframes_t start_frame, jack_nfram
        Sample* b;
        Sample* tmpb;
        jack_nframes_t transport_frame;
-       AudioDiskstream& diskstream = audio_diskstream();
+       boost::shared_ptr<AudioDiskstream> diskstream = audio_diskstream();
        
        {
                Glib::RWLock::ReaderLock lm (redirect_lock, Glib::TRY_LOCK);
@@ -512,7 +502,7 @@ AudioTrack::roll (jack_nframes_t nframes, jack_nframes_t start_frame, jack_nfram
                }
        }
        
-       if (n_outputs() == 0 && _redirects.empty()) {
+       if (n_outputs().get_total() == 0 && _redirects.empty()) {
                return 0;
        }
 
@@ -528,13 +518,13 @@ AudioTrack::roll (jack_nframes_t nframes, jack_nframes_t start_frame, jack_nfram
                   playback distance to zero, thus causing diskstream::commit
                   to do nothing.
                */
-               return diskstream.process (transport_frame, 0, 0, can_record, rec_monitors_input);
+               return diskstream->process (transport_frame, 0, 0, can_record, rec_monitors_input);
        } 
 
        _silent = false;
        apply_gain_automation = false;
 
-       if ((dret = diskstream.process (transport_frame, nframes, offset, can_record, rec_monitors_input)) != 0) {
+       if ((dret = diskstream->process (transport_frame, nframes, offset, can_record, rec_monitors_input)) != 0) {
                
                silence (nframes, offset);
 
@@ -547,7 +537,7 @@ AudioTrack::roll (jack_nframes_t nframes, jack_nframes_t start_frame, jack_nfram
                just_meter_input (start_frame, end_frame, nframes, offset);
        }
 
-       if (diskstream.record_enabled() && !can_record && !_session.get_auto_input()) {
+       if (diskstream->record_enabled() && !can_record && !_session.get_auto_input()) {
 
                /* not actually recording, but we want to hear the input material anyway,
                   at least potentially (depending on monitoring options)
@@ -555,7 +545,7 @@ AudioTrack::roll (jack_nframes_t nframes, jack_nframes_t start_frame, jack_nfram
 
                passthru (start_frame, end_frame, nframes, offset, 0, true);
 
-       } else if ((b = diskstream.playback_buffer(0)) != 0) {
+       } else if ((b = diskstream->playback_buffer(0)) != 0) {
 
                /*
                  XXX is it true that the earlier test on n_outputs()
@@ -568,17 +558,16 @@ AudioTrack::roll (jack_nframes_t nframes, jack_nframes_t start_frame, jack_nfram
                
                /* copy the diskstream data to all output buffers */
                
-               vector<Sample*>& bufs = _session.get_passthru_buffers ();
-               uint32_t limit = n_process_buffers ();
+               const size_t limit = n_process_buffers().get(DataType::AUDIO);
+               BufferSet& bufs = _session.get_scratch_buffers (n_process_buffers());
                
                uint32_t n;
                uint32_t i;
 
-
                for (i = 0, n = 1; i < limit; ++i, ++n) {
-                       memcpy (bufs[i], b, sizeof (Sample) * nframes); 
-                       if (n < diskstream.n_channels()) {
-                               tmpb = diskstream.playback_buffer(n);
+                       memcpy (bufs.get_audio(i).data(nframes), b, sizeof (Sample) * nframes); 
+                       if (n < diskstream->n_channels().get(DataType::AUDIO)) {
+                               tmpb = diskstream->playback_buffer(n);
                                if (tmpb!=0) {
                                        b = tmpb;
                                }
@@ -587,7 +576,7 @@ AudioTrack::roll (jack_nframes_t nframes, jack_nframes_t start_frame, jack_nfram
 
                /* don't waste time with automation if we're recording or we've just stopped (yes it can happen) */
 
-               if (!diskstream.record_enabled() && _session.transport_rolling()) {
+               if (!diskstream->record_enabled() && _session.transport_rolling()) {
                        Glib::Mutex::Lock am (automation_lock, Glib::TRY_LOCK);
                        
                        if (am.locked() && gain_automation_playback()) {
@@ -595,7 +584,7 @@ AudioTrack::roll (jack_nframes_t nframes, jack_nframes_t start_frame, jack_nfram
                        }
                }
 
-               process_output_buffers (bufs, limit, start_frame, end_frame, nframes, offset, (!_session.get_record_enabled() || !_session.get_do_not_record_plugins()), declick, (_meter_point != MeterInput));
+               process_output_buffers (bufs, start_frame, end_frame, nframes, offset, (!_session.get_record_enabled() || !_session.get_do_not_record_plugins()), declick, (_meter_point != MeterInput));
                
        } else {
                /* problem with the diskstream; just be quiet for a bit */
@@ -609,7 +598,7 @@ int
 AudioTrack::silent_roll (jack_nframes_t nframes, jack_nframes_t start_frame, jack_nframes_t end_frame, jack_nframes_t offset, 
                         bool can_record, bool rec_monitors_input)
 {
-       if (n_outputs() == 0 && _redirects.empty()) {
+       if (n_outputs().get_total() == 0 && _redirects.empty()) {
                return 0;
        }
 
@@ -623,11 +612,11 @@ AudioTrack::silent_roll (jack_nframes_t nframes, jack_nframes_t start_frame, jac
 
        silence (nframes, offset);
 
-       return audio_diskstream().process (_session.transport_frame() + offset, nframes, offset, can_record, rec_monitors_input);
+       return audio_diskstream()->process (_session.transport_frame() + offset, nframes, offset, can_record, rec_monitors_input);
 }
 
 int
-AudioTrack::export_stuff (vector<Sample*>& buffers, uint32_t nbufs, jack_nframes_t start, jack_nframes_t nframes)
+AudioTrack::export_stuff (BufferSet& buffers, jack_nframes_t start, jack_nframes_t nframes)
 {
        gain_t  gain_automation[nframes];
        gain_t  gain_buffer[nframes];
@@ -635,34 +624,34 @@ AudioTrack::export_stuff (vector<Sample*>& buffers, uint32_t nbufs, jack_nframes
        RedirectList::iterator i;
        bool post_fader_work = false;
        gain_t this_gain = _gain;
-       vector<Sample*>::iterator bi;
-       Sample * b;
-       AudioDiskstream& diskstream = audio_diskstream();
+       boost::shared_ptr<AudioDiskstream> diskstream = audio_diskstream();
        
        Glib::RWLock::ReaderLock rlock (redirect_lock);
 
        // FIXME
-       AudioPlaylist* const apl = dynamic_cast<AudioPlaylist*>(diskstream.playlist());
+       AudioPlaylist* const apl = dynamic_cast<AudioPlaylist*>(diskstream->playlist());
        assert(apl);
 
-       if (apl->read (buffers[0], mix_buffer, gain_buffer, start, nframes) != nframes) {
+       if (apl->read (buffers.get_audio(nframes).data(nframes),
+                       mix_buffer, gain_buffer, start, nframes) != nframes) {
                return -1;
        }
 
+       assert(buffers.count().get(DataType::AUDIO) >= 1);
        uint32_t n=1;
-       bi = buffers.begin();
-       b = buffers[0];
+       Sample* b = buffers.get_audio(0).data(nframes);
+       BufferSet::audio_iterator bi = buffers.audio_begin();
        ++bi;
-       for (; bi != buffers.end(); ++bi, ++n) {
-               if (n < diskstream.n_channels()) {
-                       if (apl->read ((*bi), mix_buffer, gain_buffer, start, nframes, n) != nframes) {
+       for ( ; bi != buffers.audio_end(); ++bi, ++n) {
+               if (n < diskstream->n_channels().get(DataType::AUDIO)) {
+                       if (apl->read (bi->data(nframes), mix_buffer, gain_buffer, start, nframes, n) != nframes) {
                                return -1;
                        }
-                       b = (*bi);
+                       b = bi->data(nframes);
                }
                else {
                        /* duplicate last across remaining buffers */
-                       memcpy ((*bi), b, sizeof (Sample) * nframes); 
+                       memcpy (bi->data(nframes), b, sizeof (Sample) * nframes); 
                }
        }
 
@@ -677,7 +666,7 @@ AudioTrack::export_stuff (vector<Sample*>& buffers, uint32_t nbufs, jack_nframes
                if ((insert = boost::dynamic_pointer_cast<Insert>(*i)) != 0) {
                        switch (insert->placement()) {
                        case PreFader:
-                               insert->run (buffers, nbufs, nframes, 0);
+                               insert->run (buffers, start, start+nframes, nframes, 0);
                                break;
                        case PostFader:
                                post_fader_work = true;
@@ -690,8 +679,8 @@ AudioTrack::export_stuff (vector<Sample*>& buffers, uint32_t nbufs, jack_nframes
                
                _gain_automation_curve.get_vector (start, start + nframes, gain_automation, nframes);
 
-               for (bi = buffers.begin(); bi != buffers.end(); ++bi) {
-                       Sample *b = *bi;
+               for (BufferSet::audio_iterator bi = buffers.audio_begin(); bi != buffers.audio_end(); ++bi) {
+                       Sample *b = bi->data(nframes);
                        for (jack_nframes_t n = 0; n < nframes; ++n) {
                                b[n] *= gain_automation[n];
                        }
@@ -699,8 +688,8 @@ AudioTrack::export_stuff (vector<Sample*>& buffers, uint32_t nbufs, jack_nframes
 
        } else {
 
-               for (bi = buffers.begin(); bi != buffers.end(); ++bi) {
-                       Sample *b = *bi;
+               for (BufferSet::audio_iterator bi = buffers.audio_begin(); bi != buffers.audio_end(); ++bi) {
+                       Sample *b = bi->data(nframes);
                        for (jack_nframes_t n = 0; n < nframes; ++n) {
                                b[n] *= this_gain;
                        }
@@ -717,7 +706,7 @@ AudioTrack::export_stuff (vector<Sample*>& buffers, uint32_t nbufs, jack_nframes
                                case PreFader:
                                        break;
                                case PostFader:
-                                       insert->run (buffers, nbufs, nframes, 0);
+                                       insert->run (buffers, start, start+nframes, nframes, 0);
                                        break;
                                }
                        }
@@ -751,9 +740,9 @@ AudioTrack::freeze (InterThreadInfo& itt)
        string dir;
        AudioRegion* region;
        string region_name;
-       AudioDiskstream& diskstream = audio_diskstream();
+       boost::shared_ptr<AudioDiskstream> diskstream = audio_diskstream();
        
-       if ((_freeze_record.playlist = dynamic_cast<AudioPlaylist*>(diskstream.playlist())) == 0) {
+       if ((_freeze_record.playlist = dynamic_cast<AudioPlaylist*>(diskstream->playlist())) == 0) {
                return;
        }
 
@@ -821,13 +810,13 @@ AudioTrack::freeze (InterThreadInfo& itt)
                                  (AudioRegion::Flag) (AudioRegion::WholeFile|AudioRegion::DefaultFlags),
                                  false);
 
-       new_playlist->set_orig_diskstream_id (diskstream.id());
+       new_playlist->set_orig_diskstream_id (diskstream->id());
        new_playlist->add_region (*region, 0);
        new_playlist->set_frozen (true);
        region->set_locked (true);
 
-       diskstream.use_playlist (dynamic_cast<AudioPlaylist*>(new_playlist));
-       diskstream.set_record_enabled (false);
+       diskstream->use_playlist (dynamic_cast<AudioPlaylist*>(new_playlist));
+       diskstream->set_record_enabled (false);
 
        _freeze_record.state = Frozen;
        FreezeChange(); /* EMIT SIGNAL */
@@ -837,7 +826,7 @@ void
 AudioTrack::unfreeze ()
 {
        if (_freeze_record.playlist) {
-               audio_diskstream().use_playlist (_freeze_record.playlist);
+               audio_diskstream()->use_playlist (_freeze_record.playlist);
 
                if (_freeze_record.have_mementos) {