summaryrefslogtreecommitdiff
path: root/libs/ardour/export_file_io.cc
blob: 2f8fdeacdf6f10d91e430d45d1ad2f62d858f42f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
/*
    Copyright (C) 2008 Paul Davis
    Author: Sakari Bergen

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software
    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.

*/

#include <string.h>

#include "ardour/export_file_io.h"

#include "ardour/export_failed.h"
#include "pbd/failed_constructor.h"

#include "i18n.h"

using namespace std;
using namespace Glib;
using namespace PBD;

namespace ARDOUR
{

/* SndfileWriterBase */

SndfileWriterBase::SndfileWriterBase (int channels, nframes_t samplerate, int format, string const & path) :
  ExportFileWriter (path)
{
	char errbuf[256];
	
	sf_info.channels = channels;
	sf_info.samplerate = samplerate;
	sf_info.format = format;
	
	if (!sf_format_check (&sf_info)) {
		throw ExportFailed (X_("Invalid format given for SndfileWriter!"));
	}
	
	if (path.length() == 0) {
		throw ExportFailed (X_("No output file specified for SndFileWriter"));
	}

	/* TODO add checks that the directory path exists, and also 
	   check if we are overwriting an existing file...
	*/
	
	// Open file TODO make sure we have enough disk space for the output 
	if (path.compare ("temp")) {
		if ((sndfile = sf_open (path.c_str(), SFM_WRITE, &sf_info)) == 0) {
			sf_error_str (0, errbuf, sizeof (errbuf) - 1);
			throw ExportFailed (string_compose(X_("Cannot open output file \"%1\" for SndFileWriter (%2)"), path, errbuf));
		}
	} else {
		FILE * file;
		if (!(file = tmpfile ())) {
			throw ExportFailed (X_("Cannot open tempfile"));
		}
		sndfile = sf_open_fd (fileno(file), SFM_RDWR, &sf_info, true);
	}
}

SndfileWriterBase::~SndfileWriterBase ()
{
	sf_close (sndfile);
}

/* SndfileWriter */

template <typename T>
SndfileWriter<T>::SndfileWriter (int channels, nframes_t samplerate, int format, string const & path) :
  SndfileWriterBase (channels, samplerate, format, path)
{
	// init write function
	init ();
}

template <>
void
SndfileWriter<float>::init ()
{
	write_func = &sf_writef_float;
}

template <>
void
SndfileWriter<int>::init ()
{
	write_func = &sf_writef_int;
}

template <>
void
SndfileWriter<short>::init ()
{
	write_func = &sf_writef_short;
}

template <typename T>
nframes_t
SndfileWriter<T>::write (T * data, nframes_t frames)
{
	char errbuf[256];
	nframes_t written = (*write_func) (sndfile, data, frames);
	if (written != frames) {
		sf_error_str (sndfile, errbuf, sizeof (errbuf) - 1);
		throw ExportFailed (string_compose(_("Could not write data to output file (%1)"), errbuf));
	}
	
	if (GraphSink<T>::end_of_input) {
		sf_write_sync (sndfile);
	}
	
	return frames;
}

template class SndfileWriter<short>;
template class SndfileWriter<int>;
template class SndfileWriter<float>;

/* ExportTempFile */

ExportTempFile::ExportTempFile (uint32_t channels, nframes_t samplerate) :
  SndfileWriter<float> (channels, samplerate, SF_FORMAT_RAW | SF_FORMAT_FLOAT | SF_ENDIAN_FILE, "temp"),
  channels (channels),
  reading (false),
  start (0),
  end (0),
  beginning_processed (false),
  end_processed (false),
  silence_beginning (0),
  silence_end (0),
  end_set (false)
{
}

nframes_t
ExportTempFile::read (float * data, nframes_t frames)
{
	nframes_t frames_read = 0;
	nframes_t to_read = 0;
	sf_count_t read_status = 0;
	
	/* Initialize state at first read */
	
	if (!reading) {
		if (!end_set) {
			end = get_length();
			end_set = true;
		}
		locate_to (start);
		reading = true;
	}
	
	/* Add silence to beginning */
	
	if (silence_beginning > 0) {
		if (silence_beginning >= frames) {
			memset (data, 0, channels * frames * sizeof (float));
			silence_beginning -= frames;
			return frames;
		}
		
		memset (data, 0, channels * silence_beginning * sizeof (float));
		
		frames_read += silence_beginning;
		data += channels * silence_beginning;
		silence_beginning = 0;
	}
	
	/* Read file, but don't read past end */
	
	if (get_read_position() >= end) {
		// File already read, do nothing!
	} else {
		if ((get_read_position() + (frames - frames_read)) > end) {
			to_read = end - get_read_position();
		} else {
			to_read = frames - frames_read;
		}
		
		read_status = sf_readf_float (sndfile, data, to_read);
		
		frames_read += to_read;
		data += channels * to_read;
	}
	
	/* Check for errors */
	
	if (read_status != to_read) {
		throw ExportFailed (X_("Error reading temporary export file, export might not be complete!"));
	}
	
	/* Add silence at end */
	
	if (silence_end > 0) {
		to_read = frames - frames_read;
		if (silence_end < to_read) {
			to_read = silence_end;
		}
		
		memset (data, 0, channels * to_read * sizeof (float));
		silence_end -= to_read;
		frames_read += to_read;
	}
	
	return frames_read;
}

nframes_t
ExportTempFile::trim_beginning (bool yn)
{
	if (!yn) {
		start = 0;
		return start;
	}

	if (!beginning_processed) {
		process_beginning ();
	}
	
	start = silent_frames_beginning;
	return start;
	
}

nframes_t
ExportTempFile::trim_end (bool yn)
{
	end_set = true;

	if (!yn) {
		end = get_length();
		return end;
	}

	if (!end_processed) {
		process_end ();
	}
	
	end = silent_frames_end;
	return end;
}


void
ExportTempFile::process_beginning ()
{
	nframes_t frames = 1024;
	nframes_t frames_read;
	float * buf = new float[channels * frames];
	
	nframes_t pos = 0;
	locate_to (pos);
	
	while ((frames_read = _read (buf, frames)) > 0) {
		for (nframes_t i = 0; i < frames_read; i++) {
			for (uint32_t chn = 0; chn < channels; ++chn) {
				if (buf[chn + i * channels] != 0.0f) {
					--pos;
					goto out;
				}
			}
			++pos;
		}
	}
	
	out:
	
	silent_frames_beginning = pos;
	beginning_processed = true;
	
	delete [] buf;
}

void
ExportTempFile::process_end ()
{
	nframes_t frames = 1024;
	nframes_t frames_read;
	float * buf = new float[channels * frames];
	
	nframes_t pos = get_length() - 1;
	
	while (pos > 0) {
		if (pos > frames) {
			locate_to (pos - frames);
			frames_read = _read (buf, frames);
		} else {
			// Last time reading
			locate_to (0);
			frames_read = _read (buf, pos);
		}
		
		for (nframes_t i = frames_read; i > 0; --i) {
			for (uint32_t chn = 0; chn < channels; ++chn) {
				if (buf[chn + (i - 1) * channels] != 0.0f) {
					goto out;
				}
			}
			--pos;
		}
	}
	
	out:
	
	silent_frames_end = pos;
	end_processed = true;
	
	delete [] buf;
}

void
ExportTempFile::set_silence_beginning (nframes_t frames)
{
	silence_beginning = frames;
}

void
ExportTempFile::set_silence_end (nframes_t frames)
{
	silence_end = frames;
}

sf_count_t
ExportTempFile::get_length ()
{
	sf_count_t pos = get_position();
	sf_count_t len = sf_seek (sndfile, 0, SEEK_END);
	locate_to (pos);
	return len;
}

sf_count_t
ExportTempFile::get_position ()
{
	return sf_seek (sndfile, 0, SEEK_CUR);
}

sf_count_t
ExportTempFile::get_read_position ()
{
	return sf_seek (sndfile, 0, SEEK_CUR | SFM_READ);
}

sf_count_t
ExportTempFile::locate_to (nframes_t frames)
{
	return sf_seek (sndfile, frames, SEEK_SET);
}

sf_count_t
ExportTempFile::_read (float * data, nframes_t frames)
{
	return sf_readf_float (sndfile, data, frames);
}

ExportFileFactory::FilePair
ExportFileFactory::create (FormatPtr format, uint32_t channels, ustring const & filename)
{
	switch (format->type()) {
	  case ExportFormatBase::T_Sndfile:
		return create_sndfile (format, channels, filename);

	  default:
		throw ExportFailed (X_("Invalid format given for ExportFileFactory::create!"));
	}
}

bool
ExportFileFactory::check (FormatPtr format, uint32_t channels)
{
	switch (format->type()) {
	  case ExportFormatBase::T_Sndfile:
		return check_sndfile (format, channels);

	  default:
		throw ExportFailed (X_("Invalid format given for ExportFileFactory::check!"));
	}
}

ExportFileFactory::FilePair
ExportFileFactory::create_sndfile (FormatPtr format, unsigned int channels, ustring const & filename)
{
	typedef boost::shared_ptr<SampleFormatConverter<short> > ShortConverterPtr;
	typedef boost::shared_ptr<SampleFormatConverter<int> > IntConverterPtr;
	typedef boost::shared_ptr<SampleFormatConverter<float> > FloatConverterPtr;
	
	typedef boost::shared_ptr<SndfileWriter<short> > ShortWriterPtr;
	typedef boost::shared_ptr<SndfileWriter<int> > IntWriterPtr;
	typedef boost::shared_ptr<SndfileWriter<float> > FloatWriterPtr;
	
	int real_format = format->format_id() | format->sample_format() | format->endianness();

	uint32_t data_width = sndfile_data_width (real_format);

	if (data_width == 8 || data_width == 16) {
	
		ShortConverterPtr sfc = ShortConverterPtr (new SampleFormatConverter<short> (channels, format->dither_type(), data_width));
		ShortWriterPtr sfw = ShortWriterPtr (new SndfileWriter<short> (channels, format->sample_rate(), real_format, filename));
		sfc->pipe_to (sfw);
		
		return std::make_pair (boost::static_pointer_cast<FloatSink> (sfc), boost::static_pointer_cast<ExportFileWriter> (sfw));

	} else if (data_width == 24 || data_width == 32) {
	
		IntConverterPtr sfc = IntConverterPtr (new SampleFormatConverter<int> (channels, format->dither_type(), data_width));
		IntWriterPtr sfw = IntWriterPtr (new SndfileWriter<int> (channels, format->sample_rate(), real_format, filename));
		sfc->pipe_to (sfw);
		
		return std::make_pair (boost::static_pointer_cast<FloatSink> (sfc), boost::static_pointer_cast<ExportFileWriter> (sfw));

	}
	
	FloatConverterPtr sfc = FloatConverterPtr (new SampleFormatConverter<float> (channels, format->dither_type(), data_width));
	FloatWriterPtr sfw = FloatWriterPtr (new SndfileWriter<float> (channels, format->sample_rate(), real_format, filename));
	sfc->pipe_to (sfw);
	
	return std::make_pair (boost::static_pointer_cast<FloatSink> (sfc), boost::static_pointer_cast<ExportFileWriter> (sfw));
}

bool
ExportFileFactory::check_sndfile (FormatPtr format, unsigned int channels)
{
	SF_INFO sf_info;
	sf_info.channels = channels;
	sf_info.samplerate = format->sample_rate ();
	sf_info.format = format->format_id () | format->sample_format ();

	return (sf_format_check (&sf_info) == SF_TRUE ? true : false);
}

} // namespace ARDOUR