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