add config var for region fade visibility
[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   protected:
81         // Should only be created vie ExportFileFactory and derived classes
82         friend class ExportFileFactory;
83         SndfileWriter (int channels, nframes_t samplerate, int format, string const & path);
84
85   public:
86         nframes_t write (T * data, nframes_t frames);
87         virtual ~SndfileWriter () {}
88
89   protected:
90
91         sf_count_t (*write_func)(SNDFILE *, const T *, sf_count_t);
92
93   private:
94         void init (); // Inits write function
95 };
96
97 /// Writes and reads a RAW tempfile (file aquired with tmpfile())
98 class ExportTempFile : public SndfileWriter<float>, public GraphSource<float>
99 {
100   public:
101         ExportTempFile (uint32_t channels, nframes_t samplerate);
102         ~ExportTempFile () {}
103         
104         /// Causes the file to be read from the beginning again
105         void reset_read () { reading = false; }
106         nframes_t read (float * data, nframes_t frames);
107         
108         /* Silence management */
109         
110         nframes_t trim_beginning (bool yn = true);
111         nframes_t trim_end (bool yn = true);
112         
113         void set_silence_beginning (nframes_t frames);
114         void set_silence_end (nframes_t frames);
115
116   private:
117         /* File access */
118         
119         sf_count_t get_length ();
120         sf_count_t get_position ();
121         sf_count_t get_read_position (); // get position seems to default to the write pointer
122         sf_count_t locate_to (nframes_t frames);
123         sf_count_t _read (float * data, nframes_t frames);
124         
125         uint32_t channels;
126         bool reading;
127         
128         /* Silence related */
129         
130         /* start and end are used by read() */
131         
132         nframes_t start;
133         nframes_t end;
134         
135         /* these are the silence processing results and state */
136         
137         void process_beginning ();
138         void process_end ();
139         
140         bool beginning_processed;
141         bool end_processed;
142         
143         nframes_t silent_frames_beginning;
144         nframes_t silent_frames_end;
145         
146         /* Silence to add to start and end */
147         
148         nframes_t silence_beginning;
149         nframes_t silence_end;
150         
151         /* Takes care that the end postion gets set at some stage */
152         
153         bool end_set;
154         
155 };
156
157 class ExportFileFactory
158 {
159   public:
160         typedef boost::shared_ptr<ExportFormatSpecification const> FormatPtr;
161         typedef GraphSink<float> FloatSink;
162         typedef boost::shared_ptr<FloatSink> FloatSinkPtr;
163         typedef boost::shared_ptr<ExportFileWriter> FileWriterPtr;
164         
165         typedef std::pair<FloatSinkPtr, FileWriterPtr> FilePair;
166
167         static FilePair create (FormatPtr format, uint32_t channels, ustring const & filename);
168         static bool check (FormatPtr format, uint32_t channels);
169
170   private:
171
172         static FilePair create_sndfile (FormatPtr format, unsigned int channels, ustring const & filename);
173         static bool check_sndfile (FormatPtr format, unsigned int channels);
174 };
175
176 } // namespace ARDOUR
177
178 #endif /* __ardour_export_file_io_h__ */