]> git.sesse.net Git - nageru/blobdiff - resampling_queue.cpp
Fix an issue where the mixer lagging too much behind CEF would cause us to display...
[nageru] / resampling_queue.cpp
index d7e811d87c4bd4bb7b1325c2e262a5fedbf0cc71..24811ebe5bbab43fa7cd68471c4ca69f82e673e8 100644 (file)
@@ -77,6 +77,11 @@ bool ResamplingQueue::get_output_samples(steady_clock::time_point ts, float *sam
                return true;
        }
 
+       // This can happen when we get dropped frames on the master card.
+       if (duration<double>(ts.time_since_epoch()).count() <= 0.0) {
+               rate_adjustment_policy = DO_NOT_ADJUST_RATE;
+       }
+
        if (rate_adjustment_policy == ADJUST_RATE && (a0.good_sample || a1.good_sample)) {
                // Estimate the current number of input samples produced at
                // this instant in time, by extrapolating from the last known
@@ -105,9 +110,7 @@ bool ResamplingQueue::get_output_samples(steady_clock::time_point ts, float *sam
                                err += delay_samples_to_add;
                        } else if (err > 0.0) {
                                int delay_samples_to_remove = min<int>(lrintf(err), buffer.size() / num_channels);
-                               for (ssize_t i = 0; i < delay_samples_to_remove * num_channels; ++i) {
-                                       buffer.pop_front();
-                               }
+                               buffer.erase(buffer.begin(), buffer.begin() + delay_samples_to_remove * num_channels);
                                total_consumed_samples += delay_samples_to_remove;
                                err -= delay_samples_to_remove;
                        }
@@ -145,8 +148,6 @@ bool ResamplingQueue::get_output_samples(steady_clock::time_point ts, float *sam
                if (rcorr < 0.95) rcorr = 0.95;
                assert(!isnan(rcorr));
                vresampler.set_rratio(rcorr);
-       } else {
-               assert(rate_adjustment_policy == DO_NOT_ADJUST_RATE);
        }
 
        // Finally actually resample, producing exactly <num_samples> output samples.
@@ -159,6 +160,10 @@ bool ResamplingQueue::get_output_samples(steady_clock::time_point ts, float *sam
                        fprintf(stderr, "Card %u: PANIC: Out of input samples to resample, still need %d output samples! (correction factor is %f)\n",
                                card_num, int(vresampler.out_count), rcorr);
                        memset(vresampler.out_data, 0, vresampler.out_count * num_channels * sizeof(float));
+
+                       // Reset the loop filter.
+                       z1 = z2 = z3 = 0.0;
+
                        return false;
                }