Merge branch 'export-dialog' into cairocanvas
[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 #include <fstream>
26
27 #include <boost/operators.hpp>
28 #include <boost/shared_ptr.hpp>
29
30 #include "ardour/export_pointers.h"
31 #include "ardour/session.h"
32 #include "ardour/libardour_visibility.h"
33 #include "ardour/types.h"
34 #include "pbd/signals.h"
35
36 namespace AudioGrapher {
37         class BroadcastInfo;
38 }
39
40 namespace ARDOUR
41 {
42
43 class ExportTimespan;
44 class ExportChannelConfiguration;
45 class ExportFormatSpecification;
46 class ExportFilename;
47 class ExportGraphBuilder;
48 class Location;
49
50 class LIBARDOUR_API ExportElementFactory
51 {
52   public:
53
54         ExportElementFactory (Session & session);
55         ~ExportElementFactory ();
56
57         ExportTimespanPtr add_timespan ();
58
59         ExportChannelConfigPtr add_channel_config ();
60
61         ExportFormatSpecPtr    add_format ();
62         ExportFormatSpecPtr    add_format (XMLNode const & state);
63         ExportFormatSpecPtr    add_format_copy (ExportFormatSpecPtr other);
64
65         ExportFilenamePtr      add_filename ();
66         ExportFilenamePtr      add_filename_copy (ExportFilenamePtr other);
67
68   private:
69         Session & session;
70 };
71
72 class LIBARDOUR_API ExportHandler : public ExportElementFactory, public sigc::trackable
73 {
74   public:
75         struct FileSpec {
76                 FileSpec() {}
77                 FileSpec (ExportChannelConfigPtr channel_config, ExportFormatSpecPtr format,
78                           ExportFilenamePtr filename, BroadcastInfoPtr broadcast_info)
79                   : channel_config (channel_config)
80                   , format (format)
81                   , filename (filename)
82                   , broadcast_info (broadcast_info)
83                         {}
84
85                 ExportChannelConfigPtr channel_config;
86                 ExportFormatSpecPtr    format;
87                 ExportFilenamePtr      filename;
88                 BroadcastInfoPtr       broadcast_info;
89         };
90
91   private:
92         /* Session::get_export_handler() should be used to obtain an export handler
93          * This ensures that it doesn't go out of scope before finalize_audio_export is called
94          */
95
96         friend boost::shared_ptr<ExportHandler> Session::get_export_handler();
97         ExportHandler (Session & session);
98
99         void command_output(std::string output, size_t size);
100
101   public:
102         ~ExportHandler ();
103
104         bool add_export_config (ExportTimespanPtr timespan, ExportChannelConfigPtr channel_config,
105                                 ExportFormatSpecPtr format, ExportFilenamePtr filename,
106                                 BroadcastInfoPtr broadcast_info);
107         void do_export ();
108
109         std::string get_cd_marker_filename(std::string filename, CDMarkerFormat format);
110
111         /** signal emitted when soundcloud export reports progress updates during upload.
112          * The parameters are total and current bytes downloaded, and the current filename
113          */
114         PBD::Signal3<void, double, double, std::string> SoundcloudProgress;
115
116         /* upload credentials & preferences */
117         std::string upload_username;
118         std::string upload_password;
119         bool upload_public;
120         bool upload_open;
121
122   private:
123
124         void handle_duplicate_format_extensions();
125         int process (framecnt_t frames);
126
127         Session &          session;
128         boost::shared_ptr<ExportGraphBuilder> graph_builder;
129         ExportStatusPtr    export_status;
130
131         /* The timespan and corresponding file specifications that we are exporting;
132            there can be multiple FileSpecs for each ExportTimespan.
133         */
134         typedef std::multimap<ExportTimespanPtr, FileSpec> ConfigMap;
135         ConfigMap          config_map;
136
137         bool               normalizing;
138
139         /* Timespan management */
140
141         void start_timespan ();
142         int  process_timespan (framecnt_t frames);
143         int  process_normalize ();
144         void finish_timespan ();
145
146         typedef std::pair<ConfigMap::iterator, ConfigMap::iterator> TimespanBounds;
147         ExportTimespanPtr     current_timespan;
148         TimespanBounds        timespan_bounds;
149
150         PBD::ScopedConnection process_connection;
151         framepos_t             process_position;
152
153         /* CD Marker stuff */
154
155         struct CDMarkerStatus {
156                 CDMarkerStatus (std::string out_file, ExportTimespanPtr timespan,
157                                 ExportFormatSpecPtr format, std::string filename)
158                   : out (out_file.c_str()), timespan (timespan), format (format), filename (filename), marker(0)
159                   , track_number (1), track_position (0), track_duration (0), track_start_frame (0)
160                   , index_number (1), index_position (0)
161                   {}
162
163                 /* General info */
164                 std::ofstream  out;
165                 ExportTimespanPtr   timespan;
166                 ExportFormatSpecPtr format;
167                 std::string         filename;
168                 Location *          marker;
169
170                 /* Track info */
171                 uint32_t        track_number;
172                 framepos_t      track_position;
173                 framepos_t      track_duration;
174                 framepos_t      track_start_frame;
175
176                 /* Index info */
177                 uint32_t       index_number;
178                 framepos_t      index_position;
179         };
180
181
182         void export_cd_marker_file (ExportTimespanPtr timespan, ExportFormatSpecPtr file_format,
183                                     std::string filename, CDMarkerFormat format);
184
185         void write_cue_header (CDMarkerStatus & status);
186         void write_toc_header (CDMarkerStatus & status);
187
188         void write_track_info_cue (CDMarkerStatus & status);
189         void write_track_info_toc (CDMarkerStatus & status);
190
191         void write_index_info_cue (CDMarkerStatus & status);
192         void write_index_info_toc (CDMarkerStatus & status);
193
194         void frames_to_cd_frames_string (char* buf, framepos_t when);
195         std::string toc_escape_cdtext (const std::string&);
196         std::string toc_escape_filename (const std::string&);
197         std::string cue_escape_cdtext (const std::string& txt);
198
199         int cue_tracknum;
200         int cue_indexnum;
201 };
202
203 } // namespace ARDOUR
204
205 #endif /* __ardour_export_handler_h__ */