annotate base/RangeMapper.cpp @ 573:bdc9bb371a9f

...
author Chris Cannam
date Fri, 13 Mar 2009 17:53:43 +0000
parents 9525c9d7e54d
children b4a8d8221eaf
rev   line source
Chris@189 1 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */
Chris@189 2
Chris@189 3 /*
Chris@189 4 Sonic Visualiser
Chris@189 5 An audio file viewer and annotation editor.
Chris@189 6 Centre for Digital Music, Queen Mary, University of London.
Chris@202 7 This file copyright 2006 QMUL.
Chris@189 8
Chris@189 9 This program is free software; you can redistribute it and/or
Chris@189 10 modify it under the terms of the GNU General Public License as
Chris@189 11 published by the Free Software Foundation; either version 2 of the
Chris@189 12 License, or (at your option) any later version. See the file
Chris@189 13 COPYING included with this distribution for more information.
Chris@189 14 */
Chris@189 15
Chris@189 16 #include "RangeMapper.h"
Chris@573 17 #include "system/System.h"
Chris@189 18
Chris@189 19 #include <cassert>
Chris@189 20 #include <cmath>
Chris@189 21
Chris@189 22 #include <iostream>
Chris@189 23
Chris@189 24 LinearRangeMapper::LinearRangeMapper(int minpos, int maxpos,
Chris@189 25 float minval, float maxval,
Chris@464 26 QString unit, bool inverted) :
Chris@189 27 m_minpos(minpos),
Chris@189 28 m_maxpos(maxpos),
Chris@189 29 m_minval(minval),
Chris@189 30 m_maxval(maxval),
Chris@464 31 m_unit(unit),
Chris@464 32 m_inverted(inverted)
Chris@189 33 {
Chris@189 34 assert(m_maxval != m_minval);
Chris@189 35 assert(m_maxpos != m_minpos);
Chris@189 36 }
Chris@189 37
Chris@189 38 int
Chris@189 39 LinearRangeMapper::getPositionForValue(float value) const
Chris@189 40 {
Chris@190 41 int position = m_minpos +
Chris@190 42 lrintf(((value - m_minval) / (m_maxval - m_minval))
Chris@190 43 * (m_maxpos - m_minpos));
Chris@189 44 if (position < m_minpos) position = m_minpos;
Chris@189 45 if (position > m_maxpos) position = m_maxpos;
Chris@241 46 // std::cerr << "LinearRangeMapper::getPositionForValue: " << value << " -> "
Chris@241 47 // << position << " (minpos " << m_minpos << ", maxpos " << m_maxpos << ", minval " << m_minval << ", maxval " << m_maxval << ")" << std::endl;
Chris@464 48 if (m_inverted) return m_maxpos - position;
Chris@464 49 else return position;
Chris@189 50 }
Chris@189 51
Chris@189 52 float
Chris@189 53 LinearRangeMapper::getValueForPosition(int position) const
Chris@189 54 {
Chris@464 55 if (m_inverted) position = m_maxpos - position;
Chris@190 56 float value = m_minval +
Chris@190 57 ((float(position - m_minpos) / float(m_maxpos - m_minpos))
Chris@190 58 * (m_maxval - m_minval));
Chris@189 59 if (value < m_minval) value = m_minval;
Chris@189 60 if (value > m_maxval) value = m_maxval;
Chris@241 61 // std::cerr << "LinearRangeMapper::getValueForPosition: " << position << " -> "
Chris@241 62 // << value << " (minpos " << m_minpos << ", maxpos " << m_maxpos << ", minval " << m_minval << ", maxval " << m_maxval << ")" << std::endl;
Chris@189 63 return value;
Chris@189 64 }
Chris@189 65
Chris@189 66 LogRangeMapper::LogRangeMapper(int minpos, int maxpos,
Chris@356 67 float minval, float maxval,
Chris@464 68 QString unit, bool inverted) :
Chris@189 69 m_minpos(minpos),
Chris@189 70 m_maxpos(maxpos),
Chris@464 71 m_unit(unit),
Chris@464 72 m_inverted(inverted)
Chris@189 73 {
Chris@356 74 convertMinMax(minpos, maxpos, minval, maxval, m_minlog, m_ratio);
Chris@356 75
Chris@356 76 std::cerr << "LogRangeMapper: minpos " << minpos << ", maxpos "
Chris@356 77 << maxpos << ", minval " << minval << ", maxval "
Chris@356 78 << maxval << ", minlog " << m_minlog << ", ratio " << m_ratio
Chris@356 79 << ", unit " << unit.toStdString() << std::endl;
Chris@356 80
Chris@189 81 assert(m_maxpos != m_minpos);
Chris@189 82
Chris@189 83 m_maxlog = (m_maxpos - m_minpos) / m_ratio + m_minlog;
Chris@189 84 }
Chris@189 85
Chris@356 86 void
Chris@356 87 LogRangeMapper::convertMinMax(int minpos, int maxpos,
Chris@356 88 float minval, float maxval,
Chris@356 89 float &minlog, float &ratio)
Chris@356 90 {
Chris@356 91 static float thresh = powf(10, -10);
Chris@356 92 if (minval < thresh) minval = thresh;
Chris@356 93 minlog = log10f(minval);
Chris@356 94 ratio = (maxpos - minpos) / (log10f(maxval) - minlog);
Chris@356 95 }
Chris@356 96
Chris@356 97 void
Chris@356 98 LogRangeMapper::convertRatioMinLog(float ratio, float minlog,
Chris@356 99 int minpos, int maxpos,
Chris@356 100 float &minval, float &maxval)
Chris@356 101 {
Chris@356 102 minval = powf(10, minlog);
Chris@356 103 maxval = powf(10, (maxpos - minpos) / ratio + minlog);
Chris@356 104 }
Chris@356 105
Chris@189 106 int
Chris@189 107 LogRangeMapper::getPositionForValue(float value) const
Chris@189 108 {
Chris@341 109 int position = (log10(value) - m_minlog) * m_ratio + m_minpos;
Chris@189 110 if (position < m_minpos) position = m_minpos;
Chris@189 111 if (position > m_maxpos) position = m_maxpos;
Chris@464 112 // std::cerr << "LogRangeMapper::getPositionForValue: " << value << " -> "
Chris@464 113 // << position << " (minpos " << m_minpos << ", maxpos " << m_maxpos << ", ratio " << m_ratio << ", minlog " << m_minlog << ")" << std::endl;
Chris@464 114 if (m_inverted) return m_maxpos - position;
Chris@464 115 else return position;
Chris@189 116 }
Chris@189 117
Chris@189 118 float
Chris@189 119 LogRangeMapper::getValueForPosition(int position) const
Chris@189 120 {
Chris@464 121 if (m_inverted) position = m_maxpos - position;
Chris@189 122 float value = powf(10, (position - m_minpos) / m_ratio + m_minlog);
Chris@464 123 // std::cerr << "LogRangeMapper::getValueForPosition: " << position << " -> "
Chris@464 124 // << value << " (minpos " << m_minpos << ", maxpos " << m_maxpos << ", ratio " << m_ratio << ", minlog " << m_minlog << ")" << std::endl;
Chris@189 125 return value;
Chris@189 126 }
Chris@189 127