Fix the build for older macOS.
[dcpomatic.git] / src / lib / resampler.cc
1 /*
2     Copyright (C) 2013-2021 Carl Hetherington <cth@carlh.net>
3
4     This file is part of DCP-o-matic.
5
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.
10
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.
15
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/>.
18
19 */
20
21
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>
28 #include <iostream>
29 #include <cmath>
30
31 #include "i18n.h"
32
33
34 using std::cout;
35 using std::make_pair;
36 using std::make_shared;
37 using std::pair;
38 using std::runtime_error;
39 using std::shared_ptr;
40
41
42 /** @param in Input sampling rate (Hz)
43  *  @param out Output sampling rate (Hz)
44  *  @param channels Number of channels.
45  */
46 Resampler::Resampler (int in, int out, int channels)
47         : _in_rate (in)
48         , _out_rate (out)
49         , _channels (channels)
50 {
51         int error;
52         _src = src_new (SRC_SINC_BEST_QUALITY, _channels, &error);
53         if (!_src) {
54                 throw runtime_error (String::compose(N_("could not create sample-rate converter (%1)"), error));
55         }
56 }
57
58
59 Resampler::~Resampler ()
60 {
61         if (_src) {
62                 src_delete (_src);
63         }
64 }
65
66
67 void
68 Resampler::set_fast ()
69 {
70         src_delete (_src);
71         _src = nullptr;
72
73         int error;
74         _src = src_new (SRC_LINEAR, _channels, &error);
75         if (!_src) {
76                 throw runtime_error (String::compose(N_("could not create sample-rate converter (%1)"), error));
77         }
78 }
79
80
81 shared_ptr<const AudioBuffers>
82 Resampler::run (shared_ptr<const AudioBuffers> in)
83 {
84         int in_frames = in->frames ();
85         int in_offset = 0;
86         int out_offset = 0;
87         auto resampled = make_shared<AudioBuffers>(_channels, 0);
88
89         while (in_frames > 0) {
90
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;
93
94                 SRC_DATA data;
95                 std::vector<float> in_buffer(in_frames * _channels);
96                 std::vector<float> out_buffer(max_resampled_frames * _channels);
97
98                 {
99                         auto p = in->data ();
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];
104                                 }
105                         }
106                 }
107
108                 data.data_in = in_buffer.data();
109                 data.input_frames = in_frames;
110
111                 data.data_out = out_buffer.data();
112                 data.output_frames = max_resampled_frames;
113
114                 data.end_of_input = 0;
115                 data.src_ratio = double (_out_rate) / _in_rate;
116
117                 int const r = src_process (_src, &data);
118                 if (r) {
119                         throw EncodeError (
120                                 String::compose (
121                                         N_("could not run sample-rate converter (%1) [processing %2 to %3, %4 channels]"),
122                                         src_strerror (r),
123                                         in_frames,
124                                         max_resampled_frames,
125                                         _channels
126                                         )
127                                 );
128                 }
129
130                 if (data.output_frames_gen == 0) {
131                         break;
132                 }
133
134                 resampled->ensure_size (out_offset + data.output_frames_gen);
135                 resampled->set_frames (out_offset + data.output_frames_gen);
136
137                 {
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++;
143                                 }
144                         }
145                 }
146
147                 in_frames -= data.input_frames_used;
148                 in_offset += data.input_frames_used;
149                 out_offset += data.output_frames_gen;
150         }
151
152         return resampled;
153 }
154
155
156 shared_ptr<const AudioBuffers>
157 Resampler::flush ()
158 {
159         auto out = make_shared<AudioBuffers>(_channels, 0);
160         int out_offset = 0;
161         int64_t const output_size = 65536;
162
163         float dummy[1];
164         std::vector<float> buffer(output_size);
165
166         SRC_DATA data;
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;
173
174         int const r = src_process (_src, &data);
175         if (r) {
176                 throw EncodeError (String::compose(N_("could not run sample-rate converter (%1)"), src_strerror(r)));
177         }
178
179         out->ensure_size (out_offset + data.output_frames_gen);
180
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++;
186                 }
187         }
188
189         out_offset += data.output_frames_gen;
190         out->set_frames (out_offset);
191
192         return out;
193 }
194
195
196 void
197 Resampler::reset ()
198 {
199         src_reset (_src);
200 }
201