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