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)
48 _src = src_new (SRC_SINC_BEST_QUALITY, _channels, &error);
50 throw runtime_error (String::compose (N_("could not create sample-rate converter (%1)"), error));
54 Resampler::~Resampler ()
60 Resampler::set_fast ()
64 _src = src_new (SRC_LINEAR, _channels, &error);
66 throw runtime_error (String::compose (N_("could not create sample-rate converter (%1)"), error));
70 shared_ptr<const AudioBuffers>
71 Resampler::run (shared_ptr<const AudioBuffers> in)
73 int in_frames = in->frames ();
76 shared_ptr<AudioBuffers> resampled (new AudioBuffers (_channels, 0));
78 while (in_frames > 0) {
80 /* Compute the resampled frames count and add 32 for luck */
81 int const max_resampled_frames = ceil ((double) in_frames * _out_rate / _in_rate) + 32;
84 float* in_buffer = new float[in_frames * _channels];
87 float** p = in->data ();
89 for (int i = 0; i < in_frames; ++i) {
90 for (int j = 0; j < _channels; ++j) {
91 *q++ = p[j][in_offset + i];
96 data.data_in = in_buffer;
97 data.input_frames = in_frames;
99 data.data_out = new float[max_resampled_frames * _channels];
100 data.output_frames = max_resampled_frames;
102 data.end_of_input = 0;
103 data.src_ratio = double (_out_rate) / _in_rate;
105 int const r = src_process (_src, &data);
107 delete[] data.data_in;
108 delete[] data.data_out;
111 N_("could not run sample-rate converter (%1) [processing %2 to %3, %4 channels]"),
114 max_resampled_frames,
120 if (data.output_frames_gen == 0) {
124 resampled->ensure_size (out_offset + data.output_frames_gen);
125 resampled->set_frames (out_offset + data.output_frames_gen);
128 float* p = data.data_out;
129 float** q = resampled->data ();
130 for (int i = 0; i < data.output_frames_gen; ++i) {
131 for (int j = 0; j < _channels; ++j) {
132 q[j][out_offset + i] = *p++;
137 in_frames -= data.input_frames_used;
138 in_offset += data.input_frames_used;
139 out_offset += data.output_frames_gen;
141 delete[] data.data_in;
142 delete[] data.data_out;
148 shared_ptr<const AudioBuffers>
151 shared_ptr<AudioBuffers> out (new AudioBuffers (_channels, 0));
153 int64_t const output_size = 65536;
156 float* buffer = new float[output_size];
159 data.data_in = dummy;
160 data.input_frames = 0;
161 data.data_out = buffer;
162 data.output_frames = output_size;
163 data.end_of_input = 1;
164 data.src_ratio = double (_out_rate) / _in_rate;
166 int const r = src_process (_src, &data);
169 throw EncodeError (String::compose (N_("could not run sample-rate converter (%1)"), src_strerror (r)));
172 out->ensure_size (out_offset + data.output_frames_gen);
174 float* p = data.data_out;
175 float** q = out->data ();
176 for (int i = 0; i < data.output_frames_gen; ++i) {
177 for (int j = 0; j < _channels; ++j) {
178 q[j][out_offset + i] = *p++;
182 out_offset += data.output_frames_gen;
183 out->set_frames (out_offset);