fixes for various bugs including dangling ref to route in session, opening sessions...
[ardour.git] / libs / ardour / auditioner.cc
index ed97cf7b393e09459e9a5089695a5891796b5319..7bf9f0542a3584f479a500f55e24a114d31a7d23 100644 (file)
     $Id$
 */
 
-#include <pbd/lockmonitor.h>
+#include <glibmm/thread.h>
 
-#include <ardour/diskstream.h>
+#include <pbd/error.h>
+
+#include <ardour/audio_diskstream.h>
 #include <ardour/audioregion.h>
 #include <ardour/route.h>
 #include <ardour/session.h>
 #include <ardour/auditioner.h>
 #include <ardour/audioplaylist.h>
 #include <ardour/panner.h>
+#include <ardour/data_type.h>
+#include <ardour/region_factory.h>
 
 using namespace std;
 using namespace ARDOUR;
+using namespace PBD;
+
+#include "i18n.h"
 
 Auditioner::Auditioner (Session& s)
        : AudioTrack (s, "auditioner", Route::Hidden)
@@ -44,20 +51,20 @@ Auditioner::Auditioner (Session& s)
        defer_pan_reset ();
 
        if (left.length()) {
-               add_output_port (left, this);
+               add_output_port (left, this, DataType::AUDIO);
        }
 
        if (right.length()) {
-               disk_stream().add_channel();
-               add_output_port (right, this);
+               audio_diskstream()->add_channel();
+               add_output_port (right, this, DataType::AUDIO);
        }
 
        allow_pan_reset ();
        
        IO::output_changed.connect (mem_fun (*this, &Auditioner::output_changed));
 
-       the_region = 0;
-       atomic_set (&_active, 0);
+       the_region.reset ((AudioRegion*) 0);
+       g_atomic_int_set (&_active, 0);
 }
 
 Auditioner::~Auditioner ()
@@ -67,90 +74,101 @@ Auditioner::~Auditioner ()
 AudioPlaylist&
 Auditioner::prepare_playlist ()
 {
-       diskstream->playlist()->clear (false, false);
-       return *diskstream->playlist();
+       // FIXME auditioner is still audio-only
+       AudioPlaylist* const apl = dynamic_cast<AudioPlaylist*>(_diskstream->playlist());
+       assert(apl);
+
+       apl->clear (false);
+       return *apl;
 }
 
 void
 Auditioner::audition_current_playlist ()
 {
-       if (atomic_read (&_active)) {
+       if (g_atomic_int_get (&_active)) {
                /* don't go via session for this, because we are going
                   to remain active.
                */
                cancel_audition ();
        }
 
-       LockMonitor lm (lock, __LINE__, __FILE__);
-       diskstream->seek (0);
-       length = diskstream->playlist()->get_maximum_extent();
+       Glib::Mutex::Lock lm (lock);
+       _diskstream->seek (0);
+       length = _diskstream->playlist()->get_maximum_extent();
        current_frame = 0;
 
        /* force a panner reset now that we have all channels */
 
-       _panner->reset (n_outputs(), diskstream->n_channels());
+       _panner->reset (n_outputs(), _diskstream->n_channels());
 
-       atomic_set (&_active, 1);
+       g_atomic_int_set (&_active, 1);
 }
 
 void
-Auditioner::audition_region (AudioRegion& region)
+Auditioner::audition_region (boost::shared_ptr<Region> region)
 {
-       if (atomic_read (&_active)) {
+       if (g_atomic_int_get (&_active)) {
                /* don't go via session for this, because we are going
                   to remain active.
                */
                cancel_audition ();
        }
 
-       LockMonitor lm (lock, __LINE__, __FILE__);
+       if (boost::dynamic_pointer_cast<AudioRegion>(region) == 0) {
+               error << _("Auditioning of non-audio regions not yet supported") << endmsg;
+               return;
+       }
+
+       Glib::Mutex::Lock lm (lock);
+
+       /* copy it */
 
-       the_region = new AudioRegion (region);
+       boost::shared_ptr<AudioRegion> the_region (boost::dynamic_pointer_cast<AudioRegion> (RegionFactory::create (region)));
        the_region->set_position (0, this);
 
-       diskstream->playlist()->clear (true, false);
-       diskstream->playlist()->add_region (*the_region, 0, 1, false);
+       _diskstream->playlist()->clear (false);
+       _diskstream->playlist()->add_region (the_region, 0, 1, false);
 
-       while (diskstream->n_channels() < the_region->n_channels()) {
-               diskstream->add_channel ();
+       while (_diskstream->n_channels() < the_region->n_channels()) {
+               audio_diskstream()->add_channel ();
        }
 
-       while (diskstream->n_channels() > the_region->n_channels()) {
-               diskstream->remove_channel ();
+       while (_diskstream->n_channels() > the_region->n_channels()) {
+               audio_diskstream()->remove_channel ();
        }
 
        /* force a panner reset now that we have all channels */
 
-       _panner->reset (n_outputs(), diskstream->n_channels());
+       _panner->reset (n_outputs(), _diskstream->n_channels());
 
        length = the_region->length();
-       diskstream->seek (0);
+       _diskstream->seek (0);
        current_frame = 0;
-       atomic_set (&_active, 1);
+       g_atomic_int_set (&_active, 1);
 }
 
 int
-Auditioner::play_audition (jack_nframes_t nframes)
+Auditioner::play_audition (nframes_t nframes)
 {
        bool need_butler;
-       jack_nframes_t this_nframes;
+       nframes_t this_nframes;
        int ret;
 
-       if (atomic_read (&_active) == 0) {
+       if (g_atomic_int_get (&_active) == 0) {
                silence (nframes, 0);
                return 0;
        }
 
        this_nframes = min (nframes, length - current_frame);
 
-       diskstream->prepare ();
+       _diskstream->prepare ();
 
        if ((ret = roll (this_nframes, current_frame, current_frame + nframes, 0, false, false, false)) != 0) {
                silence (nframes, 0);
                return ret;
        }
 
-       need_butler = diskstream->commit (this_nframes);
+       need_butler = _diskstream->commit (this_nframes);
        current_frame += this_nframes;
 
        if (current_frame >= length) {