409c87d93c3d4e12e9f3c5d57f3e72bf385e835b
[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 (user_count == 0) {
50                 /* nothing to do - nobody is feeding us anything */
51                 return;
52         }
53
54         /* XXX this should be merge() */
55
56         bufs.merge_from (buffers, nframes);
57 }
58
59 bool
60 InternalReturn::configure_io (ChanCount in, ChanCount out)
61 {
62         IOProcessor::configure_io (in, out);
63         allocate_buffers (_session.get_block_size());
64         return true;
65 }
66
67 void 
68 InternalReturn::set_block_size (nframes_t nframes)
69 {
70         allocate_buffers (nframes);
71 }
72
73 void
74 InternalReturn::allocate_buffers (nframes_t nframes)
75 {
76         buffers.ensure_buffers (DataType::AUDIO, _configured_input.n_audio(), nframes);
77         buffers.ensure_buffers (DataType::MIDI, _configured_input.n_midi(), nframes);
78 }
79
80 BufferSet*
81 InternalReturn::get_buffers ()
82 {
83         Glib::Mutex::Lock lm (_session.engine().process_lock());
84         user_count++;
85         return &buffers;
86 }
87
88 void
89 InternalReturn::release_buffers ()
90 {
91         Glib::Mutex::Lock lm (_session.engine().process_lock());
92         if (user_count) {
93                 user_count--;
94         }
95 }
96
97 void
98 InternalReturn::cycle_start (nframes_t nframes)
99 {
100         /* called from process cycle - no lock necessary */
101         if (user_count) {
102                 /* don't bother with this if nobody is going to feed us anything */
103                 buffers.silence (nframes, 0);
104         }
105 }
106
107 XMLNode&
108 InternalReturn::state (bool full)
109 {
110         XMLNode& node (Return::state (full));
111         /* override type */
112         node.add_property("type", "intreturn");
113         return node;
114 }
115
116 XMLNode&
117 InternalReturn::get_state()
118 {
119         return state (true);
120 }
121
122 int
123 InternalReturn::set_state (const XMLNode& node)
124 {
125         return Return::set_state (node);
126 }
127
128 bool 
129 InternalReturn::can_support_io_configuration (const ChanCount& in, ChanCount& out) const
130 {
131         out = in;
132         return true;
133 }
134
135
136