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