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>
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);
86 nframes_t write (T * data, nframes_t frames);
87 virtual ~SndfileWriter () {}
91 sf_count_t (*write_func)(SNDFILE *, const T *, sf_count_t);
94 void init (); // Inits write function
97 /// Writes and reads a RAW tempfile (file aquired with tmpfile())
98 class ExportTempFile : public SndfileWriter<float>, public GraphSource<float>
101 ExportTempFile (uint32_t channels, nframes_t samplerate);
102 ~ExportTempFile () {}
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);
108 /* Silence management */
110 nframes_t trim_beginning (bool yn = true);
111 nframes_t trim_end (bool yn = true);
113 void set_silence_beginning (nframes_t frames);
114 void set_silence_end (nframes_t frames);
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);
128 /* Silence related */
130 /* start and end are used by read() */
135 /* these are the silence processing results and state */
137 void process_beginning ();
140 bool beginning_processed;
143 nframes_t silent_frames_beginning;
144 nframes_t silent_frames_end;
146 /* Silence to add to start and end */
148 nframes_t silence_beginning;
149 nframes_t silence_end;
151 /* Takes care that the end postion gets set at some stage */
157 class ExportFileFactory
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;
165 typedef std::pair<FloatSinkPtr, FileWriterPtr> FilePair;
167 static FilePair create (FormatPtr format, uint32_t channels, ustring const & filename);
168 static bool check (FormatPtr format, uint32_t channels);
172 static FilePair create_sndfile (FormatPtr format, unsigned int channels, ustring const & filename);
173 static bool check_sndfile (FormatPtr format, unsigned int channels);
176 } // namespace ARDOUR
178 #endif /* __ardour_export_file_io_h__ */