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/sample_format_converter.h"
34 #include "audiographer/general/sr_converter.h"
35 #include "audiographer/general/silence_trimmer.h"
36 #include "audiographer/general/threader.h"
37 #include "audiographer/sndfile/tmp_file.h"
38 #include "audiographer/sndfile/sndfile_writer.h"
40 #include "ardour/audioengine.h"
41 #include "ardour/export_channel_configuration.h"
42 #include "ardour/export_filename.h"
43 #include "ardour/export_format_specification.h"
44 #include "ardour/export_timespan.h"
45 #include "ardour/session_directory.h"
46 #include "ardour/sndfile_helpers.h"
48 #include "pbd/file_utils.h"
51 using namespace AudioGrapher;
56 ExportGraphBuilder::ExportGraphBuilder (Session const & session)
58 , thread_pool (hardware_concurrency())
60 process_buffer_frames = session.engine().samples_per_cycle();
63 ExportGraphBuilder::~ExportGraphBuilder ()
68 ExportGraphBuilder::process (framecnt_t frames, bool last_cycle)
70 assert(frames <= process_buffer_frames);
72 for (ChannelMap::iterator it = channels.begin(); it != channels.end(); ++it) {
73 Sample const * process_buffer = 0;
74 it->first->read (process_buffer, frames);
75 ConstProcessContext<Sample> context(process_buffer, frames, 1);
76 if (last_cycle) { context().set_flag (ProcessContext<Sample>::EndOfInput); }
77 it->second->process (context);
84 ExportGraphBuilder::process_normalize ()
86 for (std::list<Normalizer *>::iterator it = normalizers.begin(); it != normalizers.end(); /* ++ in loop */) {
87 if ((*it)->process()) {
88 it = normalizers.erase (it);
94 return normalizers.empty();
98 ExportGraphBuilder::get_normalize_cycle_count() const
101 for (std::list<Normalizer *>::const_iterator it = normalizers.begin(); it != normalizers.end(); ++it) {
102 max = std::max(max, (*it)->get_normalize_cycle_count());
108 ExportGraphBuilder::reset ()
111 channel_configs.clear ();
113 normalizers.clear ();
114 analysis_map.clear();
118 ExportGraphBuilder::cleanup (bool remove_out_files/*=false*/)
120 ChannelConfigList::iterator iter = channel_configs.begin();
122 while (iter != channel_configs.end() ) {
123 iter->remove_children(remove_out_files);
124 iter = channel_configs.erase(iter);
129 ExportGraphBuilder::set_current_timespan (boost::shared_ptr<ExportTimespan> span)
135 ExportGraphBuilder::add_config (FileSpec const & config)
137 ExportChannelConfiguration::ChannelList const & channels =
138 config.channel_config->get_channels();
139 for(ExportChannelConfiguration::ChannelList::const_iterator it = channels.begin();
140 it != channels.end(); ++it) {
141 (*it)->set_max_buffer_size(process_buffer_frames);
144 // If the sample rate is "session rate", change it to the real value.
145 // However, we need to copy it to not change the config which is saved...
146 FileSpec new_config (config);
147 new_config.format.reset(new ExportFormatSpecification(*new_config.format, false));
148 if(new_config.format->sample_rate() == ExportFormatBase::SR_Session) {
149 framecnt_t session_rate = session.nominal_frame_rate();
150 new_config.format->set_sample_rate(ExportFormatBase::nearest_sample_rate(session_rate));
154 if (!new_config.channel_config->get_split ()) {
155 add_split_config (new_config);
159 // Split channel configurations are split into several channel configurations,
160 // each corresponding to a file, at this stage
161 typedef std::list<boost::shared_ptr<ExportChannelConfiguration> > ConfigList;
162 ConfigList file_configs;
163 new_config.channel_config->configurations_for_files (file_configs);
166 for (ConfigList::iterator it = file_configs.begin(); it != file_configs.end(); ++it, ++chan) {
167 FileSpec copy = new_config;
168 copy.channel_config = *it;
170 copy.filename.reset (new ExportFilename (*copy.filename));
171 copy.filename->include_channel = true;
172 copy.filename->set_channel (chan);
174 add_split_config (copy);
179 ExportGraphBuilder::get_analysis_results (AnalysisResults& results) {
180 for (AnalysisMap::iterator i = analysis_map.begin(); i != analysis_map.end(); ++i) {
181 ExportAnalysisPtr p = i->second->result ();
183 results.insert (std::make_pair (i->first, p));
189 ExportGraphBuilder::add_split_config (FileSpec const & config)
191 for (ChannelConfigList::iterator it = channel_configs.begin(); it != channel_configs.end(); ++it) {
193 it->add_child (config);
198 // No duplicate channel config found, create new one
199 channel_configs.push_back (new ChannelConfig (*this, config, channels));
205 boost::shared_ptr<AudioGrapher::Sink<Sample> >
206 ExportGraphBuilder::Encoder::init (FileSpec const & new_config)
209 init_writer (float_writer);
214 boost::shared_ptr<AudioGrapher::Sink<int> >
215 ExportGraphBuilder::Encoder::init (FileSpec const & new_config)
218 init_writer (int_writer);
223 boost::shared_ptr<AudioGrapher::Sink<short> >
224 ExportGraphBuilder::Encoder::init (FileSpec const & new_config)
227 init_writer (short_writer);
232 ExportGraphBuilder::Encoder::add_child (FileSpec const & new_config)
234 filenames.push_back (new_config.filename);
238 ExportGraphBuilder::Encoder::destroy_writer (bool delete_out_file)
240 if (delete_out_file ) {
243 float_writer->close ();
247 int_writer->close ();
251 short_writer->close ();
254 if (std::remove(writer_filename.c_str() ) != 0) {
255 std::cout << "Encoder::destroy_writer () : Error removing file: " << strerror(errno) << std::endl;
259 float_writer.reset ();
261 short_writer.reset ();
265 ExportGraphBuilder::Encoder::operator== (FileSpec const & other_config) const
267 return get_real_format (config) == get_real_format (other_config);
271 ExportGraphBuilder::Encoder::get_real_format (FileSpec const & config)
273 ExportFormatSpecification & format = *config.format;
274 return format.format_id() | format.sample_format() | format.endianness();
279 ExportGraphBuilder::Encoder::init_writer (boost::shared_ptr<AudioGrapher::SndfileWriter<T> > & writer)
281 unsigned channels = config.channel_config->get_n_chans();
282 int format = get_real_format (config);
283 config.filename->set_channel_config(config.channel_config);
284 writer_filename = config.filename->get_path (config.format);
286 writer.reset (new AudioGrapher::SndfileWriter<T> (writer_filename, format, channels, config.format->sample_rate(), config.broadcast_info));
287 writer->FileWritten.connect_same_thread (copy_files_connection, boost::bind (&ExportGraphBuilder::Encoder::copy_files, this, _1));
291 ExportGraphBuilder::Encoder::copy_files (std::string orig_path)
293 while (filenames.size()) {
294 ExportFilenamePtr & filename = filenames.front();
295 PBD::copy_file (orig_path, filename->get_path (config.format).c_str());
296 filenames.pop_front();
302 ExportGraphBuilder::SFC::SFC (ExportGraphBuilder &parent, FileSpec const & new_config, framecnt_t max_frames)
306 data_width = sndfile_data_width (Encoder::get_real_format (config));
307 unsigned channels = new_config.channel_config->get_n_chans();
308 _analyse = config.format->analyse();
310 analyser.reset (new Analyser (config.format->sample_rate(), channels, max_frames,
311 (framecnt_t) ceil (parent.timespan->get_length () * config.format->sample_rate () / (double) parent.session.nominal_frame_rate ())));
312 parent.add_analyser (config.filename->get_path (config.format), analyser);
315 if (data_width == 8 || data_width == 16) {
316 short_converter = ShortConverterPtr (new SampleFormatConverter<short> (channels));
317 short_converter->init (max_frames, config.format->dither_type(), data_width);
319 if (_analyse) { analyser->add_output (short_converter); }
321 } else if (data_width == 24 || data_width == 32) {
322 int_converter = IntConverterPtr (new SampleFormatConverter<int> (channels));
323 int_converter->init (max_frames, config.format->dither_type(), data_width);
325 if (_analyse) { analyser->add_output (int_converter); }
327 int actual_data_width = 8 * sizeof(Sample);
328 float_converter = FloatConverterPtr (new SampleFormatConverter<Sample> (channels));
329 float_converter->init (max_frames, config.format->dither_type(), actual_data_width);
331 if (_analyse) { analyser->add_output (float_converter); }
335 ExportGraphBuilder::FloatSinkPtr
336 ExportGraphBuilder::SFC::sink ()
340 } else if (data_width == 8 || data_width == 16) {
341 return short_converter;
342 } else if (data_width == 24 || data_width == 32) {
343 return int_converter;
345 return float_converter;
350 ExportGraphBuilder::SFC::add_child (FileSpec const & new_config)
352 for (boost::ptr_list<Encoder>::iterator it = children.begin(); it != children.end(); ++it) {
353 if (*it == new_config) {
354 it->add_child (new_config);
359 children.push_back (new Encoder());
360 Encoder & encoder = children.back();
362 if (data_width == 8 || data_width == 16) {
363 short_converter->add_output (encoder.init<short> (new_config));
364 } else if (data_width == 24 || data_width == 32) {
365 int_converter->add_output (encoder.init<int> (new_config));
367 float_converter->add_output (encoder.init<Sample> (new_config));
372 ExportGraphBuilder::SFC::remove_children (bool remove_out_files)
374 boost::ptr_list<Encoder>::iterator iter = children.begin ();
376 while (iter != children.end() ) {
378 if (remove_out_files) {
379 iter->destroy_writer(remove_out_files);
381 iter = children.erase (iter);
386 ExportGraphBuilder::SFC::operator== (FileSpec const & other_config) const
388 return config.format->sample_format() == other_config.format->sample_format();
393 ExportGraphBuilder::Normalizer::Normalizer (ExportGraphBuilder & parent, FileSpec const & new_config, framecnt_t /*max_frames*/)
396 std::string tmpfile_path = parent.session.session_directory().export_path();
397 tmpfile_path = Glib::build_filename(tmpfile_path, "XXXXXX");
398 std::vector<char> tmpfile_path_buf(tmpfile_path.size() + 1);
399 std::copy(tmpfile_path.begin(), tmpfile_path.end(), tmpfile_path_buf.begin());
400 tmpfile_path_buf[tmpfile_path.size()] = '\0';
403 uint32_t const channels = config.channel_config->get_n_chans();
404 max_frames_out = 4086 - (4086 % channels); // TODO good chunk size
406 buffer.reset (new AllocatingProcessContext<Sample> (max_frames_out, channels));
407 peak_reader.reset (new PeakReader ());
408 normalizer.reset (new AudioGrapher::Normalizer (config.format->normalize_target()));
409 threader.reset (new Threader<Sample> (parent.thread_pool));
411 normalizer->alloc_buffer (max_frames_out);
412 normalizer->add_output (threader);
414 int format = ExportFormatBase::F_RAW | ExportFormatBase::SF_Float;
415 tmp_file.reset (new TmpFile<float> (&tmpfile_path_buf[0], format, channels, config.format->sample_rate()));
416 tmp_file->FileWritten.connect_same_thread (post_processing_connection,
417 boost::bind (&Normalizer::start_post_processing, this));
419 add_child (new_config);
421 peak_reader->add_output (tmp_file);
424 ExportGraphBuilder::FloatSinkPtr
425 ExportGraphBuilder::Normalizer::sink ()
431 ExportGraphBuilder::Normalizer::add_child (FileSpec const & new_config)
433 for (boost::ptr_list<SFC>::iterator it = children.begin(); it != children.end(); ++it) {
434 if (*it == new_config) {
435 it->add_child (new_config);
440 children.push_back (new SFC (parent, new_config, max_frames_out));
441 threader->add_output (children.back().sink());
445 ExportGraphBuilder::Normalizer::remove_children (bool remove_out_files)
447 boost::ptr_list<SFC>::iterator iter = children.begin ();
449 while (iter != children.end() ) {
450 iter->remove_children (remove_out_files);
451 iter = children.erase (iter);
456 ExportGraphBuilder::Normalizer::operator== (FileSpec const & other_config) const
458 return config.format->normalize() == other_config.format->normalize() &&
459 config.format->normalize_target() == other_config.format->normalize_target();
463 ExportGraphBuilder::Normalizer::get_normalize_cycle_count() const
465 return static_cast<unsigned>(std::ceil(static_cast<float>(tmp_file->get_frames_written()) /
470 ExportGraphBuilder::Normalizer::process()
472 framecnt_t frames_read = tmp_file->read (*buffer);
473 return frames_read != buffer->frames();
477 ExportGraphBuilder::Normalizer::start_post_processing()
479 normalizer->set_peak (peak_reader->get_peak());
480 tmp_file->seek (0, SEEK_SET);
481 tmp_file->add_output (normalizer);
482 parent.normalizers.push_back (this);
487 ExportGraphBuilder::SRC::SRC (ExportGraphBuilder & parent, FileSpec const & new_config, framecnt_t max_frames)
491 converter.reset (new SampleRateConverter (new_config.channel_config->get_n_chans()));
492 ExportFormatSpecification & format = *new_config.format;
493 converter->init (parent.session.nominal_frame_rate(), format.sample_rate(), format.src_quality());
494 max_frames_out = converter->allocate_buffers (max_frames);
496 add_child (new_config);
499 ExportGraphBuilder::FloatSinkPtr
500 ExportGraphBuilder::SRC::sink ()
506 ExportGraphBuilder::SRC::add_child (FileSpec const & new_config)
508 if (new_config.format->normalize()) {
509 add_child_to_list (new_config, normalized_children);
511 add_child_to_list (new_config, children);
516 ExportGraphBuilder::SRC::remove_children (bool remove_out_files)
518 boost::ptr_list<SFC>::iterator sfc_iter = children.begin();
520 while (sfc_iter != children.end() ) {
521 converter->remove_output (sfc_iter->sink() );
522 sfc_iter->remove_children (remove_out_files);
523 sfc_iter = children.erase (sfc_iter);
526 boost::ptr_list<Normalizer>::iterator norm_iter = normalized_children.begin();
528 while (norm_iter != normalized_children.end() ) {
529 converter->remove_output (norm_iter->sink() );
530 norm_iter->remove_children (remove_out_files);
531 norm_iter = normalized_children.erase (norm_iter);
538 ExportGraphBuilder::SRC::add_child_to_list (FileSpec const & new_config, boost::ptr_list<T> & list)
540 for (typename boost::ptr_list<T>::iterator it = list.begin(); it != list.end(); ++it) {
541 if (*it == new_config) {
542 it->add_child (new_config);
547 list.push_back (new T (parent, new_config, max_frames_out));
548 converter->add_output (list.back().sink ());
552 ExportGraphBuilder::SRC::operator== (FileSpec const & other_config) const
554 return config.format->sample_rate() == other_config.format->sample_rate();
558 ExportGraphBuilder::SilenceHandler::SilenceHandler (ExportGraphBuilder & parent, FileSpec const & new_config, framecnt_t max_frames)
562 max_frames_in = max_frames;
563 framecnt_t sample_rate = parent.session.nominal_frame_rate();
565 silence_trimmer.reset (new SilenceTrimmer<Sample>(max_frames_in));
566 silence_trimmer->set_trim_beginning (config.format->trim_beginning());
567 silence_trimmer->set_trim_end (config.format->trim_end());
569 framecnt_t sb = config.format->silence_beginning_at (parent.timespan->get_start(), sample_rate);
570 framecnt_t se = config.format->silence_end_at (parent.timespan->get_end(), sample_rate);
572 silence_trimmer->add_silence_to_beginning (sb);
573 silence_trimmer->add_silence_to_end (se);
575 add_child (new_config);
578 ExportGraphBuilder::FloatSinkPtr
579 ExportGraphBuilder::SilenceHandler::sink ()
581 return silence_trimmer;
585 ExportGraphBuilder::SilenceHandler::add_child (FileSpec const & new_config)
587 for (boost::ptr_list<SRC>::iterator it = children.begin(); it != children.end(); ++it) {
588 if (*it == new_config) {
589 it->add_child (new_config);
594 children.push_back (new SRC (parent, new_config, max_frames_in));
595 silence_trimmer->add_output (children.back().sink());
599 ExportGraphBuilder::SilenceHandler::remove_children (bool remove_out_files)
601 boost::ptr_list<SRC>::iterator iter = children.begin();
603 while (iter != children.end() ) {
604 silence_trimmer->remove_output (iter->sink() );
605 iter->remove_children (remove_out_files);
606 iter = children.erase (iter);
611 ExportGraphBuilder::SilenceHandler::operator== (FileSpec const & other_config) const
613 ExportFormatSpecification & format = *config.format;
614 ExportFormatSpecification & other_format = *other_config.format;
615 return (format.trim_beginning() == other_format.trim_beginning()) &&
616 (format.trim_end() == other_format.trim_end()) &&
617 (format.silence_beginning_time() == other_format.silence_beginning_time()) &&
618 (format.silence_end_time() == other_format.silence_end_time());
623 ExportGraphBuilder::ChannelConfig::ChannelConfig (ExportGraphBuilder & parent, FileSpec const & new_config, ChannelMap & channel_map)
626 typedef ExportChannelConfiguration::ChannelList ChannelList;
630 framecnt_t max_frames = parent.session.engine().samples_per_cycle();
631 interleaver.reset (new Interleaver<Sample> ());
632 interleaver->init (new_config.channel_config->get_n_chans(), max_frames);
634 // Make the chunk size divisible by the channel count
635 int chan_count = new_config.channel_config->get_n_chans();
636 max_frames_out = 8192;
637 if (chan_count > 0) {
638 max_frames_out -= max_frames_out % chan_count;
640 chunker.reset (new Chunker<Sample> (max_frames_out));
641 interleaver->add_output(chunker);
643 ChannelList const & channel_list = config.channel_config->get_channels();
645 for (ChannelList::const_iterator it = channel_list.begin(); it != channel_list.end(); ++it, ++chan) {
646 ChannelMap::iterator map_it = channel_map.find (*it);
647 if (map_it == channel_map.end()) {
648 std::pair<ChannelMap::iterator, bool> result_pair =
649 channel_map.insert (std::make_pair (*it, IdentityVertexPtr (new IdentityVertex<Sample> ())));
650 assert (result_pair.second);
651 map_it = result_pair.first;
653 map_it->second->add_output (interleaver->input (chan));
656 add_child (new_config);
660 ExportGraphBuilder::ChannelConfig::add_child (FileSpec const & new_config)
662 assert (*this == new_config);
664 for (boost::ptr_list<SilenceHandler>::iterator it = children.begin(); it != children.end(); ++it) {
665 if (*it == new_config) {
666 it->add_child (new_config);
671 children.push_back (new SilenceHandler (parent, new_config, max_frames_out));
672 chunker->add_output (children.back().sink ());
676 ExportGraphBuilder::ChannelConfig::remove_children (bool remove_out_files)
678 boost::ptr_list<SilenceHandler>::iterator iter = children.begin();
680 while(iter != children.end() ) {
682 chunker->remove_output (iter->sink ());
683 iter->remove_children (remove_out_files);
684 iter = children.erase(iter);
689 ExportGraphBuilder::ChannelConfig::operator== (FileSpec const & other_config) const
691 return config.channel_config == other_config.channel_config;
694 } // namespace ARDOUR