Merge branch 'master' into windows
[ardour.git] / libs / ardour / sndfileimportable.cc
1 #include "ardour/sndfileimportable.h"
2 #include <sndfile.h>
3 #include <iostream>
4 #include <cstring>
5
6 using namespace ARDOUR;
7 using namespace std;
8
9 /* FIXME: this was copied from sndfilesource.cc, at some point these should be merged */
10 int64_t
11 SndFileImportableSource::get_timecode_info (SNDFILE* sf, SF_BROADCAST_INFO* binfo, bool& exists)
12 {
13         if (sf_command (sf, SFC_GET_BROADCAST_INFO, binfo, sizeof (*binfo)) != SF_TRUE) {
14                 exists = false;
15                 return 0;
16         }
17
18         exists = true;
19         int64_t ret = (uint32_t) binfo->time_reference_high;
20         ret <<= 32;
21         ret |= (uint32_t) binfo->time_reference_low;
22         return ret;
23 }
24
25 SndFileImportableSource::SndFileImportableSource (const string& path)
26 {
27         memset(&sf_info, 0 , sizeof(sf_info));
28         in.reset( sf_open(path.c_str(), SFM_READ, &sf_info), sf_close);
29         if (!in) throw failed_constructor();
30
31         SF_BROADCAST_INFO binfo;
32         bool timecode_exists;
33
34         memset (&binfo, 0, sizeof (binfo));
35         timecode = get_timecode_info (in.get(), &binfo, timecode_exists);
36
37         if (!timecode_exists) {
38                 timecode = 0;
39         }
40 }
41
42 SndFileImportableSource::~SndFileImportableSource ()
43 {
44 }
45
46 framecnt_t
47 SndFileImportableSource::read (Sample* buffer, framecnt_t nframes)
48 {
49         framecnt_t per_channel = nframes / sf_info.channels;
50         per_channel = sf_readf_float (in.get(), buffer, per_channel);
51         return per_channel * sf_info.channels;
52 }
53
54 uint32_t
55 SndFileImportableSource::channels () const
56 {
57         return sf_info.channels;
58 }
59
60 framecnt_t
61 SndFileImportableSource::length () const
62 {
63         return (framecnt_t) sf_info.frames;
64 }
65
66 framecnt_t
67 SndFileImportableSource::samplerate () const
68 {
69         return sf_info.samplerate;
70 }
71
72 void
73 SndFileImportableSource::seek (framepos_t /*pos*/)
74 {
75         sf_seek (in.get(), 0, SEEK_SET);
76 }
77
78 framepos_t
79 SndFileImportableSource::natural_position () const
80 {
81         return (framepos_t) timecode;
82 }
83
84 bool
85 SndFileImportableSource::clamped_at_unity () const
86 {
87         int const type = sf_info.format & SF_FORMAT_TYPEMASK;
88         int const sub = sf_info.format & SF_FORMAT_SUBMASK;
89         /* XXX: this may not be the full list of formats that are unclamped */
90         return (sub != SF_FORMAT_FLOAT && sub != SF_FORMAT_DOUBLE && type != SF_FORMAT_OGG);
91 }