1 #include "ardour/export_graph_builder.h"
3 #include <glibmm/miscutils.h>
5 #include "audiographer/process_context.h"
6 #include "audiographer/general/chunker.h"
7 #include "audiographer/general/interleaver.h"
8 #include "audiographer/general/normalizer.h"
9 #include "audiographer/general/peak_reader.h"
10 #include "audiographer/general/sample_format_converter.h"
11 #include "audiographer/general/sr_converter.h"
12 #include "audiographer/general/silence_trimmer.h"
13 #include "audiographer/general/threader.h"
14 #include "audiographer/sndfile/tmp_file.h"
15 #include "audiographer/sndfile/sndfile_writer.h"
17 #include "ardour/audioengine.h"
18 #include "ardour/export_channel_configuration.h"
19 #include "ardour/export_filename.h"
20 #include "ardour/export_format_specification.h"
21 #include "ardour/export_timespan.h"
22 #include "ardour/session_directory.h"
23 #include "ardour/sndfile_helpers.h"
25 #include "pbd/file_utils.h"
28 using namespace AudioGrapher;
33 ExportGraphBuilder::ExportGraphBuilder (Session const & session)
35 , thread_pool (hardware_concurrency())
37 process_buffer_frames = session.engine().frames_per_cycle();
40 ExportGraphBuilder::~ExportGraphBuilder ()
45 ExportGraphBuilder::process (framecnt_t frames, bool last_cycle)
47 assert(frames <= process_buffer_frames);
49 for (ChannelMap::iterator it = channels.begin(); it != channels.end(); ++it) {
50 Sample const * process_buffer = 0;
51 it->first->read (process_buffer, frames);
52 ConstProcessContext<Sample> context(process_buffer, frames, 1);
53 if (last_cycle) { context().set_flag (ProcessContext<Sample>::EndOfInput); }
54 it->second->process (context);
61 ExportGraphBuilder::process_normalize ()
63 for (std::list<Normalizer *>::iterator it = normalizers.begin(); it != normalizers.end(); /* ++ in loop */) {
64 if ((*it)->process()) {
65 it = normalizers.erase (it);
71 return normalizers.empty();
75 ExportGraphBuilder::get_normalize_cycle_count() const
78 for (std::list<Normalizer *>::const_iterator it = normalizers.begin(); it != normalizers.end(); ++it) {
79 max = std::max(max, (*it)->get_normalize_cycle_count());
85 ExportGraphBuilder::reset ()
88 channel_configs.clear ();
94 ExportGraphBuilder::set_current_timespan (boost::shared_ptr<ExportTimespan> span)
100 ExportGraphBuilder::add_config (FileSpec const & config)
102 ExportChannelConfiguration::ChannelList const & channels =
103 config.channel_config->get_channels();
104 for(ExportChannelConfiguration::ChannelList::const_iterator it = channels.begin();
105 it != channels.end(); ++it) {
106 (*it)->set_max_buffer_size(process_buffer_frames);
109 // If the sample rate is "session rate", change it to the real value.
110 // However, we need to copy it to not change the config which is saved...
111 FileSpec new_config (config);
112 new_config.format.reset(new ExportFormatSpecification(*new_config.format));
113 if(new_config.format->sample_rate() == ExportFormatBase::SR_Session) {
114 framecnt_t session_rate = session.nominal_frame_rate();
115 new_config.format->set_sample_rate(ExportFormatBase::nearest_sample_rate(session_rate));
119 if (!new_config.channel_config->get_split ()) {
120 add_split_config (new_config);
124 // Split channel configurations are split into several channel configurations,
125 // each corresponding to a file, at this stage
126 typedef std::list<boost::shared_ptr<ExportChannelConfiguration> > ConfigList;
127 ConfigList file_configs;
128 new_config.channel_config->configurations_for_files (file_configs);
131 for (ConfigList::iterator it = file_configs.begin(); it != file_configs.end(); ++it, ++chan) {
132 FileSpec copy = new_config;
133 copy.channel_config = *it;
135 copy.filename.reset (new ExportFilename (*copy.filename));
136 copy.filename->include_channel = true;
137 copy.filename->set_channel (chan);
139 add_split_config (copy);
144 ExportGraphBuilder::add_split_config (FileSpec const & config)
146 for (ChannelConfigList::iterator it = channel_configs.begin(); it != channel_configs.end(); ++it) {
148 it->add_child (config);
153 // No duplicate channel config found, create new one
154 channel_configs.push_back (new ChannelConfig (*this, config, channels));
160 boost::shared_ptr<AudioGrapher::Sink<Sample> >
161 ExportGraphBuilder::Encoder::init (FileSpec const & new_config)
164 init_writer (float_writer);
169 boost::shared_ptr<AudioGrapher::Sink<int> >
170 ExportGraphBuilder::Encoder::init (FileSpec const & new_config)
173 init_writer (int_writer);
178 boost::shared_ptr<AudioGrapher::Sink<short> >
179 ExportGraphBuilder::Encoder::init (FileSpec const & new_config)
182 init_writer (short_writer);
187 ExportGraphBuilder::Encoder::add_child (FileSpec const & new_config)
189 filenames.push_back (new_config.filename);
193 ExportGraphBuilder::Encoder::operator== (FileSpec const & other_config) const
195 return get_real_format (config) == get_real_format (other_config);
199 ExportGraphBuilder::Encoder::get_real_format (FileSpec const & config)
201 ExportFormatSpecification & format = *config.format;
202 return format.format_id() | format.sample_format() | format.endianness();
207 ExportGraphBuilder::Encoder::init_writer (boost::shared_ptr<AudioGrapher::SndfileWriter<T> > & writer)
209 unsigned channels = config.channel_config->get_n_chans();
210 int format = get_real_format (config);
211 config.filename->set_channel_config(config.channel_config);
212 string filename = config.filename->get_path (config.format);
214 writer.reset (new AudioGrapher::SndfileWriter<T> (filename, format, channels, config.format->sample_rate(), config.broadcast_info));
215 writer->FileWritten.connect_same_thread (copy_files_connection, boost::bind (&ExportGraphBuilder::Encoder::copy_files, this, _1));
219 ExportGraphBuilder::Encoder::copy_files (std::string orig_path)
221 while (filenames.size()) {
222 ExportFilenamePtr & filename = filenames.front();
223 PBD::copy_file (orig_path, filename->get_path (config.format).c_str());
224 filenames.pop_front();
230 ExportGraphBuilder::SFC::SFC (ExportGraphBuilder &, FileSpec const & new_config, framecnt_t max_frames)
234 data_width = sndfile_data_width (Encoder::get_real_format (config));
235 unsigned channels = new_config.channel_config->get_n_chans();
237 if (data_width == 8 || data_width == 16) {
238 short_converter = ShortConverterPtr (new SampleFormatConverter<short> (channels));
239 short_converter->init (max_frames, config.format->dither_type(), data_width);
241 } else if (data_width == 24 || data_width == 32) {
242 int_converter = IntConverterPtr (new SampleFormatConverter<int> (channels));
243 int_converter->init (max_frames, config.format->dither_type(), data_width);
246 int actual_data_width = 8 * sizeof(Sample);
247 float_converter = FloatConverterPtr (new SampleFormatConverter<Sample> (channels));
248 float_converter->init (max_frames, config.format->dither_type(), actual_data_width);
253 ExportGraphBuilder::FloatSinkPtr
254 ExportGraphBuilder::SFC::sink ()
256 if (data_width == 8 || data_width == 16) {
257 return short_converter;
258 } else if (data_width == 24 || data_width == 32) {
259 return int_converter;
261 return float_converter;
266 ExportGraphBuilder::SFC::add_child (FileSpec const & new_config)
268 for (boost::ptr_list<Encoder>::iterator it = children.begin(); it != children.end(); ++it) {
269 if (*it == new_config) {
270 it->add_child (new_config);
275 children.push_back (new Encoder());
276 Encoder & encoder = children.back();
278 if (data_width == 8 || data_width == 16) {
279 short_converter->add_output (encoder.init<short> (new_config));
280 } else if (data_width == 24 || data_width == 32) {
281 int_converter->add_output (encoder.init<int> (new_config));
283 float_converter->add_output (encoder.init<Sample> (new_config));
288 ExportGraphBuilder::SFC::operator== (FileSpec const & other_config) const
290 return config.format->sample_format() == other_config.format->sample_format();
295 ExportGraphBuilder::Normalizer::Normalizer (ExportGraphBuilder & parent, FileSpec const & new_config, framecnt_t /*max_frames*/)
298 std::string tmpfile_path = parent.session.session_directory().export_path();
299 tmpfile_path = Glib::build_filename(tmpfile_path, "XXXXXX");
300 char tmpfile_path_buf[tmpfile_path.size() + 1];
301 std::copy(tmpfile_path.begin(), tmpfile_path.end(), tmpfile_path_buf);
302 tmpfile_path_buf[tmpfile_path.size()] = '\0';
305 uint32_t const channels = config.channel_config->get_n_chans();
306 max_frames_out = 4086 - (4086 % channels); // TODO good chunk size
308 buffer.reset (new AllocatingProcessContext<Sample> (max_frames_out, channels));
309 peak_reader.reset (new PeakReader ());
310 normalizer.reset (new AudioGrapher::Normalizer (config.format->normalize_target()));
311 threader.reset (new Threader<Sample> (parent.thread_pool));
313 normalizer->alloc_buffer (max_frames_out);
314 normalizer->add_output (threader);
316 int format = ExportFormatBase::F_RAW | ExportFormatBase::SF_Float;
317 tmp_file.reset (new TmpFile<float> (tmpfile_path_buf, format, channels, config.format->sample_rate()));
318 tmp_file->FileWritten.connect_same_thread (post_processing_connection,
319 boost::bind (&Normalizer::start_post_processing, this));
321 add_child (new_config);
323 peak_reader->add_output (tmp_file);
326 ExportGraphBuilder::FloatSinkPtr
327 ExportGraphBuilder::Normalizer::sink ()
333 ExportGraphBuilder::Normalizer::add_child (FileSpec const & new_config)
335 for (boost::ptr_list<SFC>::iterator it = children.begin(); it != children.end(); ++it) {
336 if (*it == new_config) {
337 it->add_child (new_config);
342 children.push_back (new SFC (parent, new_config, max_frames_out));
343 threader->add_output (children.back().sink());
347 ExportGraphBuilder::Normalizer::operator== (FileSpec const & other_config) const
349 return config.format->normalize() == other_config.format->normalize() &&
350 config.format->normalize_target() == other_config.format->normalize_target();
354 ExportGraphBuilder::Normalizer::get_normalize_cycle_count() const
356 return static_cast<unsigned>(std::ceil(static_cast<float>(tmp_file->get_frames_written()) /
361 ExportGraphBuilder::Normalizer::process()
363 framecnt_t frames_read = tmp_file->read (*buffer);
364 return frames_read != buffer->frames();
368 ExportGraphBuilder::Normalizer::start_post_processing()
370 normalizer->set_peak (peak_reader->get_peak());
371 tmp_file->seek (0, SEEK_SET);
372 tmp_file->add_output (normalizer);
373 parent.normalizers.push_back (this);
378 ExportGraphBuilder::SRC::SRC (ExportGraphBuilder & parent, FileSpec const & new_config, framecnt_t max_frames)
382 converter.reset (new SampleRateConverter (new_config.channel_config->get_n_chans()));
383 ExportFormatSpecification & format = *new_config.format;
384 converter->init (parent.session.nominal_frame_rate(), format.sample_rate(), format.src_quality());
385 max_frames_out = converter->allocate_buffers (max_frames);
387 add_child (new_config);
390 ExportGraphBuilder::FloatSinkPtr
391 ExportGraphBuilder::SRC::sink ()
397 ExportGraphBuilder::SRC::add_child (FileSpec const & new_config)
399 if (new_config.format->normalize()) {
400 add_child_to_list (new_config, normalized_children);
402 add_child_to_list (new_config, children);
408 ExportGraphBuilder::SRC::add_child_to_list (FileSpec const & new_config, boost::ptr_list<T> & list)
410 for (typename boost::ptr_list<T>::iterator it = list.begin(); it != list.end(); ++it) {
411 if (*it == new_config) {
412 it->add_child (new_config);
417 list.push_back (new T (parent, new_config, max_frames_out));
418 converter->add_output (list.back().sink ());
422 ExportGraphBuilder::SRC::operator== (FileSpec const & other_config) const
424 return config.format->sample_rate() == other_config.format->sample_rate();
428 ExportGraphBuilder::SilenceHandler::SilenceHandler (ExportGraphBuilder & parent, FileSpec const & new_config, framecnt_t max_frames)
432 max_frames_in = max_frames;
433 framecnt_t sample_rate = parent.session.nominal_frame_rate();
435 silence_trimmer.reset (new SilenceTrimmer<Sample>(max_frames_in));
436 silence_trimmer->set_trim_beginning (config.format->trim_beginning());
437 silence_trimmer->set_trim_end (config.format->trim_end());
439 framecnt_t sb = config.format->silence_beginning_at (parent.timespan->get_start(), sample_rate);
440 framecnt_t se = config.format->silence_end_at (parent.timespan->get_end(), sample_rate);
442 silence_trimmer->add_silence_to_beginning (sb);
443 silence_trimmer->add_silence_to_end (se);
445 add_child (new_config);
448 ExportGraphBuilder::FloatSinkPtr
449 ExportGraphBuilder::SilenceHandler::sink ()
451 return silence_trimmer;
455 ExportGraphBuilder::SilenceHandler::add_child (FileSpec const & new_config)
457 for (boost::ptr_list<SRC>::iterator it = children.begin(); it != children.end(); ++it) {
458 if (*it == new_config) {
459 it->add_child (new_config);
464 children.push_back (new SRC (parent, new_config, max_frames_in));
465 silence_trimmer->add_output (children.back().sink());
469 ExportGraphBuilder::SilenceHandler::operator== (FileSpec const & other_config) const
471 ExportFormatSpecification & format = *config.format;
472 ExportFormatSpecification & other_format = *other_config.format;
473 return (format.trim_beginning() == other_format.trim_beginning()) &&
474 (format.trim_end() == other_format.trim_end()) &&
475 (format.silence_beginning_time() == other_format.silence_beginning_time()) &&
476 (format.silence_end_time() == other_format.silence_end_time());
481 ExportGraphBuilder::ChannelConfig::ChannelConfig (ExportGraphBuilder & parent, FileSpec const & new_config, ChannelMap & channel_map)
484 typedef ExportChannelConfiguration::ChannelList ChannelList;
488 framecnt_t max_frames = parent.session.engine().frames_per_cycle();
489 interleaver.reset (new Interleaver<Sample> ());
490 interleaver->init (new_config.channel_config->get_n_chans(), max_frames);
492 // Make the chunk size divisible by the channel count
493 int chan_count = new_config.channel_config->get_n_chans();
494 max_frames_out = 8192;
495 max_frames_out -= max_frames_out % chan_count;
496 chunker.reset (new Chunker<Sample> (max_frames_out));
497 interleaver->add_output(chunker);
499 ChannelList const & channel_list = config.channel_config->get_channels();
501 for (ChannelList::const_iterator it = channel_list.begin(); it != channel_list.end(); ++it, ++chan) {
502 ChannelMap::iterator map_it = channel_map.find (*it);
503 if (map_it == channel_map.end()) {
504 std::pair<ChannelMap::iterator, bool> result_pair =
505 channel_map.insert (std::make_pair (*it, IdentityVertexPtr (new IdentityVertex<Sample> ())));
506 assert (result_pair.second);
507 map_it = result_pair.first;
509 map_it->second->add_output (interleaver->input (chan));
512 add_child (new_config);
516 ExportGraphBuilder::ChannelConfig::add_child (FileSpec const & new_config)
518 assert (*this == new_config);
520 for (boost::ptr_list<SilenceHandler>::iterator it = children.begin(); it != children.end(); ++it) {
521 if (*it == new_config) {
522 it->add_child (new_config);
527 children.push_back (new SilenceHandler (parent, new_config, max_frames_out));
528 chunker->add_output (children.back().sink ());
532 ExportGraphBuilder::ChannelConfig::operator== (FileSpec const & other_config) const
534 return config.channel_config == other_config.channel_config;
537 } // namespace ARDOUR