2 Copyright (C) 2008-2012 Paul Davis
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.
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.
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.
21 #include "ardour/export_graph_builder.h"
25 #include <glibmm/miscutils.h>
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/tmp_file_sync.h"
41 #include "audiographer/sndfile/sndfile_writer.h"
43 #include "ardour/audioengine.h"
44 #include "ardour/export_channel_configuration.h"
45 #include "ardour/export_filename.h"
46 #include "ardour/export_format_specification.h"
47 #include "ardour/export_timespan.h"
48 #include "ardour/session_directory.h"
49 #include "ardour/sndfile_helpers.h"
51 #include "pbd/file_utils.h"
54 using namespace AudioGrapher;
59 ExportGraphBuilder::ExportGraphBuilder (Session const & session)
61 , thread_pool (hardware_concurrency())
63 process_buffer_samples = session.engine().samples_per_cycle();
66 ExportGraphBuilder::~ExportGraphBuilder ()
71 ExportGraphBuilder::process (samplecnt_t samples, bool last_cycle)
73 assert(samples <= process_buffer_samples);
75 for (ChannelMap::iterator it = channels.begin(); it != channels.end(); ++it) {
76 Sample const * process_buffer = 0;
77 it->first->read (process_buffer, samples);
78 ConstProcessContext<Sample> context(process_buffer, samples, 1);
79 if (last_cycle) { context().set_flag (ProcessContext<Sample>::EndOfInput); }
80 it->second->process (context);
87 ExportGraphBuilder::post_process ()
89 for (std::list<Intermediate *>::iterator it = intermediates.begin(); it != intermediates.end(); /* ++ in loop */) {
90 if ((*it)->process()) {
91 it = intermediates.erase (it);
97 return intermediates.empty();
101 ExportGraphBuilder::get_postprocessing_cycle_count() const
104 for (std::list<Intermediate *>::const_iterator it = intermediates.begin(); it != intermediates.end(); ++it) {
105 max = std::max(max, (*it)->get_postprocessing_cycle_count());
111 ExportGraphBuilder::reset ()
114 channel_configs.clear ();
116 intermediates.clear ();
117 analysis_map.clear();
122 ExportGraphBuilder::cleanup (bool remove_out_files/*=false*/)
124 ChannelConfigList::iterator iter = channel_configs.begin();
126 while (iter != channel_configs.end() ) {
127 iter->remove_children(remove_out_files);
128 iter = channel_configs.erase(iter);
133 ExportGraphBuilder::set_current_timespan (boost::shared_ptr<ExportTimespan> span)
139 ExportGraphBuilder::add_config (FileSpec const & config, bool rt)
141 ExportChannelConfiguration::ChannelList const & channels =
142 config.channel_config->get_channels();
143 for(ExportChannelConfiguration::ChannelList::const_iterator it = channels.begin();
144 it != channels.end(); ++it) {
145 (*it)->set_max_buffer_size(process_buffer_samples);
150 // If the sample rate is "session rate", change it to the real value.
151 // However, we need to copy it to not change the config which is saved...
152 FileSpec new_config (config);
153 new_config.format.reset(new ExportFormatSpecification(*new_config.format, false));
154 if(new_config.format->sample_rate() == ExportFormatBase::SR_Session) {
155 samplecnt_t session_rate = session.nominal_sample_rate();
156 new_config.format->set_sample_rate(ExportFormatBase::nearest_sample_rate(session_rate));
160 if (!new_config.channel_config->get_split ()) {
161 add_split_config (new_config);
165 // Split channel configurations are split into several channel configurations,
166 // each corresponding to a file, at this stage
167 typedef std::list<boost::shared_ptr<ExportChannelConfiguration> > ConfigList;
168 ConfigList file_configs;
169 new_config.channel_config->configurations_for_files (file_configs);
172 for (ConfigList::iterator it = file_configs.begin(); it != file_configs.end(); ++it, ++chan) {
173 FileSpec copy = new_config;
174 copy.channel_config = *it;
176 copy.filename.reset (new ExportFilename (*copy.filename));
177 copy.filename->include_channel = true;
178 copy.filename->set_channel (chan);
180 add_split_config (copy);
185 ExportGraphBuilder::get_analysis_results (AnalysisResults& results) {
186 for (AnalysisMap::iterator i = analysis_map.begin(); i != analysis_map.end(); ++i) {
187 ExportAnalysisPtr p = i->second->result ();
189 results.insert (std::make_pair (i->first, p));
195 ExportGraphBuilder::add_split_config (FileSpec const & config)
197 for (ChannelConfigList::iterator it = channel_configs.begin(); it != channel_configs.end(); ++it) {
199 it->add_child (config);
204 // No duplicate channel config found, create new one
205 channel_configs.push_back (new ChannelConfig (*this, config, channels));
211 boost::shared_ptr<AudioGrapher::Sink<Sample> >
212 ExportGraphBuilder::Encoder::init (FileSpec const & new_config)
215 init_writer (float_writer);
220 boost::shared_ptr<AudioGrapher::Sink<int> >
221 ExportGraphBuilder::Encoder::init (FileSpec const & new_config)
224 init_writer (int_writer);
229 boost::shared_ptr<AudioGrapher::Sink<short> >
230 ExportGraphBuilder::Encoder::init (FileSpec const & new_config)
233 init_writer (short_writer);
238 ExportGraphBuilder::Encoder::add_child (FileSpec const & new_config)
240 filenames.push_back (new_config.filename);
244 ExportGraphBuilder::Encoder::destroy_writer (bool delete_out_file)
246 if (delete_out_file ) {
249 float_writer->close ();
253 int_writer->close ();
257 short_writer->close ();
260 if (std::remove(writer_filename.c_str() ) != 0) {
261 std::cout << "Encoder::destroy_writer () : Error removing file: " << strerror(errno) << std::endl;
265 float_writer.reset ();
267 short_writer.reset ();
271 ExportGraphBuilder::Encoder::operator== (FileSpec const & other_config) const
273 return get_real_format (config) == get_real_format (other_config);
277 ExportGraphBuilder::Encoder::get_real_format (FileSpec const & config)
279 ExportFormatSpecification & format = *config.format;
280 return format.format_id() | format.sample_format() | format.endianness();
285 ExportGraphBuilder::Encoder::init_writer (boost::shared_ptr<AudioGrapher::SndfileWriter<T> > & writer)
287 unsigned channels = config.channel_config->get_n_chans();
288 int format = get_real_format (config);
289 config.filename->set_channel_config(config.channel_config);
290 writer_filename = config.filename->get_path (config.format);
292 writer.reset (new AudioGrapher::SndfileWriter<T> (writer_filename, format, channels, config.format->sample_rate(), config.broadcast_info));
293 writer->FileWritten.connect_same_thread (copy_files_connection, boost::bind (&ExportGraphBuilder::Encoder::copy_files, this, _1));
297 ExportGraphBuilder::Encoder::copy_files (std::string orig_path)
299 while (filenames.size()) {
300 ExportFilenamePtr & filename = filenames.front();
301 PBD::copy_file (orig_path, filename->get_path (config.format).c_str());
302 filenames.pop_front();
308 ExportGraphBuilder::SFC::SFC (ExportGraphBuilder &parent, FileSpec const & new_config, samplecnt_t max_samples)
312 data_width = sndfile_data_width (Encoder::get_real_format (config));
313 unsigned channels = new_config.channel_config->get_n_chans();
314 _analyse = config.format->analyse();
316 samplecnt_t sample_rate = parent.session.nominal_sample_rate();
317 samplecnt_t sb = config.format->silence_beginning_at (parent.timespan->get_start(), sample_rate);
318 samplecnt_t se = config.format->silence_end_at (parent.timespan->get_end(), sample_rate);
319 samplecnt_t duration = parent.timespan->get_length () + sb + se;
320 max_samples = min ((samplecnt_t) 8192 * channels, max ((samplecnt_t) 4096 * channels, max_samples));
321 chunker.reset (new Chunker<Sample> (max_samples));
322 analyser.reset (new Analyser (config.format->sample_rate(), channels, max_samples,
323 (samplecnt_t) ceil (duration * config.format->sample_rate () / (double) sample_rate)));
324 chunker->add_output (analyser);
326 config.filename->set_channel_config (config.channel_config);
327 parent.add_analyser (config.filename->get_path (config.format), analyser);
330 if (data_width == 8 || data_width == 16) {
331 short_converter = ShortConverterPtr (new SampleFormatConverter<short> (channels));
332 short_converter->init (max_samples, config.format->dither_type(), data_width);
334 if (_analyse) { analyser->add_output (short_converter); }
336 } else if (data_width == 24 || data_width == 32) {
337 int_converter = IntConverterPtr (new SampleFormatConverter<int> (channels));
338 int_converter->init (max_samples, config.format->dither_type(), data_width);
340 if (_analyse) { analyser->add_output (int_converter); }
342 int actual_data_width = 8 * sizeof(Sample);
343 float_converter = FloatConverterPtr (new SampleFormatConverter<Sample> (channels));
344 float_converter->init (max_samples, config.format->dither_type(), actual_data_width);
346 if (_analyse) { analyser->add_output (float_converter); }
351 ExportGraphBuilder::SFC::set_peak (float gain)
354 analyser->set_normalization_gain (gain);
358 ExportGraphBuilder::FloatSinkPtr
359 ExportGraphBuilder::SFC::sink ()
363 } else if (data_width == 8 || data_width == 16) {
364 return short_converter;
365 } else if (data_width == 24 || data_width == 32) {
366 return int_converter;
368 return float_converter;
373 ExportGraphBuilder::SFC::add_child (FileSpec const & new_config)
375 for (boost::ptr_list<Encoder>::iterator it = children.begin(); it != children.end(); ++it) {
376 if (*it == new_config) {
377 it->add_child (new_config);
382 children.push_back (new Encoder());
383 Encoder & encoder = children.back();
385 if (data_width == 8 || data_width == 16) {
386 short_converter->add_output (encoder.init<short> (new_config));
387 } else if (data_width == 24 || data_width == 32) {
388 int_converter->add_output (encoder.init<int> (new_config));
390 float_converter->add_output (encoder.init<Sample> (new_config));
395 ExportGraphBuilder::SFC::remove_children (bool remove_out_files)
397 boost::ptr_list<Encoder>::iterator iter = children.begin ();
399 while (iter != children.end() ) {
401 if (remove_out_files) {
402 iter->destroy_writer(remove_out_files);
404 iter = children.erase (iter);
409 ExportGraphBuilder::SFC::operator== (FileSpec const & other_config) const
411 return config.format->sample_format() == other_config.format->sample_format();
414 /* Intermediate (Normalizer, TmpFile) */
416 ExportGraphBuilder::Intermediate::Intermediate (ExportGraphBuilder & parent, FileSpec const & new_config, samplecnt_t max_samples)
418 , use_loudness (false)
421 std::string tmpfile_path = parent.session.session_directory().export_path();
422 tmpfile_path = Glib::build_filename(tmpfile_path, "XXXXXX");
423 std::vector<char> tmpfile_path_buf(tmpfile_path.size() + 1);
424 std::copy(tmpfile_path.begin(), tmpfile_path.end(), tmpfile_path_buf.begin());
425 tmpfile_path_buf[tmpfile_path.size()] = '\0';
428 uint32_t const channels = config.channel_config->get_n_chans();
429 max_samples_out = 4086 - (4086 % channels); // TODO good chunk size
430 use_loudness = config.format->normalize_loudness ();
431 use_peak = config.format->normalize ();
433 buffer.reset (new AllocatingProcessContext<Sample> (max_samples_out, channels));
436 peak_reader.reset (new PeakReader ());
439 loudness_reader.reset (new LoudnessReader (config.format->sample_rate(), channels, max_samples));
442 normalizer.reset (new AudioGrapher::Normalizer (use_loudness ? 0.0 : config.format->normalize_dbfs()));
443 threader.reset (new Threader<Sample> (parent.thread_pool));
444 normalizer->alloc_buffer (max_samples_out);
445 normalizer->add_output (threader);
447 int format = ExportFormatBase::F_RAW | ExportFormatBase::SF_Float;
449 if (parent._realtime) {
450 tmp_file.reset (new TmpFileRt<float> (&tmpfile_path_buf[0], format, channels, config.format->sample_rate()));
452 tmp_file.reset (new TmpFileSync<float> (&tmpfile_path_buf[0], format, channels, config.format->sample_rate()));
455 tmp_file->FileWritten.connect_same_thread (post_processing_connection,
456 boost::bind (&Intermediate::prepare_post_processing, this));
457 tmp_file->FileFlushed.connect_same_thread (post_processing_connection,
458 boost::bind (&Intermediate::start_post_processing, this));
460 add_child (new_config);
463 loudness_reader->add_output (tmp_file);
464 } else if (use_peak) {
465 peak_reader->add_output (tmp_file);
469 ExportGraphBuilder::FloatSinkPtr
470 ExportGraphBuilder::Intermediate::sink ()
473 return loudness_reader;
474 } else if (use_peak) {
481 ExportGraphBuilder::Intermediate::add_child (FileSpec const & new_config)
483 for (boost::ptr_list<SFC>::iterator it = children.begin(); it != children.end(); ++it) {
484 if (*it == new_config) {
485 it->add_child (new_config);
490 children.push_back (new SFC (parent, new_config, max_samples_out));
491 threader->add_output (children.back().sink());
495 ExportGraphBuilder::Intermediate::remove_children (bool remove_out_files)
497 boost::ptr_list<SFC>::iterator iter = children.begin ();
499 while (iter != children.end() ) {
500 iter->remove_children (remove_out_files);
501 iter = children.erase (iter);
506 ExportGraphBuilder::Intermediate::operator== (FileSpec const & other_config) const
508 return config.format->normalize() == other_config.format->normalize() &&
509 config.format->normalize_loudness () == other_config.format->normalize_loudness() &&
511 (!config.format->normalize_loudness () && config.format->normalize_dbfs() == other_config.format->normalize_dbfs())
513 // FIXME: allow simultaneous export of two formats with different loundness normalization settings
514 (config.format->normalize_loudness () /* lufs/dbtp is a result option, not an instantaion option */)
519 ExportGraphBuilder::Intermediate::get_postprocessing_cycle_count() const
521 return static_cast<unsigned>(std::ceil(static_cast<float>(tmp_file->get_samples_written()) /
526 ExportGraphBuilder::Intermediate::process()
528 samplecnt_t samples_read = tmp_file->read (*buffer);
529 return samples_read != buffer->samples();
533 ExportGraphBuilder::Intermediate::prepare_post_processing()
535 // called in sync rt-context
538 gain = normalizer->set_peak (loudness_reader->get_peak (config.format->normalize_lufs (), config.format->normalize_dbtp ()));
539 } else if (use_peak) {
540 gain = normalizer->set_peak (peak_reader->get_peak());
542 gain = normalizer->set_peak (0.0);
544 if (use_loudness || use_peak) {
545 // push info to analyzers
546 for (boost::ptr_list<SFC>::iterator i = children.begin(); i != children.end(); ++i) {
547 (*i).set_peak (gain);
550 tmp_file->add_output (normalizer);
551 parent.intermediates.push_back (this);
555 ExportGraphBuilder::Intermediate::start_post_processing()
557 // called in disk-thread (when exporting in realtime)
558 tmp_file->seek (0, SEEK_SET);
559 if (!AudioEngine::instance()->freewheeling ()) {
560 AudioEngine::instance()->freewheel (true);
566 ExportGraphBuilder::SRC::SRC (ExportGraphBuilder & parent, FileSpec const & new_config, samplecnt_t max_samples)
570 converter.reset (new SampleRateConverter (new_config.channel_config->get_n_chans()));
571 ExportFormatSpecification & format = *new_config.format;
572 converter->init (parent.session.nominal_sample_rate(), format.sample_rate(), format.src_quality());
573 max_samples_out = converter->allocate_buffers (max_samples);
575 add_child (new_config);
578 ExportGraphBuilder::FloatSinkPtr
579 ExportGraphBuilder::SRC::sink ()
585 ExportGraphBuilder::SRC::add_child (FileSpec const & new_config)
587 if (new_config.format->normalize() || parent._realtime) {
588 add_child_to_list (new_config, intermediate_children);
590 add_child_to_list (new_config, children);
595 ExportGraphBuilder::SRC::remove_children (bool remove_out_files)
597 boost::ptr_list<SFC>::iterator sfc_iter = children.begin();
599 while (sfc_iter != children.end() ) {
600 converter->remove_output (sfc_iter->sink() );
601 sfc_iter->remove_children (remove_out_files);
602 sfc_iter = children.erase (sfc_iter);
605 boost::ptr_list<Intermediate>::iterator norm_iter = intermediate_children.begin();
607 while (norm_iter != intermediate_children.end() ) {
608 converter->remove_output (norm_iter->sink() );
609 norm_iter->remove_children (remove_out_files);
610 norm_iter = intermediate_children.erase (norm_iter);
617 ExportGraphBuilder::SRC::add_child_to_list (FileSpec const & new_config, boost::ptr_list<T> & list)
619 for (typename boost::ptr_list<T>::iterator it = list.begin(); it != list.end(); ++it) {
620 if (*it == new_config) {
621 it->add_child (new_config);
626 list.push_back (new T (parent, new_config, max_samples_out));
627 converter->add_output (list.back().sink ());
631 ExportGraphBuilder::SRC::operator== (FileSpec const & other_config) const
633 return config.format->sample_rate() == other_config.format->sample_rate();
637 ExportGraphBuilder::SilenceHandler::SilenceHandler (ExportGraphBuilder & parent, FileSpec const & new_config, samplecnt_t max_samples)
641 max_samples_in = max_samples;
642 samplecnt_t sample_rate = parent.session.nominal_sample_rate();
644 /* work around partsing "-inf" config to "0" -- 7b1f97b
645 * silence trim 0dBFS makes no sense, anyway.
647 float est = Config->get_export_silence_threshold ();
648 if (est >= 0.f) est = -INFINITY;
650 // Mixbus channelstrip always dithers the signal, cut above dither level
651 silence_trimmer.reset (new SilenceTrimmer<Sample>(max_samples_in, std::max (-90.f, est)));
653 // TODO silence-threshold should be per export-preset, with Config->get_silence_threshold being the default
654 silence_trimmer.reset (new SilenceTrimmer<Sample>(max_samples_in, est));
656 silence_trimmer->set_trim_beginning (config.format->trim_beginning());
657 silence_trimmer->set_trim_end (config.format->trim_end());
659 samplecnt_t sb = config.format->silence_beginning_at (parent.timespan->get_start(), sample_rate);
660 samplecnt_t se = config.format->silence_end_at (parent.timespan->get_end(), sample_rate);
662 silence_trimmer->add_silence_to_beginning (sb);
663 silence_trimmer->add_silence_to_end (se);
665 add_child (new_config);
668 ExportGraphBuilder::FloatSinkPtr
669 ExportGraphBuilder::SilenceHandler::sink ()
671 return silence_trimmer;
675 ExportGraphBuilder::SilenceHandler::add_child (FileSpec const & new_config)
677 for (boost::ptr_list<SRC>::iterator it = children.begin(); it != children.end(); ++it) {
678 if (*it == new_config) {
679 it->add_child (new_config);
684 children.push_back (new SRC (parent, new_config, max_samples_in));
685 silence_trimmer->add_output (children.back().sink());
689 ExportGraphBuilder::SilenceHandler::remove_children (bool remove_out_files)
691 boost::ptr_list<SRC>::iterator iter = children.begin();
693 while (iter != children.end() ) {
694 silence_trimmer->remove_output (iter->sink() );
695 iter->remove_children (remove_out_files);
696 iter = children.erase (iter);
701 ExportGraphBuilder::SilenceHandler::operator== (FileSpec const & other_config) const
703 ExportFormatSpecification & format = *config.format;
704 ExportFormatSpecification & other_format = *other_config.format;
705 return (format.trim_beginning() == other_format.trim_beginning()) &&
706 (format.trim_end() == other_format.trim_end()) &&
707 (format.silence_beginning_time() == other_format.silence_beginning_time()) &&
708 (format.silence_end_time() == other_format.silence_end_time());
713 ExportGraphBuilder::ChannelConfig::ChannelConfig (ExportGraphBuilder & parent, FileSpec const & new_config, ChannelMap & channel_map)
716 typedef ExportChannelConfiguration::ChannelList ChannelList;
720 samplecnt_t max_samples = parent.session.engine().samples_per_cycle();
721 interleaver.reset (new Interleaver<Sample> ());
722 interleaver->init (new_config.channel_config->get_n_chans(), max_samples);
724 // Make the chunk size divisible by the channel count
725 int chan_count = new_config.channel_config->get_n_chans();
726 max_samples_out = 8192;
727 if (chan_count > 0) {
728 max_samples_out -= max_samples_out % chan_count;
730 chunker.reset (new Chunker<Sample> (max_samples_out));
731 interleaver->add_output(chunker);
733 ChannelList const & channel_list = config.channel_config->get_channels();
735 for (ChannelList::const_iterator it = channel_list.begin(); it != channel_list.end(); ++it, ++chan) {
736 ChannelMap::iterator map_it = channel_map.find (*it);
737 if (map_it == channel_map.end()) {
738 std::pair<ChannelMap::iterator, bool> result_pair =
739 channel_map.insert (std::make_pair (*it, IdentityVertexPtr (new IdentityVertex<Sample> ())));
740 assert (result_pair.second);
741 map_it = result_pair.first;
743 map_it->second->add_output (interleaver->input (chan));
746 add_child (new_config);
750 ExportGraphBuilder::ChannelConfig::add_child (FileSpec const & new_config)
752 assert (*this == new_config);
754 for (boost::ptr_list<SilenceHandler>::iterator it = children.begin(); it != children.end(); ++it) {
755 if (*it == new_config) {
756 it->add_child (new_config);
761 children.push_back (new SilenceHandler (parent, new_config, max_samples_out));
762 chunker->add_output (children.back().sink ());
766 ExportGraphBuilder::ChannelConfig::remove_children (bool remove_out_files)
768 boost::ptr_list<SilenceHandler>::iterator iter = children.begin();
770 while(iter != children.end() ) {
772 chunker->remove_output (iter->sink ());
773 iter->remove_children (remove_out_files);
774 iter = children.erase(iter);
779 ExportGraphBuilder::ChannelConfig::operator== (FileSpec const & other_config) const
781 return config.channel_config == other_config.channel_config;
784 } // namespace ARDOUR