merge fix for tempo branch
[ardour.git] / libs / ardour / sndfileimportable.cc
1 /*
2     Copyright (C) 2000,2015 Paul Davis
3
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.
8
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.
13
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.
17
18 */
19
20 #include <sndfile.h>
21 #include <iostream>
22 #include <cstring>
23
24 #include "pbd/error.h"
25 #include "ardour/sndfileimportable.h"
26
27 using namespace ARDOUR;
28 using namespace std;
29
30 /* FIXME: this was copied from sndfilesource.cc, at some point these should be merged */
31 int64_t
32 SndFileImportableSource::get_timecode_info (SNDFILE* sf, SF_BROADCAST_INFO* binfo, bool& exists)
33 {
34         if (sf_command (sf, SFC_GET_BROADCAST_INFO, binfo, sizeof (*binfo)) != SF_TRUE) {
35                 exists = false;
36                 return 0;
37         }
38
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)
42          *
43          * still since framepos_t is a signed int, ignore files that could
44          * lead to negative timestamps for now.
45          */
46
47         if (binfo->time_reference_high & 0x80000000) {
48                 char tmp[64];
49                 snprintf(tmp, sizeof(tmp), "%x%08x", binfo->time_reference_high, binfo->time_reference_low);
50                 PBD::warning << "Invalid Timestamp " << tmp << endmsg;
51                 exists = false;
52                 return 0;
53         }
54
55         exists = true;
56         /* libsndfile reads eactly 4 bytes for high and low, but
57          * uses "unsigned int" which may or may not be 32 bit little
58          * endian.
59          */
60         int64_t ret = (uint32_t) (binfo->time_reference_high & 0x7fffffff);
61         ret <<= 32;
62         ret |= (uint32_t) (binfo->time_reference_low & 0xffffffff);
63
64         assert(ret >= 0);
65         return ret;
66 }
67
68 SndFileImportableSource::SndFileImportableSource (const string& path)
69 {
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();
73
74         SF_BROADCAST_INFO binfo;
75         bool timecode_exists;
76
77         memset (&binfo, 0, sizeof (binfo));
78         timecode = get_timecode_info (in.get(), &binfo, timecode_exists);
79
80         if (!timecode_exists) {
81                 timecode = 0;
82         }
83 }
84
85 SndFileImportableSource::~SndFileImportableSource ()
86 {
87 }
88
89 framecnt_t
90 SndFileImportableSource::read (Sample* buffer, framecnt_t nframes)
91 {
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;
95 }
96
97 uint32_t
98 SndFileImportableSource::channels () const
99 {
100         return sf_info.channels;
101 }
102
103 framecnt_t
104 SndFileImportableSource::length () const
105 {
106         return (framecnt_t) sf_info.frames;
107 }
108
109 framecnt_t
110 SndFileImportableSource::samplerate () const
111 {
112         return sf_info.samplerate;
113 }
114
115 void
116 SndFileImportableSource::seek (framepos_t /*pos*/)
117 {
118         sf_seek (in.get(), 0, SEEK_SET);
119 }
120
121 framepos_t
122 SndFileImportableSource::natural_position () const
123 {
124         return (framepos_t) timecode;
125 }
126
127 bool
128 SndFileImportableSource::clamped_at_unity () const
129 {
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);
134 }