MIDI looping fixes from torbenh.
[ardour.git] / libs / ardour / session_butler.cc
index cf97e478030625ebd0c9a53490b9c8ed5c6d5546..02f8569a3d4d7fa0c71e8951f5c1f410bd8da2a6 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$
 */
 
 #include <algorithm>
 #include <fcntl.h>
 #include <poll.h>
 
+#include <glibmm/thread.h>
+
 #include <pbd/error.h>
-#include <pbd/lockmonitor.h>
 #include <pbd/pthread_utils.h>
+#include <pbd/stacktrace.h>
 
 #include <ardour/configuration.h>
 #include <ardour/audioengine.h>
 #include <ardour/session.h>
-#include <ardour/diskstream.h>
+#include <ardour/audio_diskstream.h>
+#include <ardour/midi_diskstream.h>
 #include <ardour/crossfade.h>
 #include <ardour/timestamps.h>
 
@@ -41,7 +43,7 @@
 
 using namespace std;
 using namespace ARDOUR;
-//using namespace sigc;
+using namespace PBD;
 
 static float _read_data_rate;
 static float _write_data_rate;
@@ -68,13 +70,17 @@ int
 Session::start_butler_thread ()
 {
        /* size is in Samples, not bytes */
-
-       dstream_buffer_size = (uint32_t) floor (Config->get_track_buffer_seconds() * (float) frame_rate());
-
-       Crossfade::set_buffer_size (dstream_buffer_size);
-
-       pthread_cond_init (&butler_paused, 0);
+       audio_dstream_buffer_size = (uint32_t) floor (Config->get_audio_track_buffer_seconds() * (float) frame_rate());
+       
+       /* size is in bytes
+        * XXX: Jack needs to tell us the MIDI buffer size
+        * (i.e. how many MIDI bytes we might see in a cycle)
+        */
+       midi_dstream_buffer_size = (uint32_t) floor (Config->get_midi_track_buffer_seconds() * (float)frame_rate());
+       MidiDiskstream::set_readahed_frames ((nframes_t) (Config->get_midi_readahead() * (float) frame_rate()));
        
+       Crossfade::set_buffer_size (audio_dstream_buffer_size);
+
        butler_should_run = false;
 
        if (pipe (butler_request_pipe)) {
@@ -105,16 +111,18 @@ Session::start_butler_thread ()
 void
 Session::terminate_butler_thread ()
 {
-       void* status;
-       char c = ButlerRequest::Quit;
-       ::write (butler_request_pipe[1], &c, 1);
-       pthread_join (butler_thread, &status);
+       if (butler_thread) {
+               void* status;
+               char c = ButlerRequest::Quit;
+               ::write (butler_request_pipe[1], &c, 1);
+               pthread_join (butler_thread, &status);
+       }
 }
 
 void
 Session::schedule_butler_transport_work ()
 {
-       atomic_inc (&butler_should_do_transport_work);
+       g_atomic_int_inc (&butler_should_do_transport_work);
        summon_butler ();
 }
 
@@ -130,55 +138,48 @@ Session::summon_butler ()
 {
        char c = ButlerRequest::Run;
        ::write (butler_request_pipe[1], &c, 1);
+       // PBD::stacktrace (cerr);
 }
 
 void
 Session::stop_butler ()
 {
-       LockMonitor lm (butler_request_lock, __LINE__, __FILE__);
+       Glib::Mutex::Lock lm (butler_request_lock);
        char c = ButlerRequest::Pause;
        ::write (butler_request_pipe[1], &c, 1);
-       pthread_cond_wait (&butler_paused, butler_request_lock.mutex());
+       butler_paused.wait(butler_request_lock);
 }
 
 void
 Session::wait_till_butler_finished ()
 {
-       LockMonitor lm (butler_request_lock, __LINE__, __FILE__);
+       Glib::Mutex::Lock lm (butler_request_lock);
        char c = ButlerRequest::Wake;
        ::write (butler_request_pipe[1], &c, 1);
-       pthread_cond_wait (&butler_paused, butler_request_lock.mutex());
+       butler_paused.wait(butler_request_lock);
 }
 
 void *
 Session::_butler_thread_work (void* arg)
 {
        PBD::ThreadCreated (pthread_self(), X_("Butler"));
-
        return ((Session *) arg)->butler_thread_work ();
        return 0;
 }
 
-#define transport_work_requested() atomic_read(&butler_should_do_transport_work)
-
 void *
 Session::butler_thread_work ()
 {
        uint32_t err = 0;
        int32_t bytes;
        bool compute_io;
-       struct timeval begin, end;
+       microseconds_t begin, end;
+
        struct pollfd pfd[1];
        bool disk_work_outstanding = false;
-       DiskStreamList::iterator i;
-
-       butler_mixdown_buffer = new Sample[DiskStream::disk_io_frames()];
-       butler_gain_buffer = new gain_t[DiskStream::disk_io_frames()];
-       // this buffer is used for temp conversion purposes in filesources
-       char * conv_buffer = conversion_buffer(ButlerContext);
+       DiskstreamList::iterator i;
 
        while (true) {
-
                pfd[0].fd = butler_request_pipe[0];
                pfd[0].events = POLLIN|POLLERR|POLLHUP;
                
@@ -195,25 +196,25 @@ Session::butler_thread_work ()
                }
 
                if (pfd[0].revents & ~POLLIN) {
-                       error << _("Error on butler thread request pipe") << endmsg;
+                       error << string_compose (_("Error on butler thread request pipe: fd=%1 err=%2"), pfd[0].fd, pfd[0].revents) << endmsg;
                        break;
                }
                
                if (pfd[0].revents & POLLIN) {
-                       
+
                        char req;
                        
                        /* empty the pipe of all current requests */
                        
                        while (1) {
                                size_t nread = ::read (butler_request_pipe[0], &req, sizeof (req));
-                               
                                if (nread == 1) {
                                        
                                        switch ((ButlerRequest::Type) req) {
                                                
                                        case ButlerRequest::Wake:
                                                break;
+
                                        case ButlerRequest::Run:
                                                butler_should_run = true;
                                                break;
@@ -241,10 +242,6 @@ Session::butler_thread_work ()
                                }
                        }
                }
-       
-               for (i = diskstreams.begin(); i != diskstreams.end(); ++i) {
-                       // cerr << "BEFORE " << (*i)->name() << ": pb = " << (*i)->playback_buffer_load() << " cp = " << (*i)->capture_buffer_load() << endl;
-               }
 
                if (transport_work_requested()) {
                        butler_transport_work ();
@@ -254,20 +251,32 @@ Session::butler_thread_work ()
                bytes = 0;
                compute_io = true;
 
-               gettimeofday (&begin, 0);
+               begin = get_microseconds();
 
-               RWLockMonitor dsm (diskstream_lock, false, __LINE__, __FILE__);
-               
-               for (i = diskstreams.begin(); !transport_work_requested() && butler_should_run && i != diskstreams.end(); ++i) {
+               boost::shared_ptr<DiskstreamList> dsl = diskstreams.reader ();
+
+//             for (i = dsl->begin(); i != dsl->end(); ++i) {
+//                     cerr << "BEFORE " << (*i)->name() << ": pb = " << (*i)->playback_buffer_load() << " cp = " << (*i)->capture_buffer_load() << endl;
+//             }
+
+               for (i = dsl->begin(); !transport_work_requested() && butler_should_run && i != dsl->end(); ++i) {
+
+                       boost::shared_ptr<Diskstream> ds = *i;
+
+                       /* don't read inactive tracks */
+
+                       IO* io = ds->io();
                        
-                       // cerr << "rah fondr " << (*i)->io()->name () << endl;
+                       if (io && !io->active()) {
+                               continue;
+                       }
 
-                       switch ((*i)->do_refill (butler_mixdown_buffer, butler_gain_buffer, conv_buffer)) {
+                       switch (ds->do_refill ()) {
                        case 0:
-                               bytes += (*i)->read_data_count();
+                               bytes += ds->read_data_count();
                                break;
                        case 1:
-                               bytes += (*i)->read_data_count();
+                               bytes += ds->read_data_count();
                                disk_work_outstanding = true;
                                break;
                                
@@ -279,7 +288,7 @@ Session::butler_thread_work ()
 
                }
 
-               if (i != diskstreams.end()) {
+               if (i != dsl->end()) {
                        /* we didn't get to all the streams */
                        disk_work_outstanding = true;
                }
@@ -289,23 +298,24 @@ Session::butler_thread_work ()
                }
 
                if (compute_io) {
-                       gettimeofday (&end, 0);
-                       
-                       double b = begin.tv_sec  + (begin.tv_usec/1000000.0);
-                       double e = end.tv_sec + (end.tv_usec / 1000000.0);
-                       
-                       _read_data_rate = bytes / (e - b);
+                       end = get_microseconds(); 
+                       if(end-begin > 0) {
+                       _read_data_rate = (float) bytes / (float) (end - begin);
+                       } else { _read_data_rate = 0; // infinity better
+                        }
                }
 
                bytes = 0;
                compute_io = true;
-               gettimeofday (&begin, 0);
-               
-               for (i = diskstreams.begin(); !transport_work_requested() && butler_should_run && i != diskstreams.end(); ++i) {
-                       
+               begin = get_microseconds();
+
+               for (i = dsl->begin(); !transport_work_requested() && butler_should_run && i != dsl->end(); ++i) {
                        // cerr << "write behind for " << (*i)->name () << endl;
+
+                       /* note that we still try to flush diskstreams attached to inactive routes
+                        */
                        
-                       switch ((*i)->do_flush (conv_buffer)) {
+                       switch ((*i)->do_flush (Session::ButlerContext)) {
                        case 0:
                                bytes += (*i)->write_data_count();
                                break;
@@ -331,7 +341,7 @@ Session::butler_thread_work ()
                        request_stop ();
                }
 
-               if (i != diskstreams.end()) {
+               if (i != dsl->end()) {
                        /* we didn't get to all the streams */
                        disk_work_outstanding = true;
                }
@@ -341,12 +351,13 @@ Session::butler_thread_work ()
                }
 
                if (compute_io) {
-                       gettimeofday (&end, 0);
-                       
-                       double b = begin.tv_sec  + (begin.tv_usec/1000000.0);
-                       double e = end.tv_sec + (end.tv_usec / 1000000.0);
-                       
-                       _write_data_rate = bytes / (e - b);
+                       // there are no apparent users for this calculation?
+                       end = get_microseconds();
+                       if(end-begin > 0) {
+                       _write_data_rate = (float) bytes / (float) (end - begin);
+                       } else {
+                       _write_data_rate = 0; // Well, infinity would be better
+                       }
                }
                
                if (!disk_work_outstanding) {
@@ -355,17 +366,17 @@ Session::butler_thread_work ()
 
 
                {
-                       LockMonitor lm (butler_request_lock, __LINE__, __FILE__);
+                       Glib::Mutex::Lock lm (butler_request_lock);
 
                        if (butler_should_run && (disk_work_outstanding || transport_work_requested())) {
-//                             for (DiskStreamList::iterator i = diskstreams.begin(); i != diskstreams.end(); ++i) {
+//                             for (DiskstreamList::iterator i = dsl->begin(); i != dsl->end(); ++i) {
 //                                     cerr << "AFTER " << (*i)->name() << ": pb = " << (*i)->playback_buffer_load() << " cp = " << (*i)->capture_buffer_load() << endl;
 //                             }
 
                                continue;
                        }
 
-                       pthread_cond_signal (&butler_paused);
+                       butler_paused.signal();
                }
        }
 
@@ -376,18 +387,17 @@ Session::butler_thread_work ()
 
 
 void
-Session::request_overwrite_buffer (DiskStream* stream)
+Session::request_overwrite_buffer (Diskstream* stream)
 {
        Event *ev = new Event (Event::Overwrite, Event::Add, Event::Immediate, 0, 0, 0.0);
        ev->set_ptr (stream);
        queue_event (ev);
 }
 
+/** Process thread. */
 void
-Session::overwrite_some_buffers (DiskStream* ds)
+Session::overwrite_some_buffers (Diskstream* ds)
 {
-       /* executed by the audio thread */
-
        if (actively_recording()) {
                return;
        }
@@ -398,8 +408,8 @@ Session::overwrite_some_buffers (DiskStream* ds)
 
        } else {
 
-               RWLockMonitor dm (diskstream_lock, false, __LINE__, __FILE__);
-               for (DiskStreamList::iterator i = diskstreams.begin(); i != diskstreams.end(); ++i) {
+               boost::shared_ptr<DiskstreamList> dsl = diskstreams.reader();
+               for (DiskstreamList::iterator i = dsl->begin(); i != dsl->end(); ++i) {
                        (*i)->set_pending_overwrite (true);
                }
        }
@@ -414,7 +424,7 @@ Session::read_data_rate () const
        /* disk i/o in excess of 10000MB/sec indicate the buffer cache
           in action. ignore it.
        */
-       return _read_data_rate > 10485760000.0f ? 0.0f : _read_data_rate;
+       return _read_data_rate > 10485.7600000f ? 0.0f : _read_data_rate;
 }
 
 float
@@ -423,42 +433,42 @@ Session::write_data_rate () const
        /* disk i/o in excess of 10000MB/sec indicate the buffer cache
           in action. ignore it.
        */
-       return _write_data_rate > 10485760000.0f ? 0.0f : _write_data_rate;
+       return _write_data_rate > 10485.7600000f ? 0.0f : _write_data_rate;
 }
 
 uint32_t
 Session::playback_load ()
 {
-       return (uint32_t) atomic_read (&_playback_load);
+       return (uint32_t) g_atomic_int_get (&_playback_load);
 }
 
 uint32_t
 Session::capture_load ()
 {
-       return (uint32_t) atomic_read (&_capture_load);
+       return (uint32_t) g_atomic_int_get (&_capture_load);
 }
 
 uint32_t
 Session::playback_load_min ()
 {
-       return (uint32_t) atomic_read (&_playback_load_min);
+       return (uint32_t) g_atomic_int_get (&_playback_load_min);
 }
 
 uint32_t
 Session::capture_load_min ()
 {
-       return (uint32_t) atomic_read (&_capture_load_min);
+       return (uint32_t) g_atomic_int_get (&_capture_load_min);
 }
 
 void
 Session::reset_capture_load_min ()
 {
-       atomic_set (&_capture_load_min, 100);
+       g_atomic_int_set (&_capture_load_min, 100);
 }
 
 
 void
 Session::reset_playback_load_min ()
 {
-       atomic_set (&_playback_load_min, 100);
+       g_atomic_int_set (&_playback_load_min, 100);
 }