changeset 33:51e158b505da

* 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).
author Chris Cannam
date Thu, 23 Feb 2006 18:01:31 +0000
parents 5e28cbb431d0
children aaf73f7309f2
files base/View.cpp base/View.h
diffstat 2 files changed, 84 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- 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
 {
--- 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;