ardour
sndfileimportable.cc
Go to the documentation of this file.
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"
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 
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 
86 {
87 }
88 
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
99 {
100  return sf_info.channels;
101 }
102 
105 {
106  return (framecnt_t) sf_info.frames;
107 }
108 
111 {
112  return sf_info.samplerate;
113 }
114 
115 void
117 {
118  sf_seek (in.get(), 0, SEEK_SET);
119 }
120 
123 {
124  return (framepos_t) timecode;
125 }
126 
127 bool
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 }
SndFileImportableSource(const std::string &path)
Definition: Beats.hpp:239
LIBPBD_API Transmitter warning
std::ostream & endmsg(std::ostream &ostr)
Definition: transmitter.h:71
framecnt_t read(Sample *buffer, framecnt_t nframes)
int64_t framecnt_t
Definition: types.h:76
float Sample
Definition: types.h:54
Definition: amp.h:29
int64_t framepos_t
Definition: types.h:66
int64_t get_timecode_info(SNDFILE *, SF_BROADCAST_INFO *, bool &)