comparison 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
comparison
equal deleted inserted replaced
188:f86b74d1b143 189:0703252c9fe8
1 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */
2
3 /*
4 Sonic Visualiser
5 An audio file viewer and annotation editor.
6 Centre for Digital Music, Queen Mary, University of London.
7 This file copyright 2006 Chris Cannam.
8
9 This program is free software; you can redistribute it and/or
10 modify it under the terms of the GNU General Public License as
11 published by the Free Software Foundation; either version 2 of the
12 License, or (at your option) any later version. See the file
13 COPYING included with this distribution for more information.
14 */
15
16 #include "RangeMapper.h"
17
18 #include <cassert>
19 #include <cmath>
20
21 #include <iostream>
22
23 LinearRangeMapper::LinearRangeMapper(int minpos, int maxpos,
24 float minval, float maxval,
25 QString unit) :
26 m_minpos(minpos),
27 m_maxpos(maxpos),
28 m_minval(minval),
29 m_maxval(maxval),
30 m_unit(unit)
31 {
32 assert(m_maxval != m_minval);
33 assert(m_maxpos != m_minpos);
34 }
35
36 int
37 LinearRangeMapper::getPositionForValue(float value) const
38 {
39 int position = lrintf(((value - m_minval) / (m_maxval - m_minval))
40 * (m_maxpos - m_minpos));
41 if (position < m_minpos) position = m_minpos;
42 if (position > m_maxpos) position = m_maxpos;
43 return position;
44 }
45
46 float
47 LinearRangeMapper::getValueForPosition(int position) const
48 {
49 float value = ((float(position - m_minpos) / float(m_maxpos - m_minpos))
50 * (m_maxval - m_minval));
51 if (value < m_minval) value = m_minval;
52 if (value > m_maxval) value = m_maxval;
53 return value;
54 }
55
56 LogRangeMapper::LogRangeMapper(int minpos, int maxpos,
57 float ratio, float minlog,
58 QString unit) :
59 m_minpos(minpos),
60 m_maxpos(maxpos),
61 m_minlog(minlog),
62 m_unit(unit)
63 {
64 assert(m_maxpos != m_minpos);
65
66 m_maxlog = (m_maxpos - m_minpos) / m_ratio + m_minlog;
67 }
68
69 int
70 LogRangeMapper::getPositionForValue(float value) const
71 {
72 float mapped = m_ratio * log10(value);
73 int position = lrintf(((mapped - m_minlog) / (m_maxlog - m_minlog))
74 * (m_maxpos - m_minpos));
75 if (position < m_minpos) position = m_minpos;
76 if (position > m_maxpos) position = m_maxpos;
77 std::cerr << "LogRangeMapper::getPositionForValue: " << value << " -> "
78 << position << std::endl;
79 return position;
80 }
81
82 float
83 LogRangeMapper::getValueForPosition(int position) const
84 {
85 float value = powf(10, (position - m_minpos) / m_ratio + m_minlog);
86 std::cerr << "LogRangeMapper::getPositionForValue: " << position << " -> "
87 << value << std::endl;
88 return value;
89 }
90