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.
23 #include "ardour/export_file_io.h"
25 #include "ardour/export_failed.h"
26 #include "pbd/failed_constructor.h"
37 /* SndfileWriterBase */
39 SndfileWriterBase::SndfileWriterBase (int channels, nframes_t samplerate, int format, string const & path) :
40 ExportFileWriter (path)
44 sf_info.channels = channels;
45 sf_info.samplerate = samplerate;
46 sf_info.format = format;
48 if (!sf_format_check (&sf_info)) {
49 throw ExportFailed (X_("Invalid format given for SndfileWriter!"));
52 if (path.length() == 0) {
53 throw ExportFailed (X_("No output file specified for SndFileWriter"));
56 /* TODO add checks that the directory path exists, and also
57 check if we are overwriting an existing file...
60 // Open file TODO make sure we have enough disk space for the output
61 if (path.compare ("temp")) {
62 if ((sndfile = sf_open (path.c_str(), SFM_WRITE, &sf_info)) == 0) {
63 sf_error_str (0, errbuf, sizeof (errbuf) - 1);
64 throw ExportFailed (string_compose(X_("Cannot open output file \"%1\" for SndFileWriter (%2)"), path, errbuf));
68 if (!(file = tmpfile ())) {
69 throw ExportFailed (X_("Cannot open tempfile"));
71 sndfile = sf_open_fd (fileno(file), SFM_RDWR, &sf_info, true);
75 SndfileWriterBase::~SndfileWriterBase ()
83 SndfileWriter<T>::SndfileWriter (int channels, nframes_t samplerate, int format, string const & path) :
84 SndfileWriterBase (channels, samplerate, format, path)
86 // init write function
92 SndfileWriter<float>::init ()
94 write_func = &sf_writef_float;
99 SndfileWriter<int>::init ()
101 write_func = &sf_writef_int;
106 SndfileWriter<short>::init ()
108 write_func = &sf_writef_short;
111 template <typename T>
113 SndfileWriter<T>::write (T * data, nframes_t frames)
116 nframes_t written = (*write_func) (sndfile, data, frames);
117 if (written != frames) {
118 sf_error_str (sndfile, errbuf, sizeof (errbuf) - 1);
119 throw ExportFailed (string_compose(_("Could not write data to output file (%1)"), errbuf));
122 if (GraphSink<T>::end_of_input) {
123 sf_write_sync (sndfile);
129 template class SndfileWriter<short>;
130 template class SndfileWriter<int>;
131 template class SndfileWriter<float>;
135 ExportTempFile::ExportTempFile (uint32_t channels, nframes_t samplerate) :
136 SndfileWriter<float> (channels, samplerate, SF_FORMAT_RAW | SF_FORMAT_FLOAT | SF_ENDIAN_FILE, "temp"),
141 beginning_processed (false),
142 end_processed (false),
143 silence_beginning (0),
150 ExportTempFile::read (float * data, nframes_t frames)
152 nframes_t frames_read = 0;
153 nframes_t to_read = 0;
154 sf_count_t read_status = 0;
156 /* Initialize state at first read */
167 /* Add silence to beginning */
169 if (silence_beginning > 0) {
170 if (silence_beginning >= frames) {
171 memset (data, 0, channels * frames * sizeof (float));
172 silence_beginning -= frames;
176 memset (data, 0, channels * silence_beginning * sizeof (float));
178 frames_read += silence_beginning;
179 data += channels * silence_beginning;
180 silence_beginning = 0;
183 /* Read file, but don't read past end */
185 if (get_read_position() >= end) {
186 // File already read, do nothing!
188 if ((get_read_position() + (frames - frames_read)) > end) {
189 to_read = end - get_read_position();
191 to_read = frames - frames_read;
194 read_status = sf_readf_float (sndfile, data, to_read);
196 frames_read += to_read;
197 data += channels * to_read;
200 /* Check for errors */
202 if (read_status != to_read) {
203 throw ExportFailed (X_("Error reading temporary export file, export might not be complete!"));
206 /* Add silence at end */
208 if (silence_end > 0) {
209 to_read = frames - frames_read;
210 if (silence_end < to_read) {
211 to_read = silence_end;
214 memset (data, 0, channels * to_read * sizeof (float));
215 silence_end -= to_read;
216 frames_read += to_read;
223 ExportTempFile::trim_beginning (bool yn)
230 if (!beginning_processed) {
231 process_beginning ();
234 start = silent_frames_beginning;
240 ExportTempFile::trim_end (bool yn)
249 if (!end_processed) {
253 end = silent_frames_end;
259 ExportTempFile::process_beginning ()
261 nframes_t frames = 1024;
262 nframes_t frames_read;
263 float * buf = new float[channels * frames];
268 while ((frames_read = _read (buf, frames)) > 0) {
269 for (nframes_t i = 0; i < frames_read; i++) {
270 for (uint32_t chn = 0; chn < channels; ++chn) {
271 if (buf[chn + i * channels] != 0.0f) {
282 silent_frames_beginning = pos;
283 beginning_processed = true;
289 ExportTempFile::process_end ()
291 nframes_t frames = 1024;
292 nframes_t frames_read;
293 float * buf = new float[channels * frames];
295 nframes_t pos = get_length() - 1;
299 locate_to (pos - frames);
300 frames_read = _read (buf, frames);
304 frames_read = _read (buf, pos);
307 for (nframes_t i = frames_read; i > 0; --i) {
308 for (uint32_t chn = 0; chn < channels; ++chn) {
309 if (buf[chn + (i - 1) * channels] != 0.0f) {
319 silent_frames_end = pos;
320 end_processed = true;
326 ExportTempFile::set_silence_beginning (nframes_t frames)
328 silence_beginning = frames;
332 ExportTempFile::set_silence_end (nframes_t frames)
334 silence_end = frames;
338 ExportTempFile::get_length ()
340 sf_count_t pos = get_position();
341 sf_count_t len = sf_seek (sndfile, 0, SEEK_END);
347 ExportTempFile::get_position ()
349 return sf_seek (sndfile, 0, SEEK_CUR);
353 ExportTempFile::get_read_position ()
355 return sf_seek (sndfile, 0, SEEK_CUR | SFM_READ);
359 ExportTempFile::locate_to (nframes_t frames)
361 return sf_seek (sndfile, frames, SEEK_SET);
365 ExportTempFile::_read (float * data, nframes_t frames)
367 return sf_readf_float (sndfile, data, frames);
370 ExportFileFactory::FilePair
371 ExportFileFactory::create (FormatPtr format, uint32_t channels, ustring const & filename)
373 switch (format->type()) {
374 case ExportFormatBase::T_Sndfile:
375 return create_sndfile (format, channels, filename);
378 throw ExportFailed (X_("Invalid format given for ExportFileFactory::create!"));
383 ExportFileFactory::check (FormatPtr format, uint32_t channels)
385 switch (format->type()) {
386 case ExportFormatBase::T_Sndfile:
387 return check_sndfile (format, channels);
390 throw ExportFailed (X_("Invalid format given for ExportFileFactory::check!"));
394 ExportFileFactory::FilePair
395 ExportFileFactory::create_sndfile (FormatPtr format, unsigned int channels, ustring const & filename)
397 typedef boost::shared_ptr<SampleFormatConverter<short> > ShortConverterPtr;
398 typedef boost::shared_ptr<SampleFormatConverter<int> > IntConverterPtr;
399 typedef boost::shared_ptr<SampleFormatConverter<float> > FloatConverterPtr;
401 typedef boost::shared_ptr<SndfileWriter<short> > ShortWriterPtr;
402 typedef boost::shared_ptr<SndfileWriter<int> > IntWriterPtr;
403 typedef boost::shared_ptr<SndfileWriter<float> > FloatWriterPtr;
405 int real_format = format->format_id() | format->sample_format() | format->endianness();
407 uint32_t data_width = sndfile_data_width (real_format);
409 if (data_width == 8 || data_width == 16) {
411 ShortConverterPtr sfc = ShortConverterPtr (new SampleFormatConverter<short> (channels, format->dither_type(), data_width));
412 ShortWriterPtr sfw = ShortWriterPtr (new SndfileWriter<short> (channels, format->sample_rate(), real_format, filename));
415 return std::make_pair (boost::static_pointer_cast<FloatSink> (sfc), boost::static_pointer_cast<ExportFileWriter> (sfw));
417 } else if (data_width == 24 || data_width == 32) {
419 IntConverterPtr sfc = IntConverterPtr (new SampleFormatConverter<int> (channels, format->dither_type(), data_width));
420 IntWriterPtr sfw = IntWriterPtr (new SndfileWriter<int> (channels, format->sample_rate(), real_format, filename));
423 return std::make_pair (boost::static_pointer_cast<FloatSink> (sfc), boost::static_pointer_cast<ExportFileWriter> (sfw));
427 FloatConverterPtr sfc = FloatConverterPtr (new SampleFormatConverter<float> (channels, format->dither_type(), data_width));
428 FloatWriterPtr sfw = FloatWriterPtr (new SndfileWriter<float> (channels, format->sample_rate(), real_format, filename));
431 return std::make_pair (boost::static_pointer_cast<FloatSink> (sfc), boost::static_pointer_cast<ExportFileWriter> (sfw));
435 ExportFileFactory::check_sndfile (FormatPtr format, unsigned int channels)
438 sf_info.channels = channels;
439 sf_info.samplerate = format->sample_rate ();
440 sf_info.format = format->format_id () | format->sample_format ();
442 return (sf_format_check (&sf_info) == SF_TRUE ? true : false);
445 } // namespace ARDOUR