fix up escaping of strings in TOC files (suggested by andreas ruge)
[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/operators.hpp>
29 #include <boost/shared_ptr.hpp>
30
31 #include "ardour/ardour.h"
32 #include "ardour/export_pointers.h"
33 #include "ardour/session.h"
34 #include "ardour/types.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
49
50 class 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 ExportHandler : public ExportElementFactory
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
93         /* Stuff for export configs
94          * The multimap maps timespans to file specifications
95          */
96
97         typedef std::pair<ExportTimespanPtr, FileSpec> ConfigPair;
98         typedef std::multimap<ExportTimespanPtr, FileSpec> ConfigMap;
99
100         typedef boost::shared_ptr<ExportGraphBuilder> GraphBuilderPtr;
101
102   private:
103         /* Session::get_export_handler() should be used to obtain an export handler
104          * This ensures that it doesn't go out of scope before finalize_audio_export is called
105          */
106
107         friend boost::shared_ptr<ExportHandler> Session::get_export_handler();
108         ExportHandler (Session & session);
109
110   public:
111         ~ExportHandler ();
112
113         bool add_export_config (ExportTimespanPtr timespan, ExportChannelConfigPtr channel_config,
114                                 ExportFormatSpecPtr format, ExportFilenamePtr filename,
115                                 BroadcastInfoPtr broadcast_info);
116         void do_export (bool rt = false);
117
118         std::string get_cd_marker_filename(std::string filename, CDMarkerFormat format);
119
120   private:
121
122         int process (framecnt_t frames);
123
124         Session &          session;
125         GraphBuilderPtr    graph_builder;
126         ExportStatusPtr    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         ExportTimespanPtr     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, ExportTimespanPtr timespan,
150                                 ExportFormatSpecPtr format, std::string filename)
151                   : out (out_file.c_str()), timespan (timespan), format (format), filename (filename), marker(0)
152                   , track_number (1), track_position (0), track_duration (0), track_start_frame (0)
153                   , index_number (1), index_position (0)
154                   {}
155
156                 /* General info */
157                 std::ofstream  out;
158                 ExportTimespanPtr   timespan;
159                 ExportFormatSpecPtr format;
160                 std::string         filename;
161                 Location *          marker;
162
163                 /* Track info */
164                 uint32_t        track_number;
165                 framepos_t      track_position;
166                 framepos_t      track_duration;
167                 framepos_t      track_start_frame;
168
169                 /* Index info */
170                 uint32_t       index_number;
171                 framepos_t      index_position;
172         };
173
174
175         void export_cd_marker_file (ExportTimespanPtr timespan, ExportFormatSpecPtr file_format,
176                                     std::string filename, CDMarkerFormat format);
177
178         void write_cue_header (CDMarkerStatus & status);
179         void write_toc_header (CDMarkerStatus & status);
180
181         void write_track_info_cue (CDMarkerStatus & status);
182         void write_track_info_toc (CDMarkerStatus & status);
183
184         void write_index_info_cue (CDMarkerStatus & status);
185         void write_index_info_toc (CDMarkerStatus & status);
186
187         void frames_to_cd_frames_string (char* buf, framepos_t when);
188         std::string toc_escape_string (const std::string&);
189
190         int cue_tracknum;
191         int cue_indexnum;
192 };
193
194 } // namespace ARDOUR
195
196 #endif /* __ardour_export_handler_h__ */