Given an audio recording (monophonic & mono), having extracted the melody with an algorithm such as Melodia, I would like to find all segments of this pitch series where the frequency is nearly stable (the maximum deviation from the mean frequency should be smaller than a given range R for a duration of at least N milliseconds). Having identified these segments, I would like to get the mean frequency of each such segment.
There is no need to perform any further calculations with these pitches (the goal is not to find the scale or to generate musical notation).
Is there already a pre-built algorithm for this purpose? If there isn't, how would you write such an algorithm (in any programming language, using any libraries)?
Thank you very much!