2 Copyright (C) 2008 Paul Davis
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.
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.
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.
21 #ifndef __ardour_export_file_io_h__
22 #define __ardour_export_file_io_h__
27 #include <boost/shared_ptr.hpp>
28 #include <glibmm/ustring.h>
29 #include <ardour/sndfile_helpers.h>
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>
42 /// Common part for all export file writers
43 class ExportFileWriter
46 virtual ~ExportFileWriter () {}
48 string filename () const { return _filename; }
49 nframes_t position () const { return _position; }
51 void set_position (nframes_t position) { _position = position; }
54 ExportFileWriter (string filename) : _filename (filename) {}
60 /// Common interface for templated libsndfile writers
61 class SndfileWriterBase : public ExportFileWriter
65 SNDFILE * get_sndfile () const { return sndfile; }
68 SndfileWriterBase (int channels, nframes_t samplerate, int format, string const & path);
69 virtual ~SndfileWriterBase ();
76 /// Template parameter specific parts of sndfile writer
78 class SndfileWriter : public SndfileWriterBase, public GraphSink<T>
80 // FIXME: having this protected doesn't work with Apple's gcc
81 // Should only be created vie ExportFileFactory and derived classes
83 friend class ExportFileFactory;
84 SndfileWriter (int channels, nframes_t samplerate, int format, string const & path);
87 nframes_t write (T * data, nframes_t frames);
88 virtual ~SndfileWriter () {}
92 sf_count_t (*write_func)(SNDFILE *, const T *, sf_count_t);
95 void init (); // Inits write function
98 /// Writes and reads a RAW tempfile (file aquired with tmpfile())
99 class ExportTempFile : public SndfileWriter<float>, public GraphSource<float>
102 ExportTempFile (uint32_t channels, nframes_t samplerate);
103 ~ExportTempFile () {}
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);
109 /* Silence management */
111 nframes_t trim_beginning (bool yn = true);
112 nframes_t trim_end (bool yn = true);
114 void set_silence_beginning (nframes_t frames);
115 void set_silence_end (nframes_t frames);
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);
129 /* Silence related */
131 /* start and end are used by read() */
136 /* these are the silence processing results and state */
138 void process_beginning ();
141 bool beginning_processed;
144 nframes_t silent_frames_beginning;
145 nframes_t silent_frames_end;
147 /* Silence to add to start and end */
149 nframes_t silence_beginning;
150 nframes_t silence_end;
152 /* Takes care that the end postion gets set at some stage */
158 class ExportFileFactory
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;
166 typedef std::pair<FloatSinkPtr, FileWriterPtr> FilePair;
168 static FilePair create (FormatPtr format, uint32_t channels, ustring const & filename);
169 static bool check (FormatPtr format, uint32_t channels);
173 static FilePair create_sndfile (FormatPtr format, unsigned int channels, ustring const & filename);
174 static bool check_sndfile (FormatPtr format, unsigned int channels);
177 } // namespace ARDOUR
179 #endif /* __ardour_export_file_io_h__ */