comparison layer/ColourScale.cpp @ 1137:4e7ed3252d80 spectrogram-minor-refactor

Re-implement dB^2 (log-power) spectrogram setting
author Chris Cannam
date Thu, 04 Aug 2016 11:26:11 +0100
parents 371320c9f8d9
children 998e31e92dbe
comparison
equal deleted inserted replaced
1136:9ff838a64461 1137:4e7ed3252d80
40 40
41 if (m_mappedMin < m_params.threshold) { 41 if (m_mappedMin < m_params.threshold) {
42 m_mappedMin = m_params.threshold; 42 m_mappedMin = m_params.threshold;
43 } 43 }
44 44
45 if (m_params.scale == ColourScaleType::Log) { 45 if (m_params.scaleType == ColourScaleType::Log) {
46 46
47 LogRange::mapRange(m_mappedMin, m_mappedMax); 47 LogRange::mapRange(m_mappedMin, m_mappedMax);
48 48
49 } else if (m_params.scale == ColourScaleType::PlusMinusOne) { 49 } else if (m_params.scaleType == ColourScaleType::PlusMinusOne) {
50 50
51 m_mappedMin = -1.0; 51 m_mappedMin = -1.0;
52 m_mappedMax = 1.0; 52 m_mappedMax = 1.0;
53 53
54 } else if (m_params.scale == ColourScaleType::Absolute) { 54 } else if (m_params.scaleType == ColourScaleType::Absolute) {
55 55
56 m_mappedMin = fabs(m_mappedMin); 56 m_mappedMin = fabs(m_mappedMin);
57 m_mappedMax = fabs(m_mappedMax); 57 m_mappedMax = fabs(m_mappedMax);
58 if (m_mappedMin >= m_mappedMax) { 58 if (m_mappedMin >= m_mappedMax) {
59 std::swap(m_mappedMin, m_mappedMax); 59 std::swap(m_mappedMin, m_mappedMax);
62 62
63 if (m_mappedMin >= m_mappedMax) { 63 if (m_mappedMin >= m_mappedMax) {
64 cerr << "ERROR: ColourScale::ColourScale: minValue = " << m_params.minValue 64 cerr << "ERROR: ColourScale::ColourScale: minValue = " << m_params.minValue
65 << ", maxValue = " << m_params.maxValue 65 << ", maxValue = " << m_params.maxValue
66 << ", threshold = " << m_params.threshold 66 << ", threshold = " << m_params.threshold
67 << ", scale = " << int(m_params.scale) 67 << ", scale = " << int(m_params.scaleType)
68 << " resulting in mapped minValue = " << m_mappedMin 68 << " resulting in mapped minValue = " << m_mappedMin
69 << ", mapped maxValue = " << m_mappedMax << endl; 69 << ", mapped maxValue = " << m_mappedMax << endl;
70 throw std::logic_error("maxValue must be greater than minValue [after mapping]"); 70 throw std::logic_error("maxValue must be greater than minValue [after mapping]");
71 } 71 }
72 } 72 }
76 } 76 }
77 77
78 ColourScaleType 78 ColourScaleType
79 ColourScale::getScale() const 79 ColourScale::getScale() const
80 { 80 {
81 return m_params.scale; 81 return m_params.scaleType;
82 } 82 }
83 83
84 int 84 int
85 ColourScale::getPixel(double value) const 85 ColourScale::getPixel(double value) const
86 { 86 {
87 double maxPixF = m_maxPixel; 87 double maxPixF = m_maxPixel;
88 88
89 if (m_params.scale == ColourScaleType::Phase) { 89 if (m_params.scaleType == ColourScaleType::Phase) {
90 double half = (maxPixF - 1.f) / 2.f; 90 double half = (maxPixF - 1.f) / 2.f;
91 return 1 + int((value * half) / M_PI + half); 91 return 1 + int((value * half) / M_PI + half);
92 } 92 }
93 93
94 value *= m_params.gain; 94 value *= m_params.gain;
95 95
96 // value = pow(value, m_params.multiple);
97
96 if (value < m_params.threshold) return 0; 98 if (value < m_params.threshold) return 0;
97 99
98 double mapped = value; 100 double mapped = value;
99 101
100 if (m_params.scale == ColourScaleType::Log) { 102 if (m_params.scaleType == ColourScaleType::Log) {
101 mapped = LogRange::map(value); 103 mapped = LogRange::map(value);
102 } else if (m_params.scale == ColourScaleType::PlusMinusOne) { 104 } else if (m_params.scaleType == ColourScaleType::PlusMinusOne) {
103 if (mapped < -1.f) mapped = -1.f; 105 if (mapped < -1.f) mapped = -1.f;
104 if (mapped > 1.f) mapped = 1.f; 106 if (mapped > 1.f) mapped = 1.f;
105 } else if (m_params.scale == ColourScaleType::Absolute) { 107 } else if (m_params.scaleType == ColourScaleType::Absolute) {
106 if (mapped < 0.f) mapped = -mapped; 108 if (mapped < 0.f) mapped = -mapped;
107 } 109 }
108 110
111 mapped *= m_params.multiple;
112
109 if (mapped < m_mappedMin) { 113 if (mapped < m_mappedMin) {
110 mapped = m_mappedMin; 114 mapped = m_mappedMin;
111 } 115 }
112 if (mapped > m_mappedMax) { 116 if (mapped > m_mappedMax) {
113 mapped = m_mappedMax; 117 mapped = m_mappedMax;
115 119
116 double proportion = (mapped - m_mappedMin) / (m_mappedMax - m_mappedMin); 120 double proportion = (mapped - m_mappedMin) / (m_mappedMax - m_mappedMin);
117 121
118 int pixel = 0; 122 int pixel = 0;
119 123
120 if (m_params.scale == ColourScaleType::Meter) { 124 if (m_params.scaleType == ColourScaleType::Meter) {
121 pixel = AudioLevel::multiplier_to_preview(proportion, m_maxPixel-1) + 1; 125 pixel = AudioLevel::multiplier_to_preview(proportion, m_maxPixel-1) + 1;
122 } else { 126 } else {
123 pixel = int(proportion * maxPixF) + 1; 127 pixel = int(proportion * maxPixF) + 1;
124 } 128 }
125 129