fix file-name reported to analyzer when stem-exporting
[ardour.git] / libs / ardour / export_graph_builder.cc
1 /*
2   Copyright (C) 2008-2012 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_graph_builder.h"
22
23 #include <vector>
24
25 #include <glibmm/miscutils.h>
26
27 #include "audiographer/process_context.h"
28 #include "audiographer/general/chunker.h"
29 #include "audiographer/general/interleaver.h"
30 #include "audiographer/general/normalizer.h"
31 #include "audiographer/general/analyser.h"
32 #include "audiographer/general/peak_reader.h"
33 #include "audiographer/general/loudness_reader.h"
34 #include "audiographer/general/sample_format_converter.h"
35 #include "audiographer/general/sr_converter.h"
36 #include "audiographer/general/silence_trimmer.h"
37 #include "audiographer/general/threader.h"
38 #include "audiographer/sndfile/tmp_file.h"
39 #include "audiographer/sndfile/tmp_file_rt.h"
40 #include "audiographer/sndfile/sndfile_writer.h"
41
42 #include "ardour/audioengine.h"
43 #include "ardour/export_channel_configuration.h"
44 #include "ardour/export_filename.h"
45 #include "ardour/export_format_specification.h"
46 #include "ardour/export_timespan.h"
47 #include "ardour/session_directory.h"
48 #include "ardour/sndfile_helpers.h"
49
50 #include "pbd/file_utils.h"
51 #include "pbd/cpus.h"
52
53 using namespace AudioGrapher;
54 using std::string;
55
56 namespace ARDOUR {
57
58 ExportGraphBuilder::ExportGraphBuilder (Session const & session)
59         : session (session)
60         , thread_pool (hardware_concurrency())
61 {
62         process_buffer_frames = session.engine().samples_per_cycle();
63 }
64
65 ExportGraphBuilder::~ExportGraphBuilder ()
66 {
67 }
68
69 int
70 ExportGraphBuilder::process (framecnt_t frames, bool last_cycle)
71 {
72         assert(frames <= process_buffer_frames);
73
74         for (ChannelMap::iterator it = channels.begin(); it != channels.end(); ++it) {
75                 Sample const * process_buffer = 0;
76                 it->first->read (process_buffer, frames);
77                 ConstProcessContext<Sample> context(process_buffer, frames, 1);
78                 if (last_cycle) { context().set_flag (ProcessContext<Sample>::EndOfInput); }
79                 it->second->process (context);
80         }
81
82         return 0;
83 }
84
85 bool
86 ExportGraphBuilder::process_normalize ()
87 {
88         for (std::list<Normalizer *>::iterator it = normalizers.begin(); it != normalizers.end(); /* ++ in loop */) {
89                 if ((*it)->process()) {
90                         it = normalizers.erase (it);
91                 } else {
92                         ++it;
93                 }
94         }
95
96         return normalizers.empty();
97 }
98
99 unsigned
100 ExportGraphBuilder::get_normalize_cycle_count() const
101 {
102         unsigned max = 0;
103         for (std::list<Normalizer *>::const_iterator it = normalizers.begin(); it != normalizers.end(); ++it) {
104                 max = std::max(max, (*it)->get_normalize_cycle_count());
105         }
106         return max;
107 }
108
109 void
110 ExportGraphBuilder::reset ()
111 {
112         timespan.reset();
113         channel_configs.clear ();
114         channels.clear ();
115         normalizers.clear ();
116         analysis_map.clear();
117 }
118
119 void
120 ExportGraphBuilder::cleanup (bool remove_out_files/*=false*/)
121 {
122         ChannelConfigList::iterator iter = channel_configs.begin();
123
124         while (iter != channel_configs.end() ) {
125                 iter->remove_children(remove_out_files);
126                 iter = channel_configs.erase(iter);
127         }
128 }
129
130 void
131 ExportGraphBuilder::set_current_timespan (boost::shared_ptr<ExportTimespan> span)
132 {
133         timespan = span;
134 }
135
136 void
137 ExportGraphBuilder::add_config (FileSpec const & config)
138 {
139         ExportChannelConfiguration::ChannelList const & channels =
140                 config.channel_config->get_channels();
141         for(ExportChannelConfiguration::ChannelList::const_iterator it = channels.begin();
142             it != channels.end(); ++it) {
143                 (*it)->set_max_buffer_size(process_buffer_frames);
144         }
145
146         // If the sample rate is "session rate", change it to the real value.
147         // However, we need to copy it to not change the config which is saved...
148         FileSpec new_config (config);
149         new_config.format.reset(new ExportFormatSpecification(*new_config.format, false));
150         if(new_config.format->sample_rate() == ExportFormatBase::SR_Session) {
151                 framecnt_t session_rate = session.nominal_frame_rate();
152                 new_config.format->set_sample_rate(ExportFormatBase::nearest_sample_rate(session_rate));
153         }
154
155
156         if (!new_config.channel_config->get_split ()) {
157                 add_split_config (new_config);
158                 return;
159         }
160
161         // Split channel configurations are split into several channel configurations,
162         // each corresponding to a file, at this stage
163         typedef std::list<boost::shared_ptr<ExportChannelConfiguration> > ConfigList;
164         ConfigList file_configs;
165         new_config.channel_config->configurations_for_files (file_configs);
166
167         unsigned chan = 1;
168         for (ConfigList::iterator it = file_configs.begin(); it != file_configs.end(); ++it, ++chan) {
169                 FileSpec copy = new_config;
170                 copy.channel_config = *it;
171
172                 copy.filename.reset (new ExportFilename (*copy.filename));
173                 copy.filename->include_channel = true;
174                 copy.filename->set_channel (chan);
175
176                 add_split_config (copy);
177         }
178 }
179
180 void
181 ExportGraphBuilder::get_analysis_results (AnalysisResults& results) {
182         for (AnalysisMap::iterator i = analysis_map.begin(); i != analysis_map.end(); ++i) {
183                 ExportAnalysisPtr p = i->second->result ();
184                 if (p) {
185                         results.insert (std::make_pair (i->first, p));
186                 }
187         }
188 }
189
190 void
191 ExportGraphBuilder::add_split_config (FileSpec const & config)
192 {
193         for (ChannelConfigList::iterator it = channel_configs.begin(); it != channel_configs.end(); ++it) {
194                 if (*it == config) {
195                         it->add_child (config);
196                         return;
197                 }
198         }
199
200         // No duplicate channel config found, create new one
201         channel_configs.push_back (new ChannelConfig (*this, config, channels));
202 }
203
204 /* Encoder */
205
206 template <>
207 boost::shared_ptr<AudioGrapher::Sink<Sample> >
208 ExportGraphBuilder::Encoder::init (FileSpec const & new_config)
209 {
210         config = new_config;
211         init_writer (float_writer);
212         return float_writer;
213 }
214
215 template <>
216 boost::shared_ptr<AudioGrapher::Sink<int> >
217 ExportGraphBuilder::Encoder::init (FileSpec const & new_config)
218 {
219         config = new_config;
220         init_writer (int_writer);
221         return int_writer;
222 }
223
224 template <>
225 boost::shared_ptr<AudioGrapher::Sink<short> >
226 ExportGraphBuilder::Encoder::init (FileSpec const & new_config)
227 {
228         config = new_config;
229         init_writer (short_writer);
230         return short_writer;
231 }
232
233 void
234 ExportGraphBuilder::Encoder::add_child (FileSpec const & new_config)
235 {
236         filenames.push_back (new_config.filename);
237 }
238
239 void
240 ExportGraphBuilder::Encoder::destroy_writer (bool delete_out_file)
241 {
242         if (delete_out_file ) {
243
244                 if (float_writer) {
245                         float_writer->close ();
246                 }
247
248                 if (int_writer) {
249                         int_writer->close ();
250                 }
251
252                 if (short_writer) {
253                         short_writer->close ();
254                 }
255
256                 if (std::remove(writer_filename.c_str() ) != 0) {
257                         std::cout << "Encoder::destroy_writer () : Error removing file: " << strerror(errno) << std::endl;
258                 }
259         }
260
261         float_writer.reset ();
262         int_writer.reset ();
263         short_writer.reset ();
264 }
265
266 bool
267 ExportGraphBuilder::Encoder::operator== (FileSpec const & other_config) const
268 {
269         return get_real_format (config) == get_real_format (other_config);
270 }
271
272 int
273 ExportGraphBuilder::Encoder::get_real_format (FileSpec const & config)
274 {
275         ExportFormatSpecification & format = *config.format;
276         return format.format_id() | format.sample_format() | format.endianness();
277 }
278
279 template<typename T>
280 void
281 ExportGraphBuilder::Encoder::init_writer (boost::shared_ptr<AudioGrapher::SndfileWriter<T> > & writer)
282 {
283         unsigned channels = config.channel_config->get_n_chans();
284         int format = get_real_format (config);
285         config.filename->set_channel_config(config.channel_config);
286         writer_filename = config.filename->get_path (config.format);
287
288         writer.reset (new AudioGrapher::SndfileWriter<T> (writer_filename, format, channels, config.format->sample_rate(), config.broadcast_info));
289         writer->FileWritten.connect_same_thread (copy_files_connection, boost::bind (&ExportGraphBuilder::Encoder::copy_files, this, _1));
290 }
291
292 void
293 ExportGraphBuilder::Encoder::copy_files (std::string orig_path)
294 {
295         while (filenames.size()) {
296                 ExportFilenamePtr & filename = filenames.front();
297                 PBD::copy_file (orig_path, filename->get_path (config.format).c_str());
298                 filenames.pop_front();
299         }
300 }
301
302 /* SFC */
303
304 ExportGraphBuilder::SFC::SFC (ExportGraphBuilder &parent, FileSpec const & new_config, framecnt_t max_frames)
305         : data_width(0)
306 {
307         config = new_config;
308         data_width = sndfile_data_width (Encoder::get_real_format (config));
309         unsigned channels = new_config.channel_config->get_n_chans();
310         _analyse = config.format->analyse();
311         if (_analyse) {
312                 framecnt_t sample_rate = parent.session.nominal_frame_rate();
313                 framecnt_t sb = config.format->silence_beginning_at (parent.timespan->get_start(), sample_rate);
314                 framecnt_t se = config.format->silence_end_at (parent.timespan->get_end(), sample_rate);
315                 framecnt_t duration = parent.timespan->get_length () + sb + se;
316                 max_frames = min ((framecnt_t) 8192 * channels, max ((framecnt_t) 4096 * channels, max_frames));
317                 chunker.reset (new Chunker<Sample> (max_frames));
318                 analyser.reset (new Analyser (config.format->sample_rate(), channels, max_frames,
319                                         (framecnt_t) ceil (duration * config.format->sample_rate () / (double) sample_rate)));
320                 chunker->add_output (analyser);
321
322                 config.filename->set_channel_config (config.channel_config);
323                 parent.add_analyser (config.filename->get_path (config.format), analyser);
324         }
325
326         if (data_width == 8 || data_width == 16) {
327                 short_converter = ShortConverterPtr (new SampleFormatConverter<short> (channels));
328                 short_converter->init (max_frames, config.format->dither_type(), data_width);
329                 add_child (config);
330                 if (_analyse) { analyser->add_output (short_converter); }
331
332         } else if (data_width == 24 || data_width == 32) {
333                 int_converter = IntConverterPtr (new SampleFormatConverter<int> (channels));
334                 int_converter->init (max_frames, config.format->dither_type(), data_width);
335                 add_child (config);
336                 if (_analyse) { analyser->add_output (int_converter); }
337         } else {
338                 int actual_data_width = 8 * sizeof(Sample);
339                 float_converter = FloatConverterPtr (new SampleFormatConverter<Sample> (channels));
340                 float_converter->init (max_frames, config.format->dither_type(), actual_data_width);
341                 add_child (config);
342                 if (_analyse) { analyser->add_output (float_converter); }
343         }
344 }
345
346 void
347 ExportGraphBuilder::SFC::set_peak (float gain)
348 {
349         if (_analyse) {
350                 analyser->set_normalization_gain (gain);
351         }
352 }
353
354 ExportGraphBuilder::FloatSinkPtr
355 ExportGraphBuilder::SFC::sink ()
356 {
357         if (_analyse) {
358                 return chunker;
359         } else if (data_width == 8 || data_width == 16) {
360                 return short_converter;
361         } else if (data_width == 24 || data_width == 32) {
362                 return int_converter;
363         } else {
364                 return float_converter;
365         }
366 }
367
368 void
369 ExportGraphBuilder::SFC::add_child (FileSpec const & new_config)
370 {
371         for (boost::ptr_list<Encoder>::iterator it = children.begin(); it != children.end(); ++it) {
372                 if (*it == new_config) {
373                         it->add_child (new_config);
374                         return;
375                 }
376         }
377
378         children.push_back (new Encoder());
379         Encoder & encoder = children.back();
380
381         if (data_width == 8 || data_width == 16) {
382                 short_converter->add_output (encoder.init<short> (new_config));
383         } else if (data_width == 24 || data_width == 32) {
384                 int_converter->add_output (encoder.init<int> (new_config));
385         } else {
386                 float_converter->add_output (encoder.init<Sample> (new_config));
387         }
388 }
389
390 void
391 ExportGraphBuilder::SFC::remove_children (bool remove_out_files)
392 {
393         boost::ptr_list<Encoder>::iterator iter = children.begin ();
394
395         while (iter != children.end() ) {
396
397                 if (remove_out_files) {
398                         iter->destroy_writer(remove_out_files);
399                 }
400                 iter = children.erase (iter);
401         }
402 }
403
404 bool
405 ExportGraphBuilder::SFC::operator== (FileSpec const & other_config) const
406 {
407         return config.format->sample_format() == other_config.format->sample_format();
408 }
409
410 /* Normalizer */
411
412 ExportGraphBuilder::Normalizer::Normalizer (ExportGraphBuilder & parent, FileSpec const & new_config, framecnt_t max_frames)
413         : parent (parent)
414         , use_loudness (false)
415 {
416         std::string tmpfile_path = parent.session.session_directory().export_path();
417         tmpfile_path = Glib::build_filename(tmpfile_path, "XXXXXX");
418         std::vector<char> tmpfile_path_buf(tmpfile_path.size() + 1);
419         std::copy(tmpfile_path.begin(), tmpfile_path.end(), tmpfile_path_buf.begin());
420         tmpfile_path_buf[tmpfile_path.size()] = '\0';
421
422         config = new_config;
423         uint32_t const channels = config.channel_config->get_n_chans();
424         max_frames_out = 4086 - (4086 % channels); // TODO good chunk size
425         use_loudness = config.format->normalize_loudness ();
426
427         buffer.reset (new AllocatingProcessContext<Sample> (max_frames_out, channels));
428         peak_reader.reset (new PeakReader ());
429         loudness_reader.reset (new LoudnessReader (config.format->sample_rate(), channels, max_frames));
430         normalizer.reset (new AudioGrapher::Normalizer (use_loudness ? 0.0 : config.format->normalize_dbfs()));
431         threader.reset (new Threader<Sample> (parent.thread_pool));
432
433         normalizer->alloc_buffer (max_frames_out);
434         normalizer->add_output (threader);
435
436         int format = ExportFormatBase::F_RAW | ExportFormatBase::SF_Float;
437         tmp_file.reset (new TmpFile<float> (&tmpfile_path_buf[0], format, channels, config.format->sample_rate()));
438         tmp_file->FileWritten.connect_same_thread (post_processing_connection,
439                                                    boost::bind (&Normalizer::start_post_processing, this));
440
441         add_child (new_config);
442
443         if (use_loudness) {
444                 loudness_reader->add_output (tmp_file);
445         } else {
446                 peak_reader->add_output (tmp_file);
447         }
448 }
449
450 ExportGraphBuilder::FloatSinkPtr
451 ExportGraphBuilder::Normalizer::sink ()
452 {
453         if (use_loudness) {
454                 return loudness_reader;
455         }
456         return peak_reader;
457 }
458
459 void
460 ExportGraphBuilder::Normalizer::add_child (FileSpec const & new_config)
461 {
462         for (boost::ptr_list<SFC>::iterator it = children.begin(); it != children.end(); ++it) {
463                 if (*it == new_config) {
464                         it->add_child (new_config);
465                         return;
466                 }
467         }
468
469         children.push_back (new SFC (parent, new_config, max_frames_out));
470         threader->add_output (children.back().sink());
471 }
472
473 void
474 ExportGraphBuilder::Normalizer::remove_children (bool remove_out_files)
475 {
476         boost::ptr_list<SFC>::iterator iter = children.begin ();
477
478         while (iter != children.end() ) {
479                 iter->remove_children (remove_out_files);
480                 iter = children.erase (iter);
481         }
482 }
483
484 bool
485 ExportGraphBuilder::Normalizer::operator== (FileSpec const & other_config) const
486 {
487         return config.format->normalize() == other_config.format->normalize() &&
488                 config.format->normalize_loudness () == other_config.format->normalize_loudness() &&
489                 (
490                  (!config.format->normalize_loudness () && config.format->normalize_dbfs() == other_config.format->normalize_dbfs())
491                  ||
492                  // FIXME: allow simultaneous export of two formats with different loundness normalization settings
493                  (config.format->normalize_loudness () /* lufs/dbtp is a result option, not an instantaion option */)
494                 );
495 }
496
497 unsigned
498 ExportGraphBuilder::Normalizer::get_normalize_cycle_count() const
499 {
500         return static_cast<unsigned>(std::ceil(static_cast<float>(tmp_file->get_frames_written()) /
501                                                max_frames_out));
502 }
503
504 bool
505 ExportGraphBuilder::Normalizer::process()
506 {
507         framecnt_t frames_read = tmp_file->read (*buffer);
508         return frames_read != buffer->frames();
509 }
510
511 void
512 ExportGraphBuilder::Normalizer::start_post_processing()
513 {
514         float gain;
515         if (use_loudness) {
516                 gain = normalizer->set_peak (loudness_reader->get_peak (config.format->normalize_lufs (), config.format->normalize_dbtp ()));
517         } else {
518                 gain = normalizer->set_peak (peak_reader->get_peak());
519         }
520         for (boost::ptr_list<SFC>::iterator i = children.begin(); i != children.end(); ++i) {
521                 (*i).set_peak (gain);
522         }
523         tmp_file->seek (0, SEEK_SET);
524         tmp_file->add_output (normalizer);
525         parent.normalizers.push_back (this);
526 }
527
528 /* SRC */
529
530 ExportGraphBuilder::SRC::SRC (ExportGraphBuilder & parent, FileSpec const & new_config, framecnt_t max_frames)
531         : parent (parent)
532 {
533         config = new_config;
534         converter.reset (new SampleRateConverter (new_config.channel_config->get_n_chans()));
535         ExportFormatSpecification & format = *new_config.format;
536         converter->init (parent.session.nominal_frame_rate(), format.sample_rate(), format.src_quality());
537         max_frames_out = converter->allocate_buffers (max_frames);
538
539         add_child (new_config);
540 }
541
542 ExportGraphBuilder::FloatSinkPtr
543 ExportGraphBuilder::SRC::sink ()
544 {
545         return converter;
546 }
547
548 void
549 ExportGraphBuilder::SRC::add_child (FileSpec const & new_config)
550 {
551         if (new_config.format->normalize()) {
552                 add_child_to_list (new_config, normalized_children);
553         } else {
554                 add_child_to_list (new_config, children);
555         }
556 }
557
558 void
559 ExportGraphBuilder::SRC::remove_children (bool remove_out_files)
560 {
561         boost::ptr_list<SFC>::iterator sfc_iter = children.begin();
562
563         while (sfc_iter != children.end() ) {
564                 converter->remove_output (sfc_iter->sink() );
565                 sfc_iter->remove_children (remove_out_files);
566                 sfc_iter = children.erase (sfc_iter);
567         }
568
569         boost::ptr_list<Normalizer>::iterator norm_iter = normalized_children.begin();
570
571         while (norm_iter != normalized_children.end() ) {
572                 converter->remove_output (norm_iter->sink() );
573                 norm_iter->remove_children (remove_out_files);
574                 norm_iter = normalized_children.erase (norm_iter);
575         }
576
577 }
578
579 template<typename T>
580 void
581 ExportGraphBuilder::SRC::add_child_to_list (FileSpec const & new_config, boost::ptr_list<T> & list)
582 {
583         for (typename boost::ptr_list<T>::iterator it = list.begin(); it != list.end(); ++it) {
584                 if (*it == new_config) {
585                         it->add_child (new_config);
586                         return;
587                 }
588         }
589
590         list.push_back (new T (parent, new_config, max_frames_out));
591         converter->add_output (list.back().sink ());
592 }
593
594 bool
595 ExportGraphBuilder::SRC::operator== (FileSpec const & other_config) const
596 {
597         return config.format->sample_rate() == other_config.format->sample_rate();
598 }
599
600 /* SilenceHandler */
601 ExportGraphBuilder::SilenceHandler::SilenceHandler (ExportGraphBuilder & parent, FileSpec const & new_config, framecnt_t max_frames)
602         : parent (parent)
603 {
604         config = new_config;
605         max_frames_in = max_frames;
606         framecnt_t sample_rate = parent.session.nominal_frame_rate();
607
608 #ifdef MIXBUS
609         silence_trimmer.reset (new SilenceTrimmer<Sample>(max_frames_in, -90));
610 #else
611         // TODO silence-threshold should be per export-preset, with Config->get_silence_threshold being the default
612         silence_trimmer.reset (new SilenceTrimmer<Sample>(max_frames_in, Config->get_export_silence_threshold ()));
613 #endif
614         silence_trimmer->set_trim_beginning (config.format->trim_beginning());
615         silence_trimmer->set_trim_end (config.format->trim_end());
616
617         framecnt_t sb = config.format->silence_beginning_at (parent.timespan->get_start(), sample_rate);
618         framecnt_t se = config.format->silence_end_at (parent.timespan->get_end(), sample_rate);
619
620         silence_trimmer->add_silence_to_beginning (sb);
621         silence_trimmer->add_silence_to_end (se);
622
623         add_child (new_config);
624 }
625
626 ExportGraphBuilder::FloatSinkPtr
627 ExportGraphBuilder::SilenceHandler::sink ()
628 {
629         return silence_trimmer;
630 }
631
632 void
633 ExportGraphBuilder::SilenceHandler::add_child (FileSpec const & new_config)
634 {
635         for (boost::ptr_list<SRC>::iterator it = children.begin(); it != children.end(); ++it) {
636                 if (*it == new_config) {
637                         it->add_child (new_config);
638                         return;
639                 }
640         }
641
642         children.push_back (new SRC (parent, new_config, max_frames_in));
643         silence_trimmer->add_output (children.back().sink());
644 }
645
646 void
647 ExportGraphBuilder::SilenceHandler::remove_children (bool remove_out_files)
648 {
649         boost::ptr_list<SRC>::iterator iter = children.begin();
650
651         while (iter != children.end() ) {
652                 silence_trimmer->remove_output (iter->sink() );
653                 iter->remove_children (remove_out_files);
654                 iter = children.erase (iter);
655         }
656 }
657
658 bool
659 ExportGraphBuilder::SilenceHandler::operator== (FileSpec const & other_config) const
660 {
661         ExportFormatSpecification & format = *config.format;
662         ExportFormatSpecification & other_format = *other_config.format;
663         return (format.trim_beginning() == other_format.trim_beginning()) &&
664                 (format.trim_end() == other_format.trim_end()) &&
665                 (format.silence_beginning_time() == other_format.silence_beginning_time()) &&
666                 (format.silence_end_time() == other_format.silence_end_time());
667 }
668
669 /* ChannelConfig */
670
671 ExportGraphBuilder::ChannelConfig::ChannelConfig (ExportGraphBuilder & parent, FileSpec const & new_config, ChannelMap & channel_map)
672         : parent (parent)
673 {
674         typedef ExportChannelConfiguration::ChannelList ChannelList;
675
676         config = new_config;
677
678         framecnt_t max_frames = parent.session.engine().samples_per_cycle();
679         interleaver.reset (new Interleaver<Sample> ());
680         interleaver->init (new_config.channel_config->get_n_chans(), max_frames);
681
682         // Make the chunk size divisible by the channel count
683         int chan_count = new_config.channel_config->get_n_chans();
684         max_frames_out = 8192;
685         if (chan_count > 0) {
686                 max_frames_out -= max_frames_out % chan_count;
687         }
688         chunker.reset (new Chunker<Sample> (max_frames_out));
689         interleaver->add_output(chunker);
690
691         ChannelList const & channel_list = config.channel_config->get_channels();
692         unsigned chan = 0;
693         for (ChannelList::const_iterator it = channel_list.begin(); it != channel_list.end(); ++it, ++chan) {
694                 ChannelMap::iterator map_it = channel_map.find (*it);
695                 if (map_it == channel_map.end()) {
696                         std::pair<ChannelMap::iterator, bool> result_pair =
697                                 channel_map.insert (std::make_pair (*it, IdentityVertexPtr (new IdentityVertex<Sample> ())));
698                         assert (result_pair.second);
699                         map_it = result_pair.first;
700                 }
701                 map_it->second->add_output (interleaver->input (chan));
702         }
703
704         add_child (new_config);
705 }
706
707 void
708 ExportGraphBuilder::ChannelConfig::add_child (FileSpec const & new_config)
709 {
710         assert (*this == new_config);
711
712         for (boost::ptr_list<SilenceHandler>::iterator it = children.begin(); it != children.end(); ++it) {
713                 if (*it == new_config) {
714                         it->add_child (new_config);
715                         return;
716                 }
717         }
718
719         children.push_back (new SilenceHandler (parent, new_config, max_frames_out));
720         chunker->add_output (children.back().sink ());
721 }
722
723 void
724 ExportGraphBuilder::ChannelConfig::remove_children (bool remove_out_files)
725 {
726         boost::ptr_list<SilenceHandler>::iterator iter = children.begin();
727
728         while(iter != children.end() ) {
729
730                 chunker->remove_output (iter->sink ());
731                 iter->remove_children (remove_out_files);
732                 iter = children.erase(iter);
733         }
734 }
735
736 bool
737 ExportGraphBuilder::ChannelConfig::operator== (FileSpec const & other_config) const
738 {
739         return config.channel_config == other_config.channel_config;
740 }
741
742 } // namespace ARDOUR