fix a bad transition in the transportFSM.
[ardour.git] / libs / ardour / sndfileimportable.cc
1 /*
2  * Copyright (C) 2008-2017 Paul Davis <paul@linuxaudiosystems.com>
3  * Copyright (C) 2009-2011 Carl Hetherington <carl@carlh.net>
4  * Copyright (C) 2009 David Robillard <d@drobilla.net>
5  * Copyright (C) 2015 Robin Gareus <robin@gareus.org>
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 2 of the License, or
10  * (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License along
18  * with this program; if not, write to the Free Software Foundation, Inc.,
19  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20  */
21
22 #include <sndfile.h>
23 #include <iostream>
24 #include <cstring>
25 #include <fcntl.h>
26
27 #include <glib.h>
28 #include "pbd/gstdio_compat.h"
29
30
31 #include "pbd/error.h"
32 #include "ardour/sndfileimportable.h"
33
34 #include <glibmm/convert.h>
35
36 using namespace ARDOUR;
37 using namespace std;
38
39 /* FIXME: this was copied from sndfilesource.cc, at some point these should be merged */
40 int64_t
41 SndFileImportableSource::get_timecode_info (SNDFILE* sf, SF_BROADCAST_INFO* binfo, bool& exists)
42 {
43         if (sf_command (sf, SFC_GET_BROADCAST_INFO, binfo, sizeof (*binfo)) != SF_TRUE) {
44                 exists = false;
45                 return 0;
46         }
47
48         /* see http://tracker.ardour.org/view.php?id=6208
49          * 0xffffffff 0xfffc5680
50          * seems to be a bug in Presonus Capture (which generated the file)
51          *
52          * still since samplepos_t is a signed int, ignore files that could
53          * lead to negative timestamps for now.
54          */
55
56         if (binfo->time_reference_high & 0x80000000) {
57                 char tmp[64];
58                 snprintf(tmp, sizeof(tmp), "%x%08x", binfo->time_reference_high, binfo->time_reference_low);
59                 PBD::warning << "Invalid Timestamp " << tmp << endmsg;
60                 exists = false;
61                 return 0;
62         }
63
64         exists = true;
65         /* libsndfile reads eactly 4 bytes for high and low, but
66          * uses "unsigned int" which may or may not be 32 bit little
67          * endian.
68          */
69         int64_t ret = (uint32_t) (binfo->time_reference_high & 0x7fffffff);
70         ret <<= 32;
71         ret |= (uint32_t) (binfo->time_reference_low & 0xffffffff);
72
73         assert(ret >= 0);
74         return ret;
75 }
76
77 SndFileImportableSource::SndFileImportableSource (const string& path)
78 {
79         int fd = g_open (path.c_str (), O_RDONLY, 0444);
80         if (fd == -1) {
81                 throw failed_constructor ();
82         }
83         memset(&sf_info, 0 , sizeof(sf_info));
84         in.reset (sf_open_fd (fd, SFM_READ, &sf_info, true), sf_close);
85         if (!in) throw failed_constructor();
86
87         SF_BROADCAST_INFO binfo;
88         bool timecode_exists;
89
90         memset (&binfo, 0, sizeof (binfo));
91         timecode = get_timecode_info (in.get(), &binfo, timecode_exists);
92
93         if (!timecode_exists) {
94                 timecode = 0;
95         }
96 }
97
98 SndFileImportableSource::~SndFileImportableSource ()
99 {
100 }
101
102 samplecnt_t
103 SndFileImportableSource::read (Sample* buffer, samplecnt_t nframes)
104 {
105         samplecnt_t per_channel = nframes / sf_info.channels;
106         per_channel = sf_readf_float (in.get(), buffer, per_channel);
107         return per_channel * sf_info.channels;
108 }
109
110 uint32_t
111 SndFileImportableSource::channels () const
112 {
113         return sf_info.channels;
114 }
115
116 samplecnt_t
117 SndFileImportableSource::length () const
118 {
119         return (samplecnt_t) sf_info.frames;
120 }
121
122 samplecnt_t
123 SndFileImportableSource::samplerate () const
124 {
125         return sf_info.samplerate;
126 }
127
128 void
129 SndFileImportableSource::seek (samplepos_t /*pos*/)
130 {
131         sf_seek (in.get(), 0, SEEK_SET);
132 }
133
134 samplepos_t
135 SndFileImportableSource::natural_position () const
136 {
137         return (samplepos_t) timecode;
138 }
139
140 bool
141 SndFileImportableSource::clamped_at_unity () const
142 {
143         int const type = sf_info.format & SF_FORMAT_TYPEMASK;
144         int const sub = sf_info.format & SF_FORMAT_SUBMASK;
145         /* XXX: this may not be the full list of formats that are unclamped */
146         return (sub != SF_FORMAT_FLOAT && sub != SF_FORMAT_DOUBLE && type != SF_FORMAT_OGG);
147 }