Inrease the export "chunk size" to speed it up over 10% at least in some situations
[ardour.git] / libs / ardour / export_graph_builder.cc
1 #include "ardour/export_graph_builder.h"
2
3 #include "audiographer/process_context.h"
4 #include "audiographer/general/chunker.h"
5 #include "audiographer/general/interleaver.h"
6 #include "audiographer/general/normalizer.h"
7 #include "audiographer/general/peak_reader.h"
8 #include "audiographer/general/sample_format_converter.h"
9 #include "audiographer/general/sr_converter.h"
10 #include "audiographer/general/silence_trimmer.h"
11 #include "audiographer/general/threader.h"
12 #include "audiographer/sndfile/tmp_file.h"
13 #include "audiographer/sndfile/sndfile_writer.h"
14
15 #include "ardour/audioengine.h"
16 #include "ardour/export_channel_configuration.h"
17 #include "ardour/export_filename.h"
18 #include "ardour/export_format_specification.h"
19 #include "ardour/export_timespan.h"
20 #include "ardour/sndfile_helpers.h"
21
22 #include "pbd/file_utils.h"
23 #include "pbd/cpus.h"
24
25 using namespace AudioGrapher;
26 using std::string;
27
28 namespace ARDOUR {
29
30 ExportGraphBuilder::ExportGraphBuilder (Session const & session)
31   : session (session)
32   , thread_pool (hardware_concurrency())
33 {
34         process_buffer_frames = session.engine().frames_per_cycle();
35 }
36
37 ExportGraphBuilder::~ExportGraphBuilder ()
38 {
39 }
40
41 int
42 ExportGraphBuilder::process (framecnt_t frames, bool last_cycle)
43 {
44         assert(frames <= process_buffer_frames);
45
46         for (ChannelMap::iterator it = channels.begin(); it != channels.end(); ++it) {
47                 Sample const * process_buffer = 0;
48                 it->first->read (process_buffer, frames);
49                 ConstProcessContext<Sample> context(process_buffer, frames, 1);
50                 if (last_cycle) { context().set_flag (ProcessContext<Sample>::EndOfInput); }
51                 it->second->process (context);
52         }
53
54         return 0;
55 }
56
57 bool
58 ExportGraphBuilder::process_normalize ()
59 {
60         for (std::list<Normalizer *>::iterator it = normalizers.begin(); it != normalizers.end(); /* ++ in loop */) {
61                 if ((*it)->process()) {
62                         it = normalizers.erase (it);
63                 } else {
64                         ++it;
65                 }
66         }
67
68         return normalizers.empty();
69 }
70
71 unsigned
72 ExportGraphBuilder::get_normalize_cycle_count() const
73 {
74         unsigned max = 0;
75         for (std::list<Normalizer *>::const_iterator it = normalizers.begin(); it != normalizers.end(); ++it) {
76                 max = std::max(max, (*it)->get_normalize_cycle_count());
77         }
78         return max;
79 }
80
81 void
82 ExportGraphBuilder::reset ()
83 {
84         timespan.reset();
85         channel_configs.clear ();
86         channels.clear ();
87         normalizers.clear ();
88 }
89
90 void
91 ExportGraphBuilder::set_current_timespan (boost::shared_ptr<ExportTimespan> span)
92 {
93         timespan = span;
94 }
95
96 void
97 ExportGraphBuilder::add_config (FileSpec const & config)
98 {
99         ExportChannelConfiguration::ChannelList const & channels =
100                 config.channel_config->get_channels();
101         for(ExportChannelConfiguration::ChannelList::const_iterator it = channels.begin();
102             it != channels.end(); ++it) {
103                 (*it)->set_max_buffer_size(process_buffer_frames);
104         }
105
106         // If the sample rate is "session rate", change it to the real value.
107         // However, we need to copy it to not change the config which is saved...
108         FileSpec new_config (config);
109         new_config.format.reset(new ExportFormatSpecification(*new_config.format));
110         if(new_config.format->sample_rate() == ExportFormatBase::SR_Session) {
111                 framecnt_t session_rate = session.nominal_frame_rate();
112                 new_config.format->set_sample_rate(ExportFormatBase::nearest_sample_rate(session_rate));
113         }
114
115
116         if (!new_config.channel_config->get_split ()) {
117                 add_split_config (new_config);
118                 return;
119         }
120
121         // Split channel configurations are split into several channel configurations,
122         // each corresponding to a file, at this stage
123         typedef std::list<boost::shared_ptr<ExportChannelConfiguration> > ConfigList;
124         ConfigList file_configs;
125         new_config.channel_config->configurations_for_files (file_configs);
126
127         unsigned chan = 1;
128         for (ConfigList::iterator it = file_configs.begin(); it != file_configs.end(); ++it, ++chan) {
129                 FileSpec copy = new_config;
130                 copy.channel_config = *it;
131
132                 copy.filename.reset (new ExportFilename (*copy.filename));
133                 copy.filename->include_channel = true;
134                 copy.filename->set_channel (chan);
135
136                 add_split_config (copy);
137         }
138 }
139
140 void
141 ExportGraphBuilder::add_split_config (FileSpec const & config)
142 {
143         for (ChannelConfigList::iterator it = channel_configs.begin(); it != channel_configs.end(); ++it) {
144                 if (*it == config) {
145                         it->add_child (config);
146                         return;
147                 }
148         }
149
150         // No duplicate channel config found, create new one
151         channel_configs.push_back (new ChannelConfig (*this, config, channels));
152 }
153
154 /* Encoder */
155
156 template <>
157 boost::shared_ptr<AudioGrapher::Sink<Sample> >
158 ExportGraphBuilder::Encoder::init (FileSpec const & new_config)
159 {
160         config = new_config;
161         init_writer (float_writer);
162         return float_writer;
163 }
164
165 template <>
166 boost::shared_ptr<AudioGrapher::Sink<int> >
167 ExportGraphBuilder::Encoder::init (FileSpec const & new_config)
168 {
169         config = new_config;
170         init_writer (int_writer);
171         return int_writer;
172 }
173
174 template <>
175 boost::shared_ptr<AudioGrapher::Sink<short> >
176 ExportGraphBuilder::Encoder::init (FileSpec const & new_config)
177 {
178         config = new_config;
179         init_writer (short_writer);
180         return short_writer;
181 }
182
183 void
184 ExportGraphBuilder::Encoder::add_child (FileSpec const & new_config)
185 {
186         filenames.push_back (new_config.filename);
187 }
188
189 bool
190 ExportGraphBuilder::Encoder::operator== (FileSpec const & other_config) const
191 {
192         return get_real_format (config) == get_real_format (other_config);
193 }
194
195 int
196 ExportGraphBuilder::Encoder::get_real_format (FileSpec const & config)
197 {
198         ExportFormatSpecification & format = *config.format;
199         return format.format_id() | format.sample_format() | format.endianness();
200 }
201
202 template<typename T>
203 void
204 ExportGraphBuilder::Encoder::init_writer (boost::shared_ptr<AudioGrapher::SndfileWriter<T> > & writer)
205 {
206         unsigned channels = config.channel_config->get_n_chans();
207         int format = get_real_format (config);
208         config.filename->set_channel_config(config.channel_config);
209         string filename = config.filename->get_path (config.format);
210
211         writer.reset (new AudioGrapher::SndfileWriter<T> (filename, format, channels, config.format->sample_rate(), config.broadcast_info));
212         writer->FileWritten.connect_same_thread (copy_files_connection, boost::bind (&ExportGraphBuilder::Encoder::copy_files, this, _1));
213 }
214
215 void
216 ExportGraphBuilder::Encoder::copy_files (std::string orig_path)
217 {
218         while (filenames.size()) {
219                 ExportFilenamePtr & filename = filenames.front();
220                 PBD::copy_file (orig_path, filename->get_path (config.format).c_str());
221                 filenames.pop_front();
222         }
223 }
224
225 /* SFC */
226
227 ExportGraphBuilder::SFC::SFC (ExportGraphBuilder &, FileSpec const & new_config, framecnt_t max_frames)
228   : data_width(0)
229 {
230         config = new_config;
231         data_width = sndfile_data_width (Encoder::get_real_format (config));
232         unsigned channels = new_config.channel_config->get_n_chans();
233
234         if (data_width == 8 || data_width == 16) {
235                 short_converter = ShortConverterPtr (new SampleFormatConverter<short> (channels));
236                 short_converter->init (max_frames, config.format->dither_type(), data_width);
237                 add_child (config);
238         } else if (data_width == 24 || data_width == 32) {
239                 int_converter = IntConverterPtr (new SampleFormatConverter<int> (channels));
240                 int_converter->init (max_frames, config.format->dither_type(), data_width);
241                 add_child (config);
242         } else {
243                 int actual_data_width = 8 * sizeof(Sample);
244                 float_converter = FloatConverterPtr (new SampleFormatConverter<Sample> (channels));
245                 float_converter->init (max_frames, config.format->dither_type(), actual_data_width);
246                 add_child (config);
247         }
248 }
249
250 ExportGraphBuilder::FloatSinkPtr
251 ExportGraphBuilder::SFC::sink ()
252 {
253         if (data_width == 8 || data_width == 16) {
254                 return short_converter;
255         } else if (data_width == 24 || data_width == 32) {
256                 return int_converter;
257         } else {
258                 return float_converter;
259         }
260 }
261
262 void
263 ExportGraphBuilder::SFC::add_child (FileSpec const & new_config)
264 {
265         for (boost::ptr_list<Encoder>::iterator it = children.begin(); it != children.end(); ++it) {
266                 if (*it == new_config) {
267                         it->add_child (new_config);
268                         return;
269                 }
270         }
271
272         children.push_back (new Encoder());
273         Encoder & encoder = children.back();
274
275         if (data_width == 8 || data_width == 16) {
276                 short_converter->add_output (encoder.init<short> (new_config));
277         } else if (data_width == 24 || data_width == 32) {
278                 int_converter->add_output (encoder.init<int> (new_config));
279         } else {
280                 float_converter->add_output (encoder.init<Sample> (new_config));
281         }
282 }
283
284 bool
285 ExportGraphBuilder::SFC::operator== (FileSpec const & other_config) const
286 {
287         return config.format->sample_format() == other_config.format->sample_format();
288 }
289
290 /* Normalizer */
291
292 ExportGraphBuilder::Normalizer::Normalizer (ExportGraphBuilder & parent, FileSpec const & new_config, framecnt_t /*max_frames*/)
293   : parent (parent)
294 {
295         config = new_config;
296         uint32_t const channels = config.channel_config->get_n_chans();
297         max_frames_out = 4086 - (4086 % channels); // TODO good chunk size
298         
299         buffer.reset (new AllocatingProcessContext<Sample> (max_frames_out, channels));
300         peak_reader.reset (new PeakReader ());
301         normalizer.reset (new AudioGrapher::Normalizer (config.format->normalize_target()));
302         threader.reset (new Threader<Sample> (parent.thread_pool));
303
304         normalizer->alloc_buffer (max_frames_out);
305         normalizer->add_output (threader);
306
307         int format = ExportFormatBase::F_RAW | ExportFormatBase::SF_Float;
308         tmp_file.reset (new TmpFile<float> (format, channels, config.format->sample_rate()));
309         tmp_file->FileWritten.connect_same_thread (post_processing_connection,
310                                                    boost::bind (&Normalizer::start_post_processing, this));
311
312         add_child (new_config);
313
314         peak_reader->add_output (tmp_file);
315 }
316
317 ExportGraphBuilder::FloatSinkPtr
318 ExportGraphBuilder::Normalizer::sink ()
319 {
320         return peak_reader;
321 }
322
323 void
324 ExportGraphBuilder::Normalizer::add_child (FileSpec const & new_config)
325 {
326         for (boost::ptr_list<SFC>::iterator it = children.begin(); it != children.end(); ++it) {
327                 if (*it == new_config) {
328                         it->add_child (new_config);
329                         return;
330                 }
331         }
332
333         children.push_back (new SFC (parent, new_config, max_frames_out));
334         threader->add_output (children.back().sink());
335 }
336
337 bool
338 ExportGraphBuilder::Normalizer::operator== (FileSpec const & other_config) const
339 {
340         return config.format->normalize() == other_config.format->normalize() &&
341                config.format->normalize_target() == other_config.format->normalize_target();
342 }
343
344 unsigned
345 ExportGraphBuilder::Normalizer::get_normalize_cycle_count() const
346 {
347         return static_cast<unsigned>(std::ceil(static_cast<float>(tmp_file->get_frames_written()) /
348                                                max_frames_out));
349 }
350
351 bool
352 ExportGraphBuilder::Normalizer::process()
353 {
354         framecnt_t frames_read = tmp_file->read (*buffer);
355         return frames_read != buffer->frames();
356 }
357
358 void
359 ExportGraphBuilder::Normalizer::start_post_processing()
360 {
361         normalizer->set_peak (peak_reader->get_peak());
362         tmp_file->seek (0, SEEK_SET);
363         tmp_file->add_output (normalizer);
364         parent.normalizers.push_back (this);
365 }
366
367 /* SRC */
368
369 ExportGraphBuilder::SRC::SRC (ExportGraphBuilder & parent, FileSpec const & new_config, framecnt_t max_frames)
370   : parent (parent)
371 {
372         config = new_config;
373         converter.reset (new SampleRateConverter (new_config.channel_config->get_n_chans()));
374         ExportFormatSpecification & format = *new_config.format;
375         converter->init (parent.session.nominal_frame_rate(), format.sample_rate(), format.src_quality());
376         max_frames_out = converter->allocate_buffers (max_frames);
377
378         add_child (new_config);
379 }
380
381 ExportGraphBuilder::FloatSinkPtr
382 ExportGraphBuilder::SRC::sink ()
383 {
384         return converter;
385 }
386
387 void
388 ExportGraphBuilder::SRC::add_child (FileSpec const & new_config)
389 {
390         if (new_config.format->normalize()) {
391                 add_child_to_list (new_config, normalized_children);
392         } else {
393                 add_child_to_list (new_config, children);
394         }
395 }
396
397 template<typename T>
398 void
399 ExportGraphBuilder::SRC::add_child_to_list (FileSpec const & new_config, boost::ptr_list<T> & list)
400 {
401         for (typename boost::ptr_list<T>::iterator it = list.begin(); it != list.end(); ++it) {
402                 if (*it == new_config) {
403                         it->add_child (new_config);
404                         return;
405                 }
406         }
407
408         list.push_back (new T (parent, new_config, max_frames_out));
409         converter->add_output (list.back().sink ());
410 }
411
412 bool
413 ExportGraphBuilder::SRC::operator== (FileSpec const & other_config) const
414 {
415         return config.format->sample_rate() == other_config.format->sample_rate();
416 }
417
418 /* SilenceHandler */
419 ExportGraphBuilder::SilenceHandler::SilenceHandler (ExportGraphBuilder & parent, FileSpec const & new_config, framecnt_t max_frames)
420   : parent (parent)
421 {
422         config = new_config;
423         max_frames_in = max_frames;
424         framecnt_t sample_rate = parent.session.nominal_frame_rate();
425
426         silence_trimmer.reset (new SilenceTrimmer<Sample>(max_frames_in));
427         silence_trimmer->set_trim_beginning (config.format->trim_beginning());
428         silence_trimmer->set_trim_end (config.format->trim_end());
429
430         framecnt_t sb = config.format->silence_beginning_at (parent.timespan->get_start(), sample_rate);
431         framecnt_t se = config.format->silence_end_at (parent.timespan->get_end(), sample_rate);
432
433         silence_trimmer->add_silence_to_beginning (sb);
434         silence_trimmer->add_silence_to_end (se);
435
436         add_child (new_config);
437 }
438
439 ExportGraphBuilder::FloatSinkPtr
440 ExportGraphBuilder::SilenceHandler::sink ()
441 {
442         return silence_trimmer;
443 }
444
445 void
446 ExportGraphBuilder::SilenceHandler::add_child (FileSpec const & new_config)
447 {
448         for (boost::ptr_list<SRC>::iterator it = children.begin(); it != children.end(); ++it) {
449                 if (*it == new_config) {
450                         it->add_child (new_config);
451                         return;
452                 }
453         }
454
455         children.push_back (new SRC (parent, new_config, max_frames_in));
456         silence_trimmer->add_output (children.back().sink());
457 }
458
459 bool
460 ExportGraphBuilder::SilenceHandler::operator== (FileSpec const & other_config) const
461 {
462         ExportFormatSpecification & format = *config.format;
463         ExportFormatSpecification & other_format = *other_config.format;
464         return (format.trim_beginning() == other_format.trim_beginning()) &&
465                (format.trim_end() == other_format.trim_end()) &&
466                (format.silence_beginning_time() == other_format.silence_beginning_time()) &&
467                (format.silence_end_time() == other_format.silence_end_time());
468 }
469
470 /* ChannelConfig */
471
472 ExportGraphBuilder::ChannelConfig::ChannelConfig (ExportGraphBuilder & parent, FileSpec const & new_config, ChannelMap & channel_map)
473   : parent (parent)
474 {
475         typedef ExportChannelConfiguration::ChannelList ChannelList;
476
477         config = new_config;
478
479         framecnt_t max_frames = parent.session.engine().frames_per_cycle();
480         interleaver.reset (new Interleaver<Sample> ());
481         interleaver->init (new_config.channel_config->get_n_chans(), max_frames);
482
483         // Make the chunk size divisible by the channel count
484         int chan_count = new_config.channel_config->get_n_chans();
485         max_frames_out = 8192;
486         max_frames_out -= max_frames_out % chan_count;
487         chunker.reset (new Chunker<Sample> (max_frames_out));
488         interleaver->add_output(chunker);
489
490         ChannelList const & channel_list = config.channel_config->get_channels();
491         unsigned chan = 0;
492         for (ChannelList::const_iterator it = channel_list.begin(); it != channel_list.end(); ++it, ++chan) {
493                 ChannelMap::iterator map_it = channel_map.find (*it);
494                 if (map_it == channel_map.end()) {
495                         std::pair<ChannelMap::iterator, bool> result_pair =
496                                 channel_map.insert (std::make_pair (*it, IdentityVertexPtr (new IdentityVertex<Sample> ())));
497                         assert (result_pair.second);
498                         map_it = result_pair.first;
499                 }
500                 map_it->second->add_output (interleaver->input (chan));
501         }
502
503         add_child (new_config);
504 }
505
506 void
507 ExportGraphBuilder::ChannelConfig::add_child (FileSpec const & new_config)
508 {
509         assert (*this == new_config);
510
511         for (boost::ptr_list<SilenceHandler>::iterator it = children.begin(); it != children.end(); ++it) {
512                 if (*it == new_config) {
513                         it->add_child (new_config);
514                         return;
515                 }
516         }
517
518         children.push_back (new SilenceHandler (parent, new_config, max_frames_out));
519         chunker->add_output (children.back().sink ());
520 }
521
522 bool
523 ExportGraphBuilder::ChannelConfig::operator== (FileSpec const & other_config) const
524 {
525         return config.channel_config == other_config.channel_config;
526 }
527
528 } // namespace ARDOUR