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/peak_reader.h"
32 #include "audiographer/general/sample_format_converter.h"
33 #include "audiographer/general/sr_converter.h"
34 #include "audiographer/general/silence_trimmer.h"
35 #include "audiographer/general/threader.h"
36 #include "audiographer/sndfile/tmp_file.h"
37 #include "audiographer/sndfile/sndfile_writer.h"
39 #include "ardour/audioengine.h"
40 #include "ardour/export_channel_configuration.h"
41 #include "ardour/export_filename.h"
42 #include "ardour/export_format_specification.h"
43 #include "ardour/export_timespan.h"
44 #include "ardour/session_directory.h"
45 #include "ardour/sndfile_helpers.h"
47 #include "pbd/file_utils.h"
50 using namespace AudioGrapher;
55 ExportGraphBuilder::ExportGraphBuilder (Session const & session)
57 , thread_pool (hardware_concurrency())
59 process_buffer_frames = session.engine().samples_per_cycle();
62 ExportGraphBuilder::~ExportGraphBuilder ()
67 ExportGraphBuilder::process (framecnt_t frames, bool last_cycle)
69 assert(frames <= process_buffer_frames);
71 for (ChannelMap::iterator it = channels.begin(); it != channels.end(); ++it) {
72 Sample const * process_buffer = 0;
73 it->first->read (process_buffer, frames);
74 ConstProcessContext<Sample> context(process_buffer, frames, 1);
75 if (last_cycle) { context().set_flag (ProcessContext<Sample>::EndOfInput); }
76 it->second->process (context);
83 ExportGraphBuilder::process_normalize ()
85 for (std::list<Normalizer *>::iterator it = normalizers.begin(); it != normalizers.end(); /* ++ in loop */) {
86 if ((*it)->process()) {
87 it = normalizers.erase (it);
93 return normalizers.empty();
97 ExportGraphBuilder::get_normalize_cycle_count() const
100 for (std::list<Normalizer *>::const_iterator it = normalizers.begin(); it != normalizers.end(); ++it) {
101 max = std::max(max, (*it)->get_normalize_cycle_count());
107 ExportGraphBuilder::reset ()
110 channel_configs.clear ();
112 normalizers.clear ();
116 ExportGraphBuilder::set_current_timespan (boost::shared_ptr<ExportTimespan> span)
122 ExportGraphBuilder::add_config (FileSpec const & config)
124 ExportChannelConfiguration::ChannelList const & channels =
125 config.channel_config->get_channels();
126 for(ExportChannelConfiguration::ChannelList::const_iterator it = channels.begin();
127 it != channels.end(); ++it) {
128 (*it)->set_max_buffer_size(process_buffer_frames);
131 // If the sample rate is "session rate", change it to the real value.
132 // However, we need to copy it to not change the config which is saved...
133 FileSpec new_config (config);
134 new_config.format.reset(new ExportFormatSpecification(*new_config.format, false));
135 if(new_config.format->sample_rate() == ExportFormatBase::SR_Session) {
136 framecnt_t session_rate = session.nominal_frame_rate();
137 new_config.format->set_sample_rate(ExportFormatBase::nearest_sample_rate(session_rate));
141 if (!new_config.channel_config->get_split ()) {
142 add_split_config (new_config);
146 // Split channel configurations are split into several channel configurations,
147 // each corresponding to a file, at this stage
148 typedef std::list<boost::shared_ptr<ExportChannelConfiguration> > ConfigList;
149 ConfigList file_configs;
150 new_config.channel_config->configurations_for_files (file_configs);
153 for (ConfigList::iterator it = file_configs.begin(); it != file_configs.end(); ++it, ++chan) {
154 FileSpec copy = new_config;
155 copy.channel_config = *it;
157 copy.filename.reset (new ExportFilename (*copy.filename));
158 copy.filename->include_channel = true;
159 copy.filename->set_channel (chan);
161 add_split_config (copy);
166 ExportGraphBuilder::add_split_config (FileSpec const & config)
168 for (ChannelConfigList::iterator it = channel_configs.begin(); it != channel_configs.end(); ++it) {
170 it->add_child (config);
175 // No duplicate channel config found, create new one
176 channel_configs.push_back (new ChannelConfig (*this, config, channels));
182 boost::shared_ptr<AudioGrapher::Sink<Sample> >
183 ExportGraphBuilder::Encoder::init (FileSpec const & new_config)
186 init_writer (float_writer);
191 boost::shared_ptr<AudioGrapher::Sink<int> >
192 ExportGraphBuilder::Encoder::init (FileSpec const & new_config)
195 init_writer (int_writer);
200 boost::shared_ptr<AudioGrapher::Sink<short> >
201 ExportGraphBuilder::Encoder::init (FileSpec const & new_config)
204 init_writer (short_writer);
209 ExportGraphBuilder::Encoder::add_child (FileSpec const & new_config)
211 filenames.push_back (new_config.filename);
215 ExportGraphBuilder::Encoder::operator== (FileSpec const & other_config) const
217 return get_real_format (config) == get_real_format (other_config);
221 ExportGraphBuilder::Encoder::get_real_format (FileSpec const & config)
223 ExportFormatSpecification & format = *config.format;
224 return format.format_id() | format.sample_format() | format.endianness();
229 ExportGraphBuilder::Encoder::init_writer (boost::shared_ptr<AudioGrapher::SndfileWriter<T> > & writer)
231 unsigned channels = config.channel_config->get_n_chans();
232 int format = get_real_format (config);
233 config.filename->set_channel_config(config.channel_config);
234 string filename = config.filename->get_path (config.format);
236 writer.reset (new AudioGrapher::SndfileWriter<T> (filename, format, channels, config.format->sample_rate(), config.broadcast_info));
237 writer->FileWritten.connect_same_thread (copy_files_connection, boost::bind (&ExportGraphBuilder::Encoder::copy_files, this, _1));
241 ExportGraphBuilder::Encoder::copy_files (std::string orig_path)
243 while (filenames.size()) {
244 ExportFilenamePtr & filename = filenames.front();
245 PBD::copy_file (orig_path, filename->get_path (config.format).c_str());
246 filenames.pop_front();
252 ExportGraphBuilder::SFC::SFC (ExportGraphBuilder &, FileSpec const & new_config, framecnt_t max_frames)
256 data_width = sndfile_data_width (Encoder::get_real_format (config));
257 unsigned channels = new_config.channel_config->get_n_chans();
259 if (data_width == 8 || data_width == 16) {
260 short_converter = ShortConverterPtr (new SampleFormatConverter<short> (channels));
261 short_converter->init (max_frames, config.format->dither_type(), data_width);
263 } else if (data_width == 24 || data_width == 32) {
264 int_converter = IntConverterPtr (new SampleFormatConverter<int> (channels));
265 int_converter->init (max_frames, config.format->dither_type(), data_width);
268 int actual_data_width = 8 * sizeof(Sample);
269 float_converter = FloatConverterPtr (new SampleFormatConverter<Sample> (channels));
270 float_converter->init (max_frames, config.format->dither_type(), actual_data_width);
275 ExportGraphBuilder::FloatSinkPtr
276 ExportGraphBuilder::SFC::sink ()
278 if (data_width == 8 || data_width == 16) {
279 return short_converter;
280 } else if (data_width == 24 || data_width == 32) {
281 return int_converter;
283 return float_converter;
288 ExportGraphBuilder::SFC::add_child (FileSpec const & new_config)
290 for (boost::ptr_list<Encoder>::iterator it = children.begin(); it != children.end(); ++it) {
291 if (*it == new_config) {
292 it->add_child (new_config);
297 children.push_back (new Encoder());
298 Encoder & encoder = children.back();
300 if (data_width == 8 || data_width == 16) {
301 short_converter->add_output (encoder.init<short> (new_config));
302 } else if (data_width == 24 || data_width == 32) {
303 int_converter->add_output (encoder.init<int> (new_config));
305 float_converter->add_output (encoder.init<Sample> (new_config));
310 ExportGraphBuilder::SFC::operator== (FileSpec const & other_config) const
312 return config.format->sample_format() == other_config.format->sample_format();
317 ExportGraphBuilder::Normalizer::Normalizer (ExportGraphBuilder & parent, FileSpec const & new_config, framecnt_t /*max_frames*/)
320 std::string tmpfile_path = parent.session.session_directory().export_path();
321 tmpfile_path = Glib::build_filename(tmpfile_path, "XXXXXX");
322 std::vector<char> tmpfile_path_buf(tmpfile_path.size() + 1);
323 std::copy(tmpfile_path.begin(), tmpfile_path.end(), tmpfile_path_buf.begin());
324 tmpfile_path_buf[tmpfile_path.size()] = '\0';
327 uint32_t const channels = config.channel_config->get_n_chans();
328 max_frames_out = 4086 - (4086 % channels); // TODO good chunk size
330 buffer.reset (new AllocatingProcessContext<Sample> (max_frames_out, channels));
331 peak_reader.reset (new PeakReader ());
332 normalizer.reset (new AudioGrapher::Normalizer (config.format->normalize_target()));
333 threader.reset (new Threader<Sample> (parent.thread_pool));
335 normalizer->alloc_buffer (max_frames_out);
336 normalizer->add_output (threader);
338 int format = ExportFormatBase::F_RAW | ExportFormatBase::SF_Float;
339 tmp_file.reset (new TmpFile<float> (&tmpfile_path_buf[0], format, channels, config.format->sample_rate()));
340 tmp_file->FileWritten.connect_same_thread (post_processing_connection,
341 boost::bind (&Normalizer::start_post_processing, this));
343 add_child (new_config);
345 peak_reader->add_output (tmp_file);
348 ExportGraphBuilder::FloatSinkPtr
349 ExportGraphBuilder::Normalizer::sink ()
355 ExportGraphBuilder::Normalizer::add_child (FileSpec const & new_config)
357 for (boost::ptr_list<SFC>::iterator it = children.begin(); it != children.end(); ++it) {
358 if (*it == new_config) {
359 it->add_child (new_config);
364 children.push_back (new SFC (parent, new_config, max_frames_out));
365 threader->add_output (children.back().sink());
369 ExportGraphBuilder::Normalizer::operator== (FileSpec const & other_config) const
371 return config.format->normalize() == other_config.format->normalize() &&
372 config.format->normalize_target() == other_config.format->normalize_target();
376 ExportGraphBuilder::Normalizer::get_normalize_cycle_count() const
378 return static_cast<unsigned>(std::ceil(static_cast<float>(tmp_file->get_frames_written()) /
383 ExportGraphBuilder::Normalizer::process()
385 framecnt_t frames_read = tmp_file->read (*buffer);
386 return frames_read != buffer->frames();
390 ExportGraphBuilder::Normalizer::start_post_processing()
392 normalizer->set_peak (peak_reader->get_peak());
393 tmp_file->seek (0, SEEK_SET);
394 tmp_file->add_output (normalizer);
395 parent.normalizers.push_back (this);
400 ExportGraphBuilder::SRC::SRC (ExportGraphBuilder & parent, FileSpec const & new_config, framecnt_t max_frames)
404 converter.reset (new SampleRateConverter (new_config.channel_config->get_n_chans()));
405 ExportFormatSpecification & format = *new_config.format;
406 converter->init (parent.session.nominal_frame_rate(), format.sample_rate(), format.src_quality());
407 max_frames_out = converter->allocate_buffers (max_frames);
409 add_child (new_config);
412 ExportGraphBuilder::FloatSinkPtr
413 ExportGraphBuilder::SRC::sink ()
419 ExportGraphBuilder::SRC::add_child (FileSpec const & new_config)
421 if (new_config.format->normalize()) {
422 add_child_to_list (new_config, normalized_children);
424 add_child_to_list (new_config, children);
430 ExportGraphBuilder::SRC::add_child_to_list (FileSpec const & new_config, boost::ptr_list<T> & list)
432 for (typename boost::ptr_list<T>::iterator it = list.begin(); it != list.end(); ++it) {
433 if (*it == new_config) {
434 it->add_child (new_config);
439 list.push_back (new T (parent, new_config, max_frames_out));
440 converter->add_output (list.back().sink ());
444 ExportGraphBuilder::SRC::operator== (FileSpec const & other_config) const
446 return config.format->sample_rate() == other_config.format->sample_rate();
450 ExportGraphBuilder::SilenceHandler::SilenceHandler (ExportGraphBuilder & parent, FileSpec const & new_config, framecnt_t max_frames)
454 max_frames_in = max_frames;
455 framecnt_t sample_rate = parent.session.nominal_frame_rate();
457 silence_trimmer.reset (new SilenceTrimmer<Sample>(max_frames_in));
458 silence_trimmer->set_trim_beginning (config.format->trim_beginning());
459 silence_trimmer->set_trim_end (config.format->trim_end());
461 framecnt_t sb = config.format->silence_beginning_at (parent.timespan->get_start(), sample_rate);
462 framecnt_t se = config.format->silence_end_at (parent.timespan->get_end(), sample_rate);
464 silence_trimmer->add_silence_to_beginning (sb);
465 silence_trimmer->add_silence_to_end (se);
467 add_child (new_config);
470 ExportGraphBuilder::FloatSinkPtr
471 ExportGraphBuilder::SilenceHandler::sink ()
473 return silence_trimmer;
477 ExportGraphBuilder::SilenceHandler::add_child (FileSpec const & new_config)
479 for (boost::ptr_list<SRC>::iterator it = children.begin(); it != children.end(); ++it) {
480 if (*it == new_config) {
481 it->add_child (new_config);
486 children.push_back (new SRC (parent, new_config, max_frames_in));
487 silence_trimmer->add_output (children.back().sink());
491 ExportGraphBuilder::SilenceHandler::operator== (FileSpec const & other_config) const
493 ExportFormatSpecification & format = *config.format;
494 ExportFormatSpecification & other_format = *other_config.format;
495 return (format.trim_beginning() == other_format.trim_beginning()) &&
496 (format.trim_end() == other_format.trim_end()) &&
497 (format.silence_beginning_time() == other_format.silence_beginning_time()) &&
498 (format.silence_end_time() == other_format.silence_end_time());
503 ExportGraphBuilder::ChannelConfig::ChannelConfig (ExportGraphBuilder & parent, FileSpec const & new_config, ChannelMap & channel_map)
506 typedef ExportChannelConfiguration::ChannelList ChannelList;
510 framecnt_t max_frames = parent.session.engine().samples_per_cycle();
511 interleaver.reset (new Interleaver<Sample> ());
512 interleaver->init (new_config.channel_config->get_n_chans(), max_frames);
514 // Make the chunk size divisible by the channel count
515 int chan_count = new_config.channel_config->get_n_chans();
516 max_frames_out = 8192;
517 if (chan_count > 0) {
518 max_frames_out -= max_frames_out % chan_count;
520 chunker.reset (new Chunker<Sample> (max_frames_out));
521 interleaver->add_output(chunker);
523 ChannelList const & channel_list = config.channel_config->get_channels();
525 for (ChannelList::const_iterator it = channel_list.begin(); it != channel_list.end(); ++it, ++chan) {
526 ChannelMap::iterator map_it = channel_map.find (*it);
527 if (map_it == channel_map.end()) {
528 std::pair<ChannelMap::iterator, bool> result_pair =
529 channel_map.insert (std::make_pair (*it, IdentityVertexPtr (new IdentityVertex<Sample> ())));
530 assert (result_pair.second);
531 map_it = result_pair.first;
533 map_it->second->add_output (interleaver->input (chan));
536 add_child (new_config);
540 ExportGraphBuilder::ChannelConfig::add_child (FileSpec const & new_config)
542 assert (*this == new_config);
544 for (boost::ptr_list<SilenceHandler>::iterator it = children.begin(); it != children.end(); ++it) {
545 if (*it == new_config) {
546 it->add_child (new_config);
551 children.push_back (new SilenceHandler (parent, new_config, max_frames_out));
552 chunker->add_output (children.back().sink ());
556 ExportGraphBuilder::ChannelConfig::operator== (FileSpec const & other_config) const
558 return config.channel_config == other_config.channel_config;
561 } // namespace ARDOUR