fix clicking when processors become active/inactive; reduce crazy 2.5sec delay for...
[ardour.git] / libs / ardour / internal_return.cc
1 /*
2     Copyright (C) 2009 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 #include <glibmm/thread.h>
20
21 #include "pbd/failed_constructor.h"
22
23 #include "ardour/internal_return.h"
24 #include "ardour/mute_master.h"
25 #include "ardour/session.h"
26
27 using namespace std;
28 using namespace ARDOUR;
29
30 sigc::signal<void,nframes_t> InternalReturn::CycleStart;
31
32 InternalReturn::InternalReturn (Session& s)
33         : Return (s, true)
34         , user_count (0)
35 {
36         CycleStart.connect (mem_fun (*this, &InternalReturn::cycle_start));
37 }
38
39 InternalReturn::InternalReturn (Session& s, const XMLNode& node)
40         : Return (s, node, true)
41         , user_count (0)
42 {
43         CycleStart.connect (mem_fun (*this, &InternalReturn::cycle_start));
44 }
45
46 void
47 InternalReturn::run (BufferSet& bufs, sframes_t start_frame, sframes_t end_frame, nframes_t nframes)
48 {
49         if (!_active && !_pending_active) {
50                 return;
51         }
52
53         /* no lock here, just atomic fetch */
54
55         if (g_atomic_int_get(&user_count) == 0) {
56                 /* nothing to do - nobody is feeding us anything */
57                 return;
58         }
59
60         bufs.merge_from (buffers, nframes);
61         _active = _pending_active;
62 }
63
64 bool
65 InternalReturn::configure_io (ChanCount in, ChanCount out)
66 {
67         IOProcessor::configure_io (in, out);
68         allocate_buffers (_session.engine().frames_per_cycle());
69         return true;
70 }
71
72 void 
73 InternalReturn::set_block_size (nframes_t nframes)
74 {
75         allocate_buffers (nframes);
76 }
77
78 void
79 InternalReturn::allocate_buffers (nframes_t nframes)
80 {
81         buffers.ensure_buffers (_configured_input, nframes);
82         buffers.set_count (_configured_input);
83 }
84
85 BufferSet*
86 InternalReturn::get_buffers ()
87 {
88         Glib::Mutex::Lock lm (_session.engine().process_lock());
89         /* use of g_atomic here is just for code consistency - its protected by the lock
90            for writing.
91         */
92         g_atomic_int_inc (&user_count);
93         return &buffers;
94 }
95
96 void
97 InternalReturn::release_buffers ()
98 {
99         Glib::Mutex::Lock lm (_session.engine().process_lock());
100         if (user_count) {
101                 /* use of g_atomic here is just for code consistency - its protected by the lock
102                    for writing.
103                 */
104                 (void) g_atomic_int_dec_and_test (&user_count);
105         }
106 }
107
108 void
109 InternalReturn::cycle_start (nframes_t nframes)
110 {
111         /* called from process cycle - no lock necessary */
112         if (user_count) {
113                 /* don't bother with this if nobody is going to feed us anything */
114                 buffers.silence (nframes, 0);
115         }
116 }
117
118 XMLNode&
119 InternalReturn::state (bool full)
120 {
121         XMLNode& node (Return::state (full));
122         /* override type */
123         node.add_property("type", "intreturn");
124         return node;
125 }
126
127 XMLNode&
128 InternalReturn::get_state()
129 {
130         return state (true);
131 }
132
133 int
134 InternalReturn::set_state (const XMLNode& node)
135 {
136         return Return::set_state (node);
137 }
138
139 bool 
140 InternalReturn::can_support_io_configuration (const ChanCount& in, ChanCount& out) const
141 {
142         out = in;
143         return true;
144 }
145
146 bool
147 InternalReturn::visible () const
148 {
149         return false; 
150 }