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