Initialize uninitialized variable
[ardour.git] / libs / ardour / export_channel.cc
1 /*
2  * Copyright (C) 2008-2011 Carl Hetherington <carl@carlh.net>
3  * Copyright (C) 2008-2011 Sakari Bergen <sakari.bergen@beatwaves.net>
4  * Copyright (C) 2009-2011 David Robillard <d@drobilla.net>
5  * Copyright (C) 2009-2017 Paul Davis <paul@linuxaudiosystems.com>
6  * Copyright (C) 2014-2017 Robin Gareus <robin@gareus.org>
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 2 of the License, or
11  * (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License along
19  * with this program; if not, write to the Free Software Foundation, Inc.,
20  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21  */
22
23 #include "ardour/audio_buffer.h"
24 #include "ardour/audio_port.h"
25 #include "ardour/audio_track.h"
26 #include "ardour/audioengine.h"
27 #include "ardour/audioregion.h"
28 #include "ardour/capturing_processor.h"
29 #include "ardour/export_channel.h"
30 #include "ardour/export_failed.h"
31 #include "ardour/session.h"
32
33 #include "pbd/error.h"
34
35 #include "pbd/i18n.h"
36
37 using namespace ARDOUR;
38
39 PortExportChannel::PortExportChannel ()
40         : _buffer_size (0)
41 {
42 }
43
44 PortExportChannel::~PortExportChannel ()
45 {
46         _delaylines.clear ();
47 }
48
49 void PortExportChannel::set_max_buffer_size(samplecnt_t samples)
50 {
51         _buffer_size = samples;
52         _buffer.reset (new Sample[samples]);
53
54         _delaylines.clear ();
55
56         for (PortSet::const_iterator it = ports.begin(); it != ports.end(); ++it) {
57                 boost::shared_ptr<AudioPort> p = it->lock ();
58                 if (!p) { continue; }
59                 samplecnt_t latency = p->private_latency_range (true).max;
60                 PBD::RingBuffer<Sample>* rb = new PBD::RingBuffer<Sample> (latency + 1 + _buffer_size);
61                 for (samplepos_t i = 0; i < latency; ++i) {
62                         Sample zero = 0;
63                         rb->write (&zero, 1);
64                 }
65                 _delaylines.push_back (boost::shared_ptr<PBD::RingBuffer<Sample> >(rb));
66         }
67 }
68
69 bool
70 PortExportChannel::operator< (ExportChannel const & other) const
71 {
72         PortExportChannel const * pec;
73         if (!(pec = dynamic_cast<PortExportChannel const *> (&other))) {
74                 return this < &other;
75         }
76         return ports < pec->ports;
77 }
78
79 void
80 PortExportChannel::read (Sample const *& data, samplecnt_t samples) const
81 {
82         assert(_buffer);
83         assert(samples <= _buffer_size);
84
85         if (ports.size() == 1 && _delaylines.size() ==1 && _delaylines.front()->bufsize () == _buffer_size + 1) {
86                 boost::shared_ptr<AudioPort> p = ports.begin()->lock ();
87                 AudioBuffer& ab (p->get_audio_buffer(samples)); // unsets AudioBuffer::_written
88                 data = ab.data();
89                 ab.set_written (true);
90                 return;
91         }
92
93         memset (_buffer.get(), 0, samples * sizeof (Sample));
94
95         std::list <boost::shared_ptr<PBD::RingBuffer<Sample> > >::const_iterator di = _delaylines.begin ();
96         for (PortSet::const_iterator it = ports.begin(); it != ports.end(); ++it) {
97                 boost::shared_ptr<AudioPort> p = it->lock ();
98                 if (!p) {
99                         continue;
100                 }
101                 AudioBuffer& ab (p->get_audio_buffer(samples)); // unsets AudioBuffer::_written
102                 Sample* port_buffer = ab.data();
103                 ab.set_written (true);
104                 (*di)->write (port_buffer, samples);
105                 // TODO optimze, get_read_vector()
106                 for (uint32_t i = 0; i < samples; ++i) {
107                         Sample spl;
108                         (*di)->read (&spl, 1);
109                         _buffer[i] += spl;
110                 }
111                 ++di;
112         }
113
114         data = _buffer.get();
115 }
116
117 void
118 PortExportChannel::get_state (XMLNode * node) const
119 {
120         XMLNode * port_node;
121         for (PortSet::const_iterator it = ports.begin(); it != ports.end(); ++it) {
122                 boost::shared_ptr<Port> p = it->lock ();
123                 if (p && (port_node = node->add_child ("Port"))) {
124                         port_node->set_property ("name", p->name());
125                 }
126         }
127 }
128
129 void
130 PortExportChannel::set_state (XMLNode * node, Session & session)
131 {
132         XMLNodeList xml_ports = node->children ("Port");
133         for (XMLNodeList::iterator it = xml_ports.begin(); it != xml_ports.end(); ++it) {
134                 std::string name;
135                 if ((*it)->get_property ("name", name)) {
136                         boost::shared_ptr<AudioPort> port = boost::dynamic_pointer_cast<AudioPort> (session.engine().get_port_by_name (name));
137                         if (port) {
138                                 ports.insert (port);
139                         } else {
140                                 PBD::warning << string_compose (_("Could not get port for export channel \"%1\", dropping the channel"), name) << endmsg;
141                         }
142                 }
143         }
144 }
145
146 RegionExportChannelFactory::RegionExportChannelFactory (Session * session, AudioRegion const & region, AudioTrack & track, Type type)
147         : region (region)
148         , track (track)
149         , type (type)
150         , samples_per_cycle (session->engine().samples_per_cycle ())
151         , buffers_up_to_date (false)
152         , region_start (region.position())
153         , position (region_start)
154 {
155         switch (type) {
156           case Raw:
157                 n_channels = region.n_channels();
158                 break;
159           case Fades:
160                 n_channels = region.n_channels();
161
162                 mixdown_buffer.reset (new Sample [samples_per_cycle]);
163                 gain_buffer.reset (new Sample [samples_per_cycle]);
164                 std::fill_n (gain_buffer.get(), samples_per_cycle, Sample (1.0));
165
166                 break;
167           case Processed:
168                 n_channels = track.n_outputs().n_audio();
169                 break;
170           default:
171                 throw ExportFailed ("Unhandled type in ExportChannelFactory constructor");
172         }
173
174         session->ProcessExport.connect_same_thread (export_connection, boost::bind (&RegionExportChannelFactory::new_cycle_started, this, _1));
175
176         buffers.ensure_buffers (DataType::AUDIO, n_channels, samples_per_cycle);
177         buffers.set_count (ChanCount (DataType::AUDIO, n_channels));
178 }
179
180 RegionExportChannelFactory::~RegionExportChannelFactory ()
181 {
182 }
183
184 ExportChannelPtr
185 RegionExportChannelFactory::create (uint32_t channel)
186 {
187         assert (channel < n_channels);
188         return ExportChannelPtr (new RegionExportChannel (*this, channel));
189 }
190
191 void
192 RegionExportChannelFactory::read (uint32_t channel, Sample const *& data, samplecnt_t samples_to_read)
193 {
194         assert (channel < n_channels);
195         assert (samples_to_read <= samples_per_cycle);
196
197         if (!buffers_up_to_date) {
198                 update_buffers(samples_to_read);
199                 buffers_up_to_date = true;
200         }
201
202         data = buffers.get_audio (channel).data();
203 }
204
205 void
206 RegionExportChannelFactory::update_buffers (samplecnt_t samples)
207 {
208         assert (samples <= samples_per_cycle);
209
210         switch (type) {
211           case Raw:
212                 for (size_t channel = 0; channel < n_channels; ++channel) {
213                         region.read (buffers.get_audio (channel).data(), position - region_start, samples, channel);
214                 }
215                 break;
216           case Fades:
217                 assert (mixdown_buffer && gain_buffer);
218                 for (size_t channel = 0; channel < n_channels; ++channel) {
219                         memset (mixdown_buffer.get(), 0, sizeof (Sample) * samples);
220                         buffers.get_audio (channel).silence(samples);
221                         region.read_at (buffers.get_audio (channel).data(), mixdown_buffer.get(), gain_buffer.get(), position, samples, channel);
222                 }
223                 break;
224         case Processed:
225                 track.export_stuff (buffers, position, samples, track.main_outs(), true, true, false);
226                 break;
227         default:
228                 throw ExportFailed ("Unhandled type in ExportChannelFactory::update_buffers");
229         }
230
231         position += samples;
232 }
233
234
235 RouteExportChannel::RouteExportChannel(boost::shared_ptr<CapturingProcessor> processor, size_t channel,
236                                        boost::shared_ptr<ProcessorRemover> remover)
237   : processor (processor)
238   , channel (channel)
239   , remover (remover)
240 {
241 }
242
243 RouteExportChannel::~RouteExportChannel()
244 {
245 }
246
247 void
248 RouteExportChannel::create_from_route(std::list<ExportChannelPtr> & result, boost::shared_ptr<Route> route)
249 {
250         boost::shared_ptr<CapturingProcessor> processor = route->add_export_point();
251         uint32_t channels = processor->input_streams().n_audio();
252
253         boost::shared_ptr<ProcessorRemover> remover (new ProcessorRemover (route, processor));
254         result.clear();
255         for (uint32_t i = 0; i < channels; ++i) {
256                 result.push_back (ExportChannelPtr (new RouteExportChannel (processor, i, remover)));
257         }
258 }
259
260 void
261 RouteExportChannel::set_max_buffer_size(samplecnt_t samples)
262 {
263         if (processor) {
264                 processor->set_block_size (samples);
265         }
266 }
267
268 void
269 RouteExportChannel::read (Sample const *& data, samplecnt_t samples) const
270 {
271         assert(processor);
272         AudioBuffer const & buffer = processor->get_capture_buffers().get_audio (channel);
273 #ifndef NDEBUG
274         (void) samples;
275 #else
276         assert (samples <= (samplecnt_t) buffer.capacity());
277 #endif
278         data = buffer.data();
279 }
280
281 void
282 RouteExportChannel::get_state (XMLNode *) const
283 {
284         // TODO
285 }
286
287 void
288 RouteExportChannel::set_state (XMLNode *, Session &)
289 {
290         // TODO
291 }
292
293 bool
294 RouteExportChannel::operator< (ExportChannel const & other) const
295 {
296         RouteExportChannel const * rec;
297         if ((rec = dynamic_cast<RouteExportChannel const *>(&other)) == 0) {
298                 return this < &other;
299         }
300
301         if (processor.get() == rec->processor.get()) {
302                 return channel < rec->channel;
303         }
304         return processor.get() < rec->processor.get();
305 }
306
307 RouteExportChannel::ProcessorRemover::~ProcessorRemover()
308 {
309         route->remove_processor (processor);
310 }