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>
35 /* SndfileWriterBase */
37 SndfileWriterBase::SndfileWriterBase (int channels, nframes_t samplerate, int format, string const & path) :
38 ExportFileWriter (path)
42 sf_info.channels = channels;
43 sf_info.samplerate = samplerate;
44 sf_info.format = format;
46 if (!sf_format_check (&sf_info)) {
47 throw ExportFailed (X_("Invalid format given for SndfileWriter!"));
50 if (path.length() == 0) {
51 throw ExportFailed (X_("No output file specified for SndFileWriter"));
54 /* TODO add checks that the directory path exists, and also
55 check if we are overwriting an existing file...
58 // Open file TODO make sure we have enough disk space for the output
59 if (path.compare ("temp")) {
60 if ((sndfile = sf_open (path.c_str(), SFM_WRITE, &sf_info)) == 0) {
61 sf_error_str (0, errbuf, sizeof (errbuf) - 1);
62 throw ExportFailed (string_compose(X_("Cannot open output file \"%1\" for SndFileWriter (%2)"), path, errbuf));
66 if (!(file = tmpfile ())) {
67 throw ExportFailed (X_("Cannot open tempfile"));
69 sndfile = sf_open_fd (fileno(file), SFM_RDWR, &sf_info, true);
73 SndfileWriterBase::~SndfileWriterBase ()
81 SndfileWriter<T>::SndfileWriter (int channels, nframes_t samplerate, int format, string const & path) :
82 SndfileWriterBase (channels, samplerate, format, path)
84 // init write function
90 SndfileWriter<float>::init ()
92 write_func = &sf_writef_float;
97 SndfileWriter<int>::init ()
99 write_func = &sf_writef_int;
104 SndfileWriter<short>::init ()
106 write_func = &sf_writef_short;
109 template <typename T>
111 SndfileWriter<T>::write (T * data, nframes_t frames)
114 nframes_t written = (*write_func) (sndfile, data, frames);
115 if (written != frames) {
116 sf_error_str (sndfile, errbuf, sizeof (errbuf) - 1);
117 throw ExportFailed (string_compose(_("Could not write data to output file (%1)"), errbuf));
120 if (GraphSink<T>::end_of_input) {
121 sf_write_sync (sndfile);
127 template class SndfileWriter<short>;
128 template class SndfileWriter<int>;
129 template class SndfileWriter<float>;
133 ExportTempFile::ExportTempFile (uint32_t channels, nframes_t samplerate) :
134 SndfileWriter<float> (channels, samplerate, SF_FORMAT_RAW | SF_FORMAT_FLOAT | SF_ENDIAN_FILE, "temp"),
139 beginning_processed (false),
140 end_processed (false),
141 silence_beginning (0),
148 ExportTempFile::read (float * data, nframes_t frames)
150 nframes_t frames_read = 0;
151 nframes_t to_read = 0;
152 sf_count_t read_status = 0;
154 /* Initialize state at first read */
165 /* Add silence to beginning */
167 if (silence_beginning > 0) {
168 if (silence_beginning >= frames) {
169 memset (data, 0, channels * frames * sizeof (float));
170 silence_beginning -= frames;
174 memset (data, 0, channels * silence_beginning * sizeof (float));
176 frames_read += silence_beginning;
177 data += channels * silence_beginning;
178 silence_beginning = 0;
181 /* Read file, but don't read past end */
183 if (get_read_position() >= end) {
184 // File already read, do nothing!
186 if ((get_read_position() + (frames - frames_read)) > end) {
187 to_read = end - get_read_position();
189 to_read = frames - frames_read;
192 read_status = sf_readf_float (sndfile, data, to_read);
194 frames_read += to_read;
195 data += channels * to_read;
198 /* Check for errors */
200 if (read_status != to_read) {
201 throw ExportFailed (X_("Error reading temporary export file, export might not be complete!"));
204 /* Add silence at end */
206 if (silence_end > 0) {
207 to_read = frames - frames_read;
208 if (silence_end < to_read) {
209 to_read = silence_end;
212 memset (data, 0, channels * to_read * sizeof (float));
213 silence_end -= to_read;
214 frames_read += to_read;
221 ExportTempFile::trim_beginning (bool yn)
228 if (!beginning_processed) {
229 process_beginning ();
232 start = silent_frames_beginning;
238 ExportTempFile::trim_end (bool yn)
247 if (!end_processed) {
251 end = silent_frames_end;
257 ExportTempFile::process_beginning ()
259 nframes_t frames = 1024;
260 nframes_t frames_read;
261 float * buf = new float[channels * frames];
266 while ((frames_read = _read (buf, frames)) > 0) {
267 for (nframes_t i = 0; i < frames_read; i++) {
268 for (uint32_t chn = 0; chn < channels; ++chn) {
269 if (buf[chn + i * channels] != 0.0f) {
280 silent_frames_beginning = pos;
281 beginning_processed = true;
287 ExportTempFile::process_end ()
289 nframes_t frames = 1024;
290 nframes_t frames_read;
291 float * buf = new float[channels * frames];
293 nframes_t pos = get_length() - 1;
297 locate_to (pos - frames);
298 frames_read = _read (buf, frames);
302 frames_read = _read (buf, pos);
305 for (nframes_t i = frames_read; i > 0; --i) {
306 for (uint32_t chn = 0; chn < channels; ++chn) {
307 if (buf[chn + (i - 1) * channels] != 0.0f) {
317 silent_frames_end = pos;
318 end_processed = true;
324 ExportTempFile::set_silence_beginning (nframes_t frames)
326 silence_beginning = frames;
330 ExportTempFile::set_silence_end (nframes_t frames)
332 silence_end = frames;
336 ExportTempFile::get_length ()
338 sf_count_t pos = get_position();
339 sf_count_t len = sf_seek (sndfile, 0, SEEK_END);
345 ExportTempFile::get_position ()
347 return sf_seek (sndfile, 0, SEEK_CUR);
351 ExportTempFile::get_read_position ()
353 return sf_seek (sndfile, 0, SEEK_CUR | SFM_READ);
357 ExportTempFile::locate_to (nframes_t frames)
359 return sf_seek (sndfile, frames, SEEK_SET);
363 ExportTempFile::_read (float * data, nframes_t frames)
365 return sf_readf_float (sndfile, data, frames);
368 ExportFileFactory::FilePair
369 ExportFileFactory::create (FormatPtr format, uint32_t channels, ustring const & filename)
371 switch (format->type()) {
372 case ExportFormatBase::T_Sndfile:
373 return create_sndfile (format, channels, filename);
376 throw ExportFailed (X_("Invalid format given for ExportFileFactory::create!"));
381 ExportFileFactory::check (FormatPtr format, uint32_t channels)
383 switch (format->type()) {
384 case ExportFormatBase::T_Sndfile:
385 return check_sndfile (format, channels);
388 throw ExportFailed (X_("Invalid format given for ExportFileFactory::check!"));
392 ExportFileFactory::FilePair
393 ExportFileFactory::create_sndfile (FormatPtr format, unsigned int channels, ustring const & filename)
395 typedef boost::shared_ptr<SampleFormatConverter<short> > ShortConverterPtr;
396 typedef boost::shared_ptr<SampleFormatConverter<int> > IntConverterPtr;
397 typedef boost::shared_ptr<SampleFormatConverter<float> > FloatConverterPtr;
399 typedef boost::shared_ptr<SndfileWriter<short> > ShortWriterPtr;
400 typedef boost::shared_ptr<SndfileWriter<int> > IntWriterPtr;
401 typedef boost::shared_ptr<SndfileWriter<float> > FloatWriterPtr;
403 int real_format = format->format_id() | format->sample_format() | format->endianness();
405 uint32_t data_width = sndfile_data_width (real_format);
407 if (data_width == 8 || data_width == 16) {
409 ShortConverterPtr sfc = ShortConverterPtr (new SampleFormatConverter<short> (channels, format->dither_type(), data_width));
410 ShortWriterPtr sfw = ShortWriterPtr (new SndfileWriter<short> (channels, format->sample_rate(), real_format, filename));
413 return std::make_pair (boost::static_pointer_cast<FloatSink> (sfc), boost::static_pointer_cast<ExportFileWriter> (sfw));
415 } else if (data_width == 24 || data_width == 32) {
417 IntConverterPtr sfc = IntConverterPtr (new SampleFormatConverter<int> (channels, format->dither_type(), data_width));
418 IntWriterPtr sfw = IntWriterPtr (new SndfileWriter<int> (channels, format->sample_rate(), real_format, filename));
421 return std::make_pair (boost::static_pointer_cast<FloatSink> (sfc), boost::static_pointer_cast<ExportFileWriter> (sfw));
425 FloatConverterPtr sfc = FloatConverterPtr (new SampleFormatConverter<float> (channels, format->dither_type(), data_width));
426 FloatWriterPtr sfw = FloatWriterPtr (new SndfileWriter<float> (channels, format->sample_rate(), real_format, filename));
429 return std::make_pair (boost::static_pointer_cast<FloatSink> (sfc), boost::static_pointer_cast<ExportFileWriter> (sfw));
433 ExportFileFactory::check_sndfile (FormatPtr format, unsigned int channels)
436 sf_info.channels = channels;
437 sf_info.samplerate = format->sample_rate ();
438 sf_info.format = format->format_id () | format->sample_format ();
440 return (sf_format_check (&sf_info) == SF_TRUE ? true : false);
443 } // namespace ARDOUR