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