1 #include "ardour/export_graph_builder.h"
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"
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/sndfile_helpers.h"
20 #include "pbd/filesystem.h"
22 using namespace AudioGrapher;
26 ExportGraphBuilder::ExportGraphBuilder (Session const & session)
28 , thread_pool (4) // FIXME thread amount to cores amount
30 process_buffer_frames = session.engine().frames_per_cycle();
31 process_buffer = new Sample[process_buffer_frames];
34 ExportGraphBuilder::~ExportGraphBuilder ()
36 delete [] process_buffer;
40 ExportGraphBuilder::process (nframes_t /* frames */, bool last_cycle)
42 for (ChannelMap::iterator it = channels.begin(); it != channels.end(); ++it) {
43 it->first->read (process_buffer, process_buffer_frames);
44 ProcessContext<Sample> context(process_buffer, process_buffer_frames, 1);
45 if (last_cycle) { context.set_flag (ProcessContext<Sample>::EndOfInput); }
46 it->second->process (context);
53 ExportGraphBuilder::process_normalize ()
55 for (std::list<Normalizer *>::iterator it = normalizers.begin(); it != normalizers.end(); /* ++ in loop */) {
56 if ((*it)->process()) {
57 it = normalizers.erase (it);
63 return normalizers.empty();
67 ExportGraphBuilder::reset ()
69 channel_configs.clear ();
75 ExportGraphBuilder::add_config (FileSpec const & config)
77 if (!config.channel_config->get_split ()) {
78 add_split_config (config);
82 // Split channel configurations are split into several channel configurations,
83 // each corresponding to a file, at this stage
84 typedef std::list<boost::shared_ptr<ExportChannelConfiguration> > ConfigList;
85 ConfigList file_configs;
86 config.channel_config->configurations_for_files (file_configs);
89 for (ConfigList::iterator it = file_configs.begin(); it != file_configs.end(); ++it, ++chan) {
90 FileSpec copy = config;
91 copy.channel_config = *it;
93 copy.filename.reset (new ExportFilename (*copy.filename));
94 copy.filename->include_channel = true;
95 copy.filename->set_channel (chan);
97 add_split_config (copy);
102 ExportGraphBuilder::add_split_config (FileSpec const & config)
104 for (ChannelConfigList::iterator it = channel_configs.begin(); it != channel_configs.end(); ++it) {
106 it->add_child (config);
111 // No duplicate channel config found, create new one
112 channel_configs.push_back (new ChannelConfig (*this, config, channels));
118 boost::shared_ptr<AudioGrapher::Sink<Sample> >
119 ExportGraphBuilder::Encoder::init (FileSpec const & new_config)
122 init_writer (float_writer);
127 boost::shared_ptr<AudioGrapher::Sink<int> >
128 ExportGraphBuilder::Encoder::init (FileSpec const & new_config)
131 init_writer (int_writer);
136 boost::shared_ptr<AudioGrapher::Sink<short> >
137 ExportGraphBuilder::Encoder::init (FileSpec const & new_config)
140 init_writer (short_writer);
145 ExportGraphBuilder::Encoder::add_child (FileSpec const & new_config)
147 filenames.push_back (new_config.filename);
151 ExportGraphBuilder::Encoder::operator== (FileSpec const & other_config) const
153 return get_real_format (config) == get_real_format (other_config);
157 ExportGraphBuilder::Encoder::get_real_format (FileSpec const & config)
159 ExportFormatSpecification & format = *config.format;
160 return format.format_id() | format.sample_format() | format.endianness();
165 ExportGraphBuilder::Encoder::init_writer (boost::shared_ptr<AudioGrapher::SndfileWriter<T> > & writer)
167 unsigned channels = config.channel_config->get_n_chans();
168 int format = get_real_format (config);
169 Glib::ustring filename = config.filename->get_path (config.format);
171 writer.reset (new AudioGrapher::SndfileWriter<T> (filename, format, channels, config.format->sample_rate()));
172 writer->FileWritten.connect_same_thread (copy_files_connection, boost::bind (&ExportGraphBuilder::Encoder::copy_files, this, _1));
176 ExportGraphBuilder::Encoder::copy_files (std::string orig_path)
178 while (filenames.size()) {
179 FilenamePtr & filename = filenames.front();
180 PBD::sys::copy_file (orig_path, filename->get_path (config.format).c_str());
181 filenames.pop_front();
187 ExportGraphBuilder::SFC::SFC (ExportGraphBuilder &, FileSpec const & new_config, nframes_t max_frames)
191 data_width = sndfile_data_width (Encoder::get_real_format (config));
192 unsigned channels = new_config.channel_config->get_n_chans();
194 if (data_width == 8 || data_width == 16) {
195 short_converter = ShortConverterPtr (new SampleFormatConverter<short> (channels));
196 short_converter->init (max_frames, config.format->dither_type(), data_width);
198 } else if (data_width == 24 || data_width == 32) {
199 int_converter = IntConverterPtr (new SampleFormatConverter<int> (channels));
200 int_converter->init (max_frames, config.format->dither_type(), data_width);
203 float_converter = FloatConverterPtr (new SampleFormatConverter<Sample> (channels));
204 float_converter->init (max_frames, config.format->dither_type(), data_width);
209 ExportGraphBuilder::FloatSinkPtr
210 ExportGraphBuilder::SFC::sink ()
212 if (data_width == 8 || data_width == 16) {
213 return short_converter;
214 } else if (data_width == 24 || data_width == 32) {
215 return int_converter;
217 return float_converter;
222 ExportGraphBuilder::SFC::add_child (FileSpec const & new_config)
224 for (boost::ptr_list<Encoder>::iterator it = children.begin(); it != children.end(); ++it) {
225 if (*it == new_config) {
226 it->add_child (new_config);
231 children.push_back (new Encoder());
232 Encoder & encoder = children.back();
234 if (data_width == 8 || data_width == 16) {
235 short_converter->add_output (encoder.init<short> (new_config));
236 } else if (data_width == 24 || data_width == 32) {
237 int_converter->add_output (encoder.init<int> (new_config));
239 float_converter->add_output (encoder.init<Sample> (new_config));
244 ExportGraphBuilder::SFC::operator== (FileSpec const & other_config) const
246 return config.format->sample_format() == other_config.format->sample_format();
251 ExportGraphBuilder::Normalizer::Normalizer (ExportGraphBuilder & parent, FileSpec const & new_config, nframes_t /*max_frames*/)
255 max_frames_out = 4086; // TODO good chunk size
257 buffer.reset (new AllocatingProcessContext<Sample> (max_frames_out, config.channel_config->get_n_chans()));
258 peak_reader.reset (new PeakReader ());
259 normalizer.reset (new AudioGrapher::Normalizer (config.format->normalize_target()));
260 threader.reset (new Threader<Sample> (parent.thread_pool));
262 normalizer->alloc_buffer (max_frames_out);
263 normalizer->add_output (threader);
265 int format = ExportFormatBase::F_RAW | ExportFormatBase::SF_Float;
266 tmp_file.reset (new TmpFile<float> (format, config.channel_config->get_n_chans(),
267 config.format->sample_rate()));
268 tmp_file->FileWritten.connect_same_thread (post_processing_connection, boost::bind (&Normalizer::start_post_processing, this));
270 add_child (new_config);
272 peak_reader->add_output (tmp_file);
275 ExportGraphBuilder::FloatSinkPtr
276 ExportGraphBuilder::Normalizer::sink ()
282 ExportGraphBuilder::Normalizer::add_child (FileSpec const & new_config)
284 for (boost::ptr_list<SFC>::iterator it = children.begin(); it != children.end(); ++it) {
285 if (*it == new_config) {
286 it->add_child (new_config);
291 children.push_back (new SFC (parent, new_config, max_frames_out));
292 threader->add_output (children.back().sink());
296 ExportGraphBuilder::Normalizer::operator== (FileSpec const & other_config) const
298 return config.format->normalize() == other_config.format->normalize() &&
299 config.format->normalize_target() == other_config.format->normalize_target();
303 ExportGraphBuilder::Normalizer::process()
305 nframes_t frames_read = tmp_file->read (*buffer);
306 return frames_read != buffer->frames();
310 ExportGraphBuilder::Normalizer::start_post_processing()
312 normalizer->set_peak (peak_reader->get_peak());
313 tmp_file->seek (0, SEEK_SET);
314 tmp_file->add_output (normalizer);
315 parent.normalizers.push_back (this);
320 ExportGraphBuilder::SRC::SRC (ExportGraphBuilder & parent, FileSpec const & new_config, nframes_t max_frames)
324 converter.reset (new SampleRateConverter (new_config.channel_config->get_n_chans()));
325 ExportFormatSpecification & format = *new_config.format;
326 converter->init (parent.session.nominal_frame_rate(), format.sample_rate(), format.src_quality());
327 max_frames_out = converter->allocate_buffers (max_frames);
329 add_child (new_config);
332 ExportGraphBuilder::FloatSinkPtr
333 ExportGraphBuilder::SRC::sink ()
339 ExportGraphBuilder::SRC::add_child (FileSpec const & new_config)
341 if (new_config.format->normalize()) {
342 add_child_to_list (new_config, normalized_children);
344 add_child_to_list (new_config, children);
350 ExportGraphBuilder::SRC::add_child_to_list (FileSpec const & new_config, boost::ptr_list<T> & list)
352 for (typename boost::ptr_list<T>::iterator it = list.begin(); it != list.end(); ++it) {
353 if (*it == new_config) {
354 it->add_child (new_config);
359 list.push_back (new T (parent, new_config, max_frames_out));
360 converter->add_output (list.back().sink ());
364 ExportGraphBuilder::SRC::operator== (FileSpec const & other_config) const
366 return config.format->sample_rate() == other_config.format->sample_rate();
370 ExportGraphBuilder::SilenceHandler::SilenceHandler (ExportGraphBuilder & parent, FileSpec const & new_config, nframes_t max_frames)
374 max_frames_in = max_frames;
375 nframes_t sample_rate = parent.session.nominal_frame_rate();
377 silence_trimmer.reset (new SilenceTrimmer<Sample>(max_frames_in));
378 silence_trimmer->set_trim_beginning (config.format->trim_beginning());
379 silence_trimmer->set_trim_end (config.format->trim_end());
380 silence_trimmer->add_silence_to_beginning (config.format->silence_beginning(sample_rate));
381 silence_trimmer->add_silence_to_end (config.format->silence_end(sample_rate));
383 add_child (new_config);
386 ExportGraphBuilder::FloatSinkPtr
387 ExportGraphBuilder::SilenceHandler::sink ()
389 return silence_trimmer;
393 ExportGraphBuilder::SilenceHandler::add_child (FileSpec const & new_config)
395 for (boost::ptr_list<SRC>::iterator it = children.begin(); it != children.end(); ++it) {
396 if (*it == new_config) {
397 it->add_child (new_config);
402 children.push_back (new SRC (parent, new_config, max_frames_in));
403 silence_trimmer->add_output (children.back().sink());
407 ExportGraphBuilder::SilenceHandler::operator== (FileSpec const & other_config) const
409 ExportFormatSpecification & format = *config.format;
410 ExportFormatSpecification & other_format = *other_config.format;
411 return (format.trim_beginning() == other_format.trim_beginning()) &&
412 (format.trim_end() == other_format.trim_end()) &&
413 (format.silence_beginning() == other_format.silence_beginning()) &&
414 (format.silence_end() == other_format.silence_end());
419 ExportGraphBuilder::ChannelConfig::ChannelConfig (ExportGraphBuilder & parent, FileSpec const & new_config, ChannelMap & channel_map)
422 typedef ExportChannelConfiguration::ChannelList ChannelList;
425 max_frames = parent.session.engine().frames_per_cycle();
427 interleaver.reset (new Interleaver<Sample> ());
428 interleaver->init (new_config.channel_config->get_n_chans(), max_frames);
430 ChannelList const & channel_list = config.channel_config->get_channels();
432 for (ChannelList::const_iterator it = channel_list.begin(); it != channel_list.end(); ++it, ++chan) {
433 ChannelMap::iterator map_it = channel_map.find (*it);
434 if (map_it == channel_map.end()) {
435 std::pair<ChannelMap::iterator, bool> result_pair =
436 channel_map.insert (std::make_pair (*it, IdentityVertexPtr (new IdentityVertex<Sample> ())));
437 assert (result_pair.second);
438 map_it = result_pair.first;
440 map_it->second->add_output (interleaver->input (chan));
443 add_child (new_config);
447 ExportGraphBuilder::ChannelConfig::add_child (FileSpec const & new_config)
449 for (boost::ptr_list<SilenceHandler>::iterator it = children.begin(); it != children.end(); ++it) {
450 if (*it == new_config) {
451 it->add_child (new_config);
456 nframes_t max_frames_out = new_config.channel_config->get_n_chans() * max_frames;
457 children.push_back (new SilenceHandler (parent, new_config, max_frames_out));
458 interleaver->add_output (children.back().sink ());
462 ExportGraphBuilder::ChannelConfig::operator== (FileSpec const & other_config) const
464 return config.channel_config == other_config.channel_config;
467 } // namespace ARDOUR