]> git.sesse.net Git - audiosync/blobdiff - estimate-skew.cpp
Move the speed variable out.
[audiosync] / estimate-skew.cpp
index f129c497e70623c82055ce87cd6e73e890b9da62..cc4835edfa227ab3dbd158d46bb390a51475b649 100644 (file)
@@ -66,20 +66,14 @@ class InterpolatingReader
 {
 private:
        FILE *f;
-       double speed;
        double p;
        int in_pos;
        short prev_sample;
 
 public:
-       InterpolatingReader(FILE *f) : f(f), speed(1.0), p(0.0), in_pos(-1) {}
+       InterpolatingReader(FILE *f) : f(f), p(0.0), in_pos(-1) {}
 
-       void update_speed(double speed)
-       {
-               this->speed = speed;
-       }
-
-       double read_sample()
+       double read_sample(double speed)
        {
                short sample;
 
@@ -100,8 +94,15 @@ public:
 
 double filter(double x)
 {
-       static double l = 1.0;
-       l = 0.01 * x + 0.99 * l;
+       static double l;
+       static bool init = false;
+
+       if (init) {
+               l = 0.05 * x + 0.95 * l;
+       } else {
+               init = true;
+               l = x;
+       }
        return l;
 }
 
@@ -164,10 +165,8 @@ int main(int argc, char **argv)
                f2.update(s);
        } while (!f2.check_flag() || !f2.get_status());
 
-       double speed = 1.000;
-
        InterpolatingReader intp(in2);
-       intp.update_speed(speed);  // test to see if we can resync OK
+       double speed = 1.000;
 
        while (!feof(in1) && !feof(in2)) {
                short refs;
@@ -185,15 +184,14 @@ int main(int argc, char **argv)
                // same here
                unsigned num_inp = 0;
                while (!feof(in2) && !f2.check_flag()) {
-                       double s = intp.read_sample();
+                       double s = intp.read_sample(speed);
                        f2.update((short)s);
                        ++num_inp;
                }
 
                double ns = filter(speed * double(num_inp) / double(num_ref));
-               printf("%u vs. %u -- ratio %f, speed %f, filtered speed %f\n", num_inp, num_ref,
+               printf("%u vs. %u -- ratio %.3f, speed %.3f, filtered speed %.3f\n", num_inp, num_ref,
                        double(num_inp) / double(num_ref), speed * double(num_inp) / double(num_ref), ns);
                speed = ns;
-               intp.update_speed(ns);
        }
 }