# HG changeset patch # User Chris Cannam # Date 1140717691 0 # Node ID 51e158b505da8acf6b3519dfde9559c94dc32436 # Parent 5e28cbb431d04e2dac943f9998fcf94835f0e5e2 * Rearrange spectrogram cacheing so that gain, normalization, instantaneous frequency calculations etc can be done from the cached data (increasing the size of the cache, but also the usability). diff -r 5e28cbb431d0 -r 51e158b505da base/View.cpp --- a/base/View.cpp Mon Feb 20 17:23:40 2006 +0000 +++ b/base/View.cpp Thu Feb 23 18:01:31 2006 +0000 @@ -255,6 +255,70 @@ return (long(x) * long(m_zoomLevel)) + getStartFrame(); } +float +View::getYForFrequency(float frequency, + float minf, + float maxf, + bool logarithmic) const +{ + int h = height(); + + if (logarithmic) { + + static float lastminf = 0.0, lastmaxf = 0.0; + static float logminf = 0.0, logmaxf = 0.0; + + if (lastminf != minf) { + lastminf = (minf == 0.0 ? 1.0 : minf); + logminf = log10f(minf); + } + if (lastmaxf != maxf) { + lastmaxf = (maxf < lastminf ? lastminf : maxf); + logmaxf = log10f(maxf); + } + + if (logminf == logmaxf) return 0; + return h - (h * (log10f(frequency) - logminf)) / (logmaxf - logminf); + + } else { + + if (minf == maxf) return 0; + return h - (h * (frequency - minf)) / (maxf - minf); + } +} + +float +View::getFrequencyForY(int y, + float minf, + float maxf, + bool logarithmic) const +{ + int h = height(); + + if (logarithmic) { + + static float lastminf = 0.0, lastmaxf = 0.0; + static float logminf = 0.0, logmaxf = 0.0; + + if (lastminf != minf) { + lastminf = (minf == 0.0 ? 1.0 : minf); + logminf = log10f(minf); + } + if (lastmaxf != maxf) { + lastmaxf = (maxf < lastminf ? lastminf : maxf); + logmaxf = log10f(maxf); + } + + if (logminf == logmaxf) return 0; + return pow(10.f, logminf + ((logmaxf - logminf) * (h - y)) / h); + + } else { + + if (minf == maxf) return 0; + return minf + ((h - y) * (maxf - minf)) / h; + } +} + int View::getZoomLevel() const { diff -r 5e28cbb431d0 -r 51e158b505da base/View.h --- a/base/View.h Mon Feb 20 17:23:40 2006 +0000 +++ b/base/View.h Thu Feb 23 18:01:31 2006 +0000 @@ -96,6 +96,26 @@ long getFrameForX(int x) const; /** + * Return the pixel y-coordinate corresponding to a given + * frequency, if the frequency range is as specified. This does + * not imply any policy about layer frequency ranges, but it might + * be useful for layers to match theirs up if desired. + * + * Not thread-safe in logarithmic mode. Call only from GUI thread. + */ + float getYForFrequency(float frequency, float minFreq, float maxFreq, + bool logarithmic) const; + + /** + * Return the closest frequency to the given pixel y-coordinate, + * if the frequency range is as specified. + * + * Not thread-safe in logarithmic mode. Call only from GUI thread. + */ + float getFrequencyForY(int y, float minFreq, float maxFreq, + bool logarithmic) const; + + /** * Return the zoom level, i.e. the number of frames per pixel */ int getZoomLevel() const;