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