annotate base/RangeMapper.cpp @ 189:0703252c9fe8

* Add spectrum icon * Start range mapper class for use in mapping between e.g. dial positions and underlying values
author Chris Cannam
date Mon, 16 Oct 2006 13:13:57 +0000
parents
children 60ba218a54bb
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@189 7 This file copyright 2006 Chris Cannam.
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@189 17
Chris@189 18 #include <cassert>
Chris@189 19 #include <cmath>
Chris@189 20
Chris@189 21 #include <iostream>
Chris@189 22
Chris@189 23 LinearRangeMapper::LinearRangeMapper(int minpos, int maxpos,
Chris@189 24 float minval, float maxval,
Chris@189 25 QString unit) :
Chris@189 26 m_minpos(minpos),
Chris@189 27 m_maxpos(maxpos),
Chris@189 28 m_minval(minval),
Chris@189 29 m_maxval(maxval),
Chris@189 30 m_unit(unit)
Chris@189 31 {
Chris@189 32 assert(m_maxval != m_minval);
Chris@189 33 assert(m_maxpos != m_minpos);
Chris@189 34 }
Chris@189 35
Chris@189 36 int
Chris@189 37 LinearRangeMapper::getPositionForValue(float value) const
Chris@189 38 {
Chris@189 39 int position = lrintf(((value - m_minval) / (m_maxval - m_minval))
Chris@189 40 * (m_maxpos - m_minpos));
Chris@189 41 if (position < m_minpos) position = m_minpos;
Chris@189 42 if (position > m_maxpos) position = m_maxpos;
Chris@189 43 return position;
Chris@189 44 }
Chris@189 45
Chris@189 46 float
Chris@189 47 LinearRangeMapper::getValueForPosition(int position) const
Chris@189 48 {
Chris@189 49 float value = ((float(position - m_minpos) / float(m_maxpos - m_minpos))
Chris@189 50 * (m_maxval - m_minval));
Chris@189 51 if (value < m_minval) value = m_minval;
Chris@189 52 if (value > m_maxval) value = m_maxval;
Chris@189 53 return value;
Chris@189 54 }
Chris@189 55
Chris@189 56 LogRangeMapper::LogRangeMapper(int minpos, int maxpos,
Chris@189 57 float ratio, float minlog,
Chris@189 58 QString unit) :
Chris@189 59 m_minpos(minpos),
Chris@189 60 m_maxpos(maxpos),
Chris@189 61 m_minlog(minlog),
Chris@189 62 m_unit(unit)
Chris@189 63 {
Chris@189 64 assert(m_maxpos != m_minpos);
Chris@189 65
Chris@189 66 m_maxlog = (m_maxpos - m_minpos) / m_ratio + m_minlog;
Chris@189 67 }
Chris@189 68
Chris@189 69 int
Chris@189 70 LogRangeMapper::getPositionForValue(float value) const
Chris@189 71 {
Chris@189 72 float mapped = m_ratio * log10(value);
Chris@189 73 int position = lrintf(((mapped - m_minlog) / (m_maxlog - m_minlog))
Chris@189 74 * (m_maxpos - m_minpos));
Chris@189 75 if (position < m_minpos) position = m_minpos;
Chris@189 76 if (position > m_maxpos) position = m_maxpos;
Chris@189 77 std::cerr << "LogRangeMapper::getPositionForValue: " << value << " -> "
Chris@189 78 << position << std::endl;
Chris@189 79 return position;
Chris@189 80 }
Chris@189 81
Chris@189 82 float
Chris@189 83 LogRangeMapper::getValueForPosition(int position) const
Chris@189 84 {
Chris@189 85 float value = powf(10, (position - m_minpos) / m_ratio + m_minlog);
Chris@189 86 std::cerr << "LogRangeMapper::getPositionForValue: " << position << " -> "
Chris@189 87 << value << std::endl;
Chris@189 88 return value;
Chris@189 89 }
Chris@189 90