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;
37 SndFileSource::SndFileSource (Session& s, const XMLNode& node)
38 : AudioFileSource (s, node)
43 throw failed_constructor ();
46 if (_build_peakfiles) {
47 if (initialize_peakfile (false, _path)) {
50 throw failed_constructor ();
55 SndFileSource::SndFileSource (Session& s, string idstr, Flag flags)
56 /* files created this way are never writable or removable */
57 : AudioFileSource (s, idstr, Flag (flags & ~(Writable|Removable|RemovableIfEmpty|RemoveAtDestroy)))
62 throw failed_constructor ();
65 if (!(_flags & NoPeakFile) && _build_peakfiles) {
66 if (initialize_peakfile (false, _path)) {
69 throw failed_constructor ();
74 SndFileSource::SndFileSource (Session& s, string idstr, SampleFormat sfmt, HeaderFormat hf, jack_nframes_t rate, Flag flags)
75 : AudioFileSource (s, idstr, flags, sfmt, hf)
84 _flags = Flag (_flags & ~Broadcast);
89 _flags = Flag (_flags & ~Broadcast);
94 _flags = Flag (_flags | Broadcast);
99 _flags = Flag (_flags & ~Broadcast);
104 _flags = Flag (_flags & ~Broadcast);
108 fatal << string_compose (_("programming error: %1"), X_("unsupported audio header format requested")) << endmsg;
116 fmt |= SF_FORMAT_FLOAT;
120 fmt |= SF_FORMAT_PCM_24;
125 _info.samplerate = rate;
129 throw failed_constructor();
132 if (writable() && (_flags & Broadcast)) {
134 _broadcast_info = new SF_BROADCAST_INFO;
135 memset (_broadcast_info, 0, sizeof (*_broadcast_info));
137 snprintf (_broadcast_info->description, sizeof (_broadcast_info->description), "BWF %s", _name.c_str());
139 struct utsname utsinfo;
141 if (uname (&utsinfo)) {
142 error << string_compose(_("FileSource: cannot get host information for BWF header (%1)"), strerror(errno)) << endmsg;
146 snprintf (_broadcast_info->originator, sizeof (_broadcast_info->originator), "ardour:%s:%s:%s:%s:%s)",
147 Glib::get_real_name().c_str(),
153 _broadcast_info->version = 1;
154 _broadcast_info->time_reference_low = 0;
155 _broadcast_info->time_reference_high = 0;
157 /* XXX do something about this field */
159 snprintf (_broadcast_info->umid, sizeof (_broadcast_info->umid), "%s", "fnord");
161 /* coding history is added by libsndfile */
163 if (sf_command (sf, SFC_SET_BROADCAST_INFO, _broadcast_info, sizeof (_broadcast_info)) != SF_TRUE) {
165 sf_error_str (0, errbuf, sizeof (errbuf) - 1);
166 error << string_compose (_("cannot set broadcast info for audio file %1 (%2); dropping broadcast info for this file"), _path, errbuf) << endmsg;
167 _flags = Flag (_flags & ~Broadcast);
168 delete _broadcast_info;
174 if (!(_flags & NoPeakFile) && _build_peakfiles) {
175 if (initialize_peakfile (true, _path)) {
178 throw failed_constructor ();
184 SndFileSource::init (const string& idstr)
186 string::size_type pos;
190 interleave_bufsize = 0;
194 if ((pos = idstr.find_last_of (':')) == string::npos) {
196 _name = Glib::path_get_basename (idstr);
198 channel = atoi (idstr.substr (pos+1).c_str());
199 _name = Glib::path_get_basename (idstr.substr (0, pos));
202 /* although libsndfile says we don't need to set this,
203 valgrind and source code shows us that we do.
206 memset (&_info, 0, sizeof(_info));
210 SndFileSource::open ()
212 if ((sf = sf_open (_path.c_str(), (writable() ? SFM_RDWR : SFM_READ), &_info)) == 0) {
214 sf_error_str (0, errbuf, sizeof (errbuf) - 1);
215 error << string_compose(_("SndFileSource: cannot open file \"%1\" for %2 (%3)"),
216 _path, (writable() ? "read+write" : "reading"), errbuf) << endmsg;
220 if (channel >= _info.channels) {
221 error << string_compose(_("SndFileSource: file only contains %1 channels; %2 is invalid as a channel number"), _info.channels, channel) << endmsg;
227 _length = _info.frames;
229 _broadcast_info = new SF_BROADCAST_INFO;
230 memset (_broadcast_info, 0, sizeof (*_broadcast_info));
232 /* lookup broadcast info */
234 if (sf_command (sf, SFC_GET_BROADCAST_INFO, _broadcast_info, sizeof (*_broadcast_info)) != SF_TRUE) {
236 /* if the file has data but no broadcast info, then clearly, there is no broadcast info */
239 delete _broadcast_info;
241 _flags = Flag (_flags & ~Broadcast);
244 set_timeline_position (header_position_offset);
248 /* XXX 64 bit alert: when JACK switches to a 64 bit frame count, this needs to use the high bits
249 of the time reference.
252 set_timeline_position ( _broadcast_info->time_reference_low );
256 sf_command (sf, SFC_SET_UPDATE_HEADER_AUTO, 0, SF_FALSE);
262 SndFileSource::~SndFileSource ()
264 GoingAway (); /* EMIT SIGNAL */
271 if (interleave_buf) {
272 delete [] interleave_buf;
275 if (_broadcast_info) {
276 delete _broadcast_info;
281 SndFileSource::sample_rate () const
283 return _info.samplerate;
287 SndFileSource::read_unlocked (Sample *dst, jack_nframes_t start, jack_nframes_t cnt) const
292 jack_nframes_t file_cnt;
294 //destructive (tape) tracks need to offset reads and writes by the timeline position
295 if (_flags && ARDOUR::Destructive == ARDOUR::Destructive) {
296 start -= timeline_position;
299 if (start > _length) {
301 /* read starts beyond end of data, just memset to zero */
305 } else if (start + cnt > _length) {
307 /* read ends beyond end of data, read some, memset the rest */
309 file_cnt = _length - start;
313 /* read is entirely within data */
320 if (sf_seek (sf, (sf_count_t) start, SEEK_SET|SFM_READ) != (sf_count_t) start) {
322 sf_error_str (0, errbuf, sizeof (errbuf) - 1);
323 error << string_compose(_("SndFileSource: could not seek to frame %1 within %2 (%3)"), start, _name.substr (1), errbuf) << endmsg;
327 if (_info.channels == 1) {
328 jack_nframes_t ret = sf_read_float (sf, dst, file_cnt);
329 _read_data_count = cnt * sizeof(float);
334 if (file_cnt != cnt) {
335 jack_nframes_t delta = cnt - file_cnt;
336 memset (dst+file_cnt, 0, sizeof (Sample) * delta);
339 real_cnt = cnt * _info.channels;
341 if (interleave_bufsize < real_cnt) {
343 if (interleave_buf) {
344 delete [] interleave_buf;
346 interleave_bufsize = real_cnt;
347 interleave_buf = new float[interleave_bufsize];
350 nread = sf_read_float (sf, interleave_buf, real_cnt);
351 ptr = interleave_buf + channel;
352 nread /= _info.channels;
354 /* stride through the interleaved data */
356 for (int32_t n = 0; n < nread; ++n) {
358 ptr += _info.channels;
361 _read_data_count = cnt * sizeof(float);
367 SndFileSource::write_unlocked (Sample *data, jack_nframes_t cnt)
373 if (_info.channels != 1) {
374 fatal << string_compose (_("programming error: %1 %2"), X_("SndFileSource::write called on non-mono file"), _path) << endmsg;
379 jack_nframes_t oldlen;
380 int32_t frame_pos = _length;
382 if (write_float (data, frame_pos, cnt) != cnt) {
387 update_length (oldlen, cnt);
389 if (_build_peakfiles) {
390 PeakBuildRecord *pbr = 0;
392 if (pending_peak_builds.size()) {
393 pbr = pending_peak_builds.back();
396 if (pbr && pbr->frame + pbr->cnt == oldlen) {
398 /* the last PBR extended to the start of the current write,
399 so just extend it again.
404 pending_peak_builds.push_back (new PeakBuildRecord (oldlen, cnt));
407 _peaks_built = false;
411 if (_build_peakfiles) {
412 queue_for_peaks (this);
415 _write_data_count = cnt;
421 SndFileSource::update_header (jack_nframes_t when, struct tm& now, time_t tnow)
423 set_timeline_position (when);
425 if (_flags & Broadcast) {
426 if (setup_broadcast_info (when, now, tnow)) {
431 return flush_header ();
435 SndFileSource::flush_header ()
437 if (!writable() || (sf == 0)) {
441 return (sf_command (sf, SFC_UPDATE_HEADER_NOW, 0, 0) != SF_TRUE);
445 SndFileSource::setup_broadcast_info (jack_nframes_t when, struct tm& now, time_t tnow)
451 if (!(_flags & Broadcast)) {
455 /* random code is 9 digits */
457 int random_code = random() % 999999999;
459 snprintf (_broadcast_info->originator_reference, sizeof (_broadcast_info->originator_reference), "%2s%3s%12s%02d%02d%02d%9d",
461 bwf_organization_code,
468 snprintf (_broadcast_info->origination_date, sizeof (_broadcast_info->origination_date), "%4d-%02d-%02d",
473 snprintf (_broadcast_info->origination_time, sizeof (_broadcast_info->origination_time), "%02d:%02d:%02d",
478 /* now update header position taking header offset into account */
480 set_header_timeline_position ();
482 if (sf_command (sf, SFC_SET_BROADCAST_INFO, _broadcast_info, sizeof (*_broadcast_info)) != SF_TRUE) {
483 error << string_compose (_("cannot set broadcast info for audio file %1; Dropping broadcast info for this file"), _path) << endmsg;
484 _flags = Flag (_flags & ~Broadcast);
485 delete _broadcast_info;
494 SndFileSource::set_header_timeline_position ()
496 if (!(_flags & Broadcast)) {
500 _broadcast_info->time_reference_high = (timeline_position >> 32);
501 _broadcast_info->time_reference_low = (timeline_position & 0xffffffff);
503 if (sf_command (sf, SFC_SET_BROADCAST_INFO, _broadcast_info, sizeof (*_broadcast_info)) != SF_TRUE) {
504 error << string_compose (_("cannot set broadcast info for audio file %1; Dropping broadcast info for this file"), _path) << endmsg;
505 _flags = Flag (_flags & ~Broadcast);
506 delete _broadcast_info;
515 SndFileSource::write_float (Sample* data, jack_nframes_t frame_pos, jack_nframes_t cnt)
517 if (sf_seek (sf, frame_pos, SEEK_SET|SFM_WRITE) != frame_pos) {
518 error << string_compose (_("%1: cannot seek to %2"), _path, frame_pos) << endmsg;
522 if (sf_writef_float (sf, data, cnt) != (ssize_t) cnt) {
530 SndFileSource::natural_position() const
532 return timeline_position;