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"
28 using boost::shared_ptr;
30 ExternalAudioDecoder::ExternalAudioDecoder (shared_ptr<Film> f, shared_ptr<const Options> o, Job* j)
32 , AudioDecoder (f, o, j)
38 ExternalAudioDecoder::pass ()
40 vector<string> const files = _film->external_audio ();
43 for (size_t i = 0; i < files.size(); ++i) {
44 if (!files[i].empty()) {
54 sf_count_t frames = 0;
56 vector<SNDFILE*> sndfiles;
57 for (vector<string>::const_iterator i = files.begin(); i != files.end(); ++i) {
59 sndfiles.push_back (0);
62 SNDFILE* s = sf_open (i->c_str(), SFM_READ, &info);
64 throw DecodeError ("could not open external audio file for reading");
67 if (info.channels != 1) {
68 throw DecodeError ("external audio files must be mono");
71 sndfiles.push_back (s);
74 /* XXX: nasty magic value */
75 AudioStream st ("DVDOMATIC-EXTERNAL", -1, info.samplerate, av_get_default_channel_layout (info.channels));
76 _audio_streams.push_back (st);
81 if (info.frames != frames) {
82 throw DecodeError ("external audio files have differing lengths");
88 sf_count_t const block = 65536;
90 shared_ptr<AudioBuffers> audio (new AudioBuffers (_audio_stream.get().channels(), block));
92 sf_count_t const this_time = min (block, frames);
93 for (size_t i = 0; i < sndfiles.size(); ++i) {
95 audio->make_silent (i);
97 sf_read_float (sndfiles[i], audio->data(i), block);