97b05c2f1eee17574670c972ab9498bc7308b578
[ardour.git] / libs / ardour / export_channel_configuration.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/export_channel_configuration.h>
22
23 #include <ardour/export_handler.h>
24 #include <ardour/export_filename.h>
25 #include <ardour/export_processor.h>
26 #include <ardour/export_timespan.h>
27
28 #include <ardour/audio_port.h>
29 #include <ardour/export_failed.h>
30 #include <ardour/midi_port.h>
31 #include <ardour/session.h>
32 #include <ardour/audioengine.h>
33
34 #include <pbd/convert.h>
35 #include <pbd/pthread_utils.h>
36
37 namespace ARDOUR
38 {
39
40 /* ExportChannel */
41
42 void
43 ExportChannel::read_ports (float * data, nframes_t frames) const
44 {
45         memset (data, 0, frames * sizeof (float));
46
47         for (iterator it = begin(); it != end(); ++it) {
48                 if (*it != 0) {
49                         Sample* port_buffer = (*it)->get_audio_buffer().data();
50                         
51                         for (uint32_t i = 0; i < frames; ++i) {
52                                 data[i] += (float) port_buffer[i];
53                         }
54                 }
55         }
56 }
57
58 /* ExportChannelConfiguration */
59
60 ExportChannelConfiguration::ExportChannelConfiguration (ExportStatus & status,  Session & session) :
61   session (session),
62   writer_thread (*this),
63   status (status),
64   files_written (false),
65   split (false)
66 {
67
68 }
69
70
71 XMLNode &
72 ExportChannelConfiguration::get_state ()
73 {
74         XMLNode * root = new XMLNode ("ExportChannelConfiguration");
75         XMLNode * channel;
76         XMLNode * port_node;
77         
78         root->add_property ("split", get_split() ? "true" : "false");
79         root->add_property ("channels", to_string (get_n_chans(), std::dec));
80         
81         uint32_t i = 1;
82         for (ExportChannelConfiguration::ChannelList::const_iterator c_it = channels.begin(); c_it != channels.end(); ++c_it) {
83                 channel = root->add_child ("Channel");
84                 if (!channel) { continue; }
85                 
86                 channel->add_property ("number", to_string (i, std::dec));
87                 
88                 for (ExportChannel::const_iterator p_it = (*c_it)->begin(); p_it != (*c_it)->end(); ++p_it) {
89                         if ((port_node = channel->add_child ("Port"))) {
90                                 port_node->add_property ("name", (*p_it)->name());
91                         }
92                 }
93                 
94                 ++i;
95         }
96         
97         return *root;
98 }
99
100 int
101 ExportChannelConfiguration::set_state (const XMLNode & root)
102 {
103         XMLProperty const * prop;
104         
105         if ((prop = root.property ("split"))) {
106                 set_split (!prop->value().compare ("true"));
107         }
108
109         XMLNodeList channels = root.children ("Channel");
110         for (XMLNodeList::iterator it = channels.begin(); it != channels.end(); ++it) {
111                 boost::shared_ptr<ExportChannel> channel (new ExportChannel ());
112         
113                 XMLNodeList ports = (*it)->children ("Port");
114                 for (XMLNodeList::iterator p_it = ports.begin(); p_it != ports.end(); ++p_it) {
115                         if ((prop = (*p_it)->property ("name"))) {
116                                 channel->add_port (dynamic_cast<AudioPort *> (session.engine().get_port_by_name (prop->value())));
117                         }
118                 }
119         
120                 register_channel (channel);
121         }
122
123         return 0;
124 }
125
126 bool
127 ExportChannelConfiguration::all_channels_have_ports ()
128 {
129         for (ChannelList::iterator it = channels.begin(); it != channels.end(); ++it) {
130                 if ((*it)->empty ()) { return false; }
131         }
132         
133         return true;
134 }
135
136 bool
137 ExportChannelConfiguration::write_files (boost::shared_ptr<ExportProcessor> new_processor)
138 {
139         if (files_written || writer_thread.running) {
140                 return false;
141         }
142         
143         files_written = true;
144
145         if (!timespan) {
146                 throw ExportFailed (_("Export failed due to a programming error"), _("No timespan registered to channel configuration when requesting files to be written"));
147         }
148         
149         /* Take a local copy of the processor to be used in the thread that is created below */
150         
151         processor.reset (new_processor->copy());
152         
153         /* Create new thread for post processing */
154         
155         pthread_create (&writer_thread.thread, 0, _write_files, &writer_thread);
156         writer_thread.running = true;
157         pthread_detach (writer_thread.thread);
158         
159         return true;
160 }
161
162 void
163 ExportChannelConfiguration::write_file ()
164 {
165         timespan->rewind ();
166         nframes_t progress = 0;
167         nframes_t timespan_length = timespan->get_length();
168
169         nframes_t frames = 2048; // TODO good block size ?
170         nframes_t frames_read = 0;
171         
172         float * channel_buffer = new float [frames];
173         float * file_buffer = new float [channels.size() * frames];
174         uint32_t channel_count = channels.size();
175         uint32_t channel;
176         
177         do {
178                 if (status.aborted()) { break; }
179         
180                 channel = 0;
181                 for (ChannelList::iterator it = channels.begin(); it != channels.end(); ++it) {
182                         
183                         /* Get channel data */
184                         
185                         frames_read = timespan->get_data (channel_buffer, frames, **it);
186                         
187                         /* Interleave into file buffer */
188                         
189                         for (uint32_t i = 0; i < frames_read; ++i) {
190                                 file_buffer[channel + (channel_count * i)] = channel_buffer[i];
191                         }
192                         
193                         ++channel;
194                 }
195                 
196                 progress += frames_read;
197                 status.progress = (float) progress / timespan_length;
198                 
199         } while (processor->process (file_buffer, frames_read) > 0);
200         
201         delete [] channel_buffer;
202         delete [] file_buffer;
203 }
204
205 void *
206 ExportChannelConfiguration::_write_files (void *arg)
207 {
208
209         PBD::ThreadCreated (pthread_self(), "Export post-processing");
210         
211         // cc can be trated like 'this'
212         WriterThread & cc (*((WriterThread *) arg));
213         
214         for (FileConfigList::iterator it = cc->file_configs.begin(); it != cc->file_configs.end(); ++it) {
215                 if (cc->status.aborted()) {
216                         break;
217                 }
218                 cc->processor->prepare (it->first, it->second, cc->channels.size(), cc->split, cc->timespan->get_start());
219                 cc->write_file (); // Writes tempfile
220                 cc->processor->prepare_post_processors ();
221                 cc->processor->write_files();
222         }
223         
224         cc.running = false;
225         cc->files_written = true;
226         cc->FilesWritten();
227         
228         return 0; // avoid compiler warnings
229 }
230
231 void
232 ExportChannelConfiguration::register_with_timespan (TimespanPtr new_timespan)
233 {
234         timespan = new_timespan;
235         
236         for (ChannelList::iterator it = channels.begin(); it != channels.end(); ++it) {
237                 timespan->register_channel (**it);
238         }
239 }
240
241 void
242 ExportChannelConfiguration::unregister_all ()
243 {
244         timespan.reset();
245         processor.reset();
246         file_configs.clear();
247         files_written = false;
248 }
249
250 } // namespace ARDOUR