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_content.h"
23 #include "sndfile_decoder.h"
25 #include "exceptions.h"
26 #include "audio_buffers.h"
34 using boost::shared_ptr;
36 SndfileDecoder::SndfileDecoder (shared_ptr<const Film> f, shared_ptr<const SndfileContent> c)
39 , _sndfile_content (c)
40 , _deinterleave_buffer (0)
43 _sndfile = sf_open (_sndfile_content->path(0).string().c_str(), SFM_READ, &_info);
45 throw DecodeError (_("could not open audio file for reading"));
48 _remaining = _info.frames;
51 SndfileDecoder::~SndfileDecoder ()
54 delete[] _deinterleave_buffer;
58 SndfileDecoder::pass ()
60 if (_remaining == 0) {
64 /* Do things in half second blocks as I think there may be limits
65 to what FFmpeg (and in particular the resampler) can cope with.
67 sf_count_t const block = _sndfile_content->content_audio_frame_rate() / 2;
68 sf_count_t const this_time = min (block, _remaining);
70 int const channels = _sndfile_content->audio_channels ();
72 shared_ptr<AudioBuffers> data (new AudioBuffers (channels, this_time));
74 if (_sndfile_content->audio_channels() == 1) {
75 /* No de-interleaving required */
76 sf_read_float (_sndfile, data->data(0), this_time);
79 if (!_deinterleave_buffer) {
80 _deinterleave_buffer = new float[block * channels];
82 sf_readf_float (_sndfile, _deinterleave_buffer, this_time);
83 vector<float*> out_ptr (channels);
84 for (int i = 0; i < channels; ++i) {
85 out_ptr[i] = data->data(i);
87 float* in_ptr = _deinterleave_buffer;
88 for (int i = 0; i < this_time; ++i) {
89 for (int j = 0; j < channels; ++j) {
90 *out_ptr[j]++ = *in_ptr++;
95 data->set_frames (this_time);
97 _remaining -= this_time;
99 return _remaining == 0;
103 SndfileDecoder::audio_channels () const
105 return _info.channels;
109 SndfileDecoder::audio_length () const
115 SndfileDecoder::audio_frame_rate () const
117 return _info.samplerate;
121 SndfileDecoder::seek (ContentTime t, bool accurate)
123 Decoder::seek (t, accurate);
124 AudioDecoder::seek (t, accurate);
126 _remaining = _info.frames - _done;