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