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