2 Copyright (C) 2000,2015 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
24 #include "pbd/error.h"
25 #include "ardour/sndfileimportable.h"
27 using namespace ARDOUR;
30 /* FIXME: this was copied from sndfilesource.cc, at some point these should be merged */
32 SndFileImportableSource::get_timecode_info (SNDFILE* sf, SF_BROADCAST_INFO* binfo, bool& exists)
34 if (sf_command (sf, SFC_GET_BROADCAST_INFO, binfo, sizeof (*binfo)) != SF_TRUE) {
39 /* see http://tracker.ardour.org/view.php?id=6208
40 * 0xffffffff 0xfffc5680
41 * seems to be a bug in Presonus Capture (which generated the file)
43 * still since framepos_t is a signed int, ignore files that could
44 * lead to negative timestamps for now.
47 if (binfo->time_reference_high & 0x80000000) {
49 snprintf(tmp, sizeof(tmp), "%x%08x", binfo->time_reference_high, binfo->time_reference_low);
50 PBD::warning << "Invalid Timestamp " << tmp << endmsg;
56 /* libsndfile reads eactly 4 bytes for high and low, but
57 * uses "unsigned int" which may or may not be 32 bit little
60 int64_t ret = (uint32_t) (binfo->time_reference_high & 0x7fffffff);
62 ret |= (uint32_t) (binfo->time_reference_low & 0xffffffff);
68 SndFileImportableSource::SndFileImportableSource (const string& path)
70 memset(&sf_info, 0 , sizeof(sf_info));
71 in.reset( sf_open(path.c_str(), SFM_READ, &sf_info), sf_close);
72 if (!in) throw failed_constructor();
74 SF_BROADCAST_INFO binfo;
77 memset (&binfo, 0, sizeof (binfo));
78 timecode = get_timecode_info (in.get(), &binfo, timecode_exists);
80 if (!timecode_exists) {
85 SndFileImportableSource::~SndFileImportableSource ()
90 SndFileImportableSource::read (Sample* buffer, framecnt_t nframes)
92 framecnt_t per_channel = nframes / sf_info.channels;
93 per_channel = sf_readf_float (in.get(), buffer, per_channel);
94 return per_channel * sf_info.channels;
98 SndFileImportableSource::channels () const
100 return sf_info.channels;
104 SndFileImportableSource::length () const
106 return (framecnt_t) sf_info.frames;
110 SndFileImportableSource::samplerate () const
112 return sf_info.samplerate;
116 SndFileImportableSource::seek (framepos_t /*pos*/)
118 sf_seek (in.get(), 0, SEEK_SET);
122 SndFileImportableSource::natural_position () const
124 return (framepos_t) timecode;
128 SndFileImportableSource::clamped_at_unity () const
130 int const type = sf_info.format & SF_FORMAT_TYPEMASK;
131 int const sub = sf_info.format & SF_FORMAT_SUBMASK;
132 /* XXX: this may not be the full list of formats that are unclamped */
133 return (sub != SF_FORMAT_FLOAT && sub != SF_FORMAT_DOUBLE && type != SF_FORMAT_OGG);