- Fix process callbakc handling during export
[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 <list>
26 #include <fstream>
27
28 #include <boost/shared_ptr.hpp>
29
30 #include "ardour/ardour.h"
31 #include "ardour/session.h"
32 #include "ardour/types.h"
33
34 namespace ARDOUR
35 {
36
37 class ExportTimespan;
38 class ExportChannelConfiguration;
39 class ExportFormatSpecification;
40 class ExportFilename;
41 class ExportGraphBuilder;
42
43 class ExportElementFactory
44 {
45   public:
46         typedef boost::shared_ptr<ExportTimespan> TimespanPtr;
47         typedef boost::shared_ptr<ExportChannelConfiguration> ChannelConfigPtr;
48         typedef boost::shared_ptr<ExportFormatSpecification> FormatPtr;
49         typedef boost::shared_ptr<ExportFilename> FilenamePtr;
50
51   public:
52
53         ExportElementFactory (Session & session);
54         ~ExportElementFactory ();
55
56         TimespanPtr      add_timespan ();
57
58         ChannelConfigPtr add_channel_config ();
59
60         FormatPtr        add_format ();
61         FormatPtr        add_format (XMLNode const & state);
62         FormatPtr        add_format_copy (FormatPtr other);
63
64         FilenamePtr      add_filename ();
65         FilenamePtr      add_filename_copy (FilenamePtr other);
66
67   private:
68         Session & session;
69 };
70
71 class ExportHandler : public ExportElementFactory
72 {
73   public:
74         struct FileSpec {
75                 FileSpec() {}
76                 FileSpec (ChannelConfigPtr channel_config, FormatPtr format, FilenamePtr filename)
77                   : channel_config (channel_config)
78                   , format (format)
79                   , filename (filename)
80                         {}
81
82                 ChannelConfigPtr channel_config;
83                 FormatPtr        format;
84                 FilenamePtr      filename;
85         };
86         
87   private:
88
89         /* Stuff for export configs
90          * The multimap maps timespans to file specifications
91          */
92
93         typedef std::pair<TimespanPtr, FileSpec> ConfigPair;
94         typedef std::multimap<TimespanPtr, FileSpec> ConfigMap;
95
96         typedef boost::shared_ptr<ExportGraphBuilder> GraphBuilderPtr;
97         typedef boost::shared_ptr<ExportStatus> StatusPtr;
98
99   private:
100         /* Session::get_export_handler() should be used to obtain an export handler
101          * This ensures that it doesn't go out of scope before finalize_audio_export is called
102          */
103
104         friend boost::shared_ptr<ExportHandler> Session::get_export_handler();
105         ExportHandler (Session & session);
106
107   public:
108         ~ExportHandler ();
109
110         bool add_export_config (TimespanPtr timespan, ChannelConfigPtr channel_config, FormatPtr format, FilenamePtr filename);
111         void do_export (bool rt = false);
112
113   private:
114
115         int process (nframes_t frames);
116         
117         Session &          session;
118         GraphBuilderPtr    graph_builder;
119         StatusPtr          export_status;
120         ConfigMap          config_map;
121
122         bool               realtime;
123         bool               normalizing;
124
125         /* Timespan management */
126
127         void start_timespan ();
128         int  process_timespan (nframes_t frames);
129         int  process_normalize ();
130         void finish_timespan ();
131
132         typedef std::pair<ConfigMap::iterator, ConfigMap::iterator> TimespanBounds;
133         TimespanPtr           current_timespan;
134         TimespanBounds        timespan_bounds;
135         
136         PBD::ScopedConnection process_connection;
137         sframes_t             process_position;
138
139         /* CD Marker stuff */
140
141         struct CDMarkerStatus {
142                 CDMarkerStatus (std::string out_file, TimespanPtr timespan, FormatPtr format, std::string filename) :
143                   out (out_file.c_str()), timespan (timespan), format (format), filename (filename),
144                   track_number (1), track_position (0), track_duration (0), track_start_frame (0),
145                   index_number (1), index_position (0)
146                   {}
147
148                 /* General info */
149                 std::ofstream  out;
150                 TimespanPtr    timespan;
151                 FormatPtr      format;
152                 std::string    filename;
153                 Location *     marker;
154
155                 /* Track info */
156                 uint32_t       track_number;
157                 sframes_t      track_position;
158                 sframes_t      track_duration;
159                 sframes_t      track_start_frame;
160
161                 /* Index info */
162                 uint32_t       index_number;
163                 sframes_t      index_position;
164         };
165
166
167         void export_cd_marker_file (TimespanPtr timespan, FormatPtr file_format, std::string filename, CDMarkerFormat format);
168
169         void write_cue_header (CDMarkerStatus & status);
170         void write_toc_header (CDMarkerStatus & status);
171
172         void write_track_info_cue (CDMarkerStatus & status);
173         void write_track_info_toc (CDMarkerStatus & status);
174
175         void write_index_info_cue (CDMarkerStatus & status);
176         void write_index_info_toc (CDMarkerStatus & status);
177
178         void frames_to_cd_frames_string (char* buf, sframes_t when);
179
180         int cue_tracknum;
181         int cue_indexnum;
182 };
183
184 } // namespace ARDOUR
185
186 #endif /* __ardour_export_handler_h__ */