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