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