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