Merge branch 'master' into cairocanvas
[ardour.git] / libs / ardour / session_butler.cc
1 /*
2     Copyright (C) 1999-2002 Paul Davis
3
4     This program is free software; you can redistribute it and/or modify
5     it under the terms of the GNU General Public License as published by
6     the Free Software Foundation; either version 2 of the License, or
7     (at your option) any later version.
8
9     This program is distributed in the hope that it will be useful,
10     but WITHOUT ANY WARRANTY; without even the implied warranty of
11     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12     GNU General Public License for more details.
13
14     You should have received a copy of the GNU General Public License
15     along with this program; if not, write to the Free Software
16     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17
18 */
19
20 #include "pbd/error.h"
21 #include "pbd/pthread_utils.h"
22 #include "pbd/stacktrace.h"
23
24 #include "ardour/butler.h"
25 #include "ardour/route.h"
26 #include "ardour/session.h"
27 #include "ardour/session_event.h"
28 #include "ardour/track.h"
29 #include "ardour/types.h"
30
31 #include "i18n.h"
32
33 using namespace std;
34 using namespace ARDOUR;
35 using namespace PBD;
36
37 /* XXX put this in the right place */
38
39 static inline uint32_t next_power_of_two (uint32_t n)
40 {
41         --n;
42         n |= n >> 16;
43         n |= n >> 8;
44         n |= n >> 4;
45         n |= n >> 2;
46         n |= n >> 1;
47         ++n;
48         return n;
49 }
50
51 /*---------------------------------------------------------------------------
52  BUTLER THREAD
53  ---------------------------------------------------------------------------*/
54
55 void
56 Session::adjust_playback_buffering ()
57 {
58         request_stop (false, false);
59         SessionEvent *ev = new SessionEvent (SessionEvent::AdjustPlaybackBuffering, SessionEvent::Add, SessionEvent::Immediate, 0, 0, 0.0);
60         queue_event (ev);
61 }
62
63 void
64 Session::adjust_capture_buffering ()
65 {
66         request_stop (false, false);
67         SessionEvent *ev = new SessionEvent (SessionEvent::AdjustCaptureBuffering, SessionEvent::Add, SessionEvent::Immediate, 0, 0, 0.0);
68         queue_event (ev);
69 }
70
71 void
72 Session::schedule_playback_buffering_adjustment ()
73 {
74         add_post_transport_work (PostTransportAdjustPlaybackBuffering);
75         _butler->schedule_transport_work ();
76 }
77
78 void
79 Session::schedule_capture_buffering_adjustment ()
80 {
81         add_post_transport_work (PostTransportAdjustCaptureBuffering);
82         _butler->schedule_transport_work ();
83 }
84
85 void
86 Session::schedule_curve_reallocation ()
87 {
88         add_post_transport_work (PostTransportCurveRealloc);
89         _butler->schedule_transport_work ();
90 }
91
92 void
93 Session::request_overwrite_buffer (Track* t)
94 {
95         SessionEvent *ev = new SessionEvent (SessionEvent::Overwrite, SessionEvent::Add, SessionEvent::Immediate, 0, 0, 0.0);
96         ev->set_ptr (t);
97         queue_event (ev);
98 }
99
100 /** Process thread. */
101 void
102 Session::overwrite_some_buffers (Track* t)
103 {
104         if (actively_recording()) {
105                 return;
106         }
107
108         if (t) {
109
110                 t->set_pending_overwrite (true);
111
112         } else {
113
114                 boost::shared_ptr<RouteList> rl = routes.reader();
115                 for (RouteList::iterator i = rl->begin(); i != rl->end(); ++i) {
116                         boost::shared_ptr<Track> tr = boost::dynamic_pointer_cast<Track> (*i);
117                         if (tr) {
118                                 tr->set_pending_overwrite (true);
119                         }
120                 }
121         }
122
123         add_post_transport_work (PostTransportOverWrite);
124         _butler->schedule_transport_work ();
125 }
126
127 uint32_t
128 Session::playback_load ()
129 {
130         return (uint32_t) g_atomic_int_get (&_playback_load);
131 }
132
133 uint32_t
134 Session::capture_load ()
135 {
136         return (uint32_t) g_atomic_int_get (&_capture_load);
137 }