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@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@190
|
39 int position = m_minpos +
|
Chris@190
|
40 lrintf(((value - m_minval) / (m_maxval - m_minval))
|
Chris@190
|
41 * (m_maxpos - m_minpos));
|
Chris@189
|
42 if (position < m_minpos) position = m_minpos;
|
Chris@189
|
43 if (position > m_maxpos) position = m_maxpos;
|
Chris@241
|
44 // std::cerr << "LinearRangeMapper::getPositionForValue: " << value << " -> "
|
Chris@241
|
45 // << position << " (minpos " << m_minpos << ", maxpos " << m_maxpos << ", minval " << m_minval << ", maxval " << m_maxval << ")" << std::endl;
|
Chris@189
|
46 return position;
|
Chris@189
|
47 }
|
Chris@189
|
48
|
Chris@189
|
49 float
|
Chris@189
|
50 LinearRangeMapper::getValueForPosition(int position) const
|
Chris@189
|
51 {
|
Chris@190
|
52 float value = m_minval +
|
Chris@190
|
53 ((float(position - m_minpos) / float(m_maxpos - m_minpos))
|
Chris@190
|
54 * (m_maxval - m_minval));
|
Chris@189
|
55 if (value < m_minval) value = m_minval;
|
Chris@189
|
56 if (value > m_maxval) value = m_maxval;
|
Chris@241
|
57 // std::cerr << "LinearRangeMapper::getValueForPosition: " << position << " -> "
|
Chris@241
|
58 // << value << " (minpos " << m_minpos << ", maxpos " << m_maxpos << ", minval " << m_minval << ", maxval " << m_maxval << ")" << std::endl;
|
Chris@189
|
59 return value;
|
Chris@189
|
60 }
|
Chris@189
|
61
|
Chris@189
|
62 LogRangeMapper::LogRangeMapper(int minpos, int maxpos,
|
Chris@384
|
63 float minval, float maxval,
|
Chris@189
|
64 QString unit) :
|
Chris@189
|
65 m_minpos(minpos),
|
Chris@189
|
66 m_maxpos(maxpos),
|
Chris@189
|
67 m_unit(unit)
|
Chris@189
|
68 {
|
Chris@384
|
69 convertMinMax(minpos, maxpos, minval, maxval, m_minlog, m_ratio);
|
Chris@384
|
70
|
Chris@384
|
71 std::cerr << "LogRangeMapper: minpos " << minpos << ", maxpos "
|
Chris@384
|
72 << maxpos << ", minval " << minval << ", maxval "
|
Chris@384
|
73 << maxval << ", minlog " << m_minlog << ", ratio " << m_ratio
|
Chris@384
|
74 << ", unit " << unit.toStdString() << std::endl;
|
Chris@384
|
75
|
Chris@189
|
76 assert(m_maxpos != m_minpos);
|
Chris@189
|
77
|
Chris@189
|
78 m_maxlog = (m_maxpos - m_minpos) / m_ratio + m_minlog;
|
Chris@189
|
79 }
|
Chris@189
|
80
|
Chris@384
|
81 void
|
Chris@384
|
82 LogRangeMapper::convertMinMax(int minpos, int maxpos,
|
Chris@384
|
83 float minval, float maxval,
|
Chris@384
|
84 float &minlog, float &ratio)
|
Chris@384
|
85 {
|
Chris@384
|
86 static float thresh = powf(10, -10);
|
Chris@384
|
87 if (minval < thresh) minval = thresh;
|
Chris@384
|
88 minlog = log10f(minval);
|
Chris@384
|
89 ratio = (maxpos - minpos) / (log10f(maxval) - minlog);
|
Chris@384
|
90 }
|
Chris@384
|
91
|
Chris@384
|
92 void
|
Chris@384
|
93 LogRangeMapper::convertRatioMinLog(float ratio, float minlog,
|
Chris@384
|
94 int minpos, int maxpos,
|
Chris@384
|
95 float &minval, float &maxval)
|
Chris@384
|
96 {
|
Chris@384
|
97 minval = powf(10, minlog);
|
Chris@384
|
98 maxval = powf(10, (maxpos - minpos) / ratio + minlog);
|
Chris@384
|
99 }
|
Chris@384
|
100
|
Chris@189
|
101 int
|
Chris@189
|
102 LogRangeMapper::getPositionForValue(float value) const
|
Chris@189
|
103 {
|
Chris@384
|
104 int position = (log10(value) - m_minlog) * m_ratio + m_minpos;
|
Chris@189
|
105 if (position < m_minpos) position = m_minpos;
|
Chris@189
|
106 if (position > m_maxpos) position = m_maxpos;
|
Chris@384
|
107 std::cerr << "LogRangeMapper::getPositionForValue: " << value << " -> "
|
Chris@384
|
108 << position << " (minpos " << m_minpos << ", maxpos " << m_maxpos << ", ratio " << m_ratio << ", minlog " << m_minlog << ")" << std::endl;
|
Chris@189
|
109 return position;
|
Chris@189
|
110 }
|
Chris@189
|
111
|
Chris@189
|
112 float
|
Chris@189
|
113 LogRangeMapper::getValueForPosition(int position) const
|
Chris@189
|
114 {
|
Chris@189
|
115 float value = powf(10, (position - m_minpos) / m_ratio + m_minlog);
|
Chris@384
|
116 std::cerr << "LogRangeMapper::getValueForPosition: " << position << " -> "
|
Chris@384
|
117 << value << " (minpos " << m_minpos << ", maxpos " << m_maxpos << ", ratio " << m_ratio << ", minlog " << m_minlog << ")" << std::endl;
|
Chris@189
|
118 return value;
|
Chris@189
|
119 }
|
Chris@189
|
120
|