summaryrefslogtreecommitdiff
path: root/libs/ardour/ardour/export_graph_builder.h
blob: 07a878ae8b8cccc4091bffaeafddfb7e5c465bd6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
/*
 * Copyright (C) 2009-2012 Sakari Bergen <sakari.bergen@beatwaves.net>
 * Copyright (C) 2010-2012 David Robillard <d@drobilla.net>
 * Copyright (C) 2013-2017 Paul Davis <paul@linuxaudiosystems.com>
 * Copyright (C) 2016-2019 Robin Gareus <robin@gareus.org>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 */

#ifndef __ardour_export_graph_builder_h__
#define __ardour_export_graph_builder_h__

#include "ardour/export_handler.h"
#include "ardour/export_analysis.h"

#include "audiographer/utils/identity_vertex.h"

#include <boost/ptr_container/ptr_list.hpp>
#include <glibmm/threadpool.h>

namespace AudioGrapher {
	class SampleRateConverter;
	class PeakReader;
	class LoudnessReader;
	class Normalizer;
	class Analyser;
	template <typename T> class Chunker;
	template <typename T> class SampleFormatConverter;
	template <typename T> class Interleaver;
	template <typename T> class SndfileWriter;
	template <typename T> class CmdPipeWriter;
	template <typename T> class SilenceTrimmer;
	template <typename T> class TmpFile;
	template <typename T> class Threader;
	template <typename T> class AllocatingProcessContext;
}

namespace ARDOUR
{

class ExportTimespan;
class Session;

class LIBARDOUR_API ExportGraphBuilder
{
  private:
	typedef ExportHandler::FileSpec FileSpec;

	typedef boost::shared_ptr<AudioGrapher::Sink<Sample> > FloatSinkPtr;
	typedef boost::shared_ptr<AudioGrapher::IdentityVertex<Sample> > IdentityVertexPtr;
	typedef boost::shared_ptr<AudioGrapher::Analyser> AnalysisPtr;
	typedef std::map<ExportChannelPtr,  IdentityVertexPtr> ChannelMap;
	typedef std::map<std::string, AnalysisPtr> AnalysisMap;

  public:

	ExportGraphBuilder (Session const & session);
	~ExportGraphBuilder ();

	samplecnt_t process (samplecnt_t samples, bool last_cycle);
	bool post_process (); // returns true when finished
	bool need_postprocessing () const { return !intermediates.empty(); }
	bool realtime() const { return _realtime; }
	unsigned get_postprocessing_cycle_count() const;

	void reset ();
	void cleanup (bool remove_out_files = false);
	void set_current_timespan (boost::shared_ptr<ExportTimespan> span);
	void add_config (FileSpec const & config, bool rt);
	void get_analysis_results (AnalysisResults& results);

  private:

	void add_analyser (const std::string& fn, AnalysisPtr ap) {
		analysis_map.insert (std::make_pair (fn, ap));
	}

	void add_split_config (FileSpec const & config);

	class Encoder {
            public:
		template <typename T> boost::shared_ptr<AudioGrapher::Sink<T> > init (FileSpec const & new_config);
		void add_child (FileSpec const & new_config);
		void remove_children ();
		void destroy_writer (bool delete_out_file);
		bool operator== (FileSpec const & other_config) const;

		static int get_real_format (FileSpec const & config);

	                                        private:
		typedef boost::shared_ptr<AudioGrapher::SndfileWriter<Sample> > FloatWriterPtr;
		typedef boost::shared_ptr<AudioGrapher::SndfileWriter<int> >    IntWriterPtr;
		typedef boost::shared_ptr<AudioGrapher::SndfileWriter<short> >  ShortWriterPtr;

		typedef boost::shared_ptr<AudioGrapher::CmdPipeWriter<Sample> > FloatPipePtr;

		template<typename T> void init_writer (boost::shared_ptr<AudioGrapher::SndfileWriter<T> > & writer);
		template<typename T> void init_writer (boost::shared_ptr<AudioGrapher::CmdPipeWriter<T> > & writer);

		void copy_files (std::string orig_path);

		FileSpec               config;
		std::list<ExportFilenamePtr> filenames;
		PBD::ScopedConnection  copy_files_connection;

		std::string writer_filename;

		// Only one of these should be available at a time
		FloatWriterPtr float_writer;
		IntWriterPtr   int_writer;
		ShortWriterPtr short_writer;
		FloatPipePtr   pipe_writer;
	};

	// sample format converter
	class SFC {
            public:
		// This constructor so that this can be constructed like a Normalizer
		SFC (ExportGraphBuilder &, FileSpec const & new_config, samplecnt_t max_samples);
		FloatSinkPtr sink ();
		void add_child (FileSpec const & new_config);
		void remove_children (bool remove_out_files);
		bool operator== (FileSpec const & other_config) const;
		void set_peak (float);

	                                        private:
		typedef boost::shared_ptr<AudioGrapher::Chunker<float> > ChunkerPtr;
		typedef boost::shared_ptr<AudioGrapher::SampleFormatConverter<Sample> > FloatConverterPtr;
		typedef boost::shared_ptr<AudioGrapher::SampleFormatConverter<int> >   IntConverterPtr;
		typedef boost::shared_ptr<AudioGrapher::SampleFormatConverter<short> > ShortConverterPtr;

		FileSpec           config;
		boost::ptr_list<Encoder> children;
		int                data_width;

		ChunkerPtr      chunker;
		AnalysisPtr     analyser;
		bool            _analyse;
		// Only one of these should be available at a time
		FloatConverterPtr float_converter;
		IntConverterPtr int_converter;
		ShortConverterPtr short_converter;
	};

	class Intermediate {
	                                        public:
		Intermediate (ExportGraphBuilder & parent, FileSpec const & new_config, samplecnt_t max_samples);
		FloatSinkPtr sink ();
		void add_child (FileSpec const & new_config);
		void remove_children (bool remove_out_files);
		bool operator== (FileSpec const & other_config) const;

		unsigned get_postprocessing_cycle_count() const;

		/// Returns true when finished
		bool process ();

	                                        private:
		typedef boost::shared_ptr<AudioGrapher::PeakReader> PeakReaderPtr;
		typedef boost::shared_ptr<AudioGrapher::LoudnessReader> LoudnessReaderPtr;
		typedef boost::shared_ptr<AudioGrapher::Normalizer> NormalizerPtr;
		typedef boost::shared_ptr<AudioGrapher::TmpFile<Sample> > TmpFilePtr;
		typedef boost::shared_ptr<AudioGrapher::Threader<Sample> > ThreaderPtr;
		typedef boost::shared_ptr<AudioGrapher::AllocatingProcessContext<Sample> > BufferPtr;

		void prepare_post_processing ();
		void start_post_processing ();

		ExportGraphBuilder & parent;

		FileSpec        config;
		samplecnt_t     max_samples_out;
		bool            use_loudness;
		bool            use_peak;
		BufferPtr       buffer;
		PeakReaderPtr   peak_reader;
		TmpFilePtr      tmp_file;
		NormalizerPtr   normalizer;
		ThreaderPtr     threader;

		LoudnessReaderPtr    loudness_reader;
		boost::ptr_list<SFC> children;

		PBD::ScopedConnectionList post_processing_connection;
	};

	// sample rate converter
	class SRC {
            public:
		SRC (ExportGraphBuilder & parent, FileSpec const & new_config, samplecnt_t max_samples);
		FloatSinkPtr sink ();
		void add_child (FileSpec const & new_config);
		void remove_children (bool remove_out_files);

		bool operator== (FileSpec const & other_config) const;

	                                        private:
		typedef boost::shared_ptr<AudioGrapher::SampleRateConverter> SRConverterPtr;

		template<typename T>
		void add_child_to_list (FileSpec const & new_config, boost::ptr_list<T> & list);

		ExportGraphBuilder &  parent;
		FileSpec              config;
		boost::ptr_list<SFC>  children;
		boost::ptr_list<Intermediate> intermediate_children;
		SRConverterPtr        converter;
		samplecnt_t           max_samples_out;
	};

	// Silence trimmer + adder
	class SilenceHandler {
	    public:
		SilenceHandler (ExportGraphBuilder & parent, FileSpec const & new_config, samplecnt_t max_samples);
		FloatSinkPtr sink ();
		void add_child (FileSpec const & new_config);
		void remove_children (bool remove_out_files);
		bool operator== (FileSpec const & other_config) const;

	                                        private:
		typedef boost::shared_ptr<AudioGrapher::SilenceTrimmer<Sample> > SilenceTrimmerPtr;

		ExportGraphBuilder & parent;
		FileSpec             config;
		boost::ptr_list<SRC> children;
		SilenceTrimmerPtr    silence_trimmer;
		samplecnt_t          max_samples_in;
	};

	// channel configuration
	class ChannelConfig {
	    public:
		ChannelConfig (ExportGraphBuilder & parent, FileSpec const & new_config, ChannelMap & channel_map);
		void add_child (FileSpec const & new_config);
		void remove_children (bool remove_out_files);
		bool operator== (FileSpec const & other_config) const;

	                                        private:
		typedef boost::shared_ptr<AudioGrapher::Interleaver<Sample> > InterleaverPtr;
		typedef boost::shared_ptr<AudioGrapher::Chunker<Sample> > ChunkerPtr;

		ExportGraphBuilder &      parent;
		FileSpec                  config;
		boost::ptr_list<SilenceHandler> children;
		InterleaverPtr            interleaver;
		ChunkerPtr                chunker;
		samplecnt_t               max_samples_out;
	};

	Session const & session;
	boost::shared_ptr<ExportTimespan> timespan;

	// Roots for export processor trees
	typedef boost::ptr_list<ChannelConfig> ChannelConfigList;
	ChannelConfigList channel_configs;

	// The sources of all data, each channel is read only once
	ChannelMap channels;

	samplecnt_t process_buffer_samples;

	std::list<Intermediate *> intermediates;

	AnalysisMap analysis_map;

	bool        _realtime;
	samplecnt_t _master_align;

	Glib::ThreadPool     thread_pool;
	Glib::Threads::Mutex engine_request_lock;
};

} // namespace ARDOUR

#endif /* __ardour_export_graph_builder_h__ */