Only show user-presets in favorite sidebar
[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 #include <fcntl.h>
24
25 #include <glib.h>
26 #include "pbd/gstdio_compat.h"
27
28
29 #include "pbd/error.h"
30 #include "ardour/sndfileimportable.h"
31
32 #include <glibmm/convert.h>
33
34 using namespace ARDOUR;
35 using namespace std;
36
37 /* FIXME: this was copied from sndfilesource.cc, at some point these should be merged */
38 int64_t
39 SndFileImportableSource::get_timecode_info (SNDFILE* sf, SF_BROADCAST_INFO* binfo, bool& exists)
40 {
41         if (sf_command (sf, SFC_GET_BROADCAST_INFO, binfo, sizeof (*binfo)) != SF_TRUE) {
42                 exists = false;
43                 return 0;
44         }
45
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)
49          *
50          * still since samplepos_t is a signed int, ignore files that could
51          * lead to negative timestamps for now.
52          */
53
54         if (binfo->time_reference_high & 0x80000000) {
55                 char tmp[64];
56                 snprintf(tmp, sizeof(tmp), "%x%08x", binfo->time_reference_high, binfo->time_reference_low);
57                 PBD::warning << "Invalid Timestamp " << tmp << endmsg;
58                 exists = false;
59                 return 0;
60         }
61
62         exists = true;
63         /* libsndfile reads eactly 4 bytes for high and low, but
64          * uses "unsigned int" which may or may not be 32 bit little
65          * endian.
66          */
67         int64_t ret = (uint32_t) (binfo->time_reference_high & 0x7fffffff);
68         ret <<= 32;
69         ret |= (uint32_t) (binfo->time_reference_low & 0xffffffff);
70
71         assert(ret >= 0);
72         return ret;
73 }
74
75 SndFileImportableSource::SndFileImportableSource (const string& path)
76 {
77         int fd = g_open (path.c_str (), O_RDONLY, 0444);
78         if (fd == -1) {
79                 throw failed_constructor ();
80         }
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();
84
85         SF_BROADCAST_INFO binfo;
86         bool timecode_exists;
87
88         memset (&binfo, 0, sizeof (binfo));
89         timecode = get_timecode_info (in.get(), &binfo, timecode_exists);
90
91         if (!timecode_exists) {
92                 timecode = 0;
93         }
94 }
95
96 SndFileImportableSource::~SndFileImportableSource ()
97 {
98 }
99
100 samplecnt_t
101 SndFileImportableSource::read (Sample* buffer, samplecnt_t nframes)
102 {
103         samplecnt_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;
106 }
107
108 uint32_t
109 SndFileImportableSource::channels () const
110 {
111         return sf_info.channels;
112 }
113
114 samplecnt_t
115 SndFileImportableSource::length () const
116 {
117         return (samplecnt_t) sf_info.frames;
118 }
119
120 samplecnt_t
121 SndFileImportableSource::samplerate () const
122 {
123         return sf_info.samplerate;
124 }
125
126 void
127 SndFileImportableSource::seek (samplepos_t /*pos*/)
128 {
129         sf_seek (in.get(), 0, SEEK_SET);
130 }
131
132 samplepos_t
133 SndFileImportableSource::natural_position () const
134 {
135         return (samplepos_t) timecode;
136 }
137
138 bool
139 SndFileImportableSource::clamped_at_unity () const
140 {
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);
145 }