2 Copyright (C) 2012 Carl Hetherington <cth@carlh.net>
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.
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.
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.
21 #include "external_audio_decoder.h"
23 #include "exceptions.h"
27 using std::stringstream;
30 using boost::shared_ptr;
31 using boost::optional;
33 ExternalAudioDecoder::ExternalAudioDecoder (shared_ptr<Film> f, shared_ptr<const Options> o, Job* j)
35 , AudioDecoder (f, o, j)
38 vector<SNDFILE*> sf = open_files (frames);
43 ExternalAudioDecoder::open_files (sf_count_t & frames)
45 vector<string> const files = _film->external_audio ();
48 for (size_t i = 0; i < files.size(); ++i) {
49 if (!files[i].empty()) {
55 return vector<SNDFILE*> ();
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);
67 SNDFILE* s = sf_open (files[i].c_str(), SFM_READ, &info);
69 throw DecodeError ("could not open external audio file for reading");
72 if (info.channels != 1) {
73 throw DecodeError ("external audio files must be mono");
76 sndfiles.push_back (s);
79 shared_ptr<ExternalAudioStream> st (
80 new ExternalAudioStream (
81 info.samplerate, av_get_default_channel_layout (N)
85 _audio_streams.push_back (st);
90 if (info.frames != frames) {
91 throw DecodeError ("external audio files have differing lengths");
101 ExternalAudioDecoder::pass ()
104 vector<SNDFILE*> sndfiles = open_files (frames);
105 if (sndfiles.empty()) {
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.
112 sf_count_t const block = _audio_stream->sample_rate() / 2;
114 shared_ptr<AudioBuffers> audio (new AudioBuffers (_audio_stream->channels(), block));
116 sf_count_t const this_time = min (block, frames);
117 for (size_t i = 0; i < sndfiles.size(); ++i) {
119 audio->make_silent (i);
121 sf_read_float (sndfiles[i], audio->data(i), block);
125 audio->set_frames (this_time);
130 close_files (sndfiles);
136 ExternalAudioDecoder::close_files (vector<SNDFILE*> const & sndfiles)
138 for (size_t i = 0; i < sndfiles.size(); ++i) {
139 sf_close (sndfiles[i]);
143 shared_ptr<ExternalAudioStream>
144 ExternalAudioStream::create ()
146 return shared_ptr<ExternalAudioStream> (new ExternalAudioStream);
149 shared_ptr<ExternalAudioStream>
150 ExternalAudioStream::create (string t, optional<int> v)
153 /* version < 1; no type in the string, and there's only FFmpeg streams anyway */
154 return shared_ptr<ExternalAudioStream> ();
160 if (type != "external") {
161 return shared_ptr<ExternalAudioStream> ();
164 return shared_ptr<ExternalAudioStream> (new ExternalAudioStream (t, v));
167 ExternalAudioStream::ExternalAudioStream (string t, optional<int> v)
173 s >> type >> _sample_rate >> _channel_layout;
176 ExternalAudioStream::ExternalAudioStream ()
182 ExternalAudioStream::to_string () const
184 return String::compose ("external %1 %2", _sample_rate, _channel_layout);