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