2 Copyright (C) 2006 Paul Davis
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2 of the License, or
7 (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
25 #include <sys/utsname.h>
27 #include <glibmm/miscutils.h>
29 #include <ardour/sndfilesource.h>
34 using namespace ARDOUR;
36 SndFileSource::SndFileSource (const XMLNode& node)
37 : AudioFileSource (node)
42 throw failed_constructor ();
45 if (_build_peakfiles) {
46 if (initialize_peakfile (false, _path)) {
49 throw failed_constructor ();
53 AudioSourceCreated (this); /* EMIT SIGNAL */
56 SndFileSource::SndFileSource (string idstr, Flag flags)
57 : AudioFileSource (idstr, Flag (flags & ~(Writable|Removable|RemovableIfEmpty|RemoveAtDestroy)))
62 throw failed_constructor ();
65 if (_build_peakfiles) {
66 if (initialize_peakfile (false, _path)) {
69 throw failed_constructor ();
74 AudioSourceCreated (this); /* EMIT SIGNAL */
77 SndFileSource::SndFileSource (string idstr, SampleFormat sfmt, HeaderFormat hf, jack_nframes_t rate, Flag flags)
78 : AudioFileSource(idstr, flags, sfmt, hf)
84 cerr << "creating " << idstr << " hf = " << hf << endl;
89 _flags = Flag (_flags & ~Broadcast);
94 _flags = Flag (_flags & ~Broadcast);
99 _flags = Flag (_flags | Broadcast);
104 _flags = Flag (_flags & ~Broadcast);
109 _flags = Flag (_flags & ~Broadcast);
113 fatal << string_compose (_("programming error: %1"), X_("unsupported audio header format requested")) << endmsg;
121 fmt |= SF_FORMAT_FLOAT;
125 fmt |= SF_FORMAT_PCM_24;
130 _info.samplerate = rate;
134 throw failed_constructor();
137 if (writable() && (_flags & Broadcast)) {
139 _broadcast_info = new SF_BROADCAST_INFO;
140 memset (_broadcast_info, 0, sizeof (*_broadcast_info));
142 snprintf (_broadcast_info->description, sizeof (_broadcast_info->description), "BWF %s", _name.c_str());
144 struct utsname utsinfo;
146 if (uname (&utsinfo)) {
147 error << string_compose(_("FileSource: cannot get host information for BWF header (%1)"), strerror(errno)) << endmsg;
151 snprintf (_broadcast_info->originator, sizeof (_broadcast_info->originator), "ardour:%s:%s:%s:%s:%s)",
152 Glib::get_real_name().c_str(),
158 _broadcast_info->version = 1;
160 /* XXX do something about this field */
162 snprintf (_broadcast_info->umid, sizeof (_broadcast_info->umid), "%s", "fnord");
164 /* coding history is added by libsndfile */
166 if (sf_command (sf, SFC_SET_BROADCAST_INFO, _broadcast_info, sizeof (_broadcast_info)) != SF_TRUE) {
168 sf_error_str (0, errbuf, sizeof (errbuf) - 1);
169 error << string_compose (_("cannot set broadcast info for audio file %1 (%2); dropping broadcast info for this file"), _path, errbuf) << endmsg;
170 _flags = Flag (_flags & ~Broadcast);
171 delete _broadcast_info;
176 if (_build_peakfiles) {
177 if (initialize_peakfile (false, _path)) {
180 throw failed_constructor ();
184 /* since SndFileSource's constructed with this constructor can be writable, make sure we update if the header info changes */
187 HeaderPositionOffsetChanged.connect (mem_fun (*this, &AudioFileSource::handle_header_position_change));
190 if (_build_peakfiles) {
191 if (initialize_peakfile (false, _path)) {
194 throw failed_constructor ();
198 AudioSourceCreated (this); /* EMIT SIGNAL */
202 SndFileSource::init (const string& idstr)
204 string::size_type pos;
208 interleave_bufsize = 0;
212 if ((pos = idstr.find_last_of (':')) == string::npos) {
214 _name = Glib::path_get_basename (idstr);
216 channel = atoi (idstr.substr (pos+1).c_str());
217 _name = Glib::path_get_basename (idstr.substr (0, pos));
220 /* although libsndfile says we don't need to set this,
221 valgrind and source code shows us that we do.
224 memset (&_info, 0, sizeof(_info));
228 SndFileSource::open ()
230 if ((sf = sf_open (_path.c_str(), (writable() ? SFM_RDWR : SFM_READ), &_info)) == 0) {
232 sf_error_str (0, errbuf, sizeof (errbuf) - 1);
233 error << string_compose(_("SndFileSource: cannot open file \"%1\" for %2 (%3)"),
234 _path, (writable() ? "read+write" : "reading"), errbuf) << endmsg;
238 if (channel >= _info.channels) {
239 error << string_compose(_("SndFileSource: file only contains %1 channels; %2 is invalid as a channel number"), _info.channels, channel) << endmsg;
245 _length = _info.frames;
248 sf_command (sf, SFC_SET_UPDATE_HEADER_AUTO, 0, SF_FALSE);
255 SndFileSource::close ()
263 SndFileSource::~SndFileSource ()
265 GoingAway (this); /* EMIT SIGNAL */
269 if (interleave_buf) {
270 delete [] interleave_buf;
273 if (_broadcast_info) {
274 delete [] _broadcast_info;
279 SndFileSource::sample_rate () const
281 return _info.samplerate;
285 SndFileSource::read_unlocked (Sample *dst, jack_nframes_t start, jack_nframes_t cnt, char * workbuf) const
290 jack_nframes_t file_cnt;
292 if (start > _length) {
294 /* read starts beyond end of data, just memset to zero */
298 } else if (start + cnt > _length) {
300 /* read ends beyond end of data, read some, memset the rest */
302 file_cnt = _length - start;
306 /* read is entirely within data */
313 if (sf_seek (sf, (off_t) start, SEEK_SET) < 0) {
315 sf_error_str (0, errbuf, sizeof (errbuf) - 1);
316 error << string_compose(_("SndFileSource: could not seek to frame %1 within %2 (%3)"), start, _name.substr (1), errbuf) << endmsg;
320 if (_info.channels == 1) {
321 jack_nframes_t ret = sf_read_float (sf, dst, file_cnt);
322 _read_data_count = cnt * sizeof(float);
327 if (file_cnt != cnt) {
328 jack_nframes_t delta = cnt - file_cnt;
329 memset (dst+file_cnt, 0, sizeof (Sample) * delta);
332 real_cnt = cnt * _info.channels;
334 if (interleave_bufsize < real_cnt) {
336 if (interleave_buf) {
337 delete [] interleave_buf;
339 interleave_bufsize = real_cnt;
340 interleave_buf = new float[interleave_bufsize];
343 nread = sf_read_float (sf, interleave_buf, real_cnt);
344 ptr = interleave_buf + channel;
345 nread /= _info.channels;
347 /* stride through the interleaved data */
349 for (int32_t n = 0; n < nread; ++n) {
351 ptr += _info.channels;
354 _read_data_count = cnt * sizeof(float);
360 SndFileSource::write_unlocked (Sample *data, jack_nframes_t cnt, char * workbuf)
366 if (_info.channels != 1) {
367 fatal << string_compose (_("programming error: %1 %2"), X_("SndFileSource::write called on non-mono file"), _path) << endmsg;
372 jack_nframes_t oldlen;
373 int32_t frame_pos = _length;
375 if (write_float (data, frame_pos, cnt) != cnt) {
380 update_length (oldlen, cnt);
382 if (_build_peakfiles) {
383 PeakBuildRecord *pbr = 0;
385 if (pending_peak_builds.size()) {
386 pbr = pending_peak_builds.back();
389 if (pbr && pbr->frame + pbr->cnt == oldlen) {
391 /* the last PBR extended to the start of the current write,
392 so just extend it again.
397 pending_peak_builds.push_back (new PeakBuildRecord (oldlen, cnt));
400 _peaks_built = false;
404 if (_build_peakfiles) {
405 queue_for_peaks (*this);
408 _write_data_count = cnt;
414 SndFileSource::update_header (jack_nframes_t when, struct tm& now, time_t tnow)
416 /* allow derived classes to override how this is done */
418 set_timeline_position (when);
420 if (_flags & Broadcast) {
421 /* this will flush the header implicitly */
422 return setup_broadcast_info (when, now, tnow);
424 return flush_header ();
429 SndFileSource::flush_header ()
431 return (sf_command (sf, SFC_UPDATE_HEADER_NOW, 0, 0) != SF_TRUE);
435 SndFileSource::setup_broadcast_info (jack_nframes_t when, struct tm& now, time_t tnow)
437 /* random code is 9 digits */
439 int random_code = random() % 999999999;
441 snprintf (_broadcast_info->originator_reference, sizeof (_broadcast_info->originator_reference), "%2s%3s%12s%02d%02d%02d%9d",
443 bwf_organization_code,
450 snprintf (_broadcast_info->origination_date, sizeof (_broadcast_info->origination_date), "%4d-%02d-%02d",
455 snprintf (_broadcast_info->origination_time, sizeof (_broadcast_info->origination_time), "%02d-%02d-%02d",
460 /* now update header position taking header offset into account */
462 set_header_timeline_position ();
464 /* note that libsndfile flushes the header to disk when resetting the broadcast info */
466 if (sf_command (sf, SFC_SET_BROADCAST_INFO, _broadcast_info, sizeof (*_broadcast_info)) != SF_TRUE) {
467 error << string_compose (_("cannot set broadcast info for audio file %1; Dropping broadcast info for this file"), _path) << endmsg;
468 _flags = Flag (_flags & ~Broadcast);
469 delete _broadcast_info;
478 SndFileSource::set_header_timeline_position ()
482 _broadcast_info->time_reference_high = 0;
484 if (header_position_negative) {
486 if (ULONG_LONG_MAX - header_position_offset < timeline_position) {
487 pos = ULONG_LONG_MAX; // impossible
489 pos = timeline_position + header_position_offset;
494 if (timeline_position < header_position_offset) {
497 pos = timeline_position - header_position_offset;
501 _broadcast_info->time_reference_high = (pos >> 32);
502 _broadcast_info->time_reference_low = (pos & 0xffffffff);
506 SndFileSource::write_float (Sample* data, jack_nframes_t frame_pos, jack_nframes_t cnt)
508 if (sf_seek (sf, frame_pos, SEEK_SET) != frame_pos) {
509 error << string_compose (_("%1: cannot seek to %2"), _path, frame_pos) << endmsg;
513 if (sf_writef_float (sf, data, cnt) != (ssize_t) cnt) {