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.
26 #include "pbd/gstdio_compat.h"
29 #include "pbd/error.h"
30 #include "ardour/sndfileimportable.h"
32 #include <glibmm/convert.h>
34 using namespace ARDOUR;
37 /* FIXME: this was copied from sndfilesource.cc, at some point these should be merged */
39 SndFileImportableSource::get_timecode_info (SNDFILE* sf, SF_BROADCAST_INFO* binfo, bool& exists)
41 if (sf_command (sf, SFC_GET_BROADCAST_INFO, binfo, sizeof (*binfo)) != SF_TRUE) {
46 /* see http://tracker.ardour.org/view.php?id=6208
47 * 0xffffffff 0xfffc5680
48 * seems to be a bug in Presonus Capture (which generated the file)
50 * still since framepos_t is a signed int, ignore files that could
51 * lead to negative timestamps for now.
54 if (binfo->time_reference_high & 0x80000000) {
56 snprintf(tmp, sizeof(tmp), "%x%08x", binfo->time_reference_high, binfo->time_reference_low);
57 PBD::warning << "Invalid Timestamp " << tmp << endmsg;
63 /* libsndfile reads eactly 4 bytes for high and low, but
64 * uses "unsigned int" which may or may not be 32 bit little
67 int64_t ret = (uint32_t) (binfo->time_reference_high & 0x7fffffff);
69 ret |= (uint32_t) (binfo->time_reference_low & 0xffffffff);
75 SndFileImportableSource::SndFileImportableSource (const string& path)
77 int fd = g_open (path.c_str (), O_RDONLY, 0444);
79 throw failed_constructor ();
81 memset(&sf_info, 0 , sizeof(sf_info));
82 in.reset (sf_open_fd (fd, SFM_READ, &sf_info, true), sf_close);
83 if (!in) throw failed_constructor();
85 SF_BROADCAST_INFO binfo;
88 memset (&binfo, 0, sizeof (binfo));
89 timecode = get_timecode_info (in.get(), &binfo, timecode_exists);
91 if (!timecode_exists) {
96 SndFileImportableSource::~SndFileImportableSource ()
101 SndFileImportableSource::read (Sample* buffer, framecnt_t nframes)
103 framecnt_t per_channel = nframes / sf_info.channels;
104 per_channel = sf_readf_float (in.get(), buffer, per_channel);
105 return per_channel * sf_info.channels;
109 SndFileImportableSource::channels () const
111 return sf_info.channels;
115 SndFileImportableSource::length () const
117 return (framecnt_t) sf_info.frames;
121 SndFileImportableSource::samplerate () const
123 return sf_info.samplerate;
127 SndFileImportableSource::seek (framepos_t /*pos*/)
129 sf_seek (in.get(), 0, SEEK_SET);
133 SndFileImportableSource::natural_position () const
135 return (framepos_t) timecode;
139 SndFileImportableSource::clamped_at_unity () const
141 int const type = sf_info.format & SF_FORMAT_TYPEMASK;
142 int const sub = sf_info.format & SF_FORMAT_SUBMASK;
143 /* XXX: this may not be the full list of formats that are unclamped */
144 return (sub != SF_FORMAT_FLOAT && sub != SF_FORMAT_DOUBLE && type != SF_FORMAT_OGG);