Only show user-presets in favorite sidebar
[ardour.git] / libs / ardour / ardour / export_graph_builder.h
1 /*
2     Copyright (C) 2009 Paul Davis
3     Author: Sakari Bergen
4
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.
9
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.
14
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.
18
19 */
20
21 #ifndef __ardour_export_graph_builder_h__
22 #define __ardour_export_graph_builder_h__
23
24 #include "ardour/export_handler.h"
25 #include "ardour/export_analysis.h"
26
27 #include "audiographer/utils/identity_vertex.h"
28
29 #include <boost/ptr_container/ptr_list.hpp>
30 #include <glibmm/threadpool.h>
31
32 namespace AudioGrapher {
33         class SampleRateConverter;
34         class PeakReader;
35         class LoudnessReader;
36         class Normalizer;
37         class Analyser;
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;
47 }
48
49 namespace ARDOUR
50 {
51
52 class ExportTimespan;
53 class Session;
54
55 class LIBARDOUR_API ExportGraphBuilder
56 {
57   private:
58         typedef ExportHandler::FileSpec FileSpec;
59
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;
65
66   public:
67
68         ExportGraphBuilder (Session const & session);
69         ~ExportGraphBuilder ();
70
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;
76
77         void reset ();
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);
82
83   private:
84
85         void add_analyser (const std::string& fn, AnalysisPtr ap) {
86                 analysis_map.insert (std::make_pair (fn, ap));
87         }
88
89         void add_split_config (FileSpec const & config);
90
91         class Encoder {
92             public:
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;
98
99                 static int get_real_format (FileSpec const & config);
100
101                                                 private:
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;
105
106                 typedef boost::shared_ptr<AudioGrapher::CmdPipeWriter<Sample> > FloatPipePtr;
107
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);
110
111                 void copy_files (std::string orig_path);
112
113                 FileSpec               config;
114                 std::list<ExportFilenamePtr> filenames;
115                 PBD::ScopedConnection  copy_files_connection;
116
117                 std::string writer_filename;
118
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;
124         };
125
126         // sample format converter
127         class SFC {
128             public:
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);
136
137                                                 private:
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;
142
143                 FileSpec           config;
144                 boost::ptr_list<Encoder> children;
145                 int                data_width;
146
147                 ChunkerPtr      chunker;
148                 AnalysisPtr     analyser;
149                 bool            _analyse;
150                 // Only one of these should be available at a time
151                 FloatConverterPtr float_converter;
152                 IntConverterPtr int_converter;
153                 ShortConverterPtr short_converter;
154         };
155
156         class Intermediate {
157                                                 public:
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;
163
164                 unsigned get_postprocessing_cycle_count() const;
165
166                 /// Returns true when finished
167                 bool process ();
168
169                                                 private:
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;
176
177                 void prepare_post_processing ();
178                 void start_post_processing ();
179
180                 ExportGraphBuilder & parent;
181
182                 FileSpec        config;
183                 samplecnt_t      max_samples_out;
184                 bool            use_loudness;
185                 bool            use_peak;
186                 BufferPtr       buffer;
187                 PeakReaderPtr   peak_reader;
188                 TmpFilePtr      tmp_file;
189                 NormalizerPtr   normalizer;
190                 ThreaderPtr     threader;
191                 LoudnessReaderPtr    loudness_reader;
192                 boost::ptr_list<SFC> children;
193
194                 PBD::ScopedConnectionList post_processing_connection;
195         };
196
197         // sample rate converter
198         class SRC {
199             public:
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);
204
205                 bool operator== (FileSpec const & other_config) const;
206
207                                                 private:
208                 typedef boost::shared_ptr<AudioGrapher::SampleRateConverter> SRConverterPtr;
209
210                 template<typename T>
211                 void add_child_to_list (FileSpec const & new_config, boost::ptr_list<T> & list);
212
213                 ExportGraphBuilder &  parent;
214                 FileSpec              config;
215                 boost::ptr_list<SFC>  children;
216                 boost::ptr_list<Intermediate> intermediate_children;
217                 SRConverterPtr        converter;
218                 samplecnt_t            max_samples_out;
219         };
220
221         // Silence trimmer + adder
222         class SilenceHandler {
223             public:
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;
229
230                                                 private:
231                 typedef boost::shared_ptr<AudioGrapher::SilenceTrimmer<Sample> > SilenceTrimmerPtr;
232
233                 ExportGraphBuilder & parent;
234                 FileSpec             config;
235                 boost::ptr_list<SRC> children;
236                 SilenceTrimmerPtr    silence_trimmer;
237                 samplecnt_t           max_samples_in;
238         };
239
240         // channel configuration
241         class ChannelConfig {
242             public:
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;
247
248                                                 private:
249                 typedef boost::shared_ptr<AudioGrapher::Interleaver<Sample> > InterleaverPtr;
250                 typedef boost::shared_ptr<AudioGrapher::Chunker<Sample> > ChunkerPtr;
251
252                 ExportGraphBuilder &      parent;
253                 FileSpec                  config;
254                 boost::ptr_list<SilenceHandler> children;
255                 InterleaverPtr            interleaver;
256                 ChunkerPtr                chunker;
257                 samplecnt_t                max_samples_out;
258         };
259
260         Session const & session;
261         boost::shared_ptr<ExportTimespan> timespan;
262
263         // Roots for export processor trees
264         typedef boost::ptr_list<ChannelConfig> ChannelConfigList;
265         ChannelConfigList channel_configs;
266
267         // The sources of all data, each channel is read only once
268         ChannelMap channels;
269
270         samplecnt_t process_buffer_samples;
271
272         std::list<Intermediate *> intermediates;
273
274         AnalysisMap analysis_map;
275
276         bool _realtime;
277
278         Glib::ThreadPool thread_pool;
279 };
280
281 } // namespace ARDOUR
282
283 #endif /* __ardour_export_graph_builder_h__ */