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 (const XMLNode& node)
38 : AudioFileSource (node)
43 throw failed_constructor ();
46 if (_build_peakfiles) {
47 if (initialize_peakfile (false, _path)) {
50 throw failed_constructor ();
54 SourceCreated (this); /* EMIT SIGNAL */
57 SndFileSource::SndFileSource (string idstr, Flag flags)
58 /* files created this way are never writable or removable */
59 : AudioFileSource (idstr, Flag (flags & ~(Writable|Removable|RemovableIfEmpty|RemoveAtDestroy)))
64 throw failed_constructor ();
67 if (!(_flags & NoPeakFile) && _build_peakfiles) {
68 if (initialize_peakfile (false, _path)) {
71 throw failed_constructor ();
76 SourceCreated (this); /* EMIT SIGNAL */
79 SndFileSource::SndFileSource (string idstr, SampleFormat sfmt, HeaderFormat hf, jack_nframes_t rate, Flag flags)
80 : AudioFileSource(idstr, flags, sfmt, hf)
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;
159 _broadcast_info->time_reference_low = 0;
160 _broadcast_info->time_reference_high = 0;
162 /* XXX do something about this field */
164 snprintf (_broadcast_info->umid, sizeof (_broadcast_info->umid), "%s", "fnord");
166 /* coding history is added by libsndfile */
168 if (sf_command (sf, SFC_SET_BROADCAST_INFO, _broadcast_info, sizeof (_broadcast_info)) != SF_TRUE) {
170 sf_error_str (0, errbuf, sizeof (errbuf) - 1);
171 error << string_compose (_("cannot set broadcast info for audio file %1 (%2); dropping broadcast info for this file"), _path, errbuf) << endmsg;
172 _flags = Flag (_flags & ~Broadcast);
173 delete _broadcast_info;
179 if (!(_flags & NoPeakFile) && _build_peakfiles) {
180 if (initialize_peakfile (true, _path)) {
183 throw failed_constructor ();
187 SourceCreated (this); /* EMIT SIGNAL */
192 SndFileSource::init (const string& idstr)
194 string::size_type pos;
198 interleave_bufsize = 0;
202 if ((pos = idstr.find_last_of (':')) == string::npos) {
204 _name = Glib::path_get_basename (idstr);
206 channel = atoi (idstr.substr (pos+1).c_str());
207 _name = Glib::path_get_basename (idstr.substr (0, pos));
210 /* although libsndfile says we don't need to set this,
211 valgrind and source code shows us that we do.
214 memset (&_info, 0, sizeof(_info));
218 SndFileSource::open ()
220 if ((sf = sf_open (_path.c_str(), (writable() ? SFM_RDWR : SFM_READ), &_info)) == 0) {
222 sf_error_str (0, errbuf, sizeof (errbuf) - 1);
223 error << string_compose(_("SndFileSource: cannot open file \"%1\" for %2 (%3)"),
224 _path, (writable() ? "read+write" : "reading"), errbuf) << endmsg;
228 if (channel >= _info.channels) {
229 error << string_compose(_("SndFileSource: file only contains %1 channels; %2 is invalid as a channel number"), _info.channels, channel) << endmsg;
235 _length = _info.frames;
238 _broadcast_info = (SF_BROADCAST_INFO*) calloc (1, sizeof (SF_BROADCAST_INFO));
240 /* lookup broadcast info */
242 if (sf_command (sf, SFC_GET_BROADCAST_INFO, _broadcast_info, sizeof (*_broadcast_info)) != SF_TRUE) {
244 /* if the file has data but no broadcast info, then clearly, there is no broadcast info */
247 free (_broadcast_info);
249 _flags = Flag (_flags & ~Broadcast);
252 set_timeline_position (header_position_offset);
256 /* XXX 64 bit alert: when JACK switches to a 64 bit frame count, this needs to use the high bits
257 of the time reference.
260 set_timeline_position ( _broadcast_info->time_reference_low );
264 sf_command (sf, SFC_SET_UPDATE_HEADER_AUTO, 0, SF_FALSE);
270 SndFileSource::~SndFileSource ()
272 GoingAway (this); /* EMIT SIGNAL */
279 if (interleave_buf) {
280 delete [] interleave_buf;
283 if (_broadcast_info) {
284 delete _broadcast_info;
289 SndFileSource::sample_rate () const
291 return _info.samplerate;
295 SndFileSource::read_unlocked (Sample *dst, jack_nframes_t start, jack_nframes_t cnt) const
300 jack_nframes_t file_cnt;
302 if (start > _length) {
304 /* read starts beyond end of data, just memset to zero */
308 } else if (start + cnt > _length) {
310 /* read ends beyond end of data, read some, memset the rest */
312 file_cnt = _length - start;
316 /* read is entirely within data */
323 if (sf_seek (sf, (sf_count_t) start, SEEK_SET|SFM_READ) != (sf_count_t) start) {
325 sf_error_str (0, errbuf, sizeof (errbuf) - 1);
326 error << string_compose(_("SndFileSource: could not seek to frame %1 within %2 (%3)"), start, _name.substr (1), errbuf) << endmsg;
330 if (_info.channels == 1) {
331 jack_nframes_t ret = sf_read_float (sf, dst, file_cnt);
332 _read_data_count = cnt * sizeof(float);
337 if (file_cnt != cnt) {
338 jack_nframes_t delta = cnt - file_cnt;
339 memset (dst+file_cnt, 0, sizeof (Sample) * delta);
342 real_cnt = cnt * _info.channels;
344 if (interleave_bufsize < real_cnt) {
346 if (interleave_buf) {
347 delete [] interleave_buf;
349 interleave_bufsize = real_cnt;
350 interleave_buf = new float[interleave_bufsize];
353 nread = sf_read_float (sf, interleave_buf, real_cnt);
354 ptr = interleave_buf + channel;
355 nread /= _info.channels;
357 /* stride through the interleaved data */
359 for (int32_t n = 0; n < nread; ++n) {
361 ptr += _info.channels;
364 _read_data_count = cnt * sizeof(float);
370 SndFileSource::write_unlocked (Sample *data, jack_nframes_t cnt)
376 if (_info.channels != 1) {
377 fatal << string_compose (_("programming error: %1 %2"), X_("SndFileSource::write called on non-mono file"), _path) << endmsg;
382 jack_nframes_t oldlen;
383 int32_t frame_pos = _length;
385 if (write_float (data, frame_pos, cnt) != cnt) {
390 update_length (oldlen, cnt);
392 if (_build_peakfiles) {
393 PeakBuildRecord *pbr = 0;
395 if (pending_peak_builds.size()) {
396 pbr = pending_peak_builds.back();
399 if (pbr && pbr->frame + pbr->cnt == oldlen) {
401 /* the last PBR extended to the start of the current write,
402 so just extend it again.
407 pending_peak_builds.push_back (new PeakBuildRecord (oldlen, cnt));
410 _peaks_built = false;
414 if (_build_peakfiles) {
415 queue_for_peaks (*this);
418 _write_data_count = cnt;
424 SndFileSource::update_header (jack_nframes_t when, struct tm& now, time_t tnow)
426 set_timeline_position (when);
428 if (_flags & Broadcast) {
429 if (setup_broadcast_info (when, now, tnow)) {
434 return flush_header ();
438 SndFileSource::flush_header ()
440 if (!writable() || (sf == 0)) {
444 return (sf_command (sf, SFC_UPDATE_HEADER_NOW, 0, 0) != SF_TRUE);
448 SndFileSource::setup_broadcast_info (jack_nframes_t when, struct tm& now, time_t tnow)
454 if (!(_flags & Broadcast)) {
458 /* random code is 9 digits */
460 int random_code = random() % 999999999;
462 snprintf (_broadcast_info->originator_reference, sizeof (_broadcast_info->originator_reference), "%2s%3s%12s%02d%02d%02d%9d",
464 bwf_organization_code,
471 snprintf (_broadcast_info->origination_date, sizeof (_broadcast_info->origination_date), "%4d-%02d-%02d",
476 snprintf (_broadcast_info->origination_time, sizeof (_broadcast_info->origination_time), "%02d:%02d:%02d",
481 /* now update header position taking header offset into account */
483 set_header_timeline_position ();
485 if (sf_command (sf, SFC_SET_BROADCAST_INFO, _broadcast_info, sizeof (*_broadcast_info)) != SF_TRUE) {
486 error << string_compose (_("cannot set broadcast info for audio file %1; Dropping broadcast info for this file"), _path) << endmsg;
487 _flags = Flag (_flags & ~Broadcast);
488 free (_broadcast_info);
497 SndFileSource::set_header_timeline_position ()
499 if (!(_flags & Broadcast)) {
503 _broadcast_info->time_reference_high = (timeline_position >> 32);
504 _broadcast_info->time_reference_low = (timeline_position & 0xffffffff);
506 if (sf_command (sf, SFC_SET_BROADCAST_INFO, _broadcast_info, sizeof (*_broadcast_info)) != SF_TRUE) {
507 error << string_compose (_("cannot set broadcast info for audio file %1; Dropping broadcast info for this file"), _path) << endmsg;
508 _flags = Flag (_flags & ~Broadcast);
509 free (_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;