b835cca55944fd3864f216500873ff0dd4ba2fd4
[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 /* ExportChannelConfiguration */
41
42 ExportChannelConfiguration::ExportChannelConfiguration (Session & session) :
43   session (session),
44   writer_thread (*this),
45   status (session.get_export_status ()),
46   files_written (false),
47   split (false)
48 {
49
50 }
51
52
53 XMLNode &
54 ExportChannelConfiguration::get_state ()
55 {
56         XMLNode * root = new XMLNode ("ExportChannelConfiguration");
57         XMLNode * channel;
58         
59         root->add_property ("split", get_split() ? "true" : "false");
60         root->add_property ("channels", to_string (get_n_chans(), std::dec));
61         
62         uint32_t i = 1;
63         for (ExportChannelConfiguration::ChannelList::const_iterator c_it = channels.begin(); c_it != channels.end(); ++c_it) {
64                 channel = root->add_child ("Channel");
65                 if (!channel) { continue; }
66                 
67                 channel->add_property ("number", to_string (i, std::dec));
68                 (*c_it)->get_state (channel);
69                 
70                 ++i;
71         }
72         
73         return *root;
74 }
75
76 int
77 ExportChannelConfiguration::set_state (const XMLNode & root)
78 {
79         XMLProperty const * prop;
80         
81         if ((prop = root.property ("split"))) {
82                 set_split (!prop->value().compare ("true"));
83         }
84
85         XMLNodeList channels = root.children ("Channel");
86         for (XMLNodeList::iterator it = channels.begin(); it != channels.end(); ++it) {
87                 ExportChannelPtr channel (new PortExportChannel ());
88                 channel->set_state (*it, session);
89                 register_channel (channel);
90         }
91
92         return 0;
93 }
94
95 bool
96 ExportChannelConfiguration::all_channels_have_ports () const
97 {
98         for (ChannelList::const_iterator it = channels.begin(); it != channels.end(); ++it) {
99                 if ((*it)->empty ()) { return false; }
100         }
101         
102         return true;
103 }
104
105 bool
106 ExportChannelConfiguration::write_files (boost::shared_ptr<ExportProcessor> new_processor)
107 {
108         if (files_written || writer_thread.running) {
109                 return false;
110         }
111         
112         files_written = true;
113
114         if (!timespan) {
115                 throw ExportFailed (X_("Programming error: No timespan registered to channel configuration when requesting files to be written"));
116         }
117         
118         /* Take a local copy of the processor to be used in the thread that is created below */
119         
120         processor.reset (new_processor->copy());
121         
122         /* Create new thread for post processing */
123         
124         pthread_create (&writer_thread.thread, 0, _write_files, &writer_thread);
125         writer_thread.running = true;
126         pthread_detach (writer_thread.thread);
127         
128         return true;
129 }
130
131 void
132 ExportChannelConfiguration::write_file ()
133 {
134         timespan->rewind ();
135         nframes_t progress = 0;
136         nframes_t timespan_length = timespan->get_length();
137
138         nframes_t frames = 2048; // TODO good block size ?
139         nframes_t frames_read = 0;
140         
141         float * channel_buffer = new float [frames];
142         float * file_buffer = new float [channels.size() * frames];
143         uint32_t channel_count = channels.size();
144         uint32_t channel;
145         
146         do {
147                 if (status->aborted()) { break; }
148         
149                 channel = 0;
150                 for (ChannelList::iterator it = channels.begin(); it != channels.end(); ++it) {
151                         
152                         /* Get channel data */
153                         
154                         frames_read = timespan->get_data (channel_buffer, frames, *it);
155                         
156                         /* Interleave into file buffer */
157                         
158                         for (uint32_t i = 0; i < frames_read; ++i) {
159                                 file_buffer[channel + (channel_count * i)] = channel_buffer[i];
160                         }
161                         
162                         ++channel;
163                 }
164                 
165                 progress += frames_read;
166                 status->progress = (float) progress / timespan_length;
167                 
168         } while (processor->process (file_buffer, frames_read) > 0);
169         
170         delete [] channel_buffer;
171         delete [] file_buffer;
172 }
173
174 void *
175 ExportChannelConfiguration::_write_files (void *arg)
176 {
177         notify_gui_about_thread_creation (pthread_self(), "Export post-processing");
178         
179         // cc can be trated like 'this'
180         WriterThread & cc (*((WriterThread *) arg));
181         
182         try {
183                 for (FileConfigList::iterator it = cc->file_configs.begin(); it != cc->file_configs.end(); ++it) {
184                         if (cc->status->aborted()) {
185                                 break;
186                         }
187                         cc->processor->prepare (it->first, it->second, cc->channels.size(), cc->split, cc->timespan->get_start());
188                         cc->write_file (); // Writes tempfile
189                         cc->processor->prepare_post_processors ();
190                         cc->processor->write_files();
191                 }
192         } catch (ExportFailed & e) {
193                 cc->status->abort (true);
194         }
195         
196         cc.running = false;
197         cc->files_written = true;
198         cc->FilesWritten();
199         
200         return 0; // avoid compiler warnings
201 }
202
203 void
204 ExportChannelConfiguration::register_with_timespan (TimespanPtr new_timespan)
205 {
206         timespan = new_timespan;
207         
208         for (ChannelList::iterator it = channels.begin(); it != channels.end(); ++it) {
209                 timespan->register_channel (*it);
210         }
211 }
212
213 void
214 ExportChannelConfiguration::unregister_all ()
215 {
216         timespan.reset();
217         processor.reset();
218         file_configs.clear();
219         files_written = false;
220 }
221
222 } // namespace ARDOUR