]> git.sesse.net Git - nageru/blobdiff - resampler.cpp
Re-run IWYU, again with lots of manual cleanup.
[nageru] / resampler.cpp
index 573146541c98cfd646138889acfb636fce18faaa..138f837250f154c35b42031654cdc15e8e19b3b0 100644 (file)
 
 #include "resampler.h"
 
-#include <stdio.h>
 #include <math.h>
+#include <stddef.h>
+#include <stdio.h>
+#include <string.h>
 #include <zita-resampler/vresampler.h>
 
 Resampler::Resampler(unsigned freq_in, unsigned freq_out, unsigned num_channels)
@@ -54,7 +56,7 @@ void Resampler::add_input_samples(double pts, const float *samples, ssize_t num_
        }
 }
 
-void Resampler::get_output_samples(double pts, float *samples, ssize_t num_samples)
+bool Resampler::get_output_samples(double pts, float *samples, ssize_t num_samples)
 {
        double last_output_len;
        if (first_output) {
@@ -87,17 +89,16 @@ void Resampler::get_output_samples(double pts, float *samples, ssize_t num_sampl
        // Compute loop filter coefficients for the two filters. We need to compute them
        // every time, since they depend on the number of samples the user asked for.
        //
-       // The loop bandwidth starts at 1.0 Hz, then goes down to 0.05 Hz after four seconds.
-       double loop_bandwidth_hz = (k_a0 < 4 * freq_in) ? 1.0 : 0.05;
-
-       // Set first filter much wider than the first one (20x as wide).
-       double w = (2.0 * M_PI) * 20.0 * loop_bandwidth_hz * num_samples / freq_out;
-       double w0 = 1.0 - exp(-w);
+       // The loop bandwidth is at 0.02 Hz; we trust the initial estimate quite well,
+       // and our jitter is pretty large since none of the threads involved run at
+       // real-time priority.
+       double loop_bandwidth_hz = 0.02;
 
-       // Set second filter.
-       w = (2.0 * M_PI) * loop_bandwidth_hz * ratio / freq_out;
-       double w1 = w * 1.6;
-       double w2 = w * num_samples / 1.6;
+       // Set filters. The first filter much wider than the first one (20x as wide).
+       double w = (2.0 * M_PI) * loop_bandwidth_hz * num_samples / freq_out;
+       double w0 = 1.0 - exp(-20.0 * w);
+       double w1 = w * 1.5 / num_samples / ratio;
+       double w2 = w / 1.5;
 
        // Filter <err> through the loop filter to find the correction ratio.
        z1 += w0 * (w1 * err - z1);
@@ -117,7 +118,8 @@ void Resampler::get_output_samples(double pts, float *samples, ssize_t num_sampl
                        // or we're dropping a lot of data.
                        fprintf(stderr, "PANIC: Out of input samples to resample, still need %d output samples!\n",
                                int(vresampler.out_count));
-                       break;
+                       memset(vresampler.out_data, 0, vresampler.out_count * sizeof(float));
+                       return false;
                }
 
                float inbuf[1024];
@@ -138,4 +140,5 @@ void Resampler::get_output_samples(double pts, float *samples, ssize_t num_sampl
                total_consumed_samples += consumed_samples;
                buffer.erase(buffer.begin(), buffer.begin() + consumed_samples * num_channels);
        }
+       return true;
 }