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.
22 #include "external_audio_decoder.h"
24 #include "exceptions.h"
28 using std::stringstream;
31 using boost::shared_ptr;
32 using boost::optional;
34 ExternalAudioDecoder::ExternalAudioDecoder (shared_ptr<Film> f, shared_ptr<const Options> o, Job* j)
36 , AudioDecoder (f, o, j)
39 vector<SNDFILE*> sf = open_files (frames);
44 ExternalAudioDecoder::open_files (sf_count_t & frames)
46 vector<string> const files = _film->external_audio ();
49 for (size_t i = 0; i < files.size(); ++i) {
50 if (!files[i].empty()) {
56 return vector<SNDFILE*> ();
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);
68 SNDFILE* s = sf_open (files[i].c_str(), SFM_READ, &info);
70 throw DecodeError ("could not open external audio file for reading");
73 if (info.channels != 1) {
74 throw DecodeError ("external audio files must be mono");
77 sndfiles.push_back (s);
80 shared_ptr<ExternalAudioStream> st (
81 new ExternalAudioStream (
82 info.samplerate, av_get_default_channel_layout (N)
86 _audio_streams.push_back (st);
91 if (info.frames != frames) {
92 throw DecodeError ("external audio files have differing lengths");
102 ExternalAudioDecoder::pass ()
105 vector<SNDFILE*> sndfiles = open_files (frames);
106 if (sndfiles.empty()) {
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.
113 sf_count_t const block = _audio_stream->sample_rate() / 2;
115 shared_ptr<AudioBuffers> audio (new AudioBuffers (_audio_stream->channels(), block));
117 sf_count_t const this_time = min (block, frames);
118 for (size_t i = 0; i < sndfiles.size(); ++i) {
120 audio->make_silent (i);
122 sf_read_float (sndfiles[i], audio->data(i), block);
126 audio->set_frames (this_time);
131 close_files (sndfiles);
137 ExternalAudioDecoder::close_files (vector<SNDFILE*> const & sndfiles)
139 for (size_t i = 0; i < sndfiles.size(); ++i) {
140 sf_close (sndfiles[i]);
144 shared_ptr<ExternalAudioStream>
145 ExternalAudioStream::create ()
147 return shared_ptr<ExternalAudioStream> (new ExternalAudioStream);
150 shared_ptr<ExternalAudioStream>
151 ExternalAudioStream::create (string t, optional<int> v)
154 /* version < 1; no type in the string, and there's only FFmpeg streams anyway */
155 return shared_ptr<ExternalAudioStream> ();
161 if (type != "external") {
162 return shared_ptr<ExternalAudioStream> ();
165 return shared_ptr<ExternalAudioStream> (new ExternalAudioStream (t, v));
168 ExternalAudioStream::ExternalAudioStream (string t, optional<int> v)
174 s >> type >> _sample_rate >> _channel_layout;
177 ExternalAudioStream::ExternalAudioStream ()
183 ExternalAudioStream::to_string () const
185 return String::compose ("external %1 %2", _sample_rate, _channel_layout);