provide support for playhead-to-next/previous-region-boundary actions, and bindings...
[ardour.git] / libs / ardour / session_export.cc
index f6ea0c9f3ff867a9e9e5db2f320514f873c501db..87bf5a5b034b6c1e049faaee7cf75d777caac1a1 100644 (file)
@@ -15,7 +15,6 @@
     along with this program; if not, write to the Free Software
     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 
-    $Id$
 */
 
 /* see gdither.cc for why we have to do this */
@@ -54,7 +53,7 @@
 
 using namespace std;
 using namespace ARDOUR;
-//using namespace sigc;
+using namespace PBD;
 
 static int
 convert_spec_to_info (AudioExportSpecification& spec, SF_INFO& sfinfo)
@@ -153,7 +152,7 @@ AudioExportSpecification::clear ()
 }
 
 int
-AudioExportSpecification::prepare (jack_nframes_t blocksize, jack_nframes_t frate)
+AudioExportSpecification::prepare (nframes_t blocksize, nframes_t frate)
 {
        char errbuf[256];
        GDitherSize dither_size;
@@ -216,7 +215,7 @@ AudioExportSpecification::prepare (jack_nframes_t blocksize, jack_nframes_t frat
                }
                
                src_data.src_ratio = sample_rate / (double) frame_rate;
-               out_samples_max = (jack_nframes_t) ceil (blocksize * src_data.src_ratio * channels);
+               out_samples_max = (nframes_t) ceil (blocksize * src_data.src_ratio * channels);
                dataF2 = new float[out_samples_max];
 
                max_leftover_frames = 4 * blocksize;
@@ -258,7 +257,7 @@ AudioExportSpecification::prepare (jack_nframes_t blocksize, jack_nframes_t frat
 }
 
 int
-AudioExportSpecification::process (jack_nframes_t nframes)
+AudioExportSpecification::process (nframes_t nframes)
 {
        float* float_buffer = 0;
        uint32_t chn;
@@ -266,9 +265,9 @@ AudioExportSpecification::process (jack_nframes_t nframes)
        uint32_t i;
        sf_count_t written;
        char errbuf[256];
-       jack_nframes_t to_write = 0;
+       nframes_t to_write = 0;
        int cnt = 0;
-
+       
        do {
 
                /* now do sample rate conversion */
@@ -413,7 +412,7 @@ AudioExportSpecification::process (jack_nframes_t nframes)
                        break;
                }
        
-               if ((jack_nframes_t) written != to_write) {
+               if ((nframes_t) written != to_write) {
                        sf_error_str (out, errbuf, sizeof (errbuf) - 1);
                        error << string_compose(_("Export: could not write data to output file (%1)"), errbuf) << endmsg;
                        return -1;
@@ -428,8 +427,6 @@ AudioExportSpecification::process (jack_nframes_t nframes)
 int
 Session::start_audio_export (AudioExportSpecification& spec)
 {
-       int ret;
-
        if (spec.prepare (current_block_size, frame_rate())) {
                return -1;
        }
@@ -437,40 +434,21 @@ Session::start_audio_export (AudioExportSpecification& spec)
        spec.pos = spec.start_frame;
        spec.end_frame = spec.end_frame;
        spec.total_frames = spec.end_frame - spec.start_frame;
+       spec.running = true; 
+       spec.do_freewheel = false; /* force a call to ::prepare_to_export() before proceeding to normal operation */
 
        spec.freewheel_connection = _engine.Freewheel.connect (sigc::bind (mem_fun (*this, &Session::process_export), &spec));
 
-       if ((ret = _engine.freewheel (true)) == 0) {
-               spec.running = true; 
-               spec.do_freewheel = false;
-       }
-
-       return ret;
+       return _engine.freewheel (true);
 }
 
 int
 Session::stop_audio_export (AudioExportSpecification& spec)
 {
-       /* can't use stop_transport() here because we need
-          an immediate halt and don't require all the declick
-          stuff that stop_transport() implements.
-       */
-
-       realtime_stop (true);
-       schedule_butler_transport_work ();
-
-       /* restart slaving */
-
-       if (post_export_slave != None) {
-               set_slave_source (post_export_slave, post_export_position);
-       } else {
-               locate (post_export_position, false, false, false);
-       }
-
-       spec.clear ();
-       _exporting = false;
+       /* don't stop freewheeling but do stop paying attention to it for now */
 
-       spec.running = false;
+       spec.freewheel_connection.disconnect ();
+       spec.clear (); /* resets running/stop etc */
 
        return 0;
 }
@@ -485,8 +463,9 @@ Session::prepare_to_export (AudioExportSpecification& spec)
        /* take everyone out of awrite to avoid disasters */
 
        {
-               Glib::RWLock::ReaderLock lm (route_lock);
-               for (RouteList::iterator i = routes.begin(); i != routes.end(); ++i) {
+               boost::shared_ptr<RouteList> r = routes.reader ();
+
+               for (RouteList::iterator i = r->begin(); i != r->end(); ++i) {
                        (*i)->protect_automation ();
                }
        }
@@ -494,8 +473,9 @@ Session::prepare_to_export (AudioExportSpecification& spec)
        /* get everyone to the right position */
 
        {
-               Glib::RWLock::ReaderLock lm (diskstream_lock);
-               for (AudioDiskstreamList::iterator i = audio_diskstreams.begin(); i != audio_diskstreams.end(); ++i) {
+               boost::shared_ptr<DiskstreamList> dsl = diskstreams.reader();
+
+               for (DiskstreamList::iterator i = dsl->begin(); i != dsl->end(); ++i) {
                        if ((*i)-> seek (spec.start_frame, true)) {
                                error << string_compose (_("%1: cannot seek to %2 for export"),
                                                  (*i)->name(), spec.start_frame)
@@ -515,10 +495,10 @@ Session::prepare_to_export (AudioExportSpecification& spec)
        
        /* no slaving */
 
-       post_export_slave = _slave_type;
+       post_export_slave = Config->get_slave_source ();
        post_export_position = _transport_frame;
 
-       set_slave_source (None, 0);
+       Config->set_slave_source (None);
 
        /* get transport ready */
 
@@ -536,12 +516,12 @@ Session::prepare_to_export (AudioExportSpecification& spec)
 }
 
 int
-Session::process_export (jack_nframes_t nframes, AudioExportSpecification* spec)
+Session::process_export (nframes_t nframes, AudioExportSpecification* spec)
 {
        uint32_t chn;
        uint32_t x;
        int ret = -1;
-       jack_nframes_t this_nframes;
+       nframes_t this_nframes;
 
        /* This is not required to be RT-safe because we are running while freewheeling */
 
@@ -638,3 +618,25 @@ Session::process_export (jack_nframes_t nframes, AudioExportSpecification* spec)
        return ret;
 }
 
+void
+Session::finalize_audio_export ()
+{
+       _engine.freewheel (false);
+       _exporting = false;
+
+       /* can't use stop_transport() here because we need
+          an immediate halt and don't require all the declick
+          stuff that stop_transport() implements.
+       */
+
+       realtime_stop (true);
+       schedule_butler_transport_work ();
+
+       /* restart slaving */
+
+       if (post_export_slave != None) {
+               Config->set_slave_source (post_export_slave);
+       } else {
+               locate (post_export_position, false, false, false);
+       }
+}