4476d9f1f9b2220cab1160ae876246e301a49661
[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 <boost/make_shared.hpp>
28 #include <iostream>
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 using boost::make_shared;
38
39 /** @param in Input sampling rate (Hz)
40  *  @param out Output sampling rate (Hz)
41  *  @param channels Number of channels.
42  *  @param fast true to be fast rather than good.
43  */
44 Resampler::Resampler (int in, int out, int channels, bool fast)
45         : _in_rate (in)
46         , _out_rate (out)
47         , _channels (channels)
48 {
49         int error;
50         _src = src_new (fast ? SRC_LINEAR : SRC_SINC_BEST_QUALITY, _channels, &error);
51         if (!_src) {
52                 throw runtime_error (String::compose (N_("could not create sample-rate converter (%1)"), error));
53         }
54 }
55
56 Resampler::~Resampler ()
57 {
58         src_delete (_src);
59 }
60
61 shared_ptr<const AudioBuffers>
62 Resampler::run (shared_ptr<const AudioBuffers> in)
63 {
64         int in_frames = in->frames ();
65         int in_offset = 0;
66         int out_offset = 0;
67         shared_ptr<AudioBuffers> resampled = make_shared<AudioBuffers> (_channels, 0);
68
69         while (in_frames > 0) {
70
71                 /* Compute the resampled frames count and add 32 for luck */
72                 int const max_resampled_frames = ceil ((double) in_frames * _out_rate / _in_rate) + 32;
73
74                 SRC_DATA data;
75                 data.data_in = new float[in_frames * _channels];
76
77                 {
78                         float** p = in->data ();
79                         float* q = data.data_in;
80                         for (int i = 0; i < in_frames; ++i) {
81                                 for (int j = 0; j < _channels; ++j) {
82                                         *q++ = p[j][in_offset + i];
83                                 }
84                         }
85                 }
86
87                 data.input_frames = in_frames;
88
89                 data.data_out = new float[max_resampled_frames * _channels];
90                 data.output_frames = max_resampled_frames;
91
92                 data.end_of_input = 0;
93                 data.src_ratio = double (_out_rate) / _in_rate;
94
95                 int const r = src_process (_src, &data);
96                 if (r) {
97                         delete[] data.data_in;
98                         delete[] data.data_out;
99                         throw EncodeError (
100                                 String::compose (
101                                         N_("could not run sample-rate converter (%1) [processing %2 to %3, %4 channels]"),
102                                         src_strerror (r),
103                                         in_frames,
104                                         max_resampled_frames,
105                                         _channels
106                                         )
107                                 );
108                 }
109
110                 if (data.output_frames_gen == 0) {
111                         break;
112                 }
113
114                 resampled->ensure_size (out_offset + data.output_frames_gen);
115                 resampled->set_frames (out_offset + data.output_frames_gen);
116
117                 {
118                         float* p = data.data_out;
119                         float** q = resampled->data ();
120                         for (int i = 0; i < data.output_frames_gen; ++i) {
121                                 for (int j = 0; j < _channels; ++j) {
122                                         q[j][out_offset + i] = *p++;
123                                 }
124                         }
125                 }
126
127                 in_frames -= data.input_frames_used;
128                 in_offset += data.input_frames_used;
129                 out_offset += data.output_frames_gen;
130
131                 delete[] data.data_in;
132                 delete[] data.data_out;
133         }
134
135         return resampled;
136 }
137
138 shared_ptr<const AudioBuffers>
139 Resampler::flush ()
140 {
141         shared_ptr<AudioBuffers> out = make_shared<AudioBuffers> (_channels, 0);
142         int out_offset = 0;
143         int64_t const output_size = 65536;
144
145         float dummy[1];
146         float* buffer = new float[output_size];
147
148         SRC_DATA data;
149         data.data_in = dummy;
150         data.input_frames = 0;
151         data.data_out = buffer;
152         data.output_frames = output_size;
153         data.end_of_input = 1;
154         data.src_ratio = double (_out_rate) / _in_rate;
155
156         int const r = src_process (_src, &data);
157         if (r) {
158                 delete[] buffer;
159                 throw EncodeError (String::compose (N_("could not run sample-rate converter (%1)"), src_strerror (r)));
160         }
161
162         out->ensure_size (out_offset + data.output_frames_gen);
163
164         float* p = data.data_out;
165         float** q = out->data ();
166         for (int i = 0; i < data.output_frames_gen; ++i) {
167                 for (int j = 0; j < _channels; ++j) {
168                         q[j][out_offset + i] = *p++;
169                 }
170         }
171
172         out_offset += data.output_frames_gen;
173         out->set_frames (out_offset);
174
175         delete[] buffer;
176         return out;
177 }