2 Copyright (C) 2009 Paul Davis
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 #ifndef __ardour_export_graph_builder_h__
22 #define __ardour_export_graph_builder_h__
24 #include "ardour/export_handler.h"
25 #include "ardour/export_analysis.h"
27 #include "audiographer/utils/identity_vertex.h"
29 #include <boost/ptr_container/ptr_list.hpp>
30 #include <glibmm/threadpool.h>
32 namespace AudioGrapher {
33 class SampleRateConverter;
38 template <typename T> class Chunker;
39 template <typename T> class SampleFormatConverter;
40 template <typename T> class Interleaver;
41 template <typename T> class SndfileWriter;
42 template <typename T> class CmdPipeWriter;
43 template <typename T> class SilenceTrimmer;
44 template <typename T> class TmpFile;
45 template <typename T> class Threader;
46 template <typename T> class AllocatingProcessContext;
55 class LIBARDOUR_API ExportGraphBuilder
58 typedef ExportHandler::FileSpec FileSpec;
60 typedef boost::shared_ptr<AudioGrapher::Sink<Sample> > FloatSinkPtr;
61 typedef boost::shared_ptr<AudioGrapher::IdentityVertex<Sample> > IdentityVertexPtr;
62 typedef boost::shared_ptr<AudioGrapher::Analyser> AnalysisPtr;
63 typedef std::map<ExportChannelPtr, IdentityVertexPtr> ChannelMap;
64 typedef std::map<std::string, AnalysisPtr> AnalysisMap;
68 ExportGraphBuilder (Session const & session);
69 ~ExportGraphBuilder ();
71 int process (samplecnt_t samples, bool last_cycle);
72 bool post_process (); // returns true when finished
73 bool need_postprocessing () const { return !intermediates.empty(); }
74 bool realtime() const { return _realtime; }
75 unsigned get_postprocessing_cycle_count() const;
78 void cleanup (bool remove_out_files = false);
79 void set_current_timespan (boost::shared_ptr<ExportTimespan> span);
80 void add_config (FileSpec const & config, bool rt);
81 void get_analysis_results (AnalysisResults& results);
85 void add_analyser (const std::string& fn, AnalysisPtr ap) {
86 analysis_map.insert (std::make_pair (fn, ap));
89 void add_split_config (FileSpec const & config);
93 template <typename T> boost::shared_ptr<AudioGrapher::Sink<T> > init (FileSpec const & new_config);
94 void add_child (FileSpec const & new_config);
95 void remove_children ();
96 void destroy_writer (bool delete_out_file);
97 bool operator== (FileSpec const & other_config) const;
99 static int get_real_format (FileSpec const & config);
102 typedef boost::shared_ptr<AudioGrapher::SndfileWriter<Sample> > FloatWriterPtr;
103 typedef boost::shared_ptr<AudioGrapher::SndfileWriter<int> > IntWriterPtr;
104 typedef boost::shared_ptr<AudioGrapher::SndfileWriter<short> > ShortWriterPtr;
106 typedef boost::shared_ptr<AudioGrapher::CmdPipeWriter<Sample> > FloatPipePtr;
108 template<typename T> void init_writer (boost::shared_ptr<AudioGrapher::SndfileWriter<T> > & writer);
109 template<typename T> void init_writer (boost::shared_ptr<AudioGrapher::CmdPipeWriter<T> > & writer);
111 void copy_files (std::string orig_path);
114 std::list<ExportFilenamePtr> filenames;
115 PBD::ScopedConnection copy_files_connection;
117 std::string writer_filename;
119 // Only one of these should be available at a time
120 FloatWriterPtr float_writer;
121 IntWriterPtr int_writer;
122 ShortWriterPtr short_writer;
123 FloatPipePtr pipe_writer;
126 // sample format converter
129 // This constructor so that this can be constructed like a Normalizer
130 SFC (ExportGraphBuilder &, FileSpec const & new_config, samplecnt_t max_samples);
131 FloatSinkPtr sink ();
132 void add_child (FileSpec const & new_config);
133 void remove_children (bool remove_out_files);
134 bool operator== (FileSpec const & other_config) const;
135 void set_peak (float);
138 typedef boost::shared_ptr<AudioGrapher::Chunker<float> > ChunkerPtr;
139 typedef boost::shared_ptr<AudioGrapher::SampleFormatConverter<Sample> > FloatConverterPtr;
140 typedef boost::shared_ptr<AudioGrapher::SampleFormatConverter<int> > IntConverterPtr;
141 typedef boost::shared_ptr<AudioGrapher::SampleFormatConverter<short> > ShortConverterPtr;
144 boost::ptr_list<Encoder> children;
148 AnalysisPtr analyser;
150 // Only one of these should be available at a time
151 FloatConverterPtr float_converter;
152 IntConverterPtr int_converter;
153 ShortConverterPtr short_converter;
158 Intermediate (ExportGraphBuilder & parent, FileSpec const & new_config, samplecnt_t max_samples);
159 FloatSinkPtr sink ();
160 void add_child (FileSpec const & new_config);
161 void remove_children (bool remove_out_files);
162 bool operator== (FileSpec const & other_config) const;
164 unsigned get_postprocessing_cycle_count() const;
166 /// Returns true when finished
170 typedef boost::shared_ptr<AudioGrapher::PeakReader> PeakReaderPtr;
171 typedef boost::shared_ptr<AudioGrapher::LoudnessReader> LoudnessReaderPtr;
172 typedef boost::shared_ptr<AudioGrapher::Normalizer> NormalizerPtr;
173 typedef boost::shared_ptr<AudioGrapher::TmpFile<Sample> > TmpFilePtr;
174 typedef boost::shared_ptr<AudioGrapher::Threader<Sample> > ThreaderPtr;
175 typedef boost::shared_ptr<AudioGrapher::AllocatingProcessContext<Sample> > BufferPtr;
177 void prepare_post_processing ();
178 void start_post_processing ();
180 ExportGraphBuilder & parent;
183 samplecnt_t max_samples_out;
187 PeakReaderPtr peak_reader;
189 NormalizerPtr normalizer;
190 ThreaderPtr threader;
191 LoudnessReaderPtr loudness_reader;
192 boost::ptr_list<SFC> children;
194 PBD::ScopedConnectionList post_processing_connection;
197 // sample rate converter
200 SRC (ExportGraphBuilder & parent, FileSpec const & new_config, samplecnt_t max_samples);
201 FloatSinkPtr sink ();
202 void add_child (FileSpec const & new_config);
203 void remove_children (bool remove_out_files);
205 bool operator== (FileSpec const & other_config) const;
208 typedef boost::shared_ptr<AudioGrapher::SampleRateConverter> SRConverterPtr;
211 void add_child_to_list (FileSpec const & new_config, boost::ptr_list<T> & list);
213 ExportGraphBuilder & parent;
215 boost::ptr_list<SFC> children;
216 boost::ptr_list<Intermediate> intermediate_children;
217 SRConverterPtr converter;
218 samplecnt_t max_samples_out;
221 // Silence trimmer + adder
222 class SilenceHandler {
224 SilenceHandler (ExportGraphBuilder & parent, FileSpec const & new_config, samplecnt_t max_samples);
225 FloatSinkPtr sink ();
226 void add_child (FileSpec const & new_config);
227 void remove_children (bool remove_out_files);
228 bool operator== (FileSpec const & other_config) const;
231 typedef boost::shared_ptr<AudioGrapher::SilenceTrimmer<Sample> > SilenceTrimmerPtr;
233 ExportGraphBuilder & parent;
235 boost::ptr_list<SRC> children;
236 SilenceTrimmerPtr silence_trimmer;
237 samplecnt_t max_samples_in;
240 // channel configuration
241 class ChannelConfig {
243 ChannelConfig (ExportGraphBuilder & parent, FileSpec const & new_config, ChannelMap & channel_map);
244 void add_child (FileSpec const & new_config);
245 void remove_children (bool remove_out_files);
246 bool operator== (FileSpec const & other_config) const;
249 typedef boost::shared_ptr<AudioGrapher::Interleaver<Sample> > InterleaverPtr;
250 typedef boost::shared_ptr<AudioGrapher::Chunker<Sample> > ChunkerPtr;
252 ExportGraphBuilder & parent;
254 boost::ptr_list<SilenceHandler> children;
255 InterleaverPtr interleaver;
257 samplecnt_t max_samples_out;
260 Session const & session;
261 boost::shared_ptr<ExportTimespan> timespan;
263 // Roots for export processor trees
264 typedef boost::ptr_list<ChannelConfig> ChannelConfigList;
265 ChannelConfigList channel_configs;
267 // The sources of all data, each channel is read only once
270 samplecnt_t process_buffer_samples;
272 std::list<Intermediate *> intermediates;
274 AnalysisMap analysis_map;
278 Glib::ThreadPool thread_pool;
281 } // namespace ARDOUR
283 #endif /* __ardour_export_graph_builder_h__ */