2 * Copyright (C) 2008-2017 Paul Davis <paul@linuxaudiosystems.com>
3 * Copyright (C) 2009-2011 Carl Hetherington <carl@carlh.net>
4 * Copyright (C) 2009 David Robillard <d@drobilla.net>
5 * Copyright (C) 2015 Robin Gareus <robin@gareus.org>
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License along
18 * with this program; if not, write to the Free Software Foundation, Inc.,
19 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
28 #include "pbd/gstdio_compat.h"
31 #include "pbd/error.h"
32 #include "ardour/sndfileimportable.h"
34 #include <glibmm/convert.h>
36 using namespace ARDOUR;
39 /* FIXME: this was copied from sndfilesource.cc, at some point these should be merged */
41 SndFileImportableSource::get_timecode_info (SNDFILE* sf, SF_BROADCAST_INFO* binfo, bool& exists)
43 if (sf_command (sf, SFC_GET_BROADCAST_INFO, binfo, sizeof (*binfo)) != SF_TRUE) {
48 /* see http://tracker.ardour.org/view.php?id=6208
49 * 0xffffffff 0xfffc5680
50 * seems to be a bug in Presonus Capture (which generated the file)
52 * still since samplepos_t is a signed int, ignore files that could
53 * lead to negative timestamps for now.
56 if (binfo->time_reference_high & 0x80000000) {
58 snprintf(tmp, sizeof(tmp), "%x%08x", binfo->time_reference_high, binfo->time_reference_low);
59 PBD::warning << "Invalid Timestamp " << tmp << endmsg;
65 /* libsndfile reads eactly 4 bytes for high and low, but
66 * uses "unsigned int" which may or may not be 32 bit little
69 int64_t ret = (uint32_t) (binfo->time_reference_high & 0x7fffffff);
71 ret |= (uint32_t) (binfo->time_reference_low & 0xffffffff);
77 SndFileImportableSource::SndFileImportableSource (const string& path)
79 int fd = g_open (path.c_str (), O_RDONLY, 0444);
81 throw failed_constructor ();
83 memset(&sf_info, 0 , sizeof(sf_info));
84 in.reset (sf_open_fd (fd, SFM_READ, &sf_info, true), sf_close);
85 if (!in) throw failed_constructor();
87 SF_BROADCAST_INFO binfo;
90 memset (&binfo, 0, sizeof (binfo));
91 timecode = get_timecode_info (in.get(), &binfo, timecode_exists);
93 if (!timecode_exists) {
98 SndFileImportableSource::~SndFileImportableSource ()
103 SndFileImportableSource::read (Sample* buffer, samplecnt_t nframes)
105 samplecnt_t per_channel = nframes / sf_info.channels;
106 per_channel = sf_readf_float (in.get(), buffer, per_channel);
107 return per_channel * sf_info.channels;
111 SndFileImportableSource::channels () const
113 return sf_info.channels;
117 SndFileImportableSource::length () const
119 return (samplecnt_t) sf_info.frames;
123 SndFileImportableSource::samplerate () const
125 return sf_info.samplerate;
129 SndFileImportableSource::seek (samplepos_t /*pos*/)
131 sf_seek (in.get(), 0, SEEK_SET);
135 SndFileImportableSource::natural_position () const
137 return (samplepos_t) timecode;
141 SndFileImportableSource::clamped_at_unity () const
143 int const type = sf_info.format & SF_FORMAT_TYPEMASK;
144 int const sub = sf_info.format & SF_FORMAT_SUBMASK;
145 /* XXX: this may not be the full list of formats that are unclamped */
146 return (sub != SF_FORMAT_FLOAT && sub != SF_FORMAT_DOUBLE && type != SF_FORMAT_OGG);