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