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