use a note tracker to resolve notes cut off during render by the end of the region
[ardour.git] / libs / ardour / export_graph_builder.cc
1 /*
2  * Copyright (C) 2009-2013 Sakari Bergen <sakari.bergen@beatwaves.net>
3  * Copyright (C) 2010-2012 Carl Hetherington <carl@carlh.net>
4  * Copyright (C) 2010-2017 Paul Davis <paul@linuxaudiosystems.com>
5  * Copyright (C) 2011-2014 David Robillard <d@drobilla.net>
6  * Copyright (C) 2012-2016 Tim Mayberry <mojofunk@gmail.com>
7  * Copyright (C) 2013-2016 John Emmas <john@creativepost.co.uk>
8  * Copyright (C) 2015-2019 Robin Gareus <robin@gareus.org>
9  *
10  * This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2 of the License, or
13  * (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License along
21  * with this program; if not, write to the Free Software Foundation, Inc.,
22  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
23  */
24
25 #include <vector>
26
27 #include <glibmm/miscutils.h>
28
29 #include "pbd/uuid.h"
30 #include "pbd/file_utils.h"
31 #include "pbd/cpus.h"
32
33 #include "audiographer/process_context.h"
34 #include "audiographer/general/chunker.h"
35 #include "audiographer/general/cmdpipe_writer.h"
36 #include "audiographer/general/interleaver.h"
37 #include "audiographer/general/normalizer.h"
38 #include "audiographer/general/analyser.h"
39 #include "audiographer/general/peak_reader.h"
40 #include "audiographer/general/loudness_reader.h"
41 #include "audiographer/general/sample_format_converter.h"
42 #include "audiographer/general/sr_converter.h"
43 #include "audiographer/general/silence_trimmer.h"
44 #include "audiographer/general/threader.h"
45 #include "audiographer/sndfile/tmp_file.h"
46 #include "audiographer/sndfile/tmp_file_rt.h"
47 #include "audiographer/sndfile/tmp_file_sync.h"
48 #include "audiographer/sndfile/sndfile_writer.h"
49
50 #include "ardour/audioengine.h"
51 #include "ardour/export_channel_configuration.h"
52 #include "ardour/export_failed.h"
53 #include "ardour/export_filename.h"
54 #include "ardour/export_format_specification.h"
55 #include "ardour/export_graph_builder.h"
56 #include "ardour/export_timespan.h"
57 #include "ardour/filesystem_paths.h"
58 #include "ardour/session_directory.h"
59 #include "ardour/session_metadata.h"
60 #include "ardour/sndfile_helpers.h"
61 #include "ardour/system_exec.h"
62
63 using namespace AudioGrapher;
64 using std::string;
65
66 namespace ARDOUR {
67
68 ExportGraphBuilder::ExportGraphBuilder (Session const & session)
69         : session (session)
70         , thread_pool (hardware_concurrency())
71 {
72         process_buffer_samples = session.engine().samples_per_cycle();
73 }
74
75 ExportGraphBuilder::~ExportGraphBuilder ()
76 {
77 }
78
79 int
80 ExportGraphBuilder::process (samplecnt_t samples, bool last_cycle)
81 {
82         assert(samples <= process_buffer_samples);
83
84         for (ChannelMap::iterator it = channels.begin(); it != channels.end(); ++it) {
85                 Sample const * process_buffer = 0;
86                 it->first->read (process_buffer, samples);
87                 ConstProcessContext<Sample> context(process_buffer, samples, 1);
88                 if (last_cycle) { context().set_flag (ProcessContext<Sample>::EndOfInput); }
89                 it->second->process (context);
90         }
91
92         return 0;
93 }
94
95 bool
96 ExportGraphBuilder::post_process ()
97 {
98         for (std::list<Intermediate *>::iterator it = intermediates.begin(); it != intermediates.end(); /* ++ in loop */) {
99                 if ((*it)->process()) {
100                         it = intermediates.erase (it);
101                 } else {
102                         ++it;
103                 }
104         }
105
106         return intermediates.empty();
107 }
108
109 unsigned
110 ExportGraphBuilder::get_postprocessing_cycle_count() const
111 {
112         unsigned max = 0;
113         for (std::list<Intermediate *>::const_iterator it = intermediates.begin(); it != intermediates.end(); ++it) {
114                 max = std::max(max, (*it)->get_postprocessing_cycle_count());
115         }
116         return max;
117 }
118
119 void
120 ExportGraphBuilder::reset ()
121 {
122         timespan.reset();
123         channel_configs.clear ();
124         channels.clear ();
125         intermediates.clear ();
126         analysis_map.clear();
127         _realtime = false;
128 }
129
130 void
131 ExportGraphBuilder::cleanup (bool remove_out_files/*=false*/)
132 {
133         ChannelConfigList::iterator iter = channel_configs.begin();
134
135         while (iter != channel_configs.end() ) {
136                 iter->remove_children(remove_out_files);
137                 iter = channel_configs.erase(iter);
138         }
139 }
140
141 void
142 ExportGraphBuilder::set_current_timespan (boost::shared_ptr<ExportTimespan> span)
143 {
144         timespan = span;
145 }
146
147 void
148 ExportGraphBuilder::add_config (FileSpec const & config, bool rt)
149 {
150         ExportChannelConfiguration::ChannelList const & channels =
151                 config.channel_config->get_channels();
152         for(ExportChannelConfiguration::ChannelList::const_iterator it = channels.begin();
153             it != channels.end(); ++it) {
154                 (*it)->set_max_buffer_size(process_buffer_samples);
155         }
156
157         _realtime = rt;
158
159         // If the sample rate is "session rate", change it to the real value.
160         // However, we need to copy it to not change the config which is saved...
161         FileSpec new_config (config);
162         new_config.format.reset(new ExportFormatSpecification(*new_config.format, false));
163         if(new_config.format->sample_rate() == ExportFormatBase::SR_Session) {
164                 samplecnt_t session_rate = session.nominal_sample_rate();
165                 new_config.format->set_sample_rate(ExportFormatBase::nearest_sample_rate(session_rate));
166         }
167
168
169         if (!new_config.channel_config->get_split ()) {
170                 add_split_config (new_config);
171                 return;
172         }
173
174         // Split channel configurations are split into several channel configurations,
175         // each corresponding to a file, at this stage
176         typedef std::list<boost::shared_ptr<ExportChannelConfiguration> > ConfigList;
177         ConfigList file_configs;
178         new_config.channel_config->configurations_for_files (file_configs);
179
180         unsigned chan = 1;
181         for (ConfigList::iterator it = file_configs.begin(); it != file_configs.end(); ++it, ++chan) {
182                 FileSpec copy = new_config;
183                 copy.channel_config = *it;
184
185                 copy.filename.reset (new ExportFilename (*copy.filename));
186                 copy.filename->include_channel = true;
187                 copy.filename->set_channel (chan);
188
189                 add_split_config (copy);
190         }
191 }
192
193 void
194 ExportGraphBuilder::get_analysis_results (AnalysisResults& results) {
195         for (AnalysisMap::iterator i = analysis_map.begin(); i != analysis_map.end(); ++i) {
196                 ExportAnalysisPtr p = i->second->result ();
197                 if (p) {
198                         results.insert (std::make_pair (i->first, p));
199                 }
200         }
201 }
202
203 void
204 ExportGraphBuilder::add_split_config (FileSpec const & config)
205 {
206         for (ChannelConfigList::iterator it = channel_configs.begin(); it != channel_configs.end(); ++it) {
207                 if (*it == config) {
208                         it->add_child (config);
209                         return;
210                 }
211         }
212
213         // No duplicate channel config found, create new one
214         channel_configs.push_back (new ChannelConfig (*this, config, channels));
215 }
216
217 /* Encoder */
218
219 template <>
220 boost::shared_ptr<AudioGrapher::Sink<Sample> >
221 ExportGraphBuilder::Encoder::init (FileSpec const & new_config)
222 {
223         config = new_config;
224         if (config.format->format_id() == ExportFormatBase::F_FFMPEG) {
225                 init_writer (pipe_writer);
226                 return pipe_writer;
227         } else {
228                 init_writer (float_writer);
229                 return float_writer;
230         }
231 }
232
233 template <>
234 boost::shared_ptr<AudioGrapher::Sink<int> >
235 ExportGraphBuilder::Encoder::init (FileSpec const & new_config)
236 {
237         config = new_config;
238         init_writer (int_writer);
239         return int_writer;
240 }
241
242 template <>
243 boost::shared_ptr<AudioGrapher::Sink<short> >
244 ExportGraphBuilder::Encoder::init (FileSpec const & new_config)
245 {
246         config = new_config;
247         init_writer (short_writer);
248         return short_writer;
249 }
250
251 void
252 ExportGraphBuilder::Encoder::add_child (FileSpec const & new_config)
253 {
254         filenames.push_back (new_config.filename);
255 }
256
257 void
258 ExportGraphBuilder::Encoder::destroy_writer (bool delete_out_file)
259 {
260         if (delete_out_file ) {
261
262                 if (float_writer) {
263                         float_writer->close ();
264                 }
265
266                 if (int_writer) {
267                         int_writer->close ();
268                 }
269
270                 if (short_writer) {
271                         short_writer->close ();
272                 }
273
274                 if (pipe_writer) {
275                         pipe_writer->close ();
276                 }
277
278                 if (std::remove(writer_filename.c_str() ) != 0) {
279                         std::cout << "Encoder::destroy_writer () : Error removing file: " << strerror(errno) << std::endl;
280                 }
281         }
282
283         float_writer.reset ();
284         int_writer.reset ();
285         short_writer.reset ();
286         pipe_writer.reset ();
287 }
288
289 bool
290 ExportGraphBuilder::Encoder::operator== (FileSpec const & other_config) const
291 {
292         return get_real_format (config) == get_real_format (other_config);
293 }
294
295 int
296 ExportGraphBuilder::Encoder::get_real_format (FileSpec const & config)
297 {
298         ExportFormatSpecification & format = *config.format;
299         return format.format_id() | format.sample_format() | format.endianness();
300 }
301
302 template<typename T>
303 void
304 ExportGraphBuilder::Encoder::init_writer (boost::shared_ptr<AudioGrapher::SndfileWriter<T> > & writer)
305 {
306         unsigned channels = config.channel_config->get_n_chans();
307         int format = get_real_format (config);
308         config.filename->set_channel_config(config.channel_config);
309         writer_filename = config.filename->get_path (config.format);
310
311         writer.reset (new AudioGrapher::SndfileWriter<T> (writer_filename, format, channels, config.format->sample_rate(), config.broadcast_info));
312         writer->FileWritten.connect_same_thread (copy_files_connection, boost::bind (&ExportGraphBuilder::Encoder::copy_files, this, _1));
313         if (format & ExportFormatBase::SF_Vorbis) {
314                 /* libsndfile uses range 0..1 (worst.. best) for
315                  * SFC_SET_VBR_ENCODING_QUALITY and maps
316                  * SFC_SET_COMPRESSION_LEVEL = 1.0 - VBR_ENCODING_QUALITY
317                  */
318                 double vorbis_quality = config.format->codec_quality () / 100.f;
319                 if (vorbis_quality >= 0 && vorbis_quality <= 1.0) {
320                         writer->command (SFC_SET_VBR_ENCODING_QUALITY, &vorbis_quality, sizeof (double));
321                 }
322         }
323 }
324
325 template<typename T>
326 void
327 ExportGraphBuilder::Encoder::init_writer (boost::shared_ptr<AudioGrapher::CmdPipeWriter<T> > & writer)
328 {
329         unsigned channels = config.channel_config->get_n_chans();
330         config.filename->set_channel_config(config.channel_config);
331         writer_filename = config.filename->get_path (config.format);
332
333         std::string ffmpeg_exe;
334         std::string unused;
335
336         if (!ArdourVideoToolPaths::transcoder_exe (ffmpeg_exe, unused)) {
337                 throw ExportFailed ("External encoder (ffmpeg) is not available.");
338         }
339
340         int quality = config.format->codec_quality ();
341
342         int a=0;
343         char **argp = (char**) calloc (100, sizeof(char*));
344         char tmp[64];
345         argp[a++] = strdup(ffmpeg_exe.c_str());
346         argp[a++] = strdup ("-f");
347         argp[a++] = strdup ("f32le");
348         argp[a++] = strdup ("-acodec");
349         argp[a++] = strdup ("pcm_f32le");
350         argp[a++] = strdup ("-ac");
351         snprintf (tmp, sizeof(tmp), "%d", channels);
352         argp[a++] = strdup (tmp);
353         argp[a++] = strdup ("-ar");
354         snprintf (tmp, sizeof(tmp), "%d", config.format->sample_rate());
355         argp[a++] = strdup (tmp);
356         argp[a++] = strdup ("-i");
357         argp[a++] = strdup ("pipe:0");
358
359         argp[a++] = strdup ("-y");
360         if (quality <= 0) {
361                 /* variable rate, lower is better */
362                 snprintf (tmp, sizeof(tmp), "%d", -quality);
363                 argp[a++] = strdup ("-q:a"); argp[a++] = strdup (tmp);
364         } else {
365                 /* fixed bitrate, higher is better */
366                 snprintf (tmp, sizeof(tmp), "%dk", quality); // eg. "192k"
367                 argp[a++] = strdup ("-b:a"); argp[a++] = strdup (tmp);
368         }
369
370         SessionMetadata::MetaDataMap meta;
371         meta["comment"] = "Created with " PROGRAM_NAME;
372
373         if (config.format->tag()) {
374                 ARDOUR::SessionMetadata* session_data = ARDOUR::SessionMetadata::Metadata();
375                 session_data->av_export_tag (meta);
376         }
377
378         for(SessionMetadata::MetaDataMap::const_iterator it = meta.begin(); it != meta.end(); ++it) {
379                 argp[a++] = strdup("-metadata");
380                 argp[a++] = SystemExec::format_key_value_parameter (it->first.c_str(), it->second.c_str());
381         }
382
383         argp[a++] = strdup (writer_filename.c_str());
384         argp[a] = (char *)0;
385
386         /* argp is free()d in ~SystemExec,
387          * SystemExec is deleted when writer is destroyed */
388         ARDOUR::SystemExec* exec = new ARDOUR::SystemExec (ffmpeg_exe, argp);
389         PBD::info << "Encode command: { " << exec->to_s () << "}" << endmsg;
390         if (exec->start (SystemExec::MergeWithStdin)) {
391                 throw ExportFailed ("External encoder (ffmpeg) cannot be started.");
392         }
393         writer.reset (new AudioGrapher::CmdPipeWriter<T> (exec, writer_filename));
394         writer->FileWritten.connect_same_thread (copy_files_connection, boost::bind (&ExportGraphBuilder::Encoder::copy_files, this, _1));
395 }
396
397 void
398 ExportGraphBuilder::Encoder::copy_files (std::string orig_path)
399 {
400         while (filenames.size()) {
401                 ExportFilenamePtr & filename = filenames.front();
402                 PBD::copy_file (orig_path, filename->get_path (config.format).c_str());
403                 filenames.pop_front();
404         }
405 }
406
407 /* SFC */
408
409 ExportGraphBuilder::SFC::SFC (ExportGraphBuilder &parent, FileSpec const & new_config, samplecnt_t max_samples)
410         : data_width(0)
411 {
412         config = new_config;
413         data_width = sndfile_data_width (Encoder::get_real_format (config));
414         unsigned channels = new_config.channel_config->get_n_chans();
415         _analyse = config.format->analyse();
416         if (_analyse) {
417                 samplecnt_t sample_rate = parent.session.nominal_sample_rate();
418                 samplecnt_t sb = config.format->silence_beginning_at (parent.timespan->get_start(), sample_rate);
419                 samplecnt_t se = config.format->silence_end_at (parent.timespan->get_end(), sample_rate);
420                 samplecnt_t duration = parent.timespan->get_length () + sb + se;
421                 max_samples = min ((samplecnt_t) 8192 * channels, max ((samplecnt_t) 4096 * channels, max_samples));
422                 chunker.reset (new Chunker<Sample> (max_samples));
423                 analyser.reset (new Analyser (config.format->sample_rate(), channels, max_samples,
424                                         (samplecnt_t) ceil (duration * config.format->sample_rate () / (double) sample_rate)));
425                 chunker->add_output (analyser);
426
427                 config.filename->set_channel_config (config.channel_config);
428                 parent.add_analyser (config.filename->get_path (config.format), analyser);
429         }
430
431         if (data_width == 8 || data_width == 16) {
432                 short_converter = ShortConverterPtr (new SampleFormatConverter<short> (channels));
433                 short_converter->init (max_samples, config.format->dither_type(), data_width);
434                 add_child (config);
435                 if (_analyse) { analyser->add_output (short_converter); }
436
437         } else if (data_width == 24 || data_width == 32) {
438                 int_converter = IntConverterPtr (new SampleFormatConverter<int> (channels));
439                 int_converter->init (max_samples, config.format->dither_type(), data_width);
440                 add_child (config);
441                 if (_analyse) { analyser->add_output (int_converter); }
442         } else {
443                 int actual_data_width = 8 * sizeof(Sample);
444                 float_converter = FloatConverterPtr (new SampleFormatConverter<Sample> (channels));
445                 float_converter->init (max_samples, config.format->dither_type(), actual_data_width);
446                 add_child (config);
447                 if (_analyse) { analyser->add_output (float_converter); }
448         }
449 }
450
451 void
452 ExportGraphBuilder::SFC::set_peak (float gain)
453 {
454         if (_analyse) {
455                 analyser->set_normalization_gain (gain);
456         }
457 }
458
459 ExportGraphBuilder::FloatSinkPtr
460 ExportGraphBuilder::SFC::sink ()
461 {
462         if (_analyse) {
463                 return chunker;
464         } else if (data_width == 8 || data_width == 16) {
465                 return short_converter;
466         } else if (data_width == 24 || data_width == 32) {
467                 return int_converter;
468         } else {
469                 return float_converter;
470         }
471 }
472
473 void
474 ExportGraphBuilder::SFC::add_child (FileSpec const & new_config)
475 {
476         for (boost::ptr_list<Encoder>::iterator it = children.begin(); it != children.end(); ++it) {
477                 if (*it == new_config) {
478                         it->add_child (new_config);
479                         return;
480                 }
481         }
482
483         children.push_back (new Encoder());
484         Encoder & encoder = children.back();
485
486         if (data_width == 8 || data_width == 16) {
487                 short_converter->add_output (encoder.init<short> (new_config));
488         } else if (data_width == 24 || data_width == 32) {
489                 int_converter->add_output (encoder.init<int> (new_config));
490         } else {
491                 float_converter->add_output (encoder.init<Sample> (new_config));
492         }
493 }
494
495 void
496 ExportGraphBuilder::SFC::remove_children (bool remove_out_files)
497 {
498         boost::ptr_list<Encoder>::iterator iter = children.begin ();
499
500         while (iter != children.end() ) {
501
502                 if (remove_out_files) {
503                         iter->destroy_writer(remove_out_files);
504                 }
505                 iter = children.erase (iter);
506         }
507 }
508
509 bool
510 ExportGraphBuilder::SFC::operator== (FileSpec const & other_config) const
511 {
512         return config.format->sample_format() == other_config.format->sample_format();
513 }
514
515 /* Intermediate (Normalizer, TmpFile) */
516
517 ExportGraphBuilder::Intermediate::Intermediate (ExportGraphBuilder & parent, FileSpec const & new_config, samplecnt_t max_samples)
518         : parent (parent)
519         , use_loudness (false)
520         , use_peak (false)
521 {
522         std::string tmpfile_path = parent.session.session_directory().export_path();
523         tmpfile_path = Glib::build_filename(tmpfile_path, "XXXXXX");
524         std::vector<char> tmpfile_path_buf(tmpfile_path.size() + 1);
525         std::copy(tmpfile_path.begin(), tmpfile_path.end(), tmpfile_path_buf.begin());
526         tmpfile_path_buf[tmpfile_path.size()] = '\0';
527
528         config = new_config;
529         uint32_t const channels = config.channel_config->get_n_chans();
530         max_samples_out = 4086 - (4086 % channels); // TODO good chunk size
531         use_loudness = config.format->normalize_loudness ();
532         use_peak = config.format->normalize ();
533
534         buffer.reset (new AllocatingProcessContext<Sample> (max_samples_out, channels));
535
536         if (use_peak) {
537                 peak_reader.reset (new PeakReader ());
538         }
539         if (use_loudness) {
540                 loudness_reader.reset (new LoudnessReader (config.format->sample_rate(), channels, max_samples));
541         }
542
543         normalizer.reset (new AudioGrapher::Normalizer (use_loudness ? 0.0 : config.format->normalize_dbfs()));
544         threader.reset (new Threader<Sample> (parent.thread_pool));
545         normalizer->alloc_buffer (max_samples_out);
546         normalizer->add_output (threader);
547
548         int format = ExportFormatBase::F_RAW | ExportFormatBase::SF_Float;
549
550         if (parent._realtime) {
551                 tmp_file.reset (new TmpFileRt<float> (&tmpfile_path_buf[0], format, channels, config.format->sample_rate()));
552         } else {
553                 tmp_file.reset (new TmpFileSync<float> (&tmpfile_path_buf[0], format, channels, config.format->sample_rate()));
554         }
555
556         tmp_file->FileWritten.connect_same_thread (post_processing_connection,
557                                                    boost::bind (&Intermediate::prepare_post_processing, this));
558         tmp_file->FileFlushed.connect_same_thread (post_processing_connection,
559                                                    boost::bind (&Intermediate::start_post_processing, this));
560
561         add_child (new_config);
562
563         if (use_loudness) {
564                 loudness_reader->add_output (tmp_file);
565         } else if (use_peak) {
566                 peak_reader->add_output (tmp_file);
567         }
568 }
569
570 ExportGraphBuilder::FloatSinkPtr
571 ExportGraphBuilder::Intermediate::sink ()
572 {
573         if (use_loudness) {
574                 return loudness_reader;
575         } else if (use_peak) {
576                 return peak_reader;
577         }
578         return tmp_file;
579 }
580
581 void
582 ExportGraphBuilder::Intermediate::add_child (FileSpec const & new_config)
583 {
584         for (boost::ptr_list<SFC>::iterator it = children.begin(); it != children.end(); ++it) {
585                 if (*it == new_config) {
586                         it->add_child (new_config);
587                         return;
588                 }
589         }
590
591         children.push_back (new SFC (parent, new_config, max_samples_out));
592         threader->add_output (children.back().sink());
593 }
594
595 void
596 ExportGraphBuilder::Intermediate::remove_children (bool remove_out_files)
597 {
598         boost::ptr_list<SFC>::iterator iter = children.begin ();
599
600         while (iter != children.end() ) {
601                 iter->remove_children (remove_out_files);
602                 iter = children.erase (iter);
603         }
604 }
605
606 bool
607 ExportGraphBuilder::Intermediate::operator== (FileSpec const & other_config) const
608 {
609         return config.format->normalize() == other_config.format->normalize() &&
610                 config.format->normalize_loudness () == other_config.format->normalize_loudness() &&
611                 (
612                  (!config.format->normalize_loudness () && config.format->normalize_dbfs() == other_config.format->normalize_dbfs())
613                  ||
614                  // FIXME: allow simultaneous export of two formats with different loundness normalization settings
615                  (config.format->normalize_loudness () /* lufs/dbtp is a result option, not an instantaion option */)
616                 );
617 }
618
619 unsigned
620 ExportGraphBuilder::Intermediate::get_postprocessing_cycle_count() const
621 {
622         return static_cast<unsigned>(std::ceil(static_cast<float>(tmp_file->get_samples_written()) /
623                                                max_samples_out));
624 }
625
626 bool
627 ExportGraphBuilder::Intermediate::process()
628 {
629         samplecnt_t samples_read = tmp_file->read (*buffer);
630         return samples_read != buffer->samples();
631 }
632
633 void
634 ExportGraphBuilder::Intermediate::prepare_post_processing()
635 {
636         // called in sync rt-context
637         float gain;
638         if (use_loudness) {
639                 gain = normalizer->set_peak (loudness_reader->get_peak (config.format->normalize_lufs (), config.format->normalize_dbtp ()));
640         } else if (use_peak) {
641                 gain = normalizer->set_peak (peak_reader->get_peak());
642         } else {
643                 gain = normalizer->set_peak (0.0);
644         }
645         if (use_loudness || use_peak) {
646                 // push info to analyzers
647                 for (boost::ptr_list<SFC>::iterator i = children.begin(); i != children.end(); ++i) {
648                         (*i).set_peak (gain);
649                 }
650         }
651         tmp_file->add_output (normalizer);
652         parent.intermediates.push_back (this);
653 }
654
655 void
656 ExportGraphBuilder::Intermediate::start_post_processing()
657 {
658         // called in disk-thread (when exporting in realtime)
659         tmp_file->seek (0, SEEK_SET);
660         if (!AudioEngine::instance()->freewheeling ()) {
661                 AudioEngine::instance()->freewheel (true);
662         }
663 }
664
665 /* SRC */
666
667 ExportGraphBuilder::SRC::SRC (ExportGraphBuilder & parent, FileSpec const & new_config, samplecnt_t max_samples)
668         : parent (parent)
669 {
670         config = new_config;
671         converter.reset (new SampleRateConverter (new_config.channel_config->get_n_chans()));
672         ExportFormatSpecification & format = *new_config.format;
673         converter->init (parent.session.nominal_sample_rate(), format.sample_rate(), format.src_quality());
674         max_samples_out = converter->allocate_buffers (max_samples);
675
676         add_child (new_config);
677 }
678
679 ExportGraphBuilder::FloatSinkPtr
680 ExportGraphBuilder::SRC::sink ()
681 {
682         return converter;
683 }
684
685 void
686 ExportGraphBuilder::SRC::add_child (FileSpec const & new_config)
687 {
688         if (new_config.format->normalize() || parent._realtime) {
689                 add_child_to_list (new_config, intermediate_children);
690         } else {
691                 add_child_to_list (new_config, children);
692         }
693 }
694
695 void
696 ExportGraphBuilder::SRC::remove_children (bool remove_out_files)
697 {
698         boost::ptr_list<SFC>::iterator sfc_iter = children.begin();
699
700         while (sfc_iter != children.end() ) {
701                 converter->remove_output (sfc_iter->sink() );
702                 sfc_iter->remove_children (remove_out_files);
703                 sfc_iter = children.erase (sfc_iter);
704         }
705
706         boost::ptr_list<Intermediate>::iterator norm_iter = intermediate_children.begin();
707
708         while (norm_iter != intermediate_children.end() ) {
709                 converter->remove_output (norm_iter->sink() );
710                 norm_iter->remove_children (remove_out_files);
711                 norm_iter = intermediate_children.erase (norm_iter);
712         }
713
714 }
715
716 template<typename T>
717 void
718 ExportGraphBuilder::SRC::add_child_to_list (FileSpec const & new_config, boost::ptr_list<T> & list)
719 {
720         for (typename boost::ptr_list<T>::iterator it = list.begin(); it != list.end(); ++it) {
721                 if (*it == new_config) {
722                         it->add_child (new_config);
723                         return;
724                 }
725         }
726
727         list.push_back (new T (parent, new_config, max_samples_out));
728         converter->add_output (list.back().sink ());
729 }
730
731 bool
732 ExportGraphBuilder::SRC::operator== (FileSpec const & other_config) const
733 {
734         return config.format->sample_rate() == other_config.format->sample_rate();
735 }
736
737 /* SilenceHandler */
738 ExportGraphBuilder::SilenceHandler::SilenceHandler (ExportGraphBuilder & parent, FileSpec const & new_config, samplecnt_t max_samples)
739         : parent (parent)
740 {
741         config = new_config;
742         max_samples_in = max_samples;
743         samplecnt_t sample_rate = parent.session.nominal_sample_rate();
744
745         /* work around partsing "-inf" config to "0" -- 7b1f97b
746          * silence trim 0dBFS makes no sense, anyway.
747          */
748         float est = Config->get_export_silence_threshold ();
749         if (est >= 0.f) est = -INFINITY;
750 #ifdef MIXBUS
751         // Mixbus channelstrip always dithers the signal, cut above dither level
752         silence_trimmer.reset (new SilenceTrimmer<Sample>(max_samples_in, std::max (-90.f, est)));
753 #else
754         // TODO silence-threshold should be per export-preset, with Config->get_silence_threshold being the default
755         silence_trimmer.reset (new SilenceTrimmer<Sample>(max_samples_in, est));
756 #endif
757         silence_trimmer->set_trim_beginning (config.format->trim_beginning());
758         silence_trimmer->set_trim_end (config.format->trim_end());
759
760         samplecnt_t sb = config.format->silence_beginning_at (parent.timespan->get_start(), sample_rate);
761         samplecnt_t se = config.format->silence_end_at (parent.timespan->get_end(), sample_rate);
762
763         silence_trimmer->add_silence_to_beginning (sb);
764         silence_trimmer->add_silence_to_end (se);
765
766         add_child (new_config);
767 }
768
769 ExportGraphBuilder::FloatSinkPtr
770 ExportGraphBuilder::SilenceHandler::sink ()
771 {
772         return silence_trimmer;
773 }
774
775 void
776 ExportGraphBuilder::SilenceHandler::add_child (FileSpec const & new_config)
777 {
778         for (boost::ptr_list<SRC>::iterator it = children.begin(); it != children.end(); ++it) {
779                 if (*it == new_config) {
780                         it->add_child (new_config);
781                         return;
782                 }
783         }
784
785         children.push_back (new SRC (parent, new_config, max_samples_in));
786         silence_trimmer->add_output (children.back().sink());
787 }
788
789 void
790 ExportGraphBuilder::SilenceHandler::remove_children (bool remove_out_files)
791 {
792         boost::ptr_list<SRC>::iterator iter = children.begin();
793
794         while (iter != children.end() ) {
795                 silence_trimmer->remove_output (iter->sink() );
796                 iter->remove_children (remove_out_files);
797                 iter = children.erase (iter);
798         }
799 }
800
801 bool
802 ExportGraphBuilder::SilenceHandler::operator== (FileSpec const & other_config) const
803 {
804         ExportFormatSpecification & format = *config.format;
805         ExportFormatSpecification & other_format = *other_config.format;
806         return (format.trim_beginning() == other_format.trim_beginning()) &&
807                 (format.trim_end() == other_format.trim_end()) &&
808                 (format.silence_beginning_time() == other_format.silence_beginning_time()) &&
809                 (format.silence_end_time() == other_format.silence_end_time());
810 }
811
812 /* ChannelConfig */
813
814 ExportGraphBuilder::ChannelConfig::ChannelConfig (ExportGraphBuilder & parent, FileSpec const & new_config, ChannelMap & channel_map)
815         : parent (parent)
816 {
817         typedef ExportChannelConfiguration::ChannelList ChannelList;
818
819         config = new_config;
820
821         samplecnt_t max_samples = parent.session.engine().samples_per_cycle();
822         interleaver.reset (new Interleaver<Sample> ());
823         interleaver->init (new_config.channel_config->get_n_chans(), max_samples);
824
825         // Make the chunk size divisible by the channel count
826         int chan_count = new_config.channel_config->get_n_chans();
827         max_samples_out = 8192;
828         if (chan_count > 0) {
829                 max_samples_out -= max_samples_out % chan_count;
830         }
831         chunker.reset (new Chunker<Sample> (max_samples_out));
832         interleaver->add_output(chunker);
833
834         ChannelList const & channel_list = config.channel_config->get_channels();
835         unsigned chan = 0;
836         for (ChannelList::const_iterator it = channel_list.begin(); it != channel_list.end(); ++it, ++chan) {
837                 ChannelMap::iterator map_it = channel_map.find (*it);
838                 if (map_it == channel_map.end()) {
839                         std::pair<ChannelMap::iterator, bool> result_pair =
840                                 channel_map.insert (std::make_pair (*it, IdentityVertexPtr (new IdentityVertex<Sample> ())));
841                         assert (result_pair.second);
842                         map_it = result_pair.first;
843                 }
844                 map_it->second->add_output (interleaver->input (chan));
845         }
846
847         add_child (new_config);
848 }
849
850 void
851 ExportGraphBuilder::ChannelConfig::add_child (FileSpec const & new_config)
852 {
853         assert (*this == new_config);
854
855         for (boost::ptr_list<SilenceHandler>::iterator it = children.begin(); it != children.end(); ++it) {
856                 if (*it == new_config) {
857                         it->add_child (new_config);
858                         return;
859                 }
860         }
861
862         children.push_back (new SilenceHandler (parent, new_config, max_samples_out));
863         chunker->add_output (children.back().sink ());
864 }
865
866 void
867 ExportGraphBuilder::ChannelConfig::remove_children (bool remove_out_files)
868 {
869         boost::ptr_list<SilenceHandler>::iterator iter = children.begin();
870
871         while(iter != children.end() ) {
872
873                 chunker->remove_output (iter->sink ());
874                 iter->remove_children (remove_out_files);
875                 iter = children.erase(iter);
876         }
877 }
878
879 bool
880 ExportGraphBuilder::ChannelConfig::operator== (FileSpec const & other_config) const
881 {
882         return config.channel_config == other_config.channel_config;
883 }
884
885 } // namespace ARDOUR