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