*** NEW CODING POLICY ***
[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/types.h"
32 #include "ardour/session.h"
33
34 namespace ARDOUR
35 {
36
37 class ExportTimespan;
38 class ExportChannelConfiguration;
39 class ExportFormatSpecification;
40 class ExportFilename;
41 class ExportProcessor;
42
43 class ExportElementFactory
44 {
45   protected:
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, public sigc::trackable
72 {
73   private:
74         
75         /* Stuff for export configs
76          * The multimap maps timespans to file specifications
77          */
78         
79         struct FileSpec {
80         
81                 FileSpec (ChannelConfigPtr channel_config, FormatPtr format, FilenamePtr filename) :
82                   channel_config (channel_config),
83                   format (format),
84                   filename (filename)
85                   {}
86         
87                 ChannelConfigPtr channel_config;
88                 FormatPtr        format;
89                 FilenamePtr      filename;
90         };
91         
92         typedef std::pair<TimespanPtr, FileSpec> ConfigPair;
93         typedef std::multimap<TimespanPtr, FileSpec> ConfigMap;
94         
95         typedef boost::shared_ptr<ExportProcessor> ProcessorPtr;
96         typedef boost::shared_ptr<ExportStatus> StatusPtr;
97
98   private:
99         /* Session::get_export_handler() should be used to obtain an export handler
100          * This ensures that it doesn't go out of scope before finalize_audio_export is called
101          */
102
103         friend boost::shared_ptr<ExportHandler> Session::get_export_handler();
104         ExportHandler (Session & session);
105         
106   public:
107         ~ExportHandler ();
108
109         bool add_export_config (TimespanPtr timespan, ChannelConfigPtr channel_config, FormatPtr format, FilenamePtr filename);
110         void do_export (bool rt = false);
111
112   private:
113
114         Session &          session;
115         ProcessorPtr       processor;
116         StatusPtr          export_status;
117         ConfigMap          config_map;
118         
119         bool               realtime;
120         
121         sigc::connection          files_written_connection;
122         std::list<Glib::ustring> files_written;
123         
124         /* CD Marker stuff */
125         
126         struct CDMarkerStatus {
127                 CDMarkerStatus (string out_file, TimespanPtr timespan, FormatPtr format, string filename) :
128                   out (out_file.c_str()), timespan (timespan), format (format), filename (filename),
129                   track_number (1), track_position (0), track_duration (0), track_start_frame (0),
130                   index_number (1), index_position (0)
131                   {}
132                 
133                 /* General info */
134                 std::ofstream  out;
135                 TimespanPtr    timespan;
136                 FormatPtr      format;
137                 string         filename;
138                 Location *     marker;
139                 
140                 /* Track info */
141                 uint32_t       track_number;
142                 nframes_t      track_position;
143                 nframes_t      track_duration;
144                 nframes_t      track_start_frame;
145                 
146                 /* Index info */
147                 uint32_t       index_number;
148                 nframes_t      index_position;
149         };
150         
151         
152         void export_cd_marker_file (TimespanPtr timespan, FormatPtr file_format, std::string filename, CDMarkerFormat format);
153         
154         void write_cue_header (CDMarkerStatus & status);
155         void write_toc_header (CDMarkerStatus & status);
156         
157         void write_track_info_cue (CDMarkerStatus & status);
158         void write_track_info_toc (CDMarkerStatus & status);
159
160         void write_index_info_cue (CDMarkerStatus & status);
161         void write_index_info_toc (CDMarkerStatus & status);
162         
163         void frames_to_cd_frames_string (char* buf, nframes_t when);
164         
165         int cue_tracknum;
166         int cue_indexnum;
167         
168         /* Timespan management */
169         
170         void start_timespan ();
171         void finish_timespan ();
172         void timespan_thread_finished ();
173         
174         typedef std::pair<ConfigMap::iterator, ConfigMap::iterator> TimespanBounds;
175         TimespanPtr          current_timespan;
176         ConfigMap::iterator  current_map_it;
177         TimespanBounds       timespan_bounds;
178         sigc::connection     channel_config_connection;
179
180 };
181
182 } // namespace ARDOUR
183
184 #endif /* __ardour_export_handler_h__ */