Only show user-presets in favorite sidebar
[ardour.git] / libs / ardour / ardour / export_handler.h
1 /*
2     Copyright (C) 2008 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_handler_h__
22 #define __ardour_export_handler_h__
23
24 #include <map>
25
26 #include <boost/operators.hpp>
27 #include <boost/shared_ptr.hpp>
28
29 #include "pbd/gstdio_compat.h"
30
31 #include "ardour/export_pointers.h"
32 #include "ardour/session.h"
33 #include "ardour/libardour_visibility.h"
34 #include "ardour/types.h"
35 #include "pbd/signals.h"
36
37 #include "pbd/i18n.h"
38
39 namespace AudioGrapher {
40         class BroadcastInfo;
41 }
42
43 namespace ARDOUR
44 {
45
46 class ExportTimespan;
47 class ExportChannelConfiguration;
48 class ExportFormatSpecification;
49 class ExportFilename;
50 class ExportGraphBuilder;
51 class Location;
52
53 class LIBARDOUR_API ExportElementFactory
54 {
55   public:
56
57         ExportElementFactory (Session & session);
58         ~ExportElementFactory ();
59
60         ExportTimespanPtr add_timespan ();
61
62         ExportChannelConfigPtr add_channel_config ();
63
64         ExportFormatSpecPtr    add_format ();
65         ExportFormatSpecPtr    add_format (XMLNode const & state);
66         ExportFormatSpecPtr    add_format_copy (ExportFormatSpecPtr other);
67
68         ExportFilenamePtr      add_filename ();
69         ExportFilenamePtr      add_filename_copy (ExportFilenamePtr other);
70
71   private:
72         Session & session;
73 };
74
75 /** Export Handler */
76 class LIBARDOUR_API ExportHandler : public ExportElementFactory, public sigc::trackable
77 {
78   public:
79         struct FileSpec {
80                 FileSpec() {}
81                 FileSpec (ExportChannelConfigPtr channel_config, ExportFormatSpecPtr format,
82                           ExportFilenamePtr filename, BroadcastInfoPtr broadcast_info)
83                   : channel_config (channel_config)
84                   , format (format)
85                   , filename (filename)
86                   , broadcast_info (broadcast_info)
87                         {}
88
89                 ExportChannelConfigPtr channel_config;
90                 ExportFormatSpecPtr    format;
91                 ExportFilenamePtr      filename;
92                 BroadcastInfoPtr       broadcast_info;
93         };
94
95   private:
96         /* Session::get_export_handler() should be used to obtain an export handler
97          * This ensures that it doesn't go out of scope before finalize_audio_export is called
98          */
99
100         friend boost::shared_ptr<ExportHandler> Session::get_export_handler();
101         ExportHandler (Session & session);
102
103         void command_output(std::string output, size_t size);
104
105   public:
106         ~ExportHandler ();
107
108         bool add_export_config (ExportTimespanPtr timespan, ExportChannelConfigPtr channel_config,
109                                 ExportFormatSpecPtr format, ExportFilenamePtr filename,
110                                 BroadcastInfoPtr broadcast_info);
111         void do_export ();
112
113         std::string get_cd_marker_filename(std::string filename, CDMarkerFormat format);
114
115         /** signal emitted when soundcloud export reports progress updates during upload.
116          * The parameters are total and current bytes downloaded, and the current filename
117          */
118         PBD::Signal3<void, double, double, std::string> SoundcloudProgress;
119
120         /* upload credentials & preferences */
121         std::string soundcloud_username;
122         std::string soundcloud_password;
123         bool soundcloud_make_public;
124         bool soundcloud_open_page;
125         bool soundcloud_downloadable;
126
127         void reset ();
128
129   private:
130
131         void handle_duplicate_format_extensions();
132         int process (samplecnt_t samples);
133
134         Session &          session;
135         boost::shared_ptr<ExportGraphBuilder> graph_builder;
136         ExportStatusPtr    export_status;
137
138         /* The timespan and corresponding file specifications that we are exporting;
139            there can be multiple FileSpecs for each ExportTimespan.
140         */
141         typedef std::multimap<ExportTimespanPtr, FileSpec> ConfigMap;
142         ConfigMap          config_map;
143
144         bool               post_processing;
145
146         /* Timespan management */
147
148         void start_timespan ();
149         int  process_timespan (samplecnt_t samples);
150         int  post_process ();
151         void finish_timespan ();
152
153         typedef std::pair<ConfigMap::iterator, ConfigMap::iterator> TimespanBounds;
154         ExportTimespanPtr     current_timespan;
155         TimespanBounds        timespan_bounds;
156
157         PBD::ScopedConnection process_connection;
158         samplepos_t             process_position;
159
160         /* CD Marker stuff */
161
162         struct CDMarkerStatus {
163                 CDMarkerStatus (std::string out_file, ExportTimespanPtr timespan,
164                                 ExportFormatSpecPtr format, std::string filename)
165                   : path (out_file)
166                   , timespan (timespan)
167                   , format (format)
168                   , filename (filename)
169                   , marker(0)
170                   , track_number (1)
171                   , track_position (0)
172                   , track_duration (0)
173                   , track_start_sample (0)
174                   , index_number (1)
175                   , index_position (0)
176                   {}
177
178                 ~CDMarkerStatus () {
179                         if (!g_file_set_contents (path.c_str(), out.str().c_str(), -1, NULL)) {
180                                 PBD::error << string_compose(_("Editor: cannot open \"%1\" as export file for CD marker file"), path) << endmsg;
181                         }
182
183                 }
184
185                 /* I/O */
186                 std::string         path;
187                 std::stringstream   out;
188
189                 /* General info */
190                 ExportTimespanPtr   timespan;
191                 ExportFormatSpecPtr format;
192                 std::string         filename;
193                 Location *          marker;
194
195                 /* Track info */
196                 uint32_t        track_number;
197                 samplepos_t      track_position;
198                 samplepos_t      track_duration;
199                 samplepos_t      track_start_sample;
200
201                 /* Index info */
202                 uint32_t       index_number;
203                 samplepos_t      index_position;
204         };
205
206
207         void export_cd_marker_file (ExportTimespanPtr timespan, ExportFormatSpecPtr file_format,
208                                     std::string filename, CDMarkerFormat format);
209
210         void write_cue_header (CDMarkerStatus & status);
211         void write_toc_header (CDMarkerStatus & status);
212         void write_mp4ch_header (CDMarkerStatus & status);
213
214         void write_track_info_cue (CDMarkerStatus & status);
215         void write_track_info_toc (CDMarkerStatus & status);
216         void write_track_info_mp4ch (CDMarkerStatus & status);
217
218         void write_index_info_cue (CDMarkerStatus & status);
219         void write_index_info_toc (CDMarkerStatus & status);
220         void write_index_info_mp4ch (CDMarkerStatus & status);
221
222         void samples_to_cd_samples_string (char* buf, samplepos_t when);
223         void samples_to_chapter_marks_string (char* buf, samplepos_t when);
224
225         std::string toc_escape_cdtext (const std::string&);
226         std::string toc_escape_filename (const std::string&);
227         std::string cue_escape_cdtext (const std::string& txt);
228
229         int cue_tracknum;
230         int cue_indexnum;
231 };
232
233 } // namespace ARDOUR
234
235 #endif /* __ardour_export_handler_h__ */