1 // Parts of the code is adapted from Adriaensen's project Zita-ajbridge,
2 // although it has been heavily reworked for this use case. Original copyright follows:
4 // Copyright (C) 2012-2015 Fons Adriaensen <fons@linuxaudio.org>
6 // This program 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 3 of the License, or
9 // (at your option) any later version.
11 // This program 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.
16 // You should have received a copy of the GNU General Public License
17 // along with this program. If not, see <http://www.gnu.org/licenses/>.
19 #include "resampler.h"
25 #include <zita-resampler/vresampler.h>
27 Resampler::Resampler(unsigned freq_in, unsigned freq_out, unsigned num_channels)
28 : freq_in(freq_in), freq_out(freq_out), num_channels(num_channels),
29 ratio(double(freq_out) / double(freq_in))
31 vresampler.setup(ratio, num_channels, /*hlen=*/32);
33 // Prime the resampler so there's no more delay.
34 vresampler.inp_count = vresampler.inpsize() / 2 - 1;
35 vresampler.out_count = 1048576;
36 vresampler.process ();
39 void Resampler::add_input_samples(double pts, const float *samples, ssize_t num_samples)
42 // Synthesize a fake length.
43 last_input_len = double(num_samples) / freq_in;
46 last_input_len = pts - last_input_pts;
54 for (ssize_t i = 0; i < num_samples * num_channels; ++i) {
55 buffer.push_back(samples[i]);
59 bool Resampler::get_output_samples(double pts, float *samples, ssize_t num_samples)
61 double last_output_len;
63 // Synthesize a fake length.
64 last_output_len = double(num_samples) / freq_out;
66 last_output_len = pts - last_output_pts;
68 last_output_pts = pts;
70 // Using the time point since just before the last call to add_input_samples() as a base,
71 // estimate actual delay based on activity since then, measured in number of input samples:
72 double actual_delay = 0.0;
73 actual_delay += (k_a1 - k_a0) * last_output_len / last_input_len; // Inserted samples since k_a0, rescaled for the different time periods.
74 actual_delay += k_a0 - total_consumed_samples; // Samples inserted before k_a0 but not consumed yet.
75 actual_delay += vresampler.inpdist(); // Delay in the resampler itself.
76 double err = actual_delay - expected_delay;
77 if (first_output && err < 0.0) {
78 // Before the very first block, insert artificial delay based on our initial estimate,
79 // so that we don't need a long period to stabilize at the beginning.
80 int delay_samples_to_add = lrintf(-err);
81 for (ssize_t i = 0; i < delay_samples_to_add * num_channels; ++i) {
82 buffer.push_front(0.0f);
84 total_consumed_samples -= delay_samples_to_add; // Equivalent to increasing k_a0 and k_a1.
85 err += delay_samples_to_add;
89 // Compute loop filter coefficients for the two filters. We need to compute them
90 // every time, since they depend on the number of samples the user asked for.
92 // The loop bandwidth is at 0.02 Hz; we trust the initial estimate quite well,
93 // and our jitter is pretty large since none of the threads involved run at
94 // real-time priority.
95 double loop_bandwidth_hz = 0.02;
97 // Set filters. The first filter much wider than the first one (20x as wide).
98 double w = (2.0 * M_PI) * loop_bandwidth_hz * num_samples / freq_out;
99 double w0 = 1.0 - exp(-20.0 * w);
100 double w1 = w * 1.5 / num_samples / ratio;
103 // Filter <err> through the loop filter to find the correction ratio.
104 z1 += w0 * (w1 * err - z1);
105 z2 += w0 * (z1 - z2);
107 double rcorr = 1.0 - z2 - z3;
108 if (rcorr > 1.05) rcorr = 1.05;
109 if (rcorr < 0.95) rcorr = 0.95;
110 vresampler.set_rratio(rcorr);
112 // Finally actually resample, consuming exactly <num_samples> output samples.
113 vresampler.out_data = samples;
114 vresampler.out_count = num_samples;
115 while (vresampler.out_count > 0) {
116 if (buffer.empty()) {
117 // This should never happen unless delay is set way too low,
118 // or we're dropping a lot of data.
119 fprintf(stderr, "PANIC: Out of input samples to resample, still need %d output samples!\n",
120 int(vresampler.out_count));
121 memset(vresampler.out_data, 0, vresampler.out_count * sizeof(float));
126 size_t num_input_samples = sizeof(inbuf) / (sizeof(float) * num_channels);
127 if (num_input_samples * num_channels > buffer.size()) {
128 num_input_samples = buffer.size() / num_channels;
130 for (size_t i = 0; i < num_input_samples * num_channels; ++i) {
131 inbuf[i] = buffer[i];
134 vresampler.inp_count = num_input_samples;
135 vresampler.inp_data = inbuf;
137 vresampler.process();
139 size_t consumed_samples = num_input_samples - vresampler.inp_count;
140 total_consumed_samples += consumed_samples;
141 buffer.erase(buffer.begin(), buffer.begin() + consumed_samples * num_channels);