Move SRC (badly) to AudioDecoder.
[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 #include <sndfile.h>
22 #include "sndfile_content.h"
23 #include "sndfile_decoder.h"
24 #include "film.h"
25 #include "exceptions.h"
26
27 #include "i18n.h"
28
29 using std::vector;
30 using std::string;
31 using std::min;
32 using std::cout;
33 using boost::shared_ptr;
34
35 SndfileDecoder::SndfileDecoder (shared_ptr<const Film> f, shared_ptr<const SndfileContent> c)
36         : Decoder (f)
37         , AudioDecoder (f, c)
38         , _sndfile_content (c)
39         , _deinterleave_buffer (0)
40 {
41         _sndfile = sf_open (_sndfile_content->file().string().c_str(), SFM_READ, &_info);
42         if (!_sndfile) {
43                 throw DecodeError (_("could not open audio file for reading"));
44         }
45
46         _done = 0;
47         _remaining = _info.frames;
48 }
49
50 SndfileDecoder::~SndfileDecoder ()
51 {
52         sf_close (_sndfile);
53         delete[] _deinterleave_buffer;
54 }
55
56 bool
57 SndfileDecoder::pass ()
58 {
59         /* Do things in half second blocks as I think there may be limits
60            to what FFmpeg (and in particular the resampler) can cope with.
61         */
62         sf_count_t const block = _sndfile_content->audio_frame_rate() / 2;
63         sf_count_t const this_time = min (block, _remaining);
64
65         int const channels = _sndfile_content->audio_channels ();
66         
67         shared_ptr<AudioBuffers> audio (new AudioBuffers (channels, this_time));
68
69         if (_sndfile_content->audio_channels() == 1) {
70                 /* No de-interleaving required */
71                 sf_read_float (_sndfile, audio->data(0), this_time);
72         } else {
73                 /* Deinterleave */
74                 if (!_deinterleave_buffer) {
75                         _deinterleave_buffer = new float[block * channels];
76                 }
77                 sf_readf_float (_sndfile, _deinterleave_buffer, this_time);
78                 vector<float*> out_ptr (channels);
79                 for (int i = 0; i < channels; ++i) {
80                         out_ptr[i] = audio->data(i);
81                 }
82                 float* in_ptr = _deinterleave_buffer;
83                 for (int i = 0; i < this_time; ++i) {
84                         for (int j = 0; j < channels; ++j) {
85                                 *out_ptr[j]++ = *in_ptr++;
86                         }
87                 }
88         }
89                 
90         audio->set_frames (this_time);
91         Audio (audio, double(_done) / audio_frame_rate());
92         _done += this_time;
93         _remaining -= this_time;
94
95         return (_remaining == 0);
96 }
97
98 int
99 SndfileDecoder::audio_channels () const
100 {
101         return _info.channels;
102 }
103
104 ContentAudioFrame
105 SndfileDecoder::audio_length () const
106 {
107         return _info.frames;
108 }
109
110 int
111 SndfileDecoder::audio_frame_rate () const
112 {
113         return _info.samplerate;
114 }