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