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