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