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