Bump version
[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_decoder.h"
23 #include "film.h"
24 #include "exceptions.h"
25
26 #include "i18n.h"
27
28 using std::vector;
29 using std::string;
30 using std::stringstream;
31 using std::min;
32 using std::cout;
33 using boost::shared_ptr;
34 using boost::optional;
35
36 SndfileDecoder::SndfileDecoder (shared_ptr<Film> f, DecodeOptions o)
37         : Decoder (f, o)
38         , AudioDecoder (f, o)
39 {
40         sf_count_t frames;
41         vector<SNDFILE*> sf = open_files (frames);
42         close_files (sf);
43 }
44
45 vector<SNDFILE*>
46 SndfileDecoder::open_files (sf_count_t & frames)
47 {
48         vector<string> const files = _film->external_audio ();
49
50         int N = 0;
51         for (size_t i = 0; i < files.size(); ++i) {
52                 if (!files[i].empty()) {
53                         N = i + 1;
54                 }
55         }
56
57         if (N == 0) {
58                 return vector<SNDFILE*> ();
59         }
60
61         bool first = true;
62         frames = 0;
63         
64         vector<SNDFILE*> sndfiles;
65         for (size_t i = 0; i < (size_t) N; ++i) {
66                 if (files[i].empty ()) {
67                         sndfiles.push_back (0);
68                 } else {
69                         SF_INFO info;
70                         SNDFILE* s = sf_open (files[i].c_str(), SFM_READ, &info);
71                         if (!s) {
72                                 throw DecodeError (_("could not open external audio file for reading"));
73                         }
74
75                         if (info.channels != 1) {
76                                 throw DecodeError (_("external audio files must be mono"));
77                         }
78                         
79                         sndfiles.push_back (s);
80
81                         if (first) {
82                                 shared_ptr<SndfileStream> st (
83                                         new SndfileStream (
84                                                 info.samplerate, av_get_default_channel_layout (N)
85                                                 )
86                                         );
87                                 
88                                 _audio_streams.push_back (st);
89                                 _audio_stream = st;
90                                 frames = info.frames;
91                                 first = false;
92                         } else {
93                                 if (info.frames != frames) {
94                                         throw DecodeError (_("external audio files have differing lengths"));
95                                 }
96                         }
97                 }
98         }
99
100         return sndfiles;
101 }
102
103 bool
104 SndfileDecoder::pass ()
105 {
106         sf_count_t frames;
107         vector<SNDFILE*> sndfiles = open_files (frames);
108         if (sndfiles.empty()) {
109                 return true;
110         }
111
112         /* Do things in half second blocks as I think there may be limits
113            to what FFmpeg (and in particular the resampler) can cope with.
114         */
115         sf_count_t const block = _audio_stream->sample_rate() / 2;
116         shared_ptr<AudioBuffers> audio (new AudioBuffers (_audio_stream->channels(), block));
117         sf_count_t done = 0;
118         while (frames > 0) {
119                 sf_count_t const this_time = min (block, frames);
120                 for (size_t i = 0; i < sndfiles.size(); ++i) {
121                         if (!sndfiles[i]) {
122                                 audio->make_silent (i);
123                         } else {
124                                 sf_read_float (sndfiles[i], audio->data(i), block);
125                         }
126                 }
127
128                 audio->set_frames (this_time);
129                 Audio (audio, double(done) / _audio_stream->sample_rate());
130                 done += this_time;
131                 frames -= this_time;
132         }
133
134         close_files (sndfiles);
135
136         return true;
137 }
138
139 void
140 SndfileDecoder::close_files (vector<SNDFILE*> const & sndfiles)
141 {
142         for (size_t i = 0; i < sndfiles.size(); ++i) {
143                 sf_close (sndfiles[i]);
144         }
145 }
146
147 shared_ptr<SndfileStream>
148 SndfileStream::create ()
149 {
150         return shared_ptr<SndfileStream> (new SndfileStream);
151 }
152
153 shared_ptr<SndfileStream>
154 SndfileStream::create (string t, optional<int> v)
155 {
156         if (!v) {
157                 /* version < 1; no type in the string, and there's only FFmpeg streams anyway */
158                 return shared_ptr<SndfileStream> ();
159         }
160
161         stringstream s (t);
162         string type;
163         s >> type;
164         if (type != N_("external")) {
165                 return shared_ptr<SndfileStream> ();
166         }
167
168         return shared_ptr<SndfileStream> (new SndfileStream (t, v));
169 }
170
171 SndfileStream::SndfileStream (string t, optional<int> v)
172 {
173         assert (v);
174
175         stringstream s (t);
176         string type;
177         s >> type >> _sample_rate >> _channel_layout;
178 }
179
180 SndfileStream::SndfileStream ()
181 {
182
183 }
184
185 string
186 SndfileStream::to_string () const
187 {
188         return String::compose (N_("external %1 %2"), _sample_rate, _channel_layout);
189 }