Logging improvements to allow prettier displays in the server GUI.
[dcpomatic.git] / src / lib / sndfile_decoder.cc
1 /*
2     Copyright (C) 2012-2015 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 "exceptions.h"
25 #include "audio_buffers.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 SndfileContent> c, bool fast)
36         : Sndfile (c)
37         , AudioDecoder (c, fast)
38         , _done (0)
39         , _remaining (_info.frames)
40         , _deinterleave_buffer (0)
41 {
42
43 }
44
45 SndfileDecoder::~SndfileDecoder ()
46 {
47         delete[] _deinterleave_buffer;
48 }
49
50 bool
51 SndfileDecoder::pass ()
52 {
53         if (_remaining == 0) {
54                 return true;
55         }
56
57         /* Do things in half second blocks as I think there may be limits
58            to what FFmpeg (and in particular the resampler) can cope with.
59         */
60         sf_count_t const block = _sndfile_content->audio_stream()->frame_rate() / 2;
61         sf_count_t const this_time = min (block, _remaining);
62
63         int const channels = _sndfile_content->audio_stream()->channels ();
64
65         shared_ptr<AudioBuffers> data (new AudioBuffers (channels, this_time));
66
67         if (_sndfile_content->audio_stream()->channels() == 1) {
68                 /* No de-interleaving required */
69                 sf_read_float (_sndfile, data->data(0), this_time);
70         } else {
71                 /* Deinterleave */
72                 if (!_deinterleave_buffer) {
73                         _deinterleave_buffer = new float[block * channels];
74                 }
75                 sf_readf_float (_sndfile, _deinterleave_buffer, this_time);
76                 vector<float*> out_ptr (channels);
77                 for (int i = 0; i < channels; ++i) {
78                         out_ptr[i] = data->data(i);
79                 }
80                 float* in_ptr = _deinterleave_buffer;
81                 for (int i = 0; i < this_time; ++i) {
82                         for (int j = 0; j < channels; ++j) {
83                                 *out_ptr[j]++ = *in_ptr++;
84                         }
85                 }
86         }
87
88         data->set_frames (this_time);
89         audio (_sndfile_content->audio_stream (), data, ContentTime::from_frames (_done, _info.samplerate));
90         _done += this_time;
91         _remaining -= this_time;
92
93         return _remaining == 0;
94 }
95
96 void
97 SndfileDecoder::seek (ContentTime t, bool accurate)
98 {
99         AudioDecoder::seek (t, accurate);
100
101         _done = t.frames_round (_info.samplerate);
102         _remaining = _info.frames - _done;
103 }