Create export temporary files in the export directory
[ardour.git] / libs / ardour / export_graph_builder.cc
1 #include "ardour/export_graph_builder.h"
2
3 #include <glibmm/miscutils.h>
4
5 #include "audiographer/process_context.h"
6 #include "audiographer/general/chunker.h"
7 #include "audiographer/general/interleaver.h"
8 #include "audiographer/general/normalizer.h"
9 #include "audiographer/general/peak_reader.h"
10 #include "audiographer/general/sample_format_converter.h"
11 #include "audiographer/general/sr_converter.h"
12 #include "audiographer/general/silence_trimmer.h"
13 #include "audiographer/general/threader.h"
14 #include "audiographer/sndfile/tmp_file.h"
15 #include "audiographer/sndfile/sndfile_writer.h"
16
17 #include "ardour/audioengine.h"
18 #include "ardour/export_channel_configuration.h"
19 #include "ardour/export_filename.h"
20 #include "ardour/export_format_specification.h"
21 #include "ardour/export_timespan.h"
22 #include "ardour/session_directory.h"
23 #include "ardour/sndfile_helpers.h"
24
25 #include "pbd/file_utils.h"
26 #include "pbd/cpus.h"
27
28 using namespace AudioGrapher;
29 using std::string;
30
31 namespace ARDOUR {
32
33 ExportGraphBuilder::ExportGraphBuilder (Session const & session)
34   : session (session)
35   , thread_pool (hardware_concurrency())
36 {
37         process_buffer_frames = session.engine().frames_per_cycle();
38 }
39
40 ExportGraphBuilder::~ExportGraphBuilder ()
41 {
42 }
43
44 int
45 ExportGraphBuilder::process (framecnt_t frames, bool last_cycle)
46 {
47         assert(frames <= process_buffer_frames);
48
49         for (ChannelMap::iterator it = channels.begin(); it != channels.end(); ++it) {
50                 Sample const * process_buffer = 0;
51                 it->first->read (process_buffer, frames);
52                 ConstProcessContext<Sample> context(process_buffer, frames, 1);
53                 if (last_cycle) { context().set_flag (ProcessContext<Sample>::EndOfInput); }
54                 it->second->process (context);
55         }
56
57         return 0;
58 }
59
60 bool
61 ExportGraphBuilder::process_normalize ()
62 {
63         for (std::list<Normalizer *>::iterator it = normalizers.begin(); it != normalizers.end(); /* ++ in loop */) {
64                 if ((*it)->process()) {
65                         it = normalizers.erase (it);
66                 } else {
67                         ++it;
68                 }
69         }
70
71         return normalizers.empty();
72 }
73
74 unsigned
75 ExportGraphBuilder::get_normalize_cycle_count() const
76 {
77         unsigned max = 0;
78         for (std::list<Normalizer *>::const_iterator it = normalizers.begin(); it != normalizers.end(); ++it) {
79                 max = std::max(max, (*it)->get_normalize_cycle_count());
80         }
81         return max;
82 }
83
84 void
85 ExportGraphBuilder::reset ()
86 {
87         timespan.reset();
88         channel_configs.clear ();
89         channels.clear ();
90         normalizers.clear ();
91 }
92
93 void
94 ExportGraphBuilder::set_current_timespan (boost::shared_ptr<ExportTimespan> span)
95 {
96         timespan = span;
97 }
98
99 void
100 ExportGraphBuilder::add_config (FileSpec const & config)
101 {
102         ExportChannelConfiguration::ChannelList const & channels =
103                 config.channel_config->get_channels();
104         for(ExportChannelConfiguration::ChannelList::const_iterator it = channels.begin();
105             it != channels.end(); ++it) {
106                 (*it)->set_max_buffer_size(process_buffer_frames);
107         }
108
109         // If the sample rate is "session rate", change it to the real value.
110         // However, we need to copy it to not change the config which is saved...
111         FileSpec new_config (config);
112         new_config.format.reset(new ExportFormatSpecification(*new_config.format));
113         if(new_config.format->sample_rate() == ExportFormatBase::SR_Session) {
114                 framecnt_t session_rate = session.nominal_frame_rate();
115                 new_config.format->set_sample_rate(ExportFormatBase::nearest_sample_rate(session_rate));
116         }
117
118
119         if (!new_config.channel_config->get_split ()) {
120                 add_split_config (new_config);
121                 return;
122         }
123
124         // Split channel configurations are split into several channel configurations,
125         // each corresponding to a file, at this stage
126         typedef std::list<boost::shared_ptr<ExportChannelConfiguration> > ConfigList;
127         ConfigList file_configs;
128         new_config.channel_config->configurations_for_files (file_configs);
129
130         unsigned chan = 1;
131         for (ConfigList::iterator it = file_configs.begin(); it != file_configs.end(); ++it, ++chan) {
132                 FileSpec copy = new_config;
133                 copy.channel_config = *it;
134
135                 copy.filename.reset (new ExportFilename (*copy.filename));
136                 copy.filename->include_channel = true;
137                 copy.filename->set_channel (chan);
138
139                 add_split_config (copy);
140         }
141 }
142
143 void
144 ExportGraphBuilder::add_split_config (FileSpec const & config)
145 {
146         for (ChannelConfigList::iterator it = channel_configs.begin(); it != channel_configs.end(); ++it) {
147                 if (*it == config) {
148                         it->add_child (config);
149                         return;
150                 }
151         }
152
153         // No duplicate channel config found, create new one
154         channel_configs.push_back (new ChannelConfig (*this, config, channels));
155 }
156
157 /* Encoder */
158
159 template <>
160 boost::shared_ptr<AudioGrapher::Sink<Sample> >
161 ExportGraphBuilder::Encoder::init (FileSpec const & new_config)
162 {
163         config = new_config;
164         init_writer (float_writer);
165         return float_writer;
166 }
167
168 template <>
169 boost::shared_ptr<AudioGrapher::Sink<int> >
170 ExportGraphBuilder::Encoder::init (FileSpec const & new_config)
171 {
172         config = new_config;
173         init_writer (int_writer);
174         return int_writer;
175 }
176
177 template <>
178 boost::shared_ptr<AudioGrapher::Sink<short> >
179 ExportGraphBuilder::Encoder::init (FileSpec const & new_config)
180 {
181         config = new_config;
182         init_writer (short_writer);
183         return short_writer;
184 }
185
186 void
187 ExportGraphBuilder::Encoder::add_child (FileSpec const & new_config)
188 {
189         filenames.push_back (new_config.filename);
190 }
191
192 bool
193 ExportGraphBuilder::Encoder::operator== (FileSpec const & other_config) const
194 {
195         return get_real_format (config) == get_real_format (other_config);
196 }
197
198 int
199 ExportGraphBuilder::Encoder::get_real_format (FileSpec const & config)
200 {
201         ExportFormatSpecification & format = *config.format;
202         return format.format_id() | format.sample_format() | format.endianness();
203 }
204
205 template<typename T>
206 void
207 ExportGraphBuilder::Encoder::init_writer (boost::shared_ptr<AudioGrapher::SndfileWriter<T> > & writer)
208 {
209         unsigned channels = config.channel_config->get_n_chans();
210         int format = get_real_format (config);
211         config.filename->set_channel_config(config.channel_config);
212         string filename = config.filename->get_path (config.format);
213
214         writer.reset (new AudioGrapher::SndfileWriter<T> (filename, format, channels, config.format->sample_rate(), config.broadcast_info));
215         writer->FileWritten.connect_same_thread (copy_files_connection, boost::bind (&ExportGraphBuilder::Encoder::copy_files, this, _1));
216 }
217
218 void
219 ExportGraphBuilder::Encoder::copy_files (std::string orig_path)
220 {
221         while (filenames.size()) {
222                 ExportFilenamePtr & filename = filenames.front();
223                 PBD::copy_file (orig_path, filename->get_path (config.format).c_str());
224                 filenames.pop_front();
225         }
226 }
227
228 /* SFC */
229
230 ExportGraphBuilder::SFC::SFC (ExportGraphBuilder &, FileSpec const & new_config, framecnt_t max_frames)
231   : data_width(0)
232 {
233         config = new_config;
234         data_width = sndfile_data_width (Encoder::get_real_format (config));
235         unsigned channels = new_config.channel_config->get_n_chans();
236
237         if (data_width == 8 || data_width == 16) {
238                 short_converter = ShortConverterPtr (new SampleFormatConverter<short> (channels));
239                 short_converter->init (max_frames, config.format->dither_type(), data_width);
240                 add_child (config);
241         } else if (data_width == 24 || data_width == 32) {
242                 int_converter = IntConverterPtr (new SampleFormatConverter<int> (channels));
243                 int_converter->init (max_frames, config.format->dither_type(), data_width);
244                 add_child (config);
245         } else {
246                 int actual_data_width = 8 * sizeof(Sample);
247                 float_converter = FloatConverterPtr (new SampleFormatConverter<Sample> (channels));
248                 float_converter->init (max_frames, config.format->dither_type(), actual_data_width);
249                 add_child (config);
250         }
251 }
252
253 ExportGraphBuilder::FloatSinkPtr
254 ExportGraphBuilder::SFC::sink ()
255 {
256         if (data_width == 8 || data_width == 16) {
257                 return short_converter;
258         } else if (data_width == 24 || data_width == 32) {
259                 return int_converter;
260         } else {
261                 return float_converter;
262         }
263 }
264
265 void
266 ExportGraphBuilder::SFC::add_child (FileSpec const & new_config)
267 {
268         for (boost::ptr_list<Encoder>::iterator it = children.begin(); it != children.end(); ++it) {
269                 if (*it == new_config) {
270                         it->add_child (new_config);
271                         return;
272                 }
273         }
274
275         children.push_back (new Encoder());
276         Encoder & encoder = children.back();
277
278         if (data_width == 8 || data_width == 16) {
279                 short_converter->add_output (encoder.init<short> (new_config));
280         } else if (data_width == 24 || data_width == 32) {
281                 int_converter->add_output (encoder.init<int> (new_config));
282         } else {
283                 float_converter->add_output (encoder.init<Sample> (new_config));
284         }
285 }
286
287 bool
288 ExportGraphBuilder::SFC::operator== (FileSpec const & other_config) const
289 {
290         return config.format->sample_format() == other_config.format->sample_format();
291 }
292
293 /* Normalizer */
294
295 ExportGraphBuilder::Normalizer::Normalizer (ExportGraphBuilder & parent, FileSpec const & new_config, framecnt_t /*max_frames*/)
296   : parent (parent)
297 {
298         std::string tmpfile_path = parent.session.session_directory().export_path();
299         tmpfile_path = Glib::build_filename(tmpfile_path, "XXXXXX");
300         char tmpfile_path_buf[tmpfile_path.size() + 1];
301         std::copy(tmpfile_path.begin(), tmpfile_path.end(), tmpfile_path_buf);
302         tmpfile_path_buf[tmpfile_path.size()] = '\0';
303
304         config = new_config;
305         uint32_t const channels = config.channel_config->get_n_chans();
306         max_frames_out = 4086 - (4086 % channels); // TODO good chunk size
307         
308         buffer.reset (new AllocatingProcessContext<Sample> (max_frames_out, channels));
309         peak_reader.reset (new PeakReader ());
310         normalizer.reset (new AudioGrapher::Normalizer (config.format->normalize_target()));
311         threader.reset (new Threader<Sample> (parent.thread_pool));
312
313         normalizer->alloc_buffer (max_frames_out);
314         normalizer->add_output (threader);
315
316         int format = ExportFormatBase::F_RAW | ExportFormatBase::SF_Float;
317         tmp_file.reset (new TmpFile<float> (tmpfile_path_buf, format, channels, config.format->sample_rate()));
318         tmp_file->FileWritten.connect_same_thread (post_processing_connection,
319                                                    boost::bind (&Normalizer::start_post_processing, this));
320
321         add_child (new_config);
322
323         peak_reader->add_output (tmp_file);
324 }
325
326 ExportGraphBuilder::FloatSinkPtr
327 ExportGraphBuilder::Normalizer::sink ()
328 {
329         return peak_reader;
330 }
331
332 void
333 ExportGraphBuilder::Normalizer::add_child (FileSpec const & new_config)
334 {
335         for (boost::ptr_list<SFC>::iterator it = children.begin(); it != children.end(); ++it) {
336                 if (*it == new_config) {
337                         it->add_child (new_config);
338                         return;
339                 }
340         }
341
342         children.push_back (new SFC (parent, new_config, max_frames_out));
343         threader->add_output (children.back().sink());
344 }
345
346 bool
347 ExportGraphBuilder::Normalizer::operator== (FileSpec const & other_config) const
348 {
349         return config.format->normalize() == other_config.format->normalize() &&
350                config.format->normalize_target() == other_config.format->normalize_target();
351 }
352
353 unsigned
354 ExportGraphBuilder::Normalizer::get_normalize_cycle_count() const
355 {
356         return static_cast<unsigned>(std::ceil(static_cast<float>(tmp_file->get_frames_written()) /
357                                                max_frames_out));
358 }
359
360 bool
361 ExportGraphBuilder::Normalizer::process()
362 {
363         framecnt_t frames_read = tmp_file->read (*buffer);
364         return frames_read != buffer->frames();
365 }
366
367 void
368 ExportGraphBuilder::Normalizer::start_post_processing()
369 {
370         normalizer->set_peak (peak_reader->get_peak());
371         tmp_file->seek (0, SEEK_SET);
372         tmp_file->add_output (normalizer);
373         parent.normalizers.push_back (this);
374 }
375
376 /* SRC */
377
378 ExportGraphBuilder::SRC::SRC (ExportGraphBuilder & parent, FileSpec const & new_config, framecnt_t max_frames)
379   : parent (parent)
380 {
381         config = new_config;
382         converter.reset (new SampleRateConverter (new_config.channel_config->get_n_chans()));
383         ExportFormatSpecification & format = *new_config.format;
384         converter->init (parent.session.nominal_frame_rate(), format.sample_rate(), format.src_quality());
385         max_frames_out = converter->allocate_buffers (max_frames);
386
387         add_child (new_config);
388 }
389
390 ExportGraphBuilder::FloatSinkPtr
391 ExportGraphBuilder::SRC::sink ()
392 {
393         return converter;
394 }
395
396 void
397 ExportGraphBuilder::SRC::add_child (FileSpec const & new_config)
398 {
399         if (new_config.format->normalize()) {
400                 add_child_to_list (new_config, normalized_children);
401         } else {
402                 add_child_to_list (new_config, children);
403         }
404 }
405
406 template<typename T>
407 void
408 ExportGraphBuilder::SRC::add_child_to_list (FileSpec const & new_config, boost::ptr_list<T> & list)
409 {
410         for (typename boost::ptr_list<T>::iterator it = list.begin(); it != list.end(); ++it) {
411                 if (*it == new_config) {
412                         it->add_child (new_config);
413                         return;
414                 }
415         }
416
417         list.push_back (new T (parent, new_config, max_frames_out));
418         converter->add_output (list.back().sink ());
419 }
420
421 bool
422 ExportGraphBuilder::SRC::operator== (FileSpec const & other_config) const
423 {
424         return config.format->sample_rate() == other_config.format->sample_rate();
425 }
426
427 /* SilenceHandler */
428 ExportGraphBuilder::SilenceHandler::SilenceHandler (ExportGraphBuilder & parent, FileSpec const & new_config, framecnt_t max_frames)
429   : parent (parent)
430 {
431         config = new_config;
432         max_frames_in = max_frames;
433         framecnt_t sample_rate = parent.session.nominal_frame_rate();
434
435         silence_trimmer.reset (new SilenceTrimmer<Sample>(max_frames_in));
436         silence_trimmer->set_trim_beginning (config.format->trim_beginning());
437         silence_trimmer->set_trim_end (config.format->trim_end());
438
439         framecnt_t sb = config.format->silence_beginning_at (parent.timespan->get_start(), sample_rate);
440         framecnt_t se = config.format->silence_end_at (parent.timespan->get_end(), sample_rate);
441
442         silence_trimmer->add_silence_to_beginning (sb);
443         silence_trimmer->add_silence_to_end (se);
444
445         add_child (new_config);
446 }
447
448 ExportGraphBuilder::FloatSinkPtr
449 ExportGraphBuilder::SilenceHandler::sink ()
450 {
451         return silence_trimmer;
452 }
453
454 void
455 ExportGraphBuilder::SilenceHandler::add_child (FileSpec const & new_config)
456 {
457         for (boost::ptr_list<SRC>::iterator it = children.begin(); it != children.end(); ++it) {
458                 if (*it == new_config) {
459                         it->add_child (new_config);
460                         return;
461                 }
462         }
463
464         children.push_back (new SRC (parent, new_config, max_frames_in));
465         silence_trimmer->add_output (children.back().sink());
466 }
467
468 bool
469 ExportGraphBuilder::SilenceHandler::operator== (FileSpec const & other_config) const
470 {
471         ExportFormatSpecification & format = *config.format;
472         ExportFormatSpecification & other_format = *other_config.format;
473         return (format.trim_beginning() == other_format.trim_beginning()) &&
474                (format.trim_end() == other_format.trim_end()) &&
475                (format.silence_beginning_time() == other_format.silence_beginning_time()) &&
476                (format.silence_end_time() == other_format.silence_end_time());
477 }
478
479 /* ChannelConfig */
480
481 ExportGraphBuilder::ChannelConfig::ChannelConfig (ExportGraphBuilder & parent, FileSpec const & new_config, ChannelMap & channel_map)
482   : parent (parent)
483 {
484         typedef ExportChannelConfiguration::ChannelList ChannelList;
485
486         config = new_config;
487
488         framecnt_t max_frames = parent.session.engine().frames_per_cycle();
489         interleaver.reset (new Interleaver<Sample> ());
490         interleaver->init (new_config.channel_config->get_n_chans(), max_frames);
491
492         // Make the chunk size divisible by the channel count
493         int chan_count = new_config.channel_config->get_n_chans();
494         max_frames_out = 8192;
495         max_frames_out -= max_frames_out % chan_count;
496         chunker.reset (new Chunker<Sample> (max_frames_out));
497         interleaver->add_output(chunker);
498
499         ChannelList const & channel_list = config.channel_config->get_channels();
500         unsigned chan = 0;
501         for (ChannelList::const_iterator it = channel_list.begin(); it != channel_list.end(); ++it, ++chan) {
502                 ChannelMap::iterator map_it = channel_map.find (*it);
503                 if (map_it == channel_map.end()) {
504                         std::pair<ChannelMap::iterator, bool> result_pair =
505                                 channel_map.insert (std::make_pair (*it, IdentityVertexPtr (new IdentityVertex<Sample> ())));
506                         assert (result_pair.second);
507                         map_it = result_pair.first;
508                 }
509                 map_it->second->add_output (interleaver->input (chan));
510         }
511
512         add_child (new_config);
513 }
514
515 void
516 ExportGraphBuilder::ChannelConfig::add_child (FileSpec const & new_config)
517 {
518         assert (*this == new_config);
519
520         for (boost::ptr_list<SilenceHandler>::iterator it = children.begin(); it != children.end(); ++it) {
521                 if (*it == new_config) {
522                         it->add_child (new_config);
523                         return;
524                 }
525         }
526
527         children.push_back (new SilenceHandler (parent, new_config, max_frames_out));
528         chunker->add_output (children.back().sink ());
529 }
530
531 bool
532 ExportGraphBuilder::ChannelConfig::operator== (FileSpec const & other_config) const
533 {
534         return config.channel_config == other_config.channel_config;
535 }
536
537 } // namespace ARDOUR