2 Copyright (C) 2013-2021 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/>.
22 #include "resampler.h"
23 #include "audio_buffers.h"
24 #include "exceptions.h"
25 #include "compose.hpp"
26 #include "dcpomatic_assert.h"
27 #include <samplerate.h>
36 using std::make_shared;
38 using std::runtime_error;
39 using std::shared_ptr;
42 /** @param in Input sampling rate (Hz)
43 * @param out Output sampling rate (Hz)
44 * @param channels Number of channels.
46 Resampler::Resampler (int in, int out, int channels)
49 , _channels (channels)
52 _src = src_new (SRC_SINC_BEST_QUALITY, _channels, &error);
54 throw runtime_error (String::compose(N_("could not create sample-rate converter (%1)"), error));
59 Resampler::~Resampler ()
68 Resampler::set_fast ()
74 _src = src_new (SRC_LINEAR, _channels, &error);
76 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 DCPOMATIC_ASSERT(in->channels() == _channels);
86 int in_frames = in->frames ();
89 auto resampled = make_shared<AudioBuffers>(_channels, 0);
91 while (in_frames > 0) {
93 /* Compute the resampled frames count and add 32 for luck */
94 int const max_resampled_frames = ceil (static_cast<double>(in_frames) * _out_rate / _in_rate) + 32;
97 std::vector<float> in_buffer(in_frames * _channels);
98 std::vector<float> out_buffer(max_resampled_frames * _channels);
101 auto p = in->data ();
102 auto q = in_buffer.data();
103 for (int i = 0; i < in_frames; ++i) {
104 for (int j = 0; j < _channels; ++j) {
105 *q++ = p[j][in_offset + i];
110 data.data_in = in_buffer.data();
111 data.input_frames = in_frames;
113 data.data_out = out_buffer.data();
114 data.output_frames = max_resampled_frames;
116 data.end_of_input = 0;
117 data.src_ratio = double (_out_rate) / _in_rate;
119 int const r = src_process (_src, &data);
123 N_("could not run sample-rate converter (%1) [processing %2 to %3, %4 channels]"),
126 max_resampled_frames,
132 if (data.output_frames_gen == 0) {
136 resampled->set_frames (out_offset + data.output_frames_gen);
139 auto p = data.data_out;
140 auto q = resampled->data ();
141 for (int i = 0; i < data.output_frames_gen; ++i) {
142 for (int j = 0; j < _channels; ++j) {
143 q[j][out_offset + i] = *p++;
148 in_frames -= data.input_frames_used;
149 in_offset += data.input_frames_used;
150 out_offset += data.output_frames_gen;
157 shared_ptr<const AudioBuffers>
160 auto out = make_shared<AudioBuffers>(_channels, 0);
162 int64_t const output_size = 65536;
165 std::vector<float> buffer(output_size);
168 data.data_in = dummy;
169 data.input_frames = 0;
170 data.data_out = buffer.data();
171 data.output_frames = output_size;
172 data.end_of_input = 1;
173 data.src_ratio = double (_out_rate) / _in_rate;
175 int const r = src_process (_src, &data);
177 throw EncodeError (String::compose(N_("could not run sample-rate converter (%1)"), src_strerror(r)));
180 out->set_frames (out_offset + data.output_frames_gen);
182 auto p = data.data_out;
183 auto q = out->data ();
184 for (int i = 0; i < data.output_frames_gen; ++i) {
185 for (int j = 0; j < _channels; ++j) {
186 q[j][out_offset + i] = *p++;
190 out_offset += data.output_frames_gen;