Do resampling in AudioDecoder rather than Player.
[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         src_delete (_src);
57 }
58
59 void
60 Resampler::set_fast ()
61 {
62         src_delete (_src);
63         int error;
64         _src = src_new (SRC_LINEAR, _channels, &error);
65         if (!_src) {
66                 throw runtime_error (String::compose (N_("could not create sample-rate converter (%1)"), error));
67         }
68 }
69
70 shared_ptr<const AudioBuffers>
71 Resampler::run (shared_ptr<const AudioBuffers> in)
72 {
73         int in_frames = in->frames ();
74         int in_offset = 0;
75         int out_offset = 0;
76         shared_ptr<AudioBuffers> resampled (new AudioBuffers (_channels, 0));
77
78         while (in_frames > 0) {
79
80                 /* Compute the resampled frames count and add 32 for luck */
81                 int const max_resampled_frames = ceil ((double) in_frames * _out_rate / _in_rate) + 32;
82
83                 SRC_DATA data;
84                 float* in_buffer = new float[in_frames * _channels];
85
86                 {
87                         float** p = in->data ();
88                         float* q = in_buffer;
89                         for (int i = 0; i < in_frames; ++i) {
90                                 for (int j = 0; j < _channels; ++j) {
91                                         *q++ = p[j][in_offset + i];
92                                 }
93                         }
94                 }
95
96                 data.data_in = in_buffer;
97                 data.input_frames = in_frames;
98
99                 data.data_out = new float[max_resampled_frames * _channels];
100                 data.output_frames = max_resampled_frames;
101
102                 data.end_of_input = 0;
103                 data.src_ratio = double (_out_rate) / _in_rate;
104
105                 int const r = src_process (_src, &data);
106                 if (r) {
107                         delete[] data.data_in;
108                         delete[] data.data_out;
109                         throw EncodeError (
110                                 String::compose (
111                                         N_("could not run sample-rate converter (%1) [processing %2 to %3, %4 channels]"),
112                                         src_strerror (r),
113                                         in_frames,
114                                         max_resampled_frames,
115                                         _channels
116                                         )
117                                 );
118                 }
119
120                 if (data.output_frames_gen == 0) {
121                         delete[] data.data_in;
122                         delete[] data.data_out;
123                         break;
124                 }
125
126                 resampled->ensure_size (out_offset + data.output_frames_gen);
127                 resampled->set_frames (out_offset + data.output_frames_gen);
128
129                 {
130                         float* p = data.data_out;
131                         float** q = resampled->data ();
132                         for (int i = 0; i < data.output_frames_gen; ++i) {
133                                 for (int j = 0; j < _channels; ++j) {
134                                         q[j][out_offset + i] = *p++;
135                                 }
136                         }
137                 }
138
139                 in_frames -= data.input_frames_used;
140                 in_offset += data.input_frames_used;
141                 out_offset += data.output_frames_gen;
142
143                 delete[] data.data_in;
144                 delete[] data.data_out;
145         }
146
147         return resampled;
148 }
149
150 shared_ptr<const AudioBuffers>
151 Resampler::flush ()
152 {
153         shared_ptr<AudioBuffers> out (new AudioBuffers (_channels, 0));
154         int out_offset = 0;
155         int64_t const output_size = 65536;
156
157         float dummy[1];
158         float* buffer = new float[output_size];
159
160         SRC_DATA data;
161         data.data_in = dummy;
162         data.input_frames = 0;
163         data.data_out = buffer;
164         data.output_frames = output_size;
165         data.end_of_input = 1;
166         data.src_ratio = double (_out_rate) / _in_rate;
167
168         int const r = src_process (_src, &data);
169         if (r) {
170                 delete[] buffer;
171                 throw EncodeError (String::compose (N_("could not run sample-rate converter (%1)"), src_strerror (r)));
172         }
173
174         out->ensure_size (out_offset + data.output_frames_gen);
175
176         float* p = data.data_out;
177         float** q = out->data ();
178         for (int i = 0; i < data.output_frames_gen; ++i) {
179                 for (int j = 0; j < _channels; ++j) {
180                         q[j][out_offset + i] = *p++;
181                 }
182         }
183
184         out_offset += data.output_frames_gen;
185         out->set_frames (out_offset);
186
187         delete[] buffer;
188         return out;
189 }
190
191 void
192 Resampler::reset ()
193 {
194         src_reset (_src);
195 }