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 "sndfile_decoder.h"
24 #include "exceptions.h"
30 using std::stringstream;
33 using boost::shared_ptr;
34 using boost::optional;
36 SndfileDecoder::SndfileDecoder (shared_ptr<Film> f, DecodeOptions o)
45 vector<string> const files = _film->external_audio ();
48 for (size_t i = 0; i < files.size(); ++i) {
49 if (!files[i].empty()) {
60 for (size_t i = 0; i < (size_t) N; ++i) {
61 if (files[i].empty ()) {
62 _sndfiles.push_back (0);
65 SNDFILE* s = sf_open (files[i].c_str(), SFM_READ, &info);
67 throw DecodeError (_("could not open external audio file for reading"));
70 if (info.channels != 1) {
71 throw DecodeError (_("external audio files must be mono"));
74 _sndfiles.push_back (s);
77 shared_ptr<SndfileStream> st (
79 info.samplerate, av_get_default_channel_layout (N)
83 _audio_streams.push_back (st);
85 _frames = info.frames;
88 if (info.frames != _frames) {
89 throw DecodeError (_("external audio files have differing lengths"));
97 SndfileDecoder::pass ()
99 /* Do things in half second blocks as I think there may be limits
100 to what FFmpeg (and in particular the resampler) can cope with.
102 sf_count_t const block = _audio_stream->sample_rate() / 2;
103 shared_ptr<AudioBuffers> audio (new AudioBuffers (_audio_stream->channels(), block));
104 sf_count_t const this_time = min (block, _frames - _done);
105 for (size_t i = 0; i < _sndfiles.size(); ++i) {
107 audio->make_silent (i);
109 sf_read_float (_sndfiles[i], audio->data(i), this_time);
113 audio->set_frames (this_time);
114 Audio (audio, double(_done) / _audio_stream->sample_rate());
117 return (_done == _frames);
120 SndfileDecoder::~SndfileDecoder ()
122 for (size_t i = 0; i < _sndfiles.size(); ++i) {
124 sf_close (_sndfiles[i]);
129 shared_ptr<SndfileStream>
130 SndfileStream::create ()
132 return shared_ptr<SndfileStream> (new SndfileStream);
135 shared_ptr<SndfileStream>
136 SndfileStream::create (string t, optional<int> v)
139 /* version < 1; no type in the string, and there's only FFmpeg streams anyway */
140 return shared_ptr<SndfileStream> ();
146 if (type != N_("external")) {
147 return shared_ptr<SndfileStream> ();
150 return shared_ptr<SndfileStream> (new SndfileStream (t, v));
153 SndfileStream::SndfileStream (string t, optional<int> v)
159 s >> type >> _sample_rate >> _channel_layout;
162 SndfileStream::SndfileStream ()
168 SndfileStream::to_string () const
170 return String::compose (N_("external %1 %2"), _sample_rate, _channel_layout);