2 Copyright (C) 2013-2015 Carl Hetherington <cth@carlh.net>
4 This file is part of DCP-o-matic.
6 DCP-o-matic is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
11 DCP-o-matic is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with DCP-o-matic. If not, see <http://www.gnu.org/licenses/>.
21 #include "resampler.h"
22 #include "audio_buffers.h"
23 #include "exceptions.h"
24 #include "compose.hpp"
25 #include "dcpomatic_assert.h"
26 #include <samplerate.h>
35 using std::runtime_error;
36 using boost::shared_ptr;
38 /** @param in Input sampling rate (Hz)
39 * @param out Output sampling rate (Hz)
40 * @param channels Number of channels.
42 Resampler::Resampler (int in, int out, int channels)
45 , _channels (channels)
49 _src = src_new (SRC_SINC_BEST_QUALITY, _channels, &error);
51 throw runtime_error (String::compose (N_("could not create sample-rate converter (%1)"), error));
55 Resampler::~Resampler ()
63 Resampler::set_fast (bool fast)
75 _src = src_new (_fast ? SRC_LINEAR : SRC_SINC_BEST_QUALITY, _channels, &error);
77 throw runtime_error (String::compose (N_("could not create sample-rate converter (%1)"), error));
81 shared_ptr<const AudioBuffers>
82 Resampler::run (shared_ptr<const AudioBuffers> in)
84 int in_frames = in->frames ();
87 shared_ptr<AudioBuffers> resampled (new AudioBuffers (_channels, 0));
89 while (in_frames > 0) {
91 /* Compute the resampled frames count and add 32 for luck */
92 int const max_resampled_frames = ceil ((double) in_frames * _out_rate / _in_rate) + 32;
95 float* in_buffer = new float[in_frames * _channels];
98 float** p = in->data ();
100 for (int i = 0; i < in_frames; ++i) {
101 for (int j = 0; j < _channels; ++j) {
102 *q++ = p[j][in_offset + i];
107 data.data_in = in_buffer;
108 data.input_frames = in_frames;
110 data.data_out = new float[max_resampled_frames * _channels];
111 data.output_frames = max_resampled_frames;
113 data.end_of_input = 0;
114 data.src_ratio = double (_out_rate) / _in_rate;
116 int const r = src_process (_src, &data);
118 delete[] data.data_in;
119 delete[] data.data_out;
122 N_("could not run sample-rate converter (%1) [processing %2 to %3, %4 channels]"),
125 max_resampled_frames,
131 if (data.output_frames_gen == 0) {
132 delete[] data.data_in;
133 delete[] data.data_out;
137 resampled->ensure_size (out_offset + data.output_frames_gen);
138 resampled->set_frames (out_offset + data.output_frames_gen);
141 float* p = data.data_out;
142 float** q = resampled->data ();
143 for (int i = 0; i < data.output_frames_gen; ++i) {
144 for (int j = 0; j < _channels; ++j) {
145 q[j][out_offset + i] = *p++;
150 in_frames -= data.input_frames_used;
151 in_offset += data.input_frames_used;
152 out_offset += data.output_frames_gen;
154 delete[] data.data_in;
155 delete[] data.data_out;
161 shared_ptr<const AudioBuffers>
164 shared_ptr<AudioBuffers> out (new AudioBuffers (_channels, 0));
166 int64_t const output_size = 65536;
169 float* buffer = new float[output_size];
172 data.data_in = dummy;
173 data.input_frames = 0;
174 data.data_out = buffer;
175 data.output_frames = output_size;
176 data.end_of_input = 1;
177 data.src_ratio = double (_out_rate) / _in_rate;
179 int const r = src_process (_src, &data);
182 throw EncodeError (String::compose (N_("could not run sample-rate converter (%1)"), src_strerror (r)));
185 out->ensure_size (out_offset + data.output_frames_gen);
187 float* p = data.data_out;
188 float** q = out->data ();
189 for (int i = 0; i < data.output_frames_gen; ++i) {
190 for (int j = 0; j < _channels; ++j) {
191 q[j][out_offset + i] = *p++;
195 out_offset += data.output_frames_gen;
196 out->set_frames (out_offset);