Merge master.
[dcpomatic.git] / src / lib / sndfile_decoder.cc
1 /*
2     Copyright (C) 2012 Carl Hetherington <cth@carlh.net>
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., 675 Mass Ave, Cambridge, MA 02139, USA.
17
18 */
19
20 #include <iostream>
21 #ifdef DCPOMATIC_WINDOWS
22 #include <windows.h>
23 #define ENABLE_SNDFILE_WINDOWS_PROTOTYPES 1
24 #endif
25 #include <sndfile.h>
26 #include "sndfile_content.h"
27 #include "sndfile_decoder.h"
28 #include "exceptions.h"
29 #include "audio_buffers.h"
30
31 #include "i18n.h"
32
33 using std::vector;
34 using std::string;
35 using std::min;
36 using std::cout;
37 using boost::shared_ptr;
38
39 SndfileDecoder::SndfileDecoder (shared_ptr<const SndfileContent> c)
40         : AudioDecoder (c)
41         , _sndfile_content (c)
42         , _deinterleave_buffer (0)
43 {
44         _info.format = 0;
45
46         /* Here be monsters.  See fopen_boost for similar shenanigans */
47 #ifdef DCPOMATIC_WINDOWS
48         _sndfile = sf_wchar_open (_sndfile_content->path(0).c_str(), SFM_READ, &_info);
49 #else   
50         _sndfile = sf_open (_sndfile_content->path(0).string().c_str(), SFM_READ, &_info);
51 #endif
52         
53         if (!_sndfile) {
54                 throw DecodeError (_("could not open audio file for reading"));
55         }
56
57         _done = 0;
58         _remaining = _info.frames;
59 }
60
61 SndfileDecoder::~SndfileDecoder ()
62 {
63         sf_close (_sndfile);
64         delete[] _deinterleave_buffer;
65 }
66
67 bool
68 SndfileDecoder::pass ()
69 {
70         if (_remaining == 0) {
71                 return true;
72         }
73         
74         /* Do things in half second blocks as I think there may be limits
75            to what FFmpeg (and in particular the resampler) can cope with.
76         */
77         sf_count_t const block = _sndfile_content->content_audio_frame_rate() / 2;
78         sf_count_t const this_time = min (block, _remaining);
79
80         int const channels = _sndfile_content->audio_channels ();
81         
82         shared_ptr<AudioBuffers> data (new AudioBuffers (channels, this_time));
83
84         if (_sndfile_content->audio_channels() == 1) {
85                 /* No de-interleaving required */
86                 sf_read_float (_sndfile, data->data(0), this_time);
87         } else {
88                 /* Deinterleave */
89                 if (!_deinterleave_buffer) {
90                         _deinterleave_buffer = new float[block * channels];
91                 }
92                 sf_readf_float (_sndfile, _deinterleave_buffer, this_time);
93                 vector<float*> out_ptr (channels);
94                 for (int i = 0; i < channels; ++i) {
95                         out_ptr[i] = data->data(i);
96                 }
97                 float* in_ptr = _deinterleave_buffer;
98                 for (int i = 0; i < this_time; ++i) {
99                         for (int j = 0; j < channels; ++j) {
100                                 *out_ptr[j]++ = *in_ptr++;
101                         }
102                 }
103         }
104                 
105         data->set_frames (this_time);
106         audio (data, ContentTime::from_frames (_done, audio_frame_rate ()));
107         _done += this_time;
108         _remaining -= this_time;
109
110         return _remaining == 0;
111 }
112
113 int
114 SndfileDecoder::audio_channels () const
115 {
116         return _info.channels;
117 }
118
119 ContentTime
120 SndfileDecoder::audio_length () const
121 {
122         return ContentTime::from_frames (_info.frames, audio_frame_rate ());
123 }
124
125 int
126 SndfileDecoder::audio_frame_rate () const
127 {
128         return _info.samplerate;
129 }
130
131 void
132 SndfileDecoder::seek (ContentTime t, bool accurate)
133 {
134         AudioDecoder::seek (t, accurate);
135
136         _done = t.frames (audio_frame_rate ());
137         _remaining = _info.frames - _done;
138 }