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