From c33e6fa2a8178eddf2db920a6be05ac9f7144b80 Mon Sep 17 00:00:00 2001 From: Nelson Ho Date: Sat, 7 Jan 2017 17:34:11 -0500 Subject: [PATCH] Fix AudioLocation::is_near function to handle a corner case where the two frames have different samples_per_frame, and sample_start is at the samples_per_frame limit. --- src/FFmpegReader.cpp | 24 ++++-------------------- 1 file changed, 4 insertions(+), 20 deletions(-) diff --git a/src/FFmpegReader.cpp b/src/FFmpegReader.cpp index a311ac8b..6af63d87 100644 --- a/src/FFmpegReader.cpp +++ b/src/FFmpegReader.cpp @@ -90,29 +90,13 @@ int AudioLocation::is_near(AudioLocation location, int samples_per_frame, int am // This is too far away to be considered return false; - int sample_diff = abs(location.sample_start - sample_start); - if (location.frame == frame && sample_diff >= 0 && sample_diff <= amount) + // Note that samples_per_frame can vary slightly frame to frame when the + // audio sampling rate is not an integer multiple of the video fps. + int diff = samples_per_frame * (location.frame - frame) + location.sample_start - sample_start; + if (abs(diff) <= amount) // close return true; - // new frame is after - if (location.frame > frame) - { - // remaining samples + new samples - sample_diff = (samples_per_frame - sample_start) + location.sample_start; - if (sample_diff >= 0 && sample_diff <= amount) - return true; - } - - // new frame is before - if (location.frame < frame) - { - // remaining new samples + old samples - sample_diff = (samples_per_frame - location.sample_start) + sample_start; - if (sample_diff >= 0 && sample_diff <= amount) - return true; - } - // not close return false; }