Normalize API rename part 2
[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 SilenceTrimmer;
43         template <typename T> class TmpFile;
44         template <typename T> class Threader;
45         template <typename T> class AllocatingProcessContext;
46 }
47
48 namespace ARDOUR
49 {
50
51 class ExportTimespan;
52 class Session;
53
54 class LIBARDOUR_API ExportGraphBuilder
55 {
56   private:
57         typedef ExportHandler::FileSpec FileSpec;
58
59         typedef boost::shared_ptr<AudioGrapher::Sink<Sample> > FloatSinkPtr;
60         typedef boost::shared_ptr<AudioGrapher::IdentityVertex<Sample> > IdentityVertexPtr;
61         typedef boost::shared_ptr<AudioGrapher::Analyser> AnalysisPtr;
62         typedef std::map<ExportChannelPtr,  IdentityVertexPtr> ChannelMap;
63         typedef std::map<std::string, AnalysisPtr> AnalysisMap;
64
65   public:
66
67         ExportGraphBuilder (Session const & session);
68         ~ExportGraphBuilder ();
69
70         int process (framecnt_t frames, bool last_cycle);
71         bool post_process (); // returns true when finished
72         bool need_postprocessing () const { return !intermediates.empty(); }
73         bool realtime() const { return _realtime; }
74         unsigned get_postprocessing_cycle_count() const;
75
76         void reset ();
77         void cleanup (bool remove_out_files = false);
78         void set_current_timespan (boost::shared_ptr<ExportTimespan> span);
79         void add_config (FileSpec const & config, bool rt);
80         void get_analysis_results (AnalysisResults& results);
81
82   private:
83
84         void add_analyser (const std::string& fn, AnalysisPtr ap) {
85                 analysis_map.insert (std::make_pair (fn, ap));
86         }
87
88         void add_split_config (FileSpec const & config);
89
90         class Encoder {
91             public:
92                 template <typename T> boost::shared_ptr<AudioGrapher::Sink<T> > init (FileSpec const & new_config);
93                 void add_child (FileSpec const & new_config);
94                 void remove_children ();
95                 void destroy_writer (bool delete_out_file);
96                 bool operator== (FileSpec const & other_config) const;
97
98                 static int get_real_format (FileSpec const & config);
99
100                                                 private:
101                 typedef boost::shared_ptr<AudioGrapher::SndfileWriter<Sample> > FloatWriterPtr;
102                 typedef boost::shared_ptr<AudioGrapher::SndfileWriter<int> >    IntWriterPtr;
103                 typedef boost::shared_ptr<AudioGrapher::SndfileWriter<short> >  ShortWriterPtr;
104
105                 template<typename T> void init_writer (boost::shared_ptr<AudioGrapher::SndfileWriter<T> > & writer);
106                 void copy_files (std::string orig_path);
107
108                 FileSpec               config;
109                 std::list<ExportFilenamePtr> filenames;
110                 PBD::ScopedConnection  copy_files_connection;
111
112                 std::string writer_filename;
113
114                 // Only one of these should be available at a time
115                 FloatWriterPtr float_writer;
116                 IntWriterPtr   int_writer;
117                 ShortWriterPtr short_writer;
118         };
119
120         // sample format converter
121         class SFC {
122             public:
123                 // This constructor so that this can be constructed like a Normalizer
124                 SFC (ExportGraphBuilder &, FileSpec const & new_config, framecnt_t max_frames);
125                 FloatSinkPtr sink ();
126                 void add_child (FileSpec const & new_config);
127                 void remove_children (bool remove_out_files);
128                 bool operator== (FileSpec const & other_config) const;
129                 void set_peak (float);
130
131                                                 private:
132                 typedef boost::shared_ptr<AudioGrapher::Chunker<float> > ChunkerPtr;
133                 typedef boost::shared_ptr<AudioGrapher::SampleFormatConverter<Sample> > FloatConverterPtr;
134                 typedef boost::shared_ptr<AudioGrapher::SampleFormatConverter<int> >   IntConverterPtr;
135                 typedef boost::shared_ptr<AudioGrapher::SampleFormatConverter<short> > ShortConverterPtr;
136
137                 FileSpec           config;
138                 boost::ptr_list<Encoder> children;
139                 int                data_width;
140
141                 ChunkerPtr      chunker;
142                 AnalysisPtr     analyser;
143                 bool            _analyse;
144                 // Only one of these should be available at a time
145                 FloatConverterPtr float_converter;
146                 IntConverterPtr int_converter;
147                 ShortConverterPtr short_converter;
148         };
149
150         class Intermediate {
151                                                 public:
152                 Intermediate (ExportGraphBuilder & parent, FileSpec const & new_config, framecnt_t max_frames);
153                 FloatSinkPtr sink ();
154                 void add_child (FileSpec const & new_config);
155                 void remove_children (bool remove_out_files);
156                 bool operator== (FileSpec const & other_config) const;
157
158                 unsigned get_postprocessing_cycle_count() const;
159
160                 /// Returns true when finished
161                 bool process ();
162
163                                                 private:
164                 typedef boost::shared_ptr<AudioGrapher::PeakReader> PeakReaderPtr;
165                 typedef boost::shared_ptr<AudioGrapher::LoudnessReader> LoudnessReaderPtr;
166                 typedef boost::shared_ptr<AudioGrapher::Normalizer> NormalizerPtr;
167                 typedef boost::shared_ptr<AudioGrapher::TmpFile<Sample> > TmpFilePtr;
168                 typedef boost::shared_ptr<AudioGrapher::Threader<Sample> > ThreaderPtr;
169                 typedef boost::shared_ptr<AudioGrapher::AllocatingProcessContext<Sample> > BufferPtr;
170
171                 void prepare_post_processing ();
172                 void start_post_processing ();
173
174                 ExportGraphBuilder & parent;
175
176                 FileSpec        config;
177                 framecnt_t      max_frames_out;
178                 bool            use_loudness;
179                 bool            use_peak;
180                 BufferPtr       buffer;
181                 PeakReaderPtr   peak_reader;
182                 TmpFilePtr      tmp_file;
183                 NormalizerPtr   normalizer;
184                 ThreaderPtr     threader;
185                 LoudnessReaderPtr    loudness_reader;
186                 boost::ptr_list<SFC> children;
187
188                 PBD::ScopedConnectionList post_processing_connection;
189         };
190
191         // sample rate converter
192         class SRC {
193             public:
194                 SRC (ExportGraphBuilder & parent, FileSpec const & new_config, framecnt_t max_frames);
195                 FloatSinkPtr sink ();
196                 void add_child (FileSpec const & new_config);
197                 void remove_children (bool remove_out_files);
198
199                 bool operator== (FileSpec const & other_config) const;
200
201                                                 private:
202                 typedef boost::shared_ptr<AudioGrapher::SampleRateConverter> SRConverterPtr;
203
204                 template<typename T>
205                 void add_child_to_list (FileSpec const & new_config, boost::ptr_list<T> & list);
206
207                 ExportGraphBuilder &  parent;
208                 FileSpec              config;
209                 boost::ptr_list<SFC>  children;
210                 boost::ptr_list<Intermediate> intermediate_children;
211                 SRConverterPtr        converter;
212                 framecnt_t            max_frames_out;
213         };
214
215         // Silence trimmer + adder
216         class SilenceHandler {
217             public:
218                 SilenceHandler (ExportGraphBuilder & parent, FileSpec const & new_config, framecnt_t max_frames);
219                 FloatSinkPtr sink ();
220                 void add_child (FileSpec const & new_config);
221                 void remove_children (bool remove_out_files);
222                 bool operator== (FileSpec const & other_config) const;
223
224                                                 private:
225                 typedef boost::shared_ptr<AudioGrapher::SilenceTrimmer<Sample> > SilenceTrimmerPtr;
226
227                 ExportGraphBuilder & parent;
228                 FileSpec             config;
229                 boost::ptr_list<SRC> children;
230                 SilenceTrimmerPtr    silence_trimmer;
231                 framecnt_t           max_frames_in;
232         };
233
234         // channel configuration
235         class ChannelConfig {
236             public:
237                 ChannelConfig (ExportGraphBuilder & parent, FileSpec const & new_config, ChannelMap & channel_map);
238                 void add_child (FileSpec const & new_config);
239                 void remove_children (bool remove_out_files);
240                 bool operator== (FileSpec const & other_config) const;
241
242                                                 private:
243                 typedef boost::shared_ptr<AudioGrapher::Interleaver<Sample> > InterleaverPtr;
244                 typedef boost::shared_ptr<AudioGrapher::Chunker<Sample> > ChunkerPtr;
245
246                 ExportGraphBuilder &      parent;
247                 FileSpec                  config;
248                 boost::ptr_list<SilenceHandler> children;
249                 InterleaverPtr            interleaver;
250                 ChunkerPtr                chunker;
251                 framecnt_t                max_frames_out;
252         };
253
254         Session const & session;
255         boost::shared_ptr<ExportTimespan> timespan;
256
257         // Roots for export processor trees
258         typedef boost::ptr_list<ChannelConfig> ChannelConfigList;
259         ChannelConfigList channel_configs;
260
261         // The sources of all data, each channel is read only once
262         ChannelMap channels;
263
264         framecnt_t process_buffer_frames;
265
266         std::list<Intermediate *> intermediates;
267
268         AnalysisMap analysis_map;
269
270         bool _realtime;
271
272         Glib::ThreadPool thread_pool;
273 };
274
275 } // namespace ARDOUR
276
277 #endif /* __ardour_export_graph_builder_h__ */