Fix sketchy casts.
[ardour.git] / libs / ardour / ardour / export_file_io.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_file_io_h__
22 #define __ardour_export_file_io_h__
23
24 #include <stdint.h>
25 #include <utility>
26
27 #include <boost/shared_ptr.hpp>
28 #include <glibmm/ustring.h>
29 #include "ardour/sndfile_helpers.h"
30
31 #include "ardour/graph.h"
32 #include "ardour/types.h"
33 #include "ardour/ardour.h"
34 #include "ardour/export_format_specification.h"
35 #include "ardour/export_utilities.h"
36
37 namespace ARDOUR
38 {
39
40 /// Common part for all export file writers
41 class ExportFileWriter
42 {
43   public:
44         virtual ~ExportFileWriter () {}
45
46         std::string filename () const { return _filename; }
47         nframes_t position () const { return _position; }
48
49         void set_position (nframes_t position) { _position = position; }
50
51   protected:
52         ExportFileWriter (std::string filename) : _filename (filename) {}
53
54         std::string _filename;
55         nframes_t _position;
56 };
57
58 /// Common interface for templated libsndfile writers
59 class SndfileWriterBase : public ExportFileWriter
60 {
61   public:
62
63         SNDFILE * get_sndfile () const { return sndfile; }
64
65   protected:
66         SndfileWriterBase (int channels, nframes_t samplerate, int format, std::string const & path);
67         virtual ~SndfileWriterBase ();
68
69         SF_INFO        sf_info;
70         SNDFILE *      sndfile;
71 };
72
73
74 /// Template parameter specific parts of sndfile writer
75 template <typename T>
76 class SndfileWriter : public SndfileWriterBase, public GraphSink<T>
77 {
78         // FIXME: having this protected doesn't work with Apple's gcc
79         // Should only be created vie ExportFileFactory and derived classes
80   public: // protected
81         friend class ExportFileFactory;
82         SndfileWriter (int channels, nframes_t samplerate, int format, std::string const & path);
83
84   public:
85         nframes_t write (T * data, nframes_t frames);
86         virtual ~SndfileWriter () {}
87
88   protected:
89
90         sf_count_t (*write_func)(SNDFILE *, const T *, sf_count_t);
91
92   private:
93         void init (); // Inits write function
94 };
95
96 /// Writes and reads a RAW tempfile (file aquired with tmpfile())
97 class ExportTempFile : public SndfileWriter<float>, public GraphSource<float>
98 {
99   public:
100         ExportTempFile (uint32_t channels, nframes_t samplerate);
101         ~ExportTempFile () {}
102
103         /// Causes the file to be read from the beginning again
104         void reset_read () { reading = false; }
105         nframes_t read (float * data, nframes_t frames);
106
107         /* Silence management */
108
109         nframes_t trim_beginning (bool yn = true);
110         nframes_t trim_end (bool yn = true);
111
112         void set_silence_beginning (nframes_t frames);
113         void set_silence_end (nframes_t frames);
114
115   private:
116         /* File access */
117
118         sf_count_t get_length ();
119         sf_count_t get_position ();
120         sf_count_t get_read_position (); // get position seems to default to the write pointer
121         sf_count_t locate_to (nframes_t frames);
122         sf_count_t _read (float * data, nframes_t frames);
123
124         uint32_t channels;
125         bool reading;
126
127         /* Silence related */
128
129         /* start and end are used by read() */
130
131         nframes_t start;
132         nframes_t end;
133
134         /* these are the silence processing results and state */
135
136         void process_beginning ();
137         void process_end ();
138
139         bool beginning_processed;
140         bool end_processed;
141
142         nframes_t silent_frames_beginning;
143         nframes_t silent_frames_end;
144
145         /* Silence to add to start and end */
146
147         nframes_t silence_beginning;
148         nframes_t silence_end;
149
150         /* Takes care that the end postion gets set at some stage */
151
152         bool end_set;
153
154 };
155
156 class ExportFileFactory
157 {
158   public:
159         typedef boost::shared_ptr<ExportFormatSpecification const> FormatPtr;
160         typedef GraphSink<float> FloatSink;
161         typedef boost::shared_ptr<FloatSink> FloatSinkPtr;
162         typedef boost::shared_ptr<ExportFileWriter> FileWriterPtr;
163
164         typedef std::pair<FloatSinkPtr, FileWriterPtr> FilePair;
165
166         static FilePair create (FormatPtr format, uint32_t channels, Glib::ustring const & filename);
167         static bool check (FormatPtr format, uint32_t channels);
168
169   private:
170
171         static FilePair create_sndfile (FormatPtr format, unsigned int channels, Glib::ustring const & filename);
172         static bool check_sndfile (FormatPtr format, unsigned int channels);
173 };
174
175 } // namespace ARDOUR
176
177 #endif /* __ardour_export_file_io_h__ */