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"
40 /// Common part for all export file writers
41 class ExportFileWriter
44 virtual ~ExportFileWriter () {}
46 std::string filename () const { return _filename; }
47 nframes_t position () const { return _position; }
49 void set_position (nframes_t position) { _position = position; }
52 ExportFileWriter (std::string filename) : _filename (filename) {}
54 std::string _filename;
58 /// Common interface for templated libsndfile writers
59 class SndfileWriterBase : public ExportFileWriter
63 SNDFILE * get_sndfile () const { return sndfile; }
66 SndfileWriterBase (int channels, nframes_t samplerate, int format, std::string const & path);
67 virtual ~SndfileWriterBase ();
74 /// Template parameter specific parts of sndfile writer
76 class SndfileWriter : public SndfileWriterBase, public GraphSink<T>
78 // FIXME: having this protected doesn't work with Apple's gcc
79 // Should only be created vie ExportFileFactory and derived classes
81 friend class ExportFileFactory;
82 SndfileWriter (int channels, nframes_t samplerate, int format, std::string const & path);
85 nframes_t write (T * data, nframes_t frames);
86 virtual ~SndfileWriter () {}
90 sf_count_t (*write_func)(SNDFILE *, const T *, sf_count_t);
93 void init (); // Inits write function
96 /// Writes and reads a RAW tempfile (file aquired with tmpfile())
97 class ExportTempFile : public SndfileWriter<float>, public GraphSource<float>
100 ExportTempFile (uint32_t channels, nframes_t samplerate);
101 ~ExportTempFile () {}
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);
107 /* Silence management */
109 nframes_t trim_beginning (bool yn = true);
110 nframes_t trim_end (bool yn = true);
112 void set_silence_beginning (nframes_t frames);
113 void set_silence_end (nframes_t frames);
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);
127 /* Silence related */
129 /* start and end are used by read() */
134 /* these are the silence processing results and state */
136 void process_beginning ();
139 bool beginning_processed;
142 nframes_t silent_frames_beginning;
143 nframes_t silent_frames_end;
145 /* Silence to add to start and end */
147 nframes_t silence_beginning;
148 nframes_t silence_end;
150 /* Takes care that the end postion gets set at some stage */
156 class ExportFileFactory
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;
164 typedef std::pair<FloatSinkPtr, FileWriterPtr> FilePair;
166 static FilePair create (FormatPtr format, uint32_t channels, Glib::ustring const & filename);
167 static bool check (FormatPtr format, uint32_t channels);
171 static FilePair create_sndfile (FormatPtr format, unsigned int channels, Glib::ustring const & filename);
172 static bool check_sndfile (FormatPtr format, unsigned int channels);
175 } // namespace ARDOUR
177 #endif /* __ardour_export_file_io_h__ */