LogRange.cpp

Go to the documentation of this file.
00001 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*-  vi:set ts=8 sts=4 sw=4: */
00002 
00003 /*
00004     Sonic Visualiser
00005     An audio file viewer and annotation editor.
00006     Centre for Digital Music, Queen Mary, University of London.
00007     This file copyright 2006 Chris Cannam.
00008     
00009     This program is free software; you can redistribute it and/or
00010     modify it under the terms of the GNU General Public License as
00011     published by the Free Software Foundation; either version 2 of the
00012     License, or (at your option) any later version.  See the file
00013     COPYING included with this distribution for more information.
00014 */
00015 
00016 #include "LogRange.h"
00017 
00018 #include <algorithm>
00019 #include <cmath>
00020 
00021 void
00022 LogRange::mapRange(float &min, float &max, float logthresh)
00023 {
00024     if (min > max) std::swap(min, max);
00025     if (max == min) max = min + 1;
00026 
00027     if (min >= 0.f) {
00028 
00029         max = log10f(max); // we know max != 0
00030 
00031         if (min == 0.f) min = std::min(logthresh, max);
00032         else min = log10f(min);
00033 
00034     } else if (max <= 0.f) {
00035         
00036         min = log10f(-min); // we know min != 0
00037         
00038         if (max == 0.f) max = std::min(logthresh, min);
00039         else max = log10f(-max);
00040         
00041         std::swap(min, max);
00042         
00043     } else {
00044         
00045         // min < 0 and max > 0
00046         
00047         max = log10f(std::max(max, -min));
00048         min = std::min(logthresh, max);
00049     }
00050 
00051     if (min == max) min = max - 1;
00052 }        
00053 
00054 float
00055 LogRange::map(float value, float thresh)
00056 {
00057     if (value == 0.f) return thresh;
00058     return log10f(fabsf(value));
00059 }
00060 
00061 float
00062 LogRange::unmap(float value)
00063 {
00064     return powf(10.0, value);
00065 }

Generated on Wed Feb 20 15:45:26 2008 for SonicVisualiser by  doxygen 1.5.1