Use PBD::copy_file in Session::create() to copy the template file.
[ardour.git] / libs / ardour / audioengine.cc
index 816e3bbf0ceeb147441f15f410471527334194af..cbdd1fda1ce8fc088c66161295cd3775a356dd66 100644 (file)
     along with this program; if not, write to the Free Software
     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 
-    $Id$
 */
 
 #include <unistd.h>
 #include <cerrno>
 #include <vector>
 #include <exception>
+#include <stdexcept>
 
 #include <glibmm/timer.h>
 #include <pbd/pthread_utils.h>
+#include <pbd/stacktrace.h>
 #include <pbd/unknown_type.h>
 
 #include <ardour/audioengine.h>
@@ -229,9 +230,9 @@ AudioEngine::jack_sync_callback (jack_transport_state_t state, jack_position_t*
 {
        if (_jack && session) {
                return session->jack_sync_callback (state, pos);
-       } else {
-               return true;
        }
+
+       return true;
 }
 
 int
@@ -254,6 +255,11 @@ AudioEngine::_graph_order_callback (void *arg)
        return 0;
 }
 
+/** Wrapped which is called by JACK as its process callback.  It is just
+ * here to get us back into C++ land by calling AudioEngine::process_callback()
+ * @param nframes Number of frames passed by JACK.
+ * @param arg User argument passed by JACK, which will be the AudioEngine*.
+ */
 int
 AudioEngine::_process_callback (nframes_t nframes, void *arg)
 {
@@ -266,11 +272,17 @@ AudioEngine::_freewheel_callback (int onoff, void *arg)
        static_cast<AudioEngine*>(arg)->_freewheeling = onoff;
 }
 
+/** Method called by JACK (via _process_callback) which says that there
+ * is work to be done.
+ * @param nframes Number of frames to process.
+ */
 int
 AudioEngine::process_callback (nframes_t nframes)
 {
        // CycleTimer ct ("AudioEngine::process");
        Glib::Mutex::Lock tm (_process_lock, Glib::TRY_LOCK);
+
+       /// The number of frames that will have been processed when we've finished
        nframes_t next_processed_frames;
        
        /* handle wrap around of total frames counter */
@@ -280,13 +292,15 @@ AudioEngine::process_callback (nframes_t nframes)
        } else {
                next_processed_frames = _processed_frames + nframes;
        }
-       
+
        if (!tm.locked() || session == 0) {
+               /* return having done nothing */
                _processed_frames = next_processed_frames;
                return 0;
        }
 
        if (session_remove_pending) {
+               /* perform the actual session removal */
                session = 0;
                session_remove_pending = false;
                session_removed.signal();
@@ -295,7 +309,9 @@ AudioEngine::process_callback (nframes_t nframes)
        }
 
        if (_freewheeling) {
+               /* emit the Freewheel signal and stop freewheeling in the event of trouble */
                if (Freewheel (nframes)) {
+                       cerr << "Freewheeling returned non-zero!\n";
                        _freewheeling = false;
                        jack_set_freewheel (_jack, false);
                }
@@ -305,23 +321,23 @@ AudioEngine::process_callback (nframes_t nframes)
        boost::shared_ptr<Ports> p = ports.reader();
 
        // Prepare ports (ie read data if necessary)
-       for (Ports::iterator i = p->begin(); i != p->end(); ++i)
-               (*i)->cycle_start(nframes);
+       for (Ports::iterator i = p->begin(); i != p->end(); ++i) {
+               (*i)->cycle_start (nframes);
+       }
        
-       session->process (nframes);
+       if (session) {
+               session->process (nframes);
+       }
 
        if (!_running) {
-               /* we were zombified, maybe because a ladspa plugin took
-                  too long, or jackd exited, or something like that.
-               */
-               
                _processed_frames = next_processed_frames;
                return 0;
        }
        
        // Finalize ports (ie write data if necessary)
-       for (Ports::iterator i = p->begin(); i != p->end(); ++i)
-               (*i)->cycle_end();
+       for (Ports::iterator i = p->begin(); i != p->end(); ++i) {
+               (*i)->cycle_end ();
+       }
 
        if (last_monitor_check + monitor_check_interval < next_processed_frames) {
 
@@ -445,14 +461,23 @@ AudioEngine::set_session (Session *s)
                   can before we really start running.
                */
                
-               session->process (blocksize);
-               session->process (blocksize);
-               session->process (blocksize);
-               session->process (blocksize);
-               session->process (blocksize);
-               session->process (blocksize);
-               session->process (blocksize);
-               session->process (blocksize);
+               boost::shared_ptr<Ports> p = ports.reader();
+
+               for (Ports::iterator i = p->begin(); i != p->end(); ++i)
+                       (*i)->cycle_start (blocksize);
+
+               s->process (blocksize);
+               s->process (blocksize);
+               s->process (blocksize);
+               s->process (blocksize);
+               s->process (blocksize);
+               s->process (blocksize);
+               s->process (blocksize);
+               s->process (blocksize);
+
+               for (Ports::iterator i = p->begin(); i != p->end(); ++i)
+                       (*i)->cycle_end ();
+
        }
 }
 
@@ -762,6 +787,7 @@ AudioEngine::halted (void *arg)
        ae->_running = false;
        ae->_buffer_size = 0;
        ae->_frame_rate = 0;
+       ae->_jack = 0;
 
        ae->Halted(); /* EMIT SIGNAL */
 }
@@ -1135,6 +1161,7 @@ AudioEngine::reconnect_to_jack ()
 
 
        if (session) {
+               session->reset_jack_connection (_jack);
                nframes_t blocksize = jack_get_buffer_size (_jack);
                session->set_block_size (blocksize);
                session->set_frame_rate (jack_get_sample_rate (_jack));
@@ -1187,8 +1214,13 @@ int
 AudioEngine::request_buffer_size (nframes_t nframes)
 {
        if (_jack) {
-               int ret = jack_set_buffer_size (_jack, nframes);
-               return ret;
+
+               if (nframes == jack_get_buffer_size (_jack)) {
+                       return 0;
+               }
+
+               return jack_set_buffer_size (_jack, nframes);
+
        } else {
                return -1;
        }
@@ -1239,3 +1271,12 @@ AudioEngine::make_port_name_non_relative (string portname)
        return str;
 }
 
+bool
+AudioEngine::is_realtime () const
+{
+       if (_jack) {
+               return jack_is_realtime (_jack);
+       } else {
+               return false;
+       }
+}