Make stem export export from right before any processors.
[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 using namespace ARDOUR;
34
35 PortExportChannel::PortExportChannel ()
36         : buffer_size(0)
37 {
38 }
39
40 void PortExportChannel::set_max_buffer_size(framecnt_t frames)
41 {
42         buffer_size = frames;
43         buffer.reset (new Sample[frames]);
44 }
45
46 bool
47 PortExportChannel::operator< (ExportChannel const & other) const
48 {
49         PortExportChannel const * pec;
50         if (!(pec = dynamic_cast<PortExportChannel const *> (&other))) {
51                 return this < &other;
52         }
53         return ports < pec->ports;
54 }
55
56 void
57 PortExportChannel::read (Sample const *& data, framecnt_t frames) const
58 {
59         assert(buffer);
60         assert(frames <= buffer_size);
61
62         if (ports.size() == 1) {
63                 data = (*ports.begin())->get_audio_buffer(frames).data();
64                 return;
65         }
66         
67         memset (buffer.get(), 0, frames * sizeof (Sample));
68
69         for (PortSet::const_iterator it = ports.begin(); it != ports.end(); ++it) {
70                 if (*it != 0) {
71                         Sample* port_buffer = (*it)->get_audio_buffer(frames).data();
72
73                         for (uint32_t i = 0; i < frames; ++i) {
74                                 buffer[i] += (float) port_buffer[i];
75                         }
76                 }
77         }
78
79         data = buffer.get();
80 }
81
82 void
83 PortExportChannel::get_state (XMLNode * node) const
84 {
85         XMLNode * port_node;
86         for (PortSet::const_iterator it = ports.begin(); it != ports.end(); ++it) {
87                 if ((port_node = node->add_child ("Port"))) {
88                         port_node->add_property ("name", (*it)->name());
89                 }
90         }
91 }
92
93 void
94 PortExportChannel::set_state (XMLNode * node, Session & session)
95 {
96         XMLProperty * prop;
97         XMLNodeList xml_ports = node->children ("Port");
98         for (XMLNodeList::iterator it = xml_ports.begin(); it != xml_ports.end(); ++it) {
99                 if ((prop = (*it)->property ("name"))) {
100                         std::string const & name = prop->value();
101                         AudioPort * port = dynamic_cast<AudioPort *> (session.engine().get_port_by_name (name));
102                         if (port) {
103                                 ports.insert (port);
104                         } else {
105                                 PBD::warning << string_compose (_("Could not get port for export channel \"%1\", dropping the channel"), name) << endmsg;
106                         }
107                 }
108         }
109 }
110
111 RegionExportChannelFactory::RegionExportChannelFactory (Session * session, AudioRegion const & region, AudioTrack & track, Type type) :
112   region (region),
113   track (track),
114   type (type),
115   frames_per_cycle (session->engine().frames_per_cycle ()),
116   buffers_up_to_date (false),
117   region_start (region.position()),
118   position (region_start)
119 {
120         switch (type) {
121           case Raw:
122                 n_channels = region.n_channels();
123                 break;
124           case Fades:
125                 n_channels = region.n_channels();
126
127                 mixdown_buffer.reset (new Sample [frames_per_cycle]);
128                 gain_buffer.reset (new Sample [frames_per_cycle]);
129                 memset (gain_buffer.get(), 1.0, sizeof (Sample) * frames_per_cycle);
130
131                 break;
132           case Processed:
133                 n_channels = track.n_outputs().n_audio();
134                 break;
135           default:
136                 throw ExportFailed ("Unhandled type in ExportChannelFactory constructor");
137         }
138
139         session->ProcessExport.connect_same_thread (export_connection, boost::bind (&RegionExportChannelFactory::new_cycle_started, this, _1));
140
141         buffers.ensure_buffers (DataType::AUDIO, n_channels, frames_per_cycle);
142         buffers.set_count (ChanCount (DataType::AUDIO, n_channels));
143 }
144
145 RegionExportChannelFactory::~RegionExportChannelFactory ()
146 {
147 }
148
149 ExportChannelPtr
150 RegionExportChannelFactory::create (uint32_t channel)
151 {
152         assert (channel < n_channels);
153         return ExportChannelPtr (new RegionExportChannel (*this, channel));
154 }
155
156 void
157 RegionExportChannelFactory::read (uint32_t channel, Sample const *& data, framecnt_t frames_to_read)
158 {
159         assert (channel < n_channels);
160         assert (frames_to_read <= frames_per_cycle);
161
162         if (!buffers_up_to_date) {
163                 update_buffers(frames_to_read);
164                 buffers_up_to_date = true;
165         }
166
167         data = buffers.get_audio (channel).data();
168 }
169
170 void
171 RegionExportChannelFactory::update_buffers (framecnt_t frames)
172 {
173         assert (frames <= frames_per_cycle);
174
175         switch (type) {
176           case Raw:
177                 for (size_t channel = 0; channel < n_channels; ++channel) {
178                         region.read (buffers.get_audio (channel).data(), position - region_start, frames, channel);
179                 }
180                 break;
181           case Fades:
182                 assert (mixdown_buffer && gain_buffer);
183                 for (size_t channel = 0; channel < n_channels; ++channel) {
184                         memset (mixdown_buffer.get(), 0, sizeof (Sample) * frames);
185                         region.read_at (buffers.get_audio (channel).data(), mixdown_buffer.get(), gain_buffer.get(), position, frames, channel);
186                 }
187                 break;
188           case Processed:
189                 track.export_stuff (buffers, position, frames);
190                 break;
191           default:
192                 throw ExportFailed ("Unhandled type in ExportChannelFactory::update_buffers");
193         }
194
195         position += frames;
196 }
197
198
199 RouteExportChannel::RouteExportChannel(boost::shared_ptr<CapturingProcessor> processor, size_t channel,
200                                        boost::shared_ptr<ProcessorRemover> remover)
201   : processor (processor)
202   , channel (channel)
203   , remover (remover)
204 {
205 }
206
207 RouteExportChannel::~RouteExportChannel()
208 {
209 }
210
211 void
212 RouteExportChannel::create_from_route(std::list<ExportChannelPtr> & result, Route & route)
213 {
214         boost::shared_ptr<CapturingProcessor> processor = route.add_export_point();
215         uint32_t channels = processor->input_streams().n_audio();
216
217         boost::shared_ptr<ProcessorRemover> remover (new ProcessorRemover (route, processor));
218         result.clear();
219         for (uint32_t i = 0; i < channels; ++i) {
220                 result.push_back (ExportChannelPtr (new RouteExportChannel (processor, i, remover)));
221         }
222 }
223
224 void
225 RouteExportChannel::set_max_buffer_size(framecnt_t frames)
226 {
227         if (processor) {
228                 processor->set_block_size (frames);
229         }
230 }
231
232 void
233 RouteExportChannel::read (Sample const *& data, framecnt_t frames) const
234 {
235         assert(processor);
236         AudioBuffer const & buffer = processor->get_capture_buffers().get_audio (channel);
237         assert (frames <= (framecnt_t) buffer.size());
238         data = buffer.data();
239 }
240
241 void
242 RouteExportChannel::get_state (XMLNode * node) const
243 {
244         // TODO
245 }
246
247 void
248 RouteExportChannel::set_state (XMLNode * node, Session & session)
249 {
250         // TODO
251 }
252
253 bool
254 RouteExportChannel::operator< (ExportChannel const & other) const
255 {
256         RouteExportChannel const * rec;
257         if ((rec = dynamic_cast<RouteExportChannel const *>(&other)) == 0) {
258                 return this < &other;
259         }
260
261         if (processor.get() == rec->processor.get()) {
262                 return channel < rec->channel;
263         }
264         return processor.get() < rec->processor.get();
265 }
266
267 RouteExportChannel::ProcessorRemover::~ProcessorRemover()
268 {
269         route.remove_processor (processor);
270 }