2 Copyright (C) 2013-2015 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.
20 #include "resampler.h"
21 #include "audio_buffers.h"
22 #include "exceptions.h"
23 #include "compose.hpp"
24 #include "dcpomatic_assert.h"
25 #include <samplerate.h>
32 using boost::shared_ptr;
34 Resampler::Resampler (int in, int out, int channels)
37 , _channels (channels)
40 _src = src_new (SRC_SINC_BEST_QUALITY, _channels, &error);
42 throw StringError (String::compose (N_("could not create sample-rate converter (%1)"), error));
46 Resampler::~Resampler ()
51 shared_ptr<const AudioBuffers>
52 Resampler::run (shared_ptr<const AudioBuffers> in)
54 int in_frames = in->frames ();
57 shared_ptr<AudioBuffers> resampled (new AudioBuffers (_channels, 0));
59 while (in_frames > 0) {
61 /* Compute the resampled frames count and add 32 for luck */
62 int const max_resampled_frames = ceil ((double) in_frames * _out_rate / _in_rate) + 32;
65 data.data_in = new float[in_frames * _channels];
68 float** p = in->data ();
69 float* q = data.data_in;
70 for (int i = 0; i < in_frames; ++i) {
71 for (int j = 0; j < _channels; ++j) {
72 *q++ = p[j][in_offset + i];
77 data.input_frames = in_frames;
79 data.data_out = new float[max_resampled_frames * _channels];
80 data.output_frames = max_resampled_frames;
82 data.end_of_input = 0;
83 data.src_ratio = double (_out_rate) / _in_rate;
85 int const r = src_process (_src, &data);
87 delete[] data.data_in;
88 delete[] data.data_out;
89 throw EncodeError (String::compose (N_("could not run sample-rate converter (%1)"), src_strerror (r)));
92 if (data.output_frames_gen == 0) {
96 resampled->ensure_size (out_offset + data.output_frames_gen);
97 resampled->set_frames (out_offset + data.output_frames_gen);
100 float* p = data.data_out;
101 float** q = resampled->data ();
102 for (int i = 0; i < data.output_frames_gen; ++i) {
103 for (int j = 0; j < _channels; ++j) {
104 q[j][out_offset + i] = *p++;
109 in_frames -= data.input_frames_used;
110 in_offset += data.input_frames_used;
111 out_offset += data.output_frames_gen;
113 delete[] data.data_in;
114 delete[] data.data_out;
120 shared_ptr<const AudioBuffers>
123 shared_ptr<AudioBuffers> out (new AudioBuffers (_channels, 0));
125 int64_t const output_size = 65536;
128 float buffer[output_size];
131 data.data_in = dummy;
132 data.input_frames = 0;
133 data.data_out = buffer;
134 data.output_frames = output_size;
135 data.end_of_input = 1;
136 data.src_ratio = double (_out_rate) / _in_rate;
138 int const r = src_process (_src, &data);
140 throw EncodeError (String::compose (N_("could not run sample-rate converter (%1)"), src_strerror (r)));
143 out->ensure_size (out_offset + data.output_frames_gen);
145 float* p = data.data_out;
146 float** q = out->data ();
147 for (int i = 0; i < data.output_frames_gen; ++i) {
148 for (int j = 0; j < _channels; ++j) {
149 q[j][out_offset + i] = *p++;
153 out_offset += data.output_frames_gen;
154 out->set_frames (out_offset);