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 int in_frames = in->frames ();
87 auto resampled = make_shared<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 (static_cast<double>(in_frames) * _out_rate / _in_rate) + 32;
95 std::vector<float> in_buffer(in_frames * _channels);
96 std::vector<float> out_buffer(max_resampled_frames * _channels);
100 auto q = in_buffer.data();
101 for (int i = 0; i < in_frames; ++i) {
102 for (int j = 0; j < _channels; ++j) {
103 *q++ = p[j][in_offset + i];
108 data.data_in = in_buffer.data();
109 data.input_frames = in_frames;
111 data.data_out = out_buffer.data();
112 data.output_frames = max_resampled_frames;
114 data.end_of_input = 0;
115 data.src_ratio = double (_out_rate) / _in_rate;
117 int const r = src_process (_src, &data);
121 N_("could not run sample-rate converter (%1) [processing %2 to %3, %4 channels]"),
124 max_resampled_frames,
130 if (data.output_frames_gen == 0) {
134 resampled->ensure_size (out_offset + data.output_frames_gen);
135 resampled->set_frames (out_offset + data.output_frames_gen);
138 auto p = data.data_out;
139 auto q = resampled->data ();
140 for (int i = 0; i < data.output_frames_gen; ++i) {
141 for (int j = 0; j < _channels; ++j) {
142 q[j][out_offset + i] = *p++;
147 in_frames -= data.input_frames_used;
148 in_offset += data.input_frames_used;
149 out_offset += data.output_frames_gen;
156 shared_ptr<const AudioBuffers>
159 auto out = make_shared<AudioBuffers>(_channels, 0);
161 int64_t const output_size = 65536;
164 std::vector<float> buffer(output_size);
167 data.data_in = dummy;
168 data.input_frames = 0;
169 data.data_out = buffer.data();
170 data.output_frames = output_size;
171 data.end_of_input = 1;
172 data.src_ratio = double (_out_rate) / _in_rate;
174 int const r = src_process (_src, &data);
176 throw EncodeError (String::compose(N_("could not run sample-rate converter (%1)"), src_strerror(r)));
179 out->ensure_size (out_offset + data.output_frames_gen);
181 auto p = data.data_out;
182 auto q = out->data ();
183 for (int i = 0; i < data.output_frames_gen; ++i) {
184 for (int j = 0; j < _channels; ++j) {
185 q[j][out_offset + i] = *p++;
189 out_offset += data.output_frames_gen;
190 out->set_frames (out_offset);