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