* Add SysEx Support to MidiModel / SMF
[ardour.git] / libs / ardour / internal_send.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
20 #include <algorithm>
21
22 #include <pbd/xml++.h>
23
24 #include <ardour/internal_send.h>
25 #include <ardour/session.h>
26 #include <ardour/port.h>
27 #include <ardour/audio_port.h>
28 #include <ardour/buffer_set.h>
29 #include <ardour/meter.h>
30 #include <ardour/panner.h>
31 #include <ardour/io.h>
32
33 #include "i18n.h"
34
35 using namespace ARDOUR;
36 using namespace PBD;
37
38 InternalSend::InternalSend (Session& s, Placement p, boost::shared_ptr<IO> dst)
39         : IOProcessor (s, string_compose (_(">%1"), dst->name()), p, 
40                        -1, -1, -1, -1,
41                        DataType::AUDIO, false)
42         , destination (dst)
43 {
44         _metering = false;
45
46         destination->input_changed.connect (mem_fun (*this, &InternalSend::destination_io_config_changed));
47
48         destination_io_config_changed (ConfigurationChanged, this);
49
50         ProcessorCreated (this); /* EMIT SIGNAL */
51 }
52
53 InternalSend::~InternalSend ()
54 {
55         GoingAway ();
56 }
57
58 void
59 InternalSend::destination_io_config_changed (IOChange c, void* src)
60 {
61         if (!(c & ConfigurationChanged)) {
62                 return;
63         }
64
65         _io->disconnect_outputs (this);
66
67         _io->ensure_io (ChanCount::ZERO, destination->n_inputs(), false, this);
68
69         PortSet::const_iterator us (_io->outputs().begin());
70         PortSet::const_iterator them (destination->inputs().begin ());
71
72         for (; us != _io->outputs().end() && them != destination->inputs().end(); ++us, ++them) {
73                 (const_cast<Port*>(&(*us)))->connect (const_cast<Port*>(&(*them)));
74         }
75 }
76
77 XMLNode&
78 InternalSend::get_state(void)
79 {
80         fatal << X_("InternalSend::get_state() called - should never happen") << endmsg;
81         /*NOTREACHED*/
82         return *(new XMLNode ("foo"));
83 }
84
85 int
86 InternalSend::set_state(const XMLNode& node)
87 {
88         fatal << X_("InternalSend::set_state() called - should never happen") << endmsg;
89         /*NOTREACHED*/
90         return 0;
91 }
92
93 void
94 InternalSend::run_in_place (BufferSet& bufs, nframes_t start_frame, nframes_t end_frame, nframes_t nframes, nframes_t offset)
95 {
96         if (active()) {
97
98                 // we have to copy the input, because IO::deliver_output may alter the buffers
99                 // in-place, which a send must never do. otherwise its gain settings will
100                 // affect the signal seen later in the parent Route.
101
102                 // BufferSet& sendbufs = _session.get_mix_buffers(bufs.count());
103
104                 // sendbufs.read_from (bufs, nframes);
105                 // assert(sendbufs.count() == bufs.count());
106
107                 _io->deliver_output (bufs, start_frame, end_frame, nframes, offset);
108
109                 if (_metering) {
110                         if (_io->effective_gain() == 0) {
111                                 _io->peak_meter().reset();
112                         } else {
113                                 _io->peak_meter().run_in_place(_io->output_buffers(), start_frame, end_frame, nframes, offset);
114                         }
115                 }
116
117         } else {
118                 _io->silence (nframes, offset);
119                 if (_metering) {
120                         _io->peak_meter().reset();
121                 }
122         }
123 }
124
125 void
126 InternalSend::set_metering (bool yn)
127 {
128         _metering = yn;
129
130         if (!_metering) {
131                 /* XXX possible thread hazard here */
132                 _io->peak_meter().reset();
133         }
134 }
135
136 bool
137 InternalSend::can_support_io_configuration (const ChanCount& in, ChanCount& out_is_ignored) const
138 {
139         /* number of outputs is fixed (though mutable by changing the I/O configuration
140            of the destination)
141         */
142
143         cerr << "IS: testing I/O config in=" << in.n_audio() << " out=" << out_is_ignored.n_audio() << endl;
144
145         if (in == _io->n_outputs()) {
146                 return 1;
147         }
148
149         return -1;
150 }
151
152 bool
153 InternalSend::configure_io (ChanCount in, ChanCount out)
154 {
155         cerr << "Configure IS for in " << in.n_audio() << " out = " << out.n_audio() << endl;
156
157         /* we're transparent no matter what.  fight the power. */
158
159         if (out != in) {
160                 return false;
161         }
162
163         _io->set_output_maximum (in);
164         _io->set_output_minimum (in);
165         _io->set_input_maximum (ChanCount::ZERO);
166         _io->set_input_minimum (ChanCount::ZERO);
167
168         out = _io->n_outputs();
169
170         Processor::configure_io(in, out);
171
172         _io->reset_panner();
173
174         return true;
175 }
176
177 ChanCount
178 InternalSend::output_streams() const
179 {
180         // this method reflects the idea that from the perspective of the Route's ProcessorList, 
181         // a send is just a passthrough. that doesn't match what the Send actually does with its 
182         // data, but since what it does is invisible to the Route, it appears to be a passthrough.
183         
184         return _io->n_outputs ();
185 }
186
187 ChanCount
188 InternalSend::input_streams() const
189 {
190         return _configured_input;
191 }
192
193
194 void
195 InternalSend::expect_inputs (const ChanCount& expected)
196 {
197         if (expected != expected_inputs) {
198                 expected_inputs = expected;
199                 _io->reset_panner ();
200         }
201 }
202
203 void
204 InternalSend::activate ()
205 {
206         Processor::activate ();
207 }
208
209 void
210 InternalSend::deactivate ()
211 {
212         Processor::deactivate ();
213 }