Merged with trunk R846
[ardour.git] / libs / ardour / audio_track.cc
index f2a63193b38da2d84ef3f361102d4c78e6b1fd7e..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,47 +55,37 @@ AudioTrack::AudioTrack (Session& sess, string name, Route::Flag flag, TrackMode
                dflags = AudioDiskstream::Flag (dflags | AudioDiskstream::Destructive);
        }
 
-       AudioDiskstream* ds = new AudioDiskstream (_session, name, dflags);
-       
-       _declickable = true;
-       _freeze_record.state = NoFreeze;
-       _saved_meter_point = _meter_point;
-       _mode = mode;
+       boost::shared_ptr<AudioDiskstream> ds (new AudioDiskstream (_session, name, dflags));
+       _session.add_diskstream (ds);
 
-       set_diskstream (*ds, this);
+       set_diskstream (boost::dynamic_pointer_cast<AudioDiskstream> (ds), this);
 }
 
 AudioTrack::AudioTrack (Session& sess, const XMLNode& node)
        : Track (sess, node)
 {
-       _freeze_record.state = NoFreeze;
        set_state (node);
-       _declickable = true;
-       _saved_meter_point = _meter_point;
 }
 
 AudioTrack::~AudioTrack ()
 {
-       if (_diskstream) {
-               _diskstream->unref();
-       }
 }
 
 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);
@@ -136,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));
@@ -155,13 +141,13 @@ AudioTrack::set_diskstream (AudioDiskstream& ds, void *src)
                }
        }
 
-       _diskstream->set_record_enabled (false, this);
+       _diskstream->set_record_enabled (false);
        _diskstream->monitor_input (false);
 
        ic_connection.disconnect();
        ic_connection = input_changed.connect (mem_fun (*_diskstream, &Diskstream::handle_input_change));
 
-       DiskstreamChanged (src); /* EMIT SIGNAL */
+       DiskstreamChanged (); /* EMIT SIGNAL */
 
        return 0;
 }      
@@ -169,68 +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);
-}
-
-bool
-AudioTrack::record_enabled () const
-{
-       return _diskstream->record_enabled ();
+       return set_diskstream (dstream, this);
 }
 
-void
-AudioTrack::set_record_enable (bool yn, void *src)
-{
-       if (_freeze_record.state == Frozen) {
-               return;
-       }
-
-       if (_mix_group && src != _mix_group && _mix_group->is_active()) {
-               _mix_group->apply (&AudioTrack::set_record_enable, yn, _mix_group);
-               return;
-       }
-
-       /* keep track of the meter point as it was before we rec-enabled */
-
-       if (!_diskstream->record_enabled()) {
-               _saved_meter_point = _meter_point;
-       }
-       
-       _diskstream->set_record_enabled (yn, src);
-
-       if (_diskstream->record_enabled()) {
-               set_meter_point (MeterInput, this);
-       } else {
-               set_meter_point (_saved_meter_point, this);
-       }
-
-       _rec_enable_control.Changed ();
-}
-
-AudioDiskstream&
+boost::shared_ptr<AudioDiskstream>
 AudioTrack::audio_diskstream() const
 {
-       return *dynamic_cast<AudioDiskstream*>(_diskstream);
+       return boost::dynamic_pointer_cast<AudioDiskstream>(_diskstream);
 }
 
 int
@@ -424,8 +375,8 @@ AudioTrack::set_state_part_two ()
                                continue;
                        }
                        
-                       FreezeRecordInsertInfo* frii = new FreezeRecordInsertInfo (*((*citer)->children().front()));
-                       frii->insert = 0;
+                       FreezeRecordInsertInfo* frii = new FreezeRecordInsertInfo (*((*citer)->children().front()),
+                                                                                  boost::shared_ptr<Insert>());
                        frii->id = prop->value ();
                        _freeze_record.insert_info.push_back (frii);
                }
@@ -446,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;
        }
 
@@ -480,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;
        
@@ -547,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);
@@ -558,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;
        }
 
@@ -574,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);
 
@@ -593,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)
@@ -601,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()
@@ -614,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;
                                }
@@ -633,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()) {
@@ -641,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 */
@@ -655,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;
        }
 
@@ -669,33 +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);
-}
-
-int
-AudioTrack::set_name (string str, void *src)
-{
-       int ret;
-
-       if (record_enabled() && _session.actively_recording()) {
-               /* this messes things up if done while recording */
-               return -1;
-       }
-
-       if (audio_diskstream().set_name (str, src)) {
-               return -1;
-       }
-
-       /* save state so that the statefile fully reflects any filename changes */
-
-       if ((ret = IO::set_name (str, src)) == 0) {
-               _session.save_state ("");
-       }
-       return ret;
+       return audio_diskstream()->process (_session.transport_frame() + offset, nframes, offset, can_record, rec_monitors_input);
 }
 
 int
-AudioTrack::export_stuff (vector<Sample*>& buffers, char * workbuf, 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];
@@ -703,34 +624,34 @@ AudioTrack::export_stuff (vector<Sample*>& buffers, char * workbuf, uint32_t nbu
        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, workbuf, 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, workbuf, 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); 
                }
        }
 
@@ -740,12 +661,12 @@ AudioTrack::export_stuff (vector<Sample*>& buffers, char * workbuf, uint32_t nbu
        */
        
        for (i = _redirects.begin(); i != _redirects.end(); ++i) {
-               Insert *insert;
+               boost::shared_ptr<Insert> insert;
                
-               if ((insert = dynamic_cast<Insert*>(*i)) != 0) {
+               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;
@@ -758,8 +679,8 @@ AudioTrack::export_stuff (vector<Sample*>& buffers, char * workbuf, uint32_t nbu
                
                _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];
                        }
@@ -767,8 +688,8 @@ AudioTrack::export_stuff (vector<Sample*>& buffers, char * workbuf, uint32_t nbu
 
        } 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;
                        }
@@ -778,14 +699,14 @@ AudioTrack::export_stuff (vector<Sample*>& buffers, char * workbuf, uint32_t nbu
        if (post_fader_work) {
 
                for (i = _redirects.begin(); i != _redirects.end(); ++i) {
-                       PluginInsert *insert;
+                       boost::shared_ptr<PluginInsert> insert;
                        
-                       if ((insert = dynamic_cast<PluginInsert*>(*i)) != 0) {
+                       if ((insert = boost::dynamic_pointer_cast<PluginInsert>(*i)) != 0) {
                                switch ((*i)->placement()) {
                                case PreFader:
                                        break;
                                case PostFader:
-                                       insert->run (buffers, nbufs, nframes, 0);
+                                       insert->run (buffers, start, start+nframes, nframes, 0);
                                        break;
                                }
                        }
@@ -795,17 +716,10 @@ AudioTrack::export_stuff (vector<Sample*>& buffers, char * workbuf, uint32_t nbu
        return 0;
 }
 
-void
-AudioTrack::set_latency_delay (jack_nframes_t longest_session_latency)
-{
-       Route::set_latency_delay (longest_session_latency);
-       audio_diskstream().set_roll_delay (_roll_delay);
-}
-
 void
 AudioTrack::bounce (InterThreadInfo& itt)
 {
-       vector<AudioSource*> srcs;
+       vector<Source*> srcs;
        _session.write_one_audio_track (*this, 0, _session.current_end_frame(), false, srcs, itt);
 }
 
@@ -813,23 +727,22 @@ AudioTrack::bounce (InterThreadInfo& itt)
 void
 AudioTrack::bounce_range (jack_nframes_t start, jack_nframes_t end, InterThreadInfo& itt)
 {
-       vector<AudioSource*> srcs;
+       vector<Source*> srcs;
        _session.write_one_audio_track (*this, start, end, false, srcs, itt);
 }
 
 void
 AudioTrack::freeze (InterThreadInfo& itt)
 {
-       Insert* insert;
-       vector<AudioSource*> srcs;
+       vector<Source*> srcs;
        string new_playlist_name;
        Playlist* new_playlist;
        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;
        }
 
@@ -851,7 +764,7 @@ AudioTrack::freeze (InterThreadInfo& itt)
        } 
 
        if (n == (UINT_MAX-1)) {
-         error << string_compose (X_("There Are too many frozen versions of playlist \"%1\""
+         error << string_compose (X_("There are too many frozen versions of playlist \"%1\""
                            " to create another one"), _freeze_record.playlist->name())
               << endmsg;
                return;
@@ -869,11 +782,12 @@ AudioTrack::freeze (InterThreadInfo& itt)
                
                for (RedirectList::iterator r = _redirects.begin(); r != _redirects.end(); ++r) {
                        
-                       if ((insert = dynamic_cast<Insert*>(*r)) != 0) {
+                       boost::shared_ptr<Insert> insert;
+
+                       if ((insert = boost::dynamic_pointer_cast<Insert>(*r)) != 0) {
                                
-                               FreezeRecordInsertInfo* frii  = new FreezeRecordInsertInfo ((*r)->get_state());
+                               FreezeRecordInsertInfo* frii  = new FreezeRecordInsertInfo ((*r)->get_state(), insert);
                                
-                               frii->insert = insert;
                                frii->id = insert->id();
                                frii->memento = (*r)->get_memento();
                                
@@ -896,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, this);
+       diskstream->use_playlist (dynamic_cast<AudioPlaylist*>(new_playlist));
+       diskstream->set_record_enabled (false);
 
        _freeze_record.state = Frozen;
        FreezeChange(); /* EMIT SIGNAL */
@@ -912,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) {
 
@@ -940,14 +854,3 @@ AudioTrack::unfreeze ()
        FreezeChange (); /* EMIT SIGNAL */
 }
 
-void
-AudioTrack::set_mode (TrackMode m)
-{
-       if (_diskstream) {
-               if (_mode != m) {
-                       _mode = m;
-                       audio_diskstream().set_destructive (m == Destructive);
-                       ModeChanged();
-               }
-       }
-}