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 #ifdef DCPOMATIC_WINDOWS
23 #define ENABLE_SNDFILE_WINDOWS_PROTOTYPES 1
26 #include "sndfile_content.h"
27 #include "sndfile_decoder.h"
28 #include "exceptions.h"
29 #include "audio_buffers.h"
37 using boost::shared_ptr;
39 SndfileDecoder::SndfileDecoder (shared_ptr<const SndfileContent> c)
41 , _sndfile_content (c)
42 , _deinterleave_buffer (0)
46 /* Here be monsters. See fopen_boost for similar shenanigans */
47 #ifdef DCPOMATIC_WINDOWS
48 _sndfile = sf_wchar_open (_sndfile_content->path(0).c_str(), SFM_READ, &_info);
50 _sndfile = sf_open (_sndfile_content->path(0).string().c_str(), SFM_READ, &_info);
54 throw DecodeError (_("could not open audio file for reading"));
58 _remaining = _info.frames;
61 SndfileDecoder::~SndfileDecoder ()
64 delete[] _deinterleave_buffer;
68 SndfileDecoder::pass ()
70 if (_remaining == 0) {
74 /* Do things in half second blocks as I think there may be limits
75 to what FFmpeg (and in particular the resampler) can cope with.
77 sf_count_t const block = _sndfile_content->audio_frame_rate() / 2;
78 sf_count_t const this_time = min (block, _remaining);
80 int const channels = _sndfile_content->audio_channels ();
82 shared_ptr<AudioBuffers> data (new AudioBuffers (channels, this_time));
84 if (_sndfile_content->audio_channels() == 1) {
85 /* No de-interleaving required */
86 sf_read_float (_sndfile, data->data(0), this_time);
89 if (!_deinterleave_buffer) {
90 _deinterleave_buffer = new float[block * channels];
92 sf_readf_float (_sndfile, _deinterleave_buffer, this_time);
93 vector<float*> out_ptr (channels);
94 for (int i = 0; i < channels; ++i) {
95 out_ptr[i] = data->data(i);
97 float* in_ptr = _deinterleave_buffer;
98 for (int i = 0; i < this_time; ++i) {
99 for (int j = 0; j < channels; ++j) {
100 *out_ptr[j]++ = *in_ptr++;
105 data->set_frames (this_time);
106 audio (data, ContentTime::from_frames (_done, audio_frame_rate ()));
108 _remaining -= this_time;
110 return _remaining == 0;
114 SndfileDecoder::audio_channels () const
116 return _info.channels;
120 SndfileDecoder::audio_length () const
122 return ContentTime::from_frames (_info.frames, audio_frame_rate ());
126 SndfileDecoder::audio_frame_rate () const
128 return _info.samplerate;
132 SndfileDecoder::seek (ContentTime t, bool accurate)
134 AudioDecoder::seek (t, accurate);
136 _done = t.frames (audio_frame_rate ());
137 _remaining = _info.frames - _done;