Merge branch '2.0' of ssh://git.carlh.net/home/carl/git/dcpomatic2 into 2.0
[dcpomatic.git] / src / lib / resampler.cc
1 /*
2     Copyright (C) 2013-2015 Carl Hetherington <cth@carlh.net>
3
4     This program is free software; you can redistribute it and/or modify
5     it under the terms of the GNU General Public License as published by
6     the Free Software Foundation; either version 2 of the License, or
7     (at your option) any later version.
8
9     This program is distributed in the hope that it will be useful,
10     but WITHOUT ANY WARRANTY; without even the implied warranty of
11     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12     GNU General Public License for more details.
13
14     You should have received a copy of the GNU General Public License
15     along with this program; if not, write to the Free Software
16     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17
18 */
19
20 #include "resampler.h"
21 #include "audio_buffers.h"
22 #include "exceptions.h"
23 #include "compose.hpp"
24 #include "dcpomatic_assert.h"
25 #include <samplerate.h>
26
27 #include "i18n.h"
28
29 using std::cout;
30 using std::pair;
31 using std::make_pair;
32 using boost::shared_ptr;
33
34 /** @param in Input sampling rate (Hz)
35  *  @param out Output sampling rate (Hz)
36  *  @param channels Number of channels.
37  *  @param fast true to be fast rather than good.
38  */
39 Resampler::Resampler (int in, int out, int channels, bool fast)
40         : _in_rate (in)
41         , _out_rate (out)
42         , _channels (channels)
43 {
44         int error;
45         _src = src_new (fast ? SRC_LINEAR : SRC_SINC_BEST_QUALITY, _channels, &error);
46         if (!_src) {
47                 throw StringError (String::compose (N_("could not create sample-rate converter (%1)"), error));
48         }
49 }
50
51 Resampler::~Resampler ()
52 {
53         src_delete (_src);
54 }
55
56 shared_ptr<const AudioBuffers>
57 Resampler::run (shared_ptr<const AudioBuffers> in)
58 {
59         int in_frames = in->frames ();
60         int in_offset = 0;
61         int out_offset = 0;
62         shared_ptr<AudioBuffers> resampled (new AudioBuffers (_channels, 0));
63
64         while (in_frames > 0) {
65
66                 /* Compute the resampled frames count and add 32 for luck */
67                 int const max_resampled_frames = ceil ((double) in_frames * _out_rate / _in_rate) + 32;
68
69                 SRC_DATA data;
70                 data.data_in = new float[in_frames * _channels];
71
72                 {
73                         float** p = in->data ();
74                         float* q = data.data_in;
75                         for (int i = 0; i < in_frames; ++i) {
76                                 for (int j = 0; j < _channels; ++j) {
77                                         *q++ = p[j][in_offset + i];
78                                 }
79                         }
80                 }
81
82                 data.input_frames = in_frames;
83
84                 data.data_out = new float[max_resampled_frames * _channels];
85                 data.output_frames = max_resampled_frames;
86
87                 data.end_of_input = 0;
88                 data.src_ratio = double (_out_rate) / _in_rate;
89
90                 int const r = src_process (_src, &data);
91                 if (r) {
92                         delete[] data.data_in;
93                         delete[] data.data_out;
94                         throw EncodeError (String::compose (N_("could not run sample-rate converter (%1)"), src_strerror (r)));
95                 }
96
97                 if (data.output_frames_gen == 0) {
98                         break;
99                 }
100
101                 resampled->ensure_size (out_offset + data.output_frames_gen);
102                 resampled->set_frames (out_offset + data.output_frames_gen);
103
104                 {
105                         float* p = data.data_out;
106                         float** q = resampled->data ();
107                         for (int i = 0; i < data.output_frames_gen; ++i) {
108                                 for (int j = 0; j < _channels; ++j) {
109                                         q[j][out_offset + i] = *p++;
110                                 }
111                         }
112                 }
113
114                 in_frames -= data.input_frames_used;
115                 in_offset += data.input_frames_used;
116                 out_offset += data.output_frames_gen;
117
118                 delete[] data.data_in;
119                 delete[] data.data_out;
120         }
121
122         return resampled;
123 }
124
125 shared_ptr<const AudioBuffers>
126 Resampler::flush ()
127 {
128         shared_ptr<AudioBuffers> out (new AudioBuffers (_channels, 0));
129         int out_offset = 0;
130         int64_t const output_size = 65536;
131
132         float dummy[1];
133         float buffer[output_size];
134
135         SRC_DATA data;
136         data.data_in = dummy;
137         data.input_frames = 0;
138         data.data_out = buffer;
139         data.output_frames = output_size;
140         data.end_of_input = 1;
141         data.src_ratio = double (_out_rate) / _in_rate;
142
143         int const r = src_process (_src, &data);
144         if (r) {
145                 throw EncodeError (String::compose (N_("could not run sample-rate converter (%1)"), src_strerror (r)));
146         }
147
148         out->ensure_size (out_offset + data.output_frames_gen);
149
150         float* p = data.data_out;
151         float** q = out->data ();
152         for (int i = 0; i < data.output_frames_gen; ++i) {
153                 for (int j = 0; j < _channels; ++j) {
154                         q[j][out_offset + i] = *p++;
155                 }
156         }
157
158         out_offset += data.output_frames_gen;
159         out->set_frames (out_offset);
160
161         return out;
162 }