Add assert.
[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         DCPOMATIC_ASSERT(in->channels() == _channels);
85
86         int in_frames = in->frames ();
87         int in_offset = 0;
88         int out_offset = 0;
89         auto resampled = make_shared<AudioBuffers>(_channels, 0);
90
91         while (in_frames > 0) {
92
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;
95
96                 SRC_DATA data;
97                 std::vector<float> in_buffer(in_frames * _channels);
98                 std::vector<float> out_buffer(max_resampled_frames * _channels);
99
100                 {
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];
106                                 }
107                         }
108                 }
109
110                 data.data_in = in_buffer.data();
111                 data.input_frames = in_frames;
112
113                 data.data_out = out_buffer.data();
114                 data.output_frames = max_resampled_frames;
115
116                 data.end_of_input = 0;
117                 data.src_ratio = double (_out_rate) / _in_rate;
118
119                 int const r = src_process (_src, &data);
120                 if (r) {
121                         throw EncodeError (
122                                 String::compose (
123                                         N_("could not run sample-rate converter (%1) [processing %2 to %3, %4 channels]"),
124                                         src_strerror (r),
125                                         in_frames,
126                                         max_resampled_frames,
127                                         _channels
128                                         )
129                                 );
130                 }
131
132                 if (data.output_frames_gen == 0) {
133                         break;
134                 }
135
136                 resampled->set_frames (out_offset + data.output_frames_gen);
137
138                 {
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++;
144                                 }
145                         }
146                 }
147
148                 in_frames -= data.input_frames_used;
149                 in_offset += data.input_frames_used;
150                 out_offset += data.output_frames_gen;
151         }
152
153         return resampled;
154 }
155
156
157 shared_ptr<const AudioBuffers>
158 Resampler::flush ()
159 {
160         auto out = make_shared<AudioBuffers>(_channels, 0);
161         int out_offset = 0;
162         int64_t const output_size = 65536;
163
164         float dummy[1];
165         std::vector<float> buffer(output_size);
166
167         SRC_DATA data;
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;
174
175         int const r = src_process (_src, &data);
176         if (r) {
177                 throw EncodeError (String::compose(N_("could not run sample-rate converter (%1)"), src_strerror(r)));
178         }
179
180         out->set_frames (out_offset + data.output_frames_gen);
181
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++;
187                 }
188         }
189
190         out_offset += data.output_frames_gen;
191
192         return out;
193 }
194
195
196 void
197 Resampler::reset ()
198 {
199         src_reset (_src);
200 }
201