File: ColourScale.cpp

package info (click to toggle)
sonic-visualiser 5.2.1-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 24,744 kB
  • sloc: cpp: 158,888; ansic: 11,920; sh: 1,785; makefile: 517; xml: 64; perl: 31
file content (182 lines) | stat: -rw-r--r-- 5,503 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
/* -*- c-basic-offset: 4 indent-tabs-mode: nil -*-  vi:set ts=8 sts=4 sw=4: */

/*
    Sonic Visualiser
    An audio file viewer and annotation editor.
    Centre for Digital Music, Queen Mary, University of London.
    This file copyright 2006-2016 Chris Cannam and QMUL.
    
    This program is free software; you can redistribute it and/or
    modify it under the terms of the GNU General Public License as
    published by the Free Software Foundation; either version 2 of the
    License, or (at your option) any later version.  See the file
    COPYING included with this distribution for more information.
*/

#include "ColourScale.h"

#include "base/AudioLevel.h"
#include "base/LogRange.h"

#include <cmath>
#include <iostream>

using namespace std;

namespace sv {

int ColourScale::m_maxPixel = 255;

ColourScale::ColourScale(Parameters parameters) :
    m_params(parameters),
    m_mapper(m_params.colourMap, m_params.inverted, 1.f, double(m_maxPixel))
{
    if (m_params.minValue >= m_params.maxValue) {
        SVCERR << "ERROR: ColourScale::ColourScale: minValue = "
             << m_params.minValue << ", maxValue = " << m_params.maxValue << endl;
        throw std::logic_error("maxValue must be greater than minValue");
    }

    m_mappedMin = m_params.minValue;
    m_mappedMax = m_params.maxValue;

    if (m_mappedMin < m_params.threshold) {
        m_mappedMin = m_params.threshold;
    }
    
    if (m_params.scaleType == ColourScaleType::Log) {

        // When used in e.g. spectrogram, we have a range with a min
        // value of zero. The LogRange converts that to a threshold
        // value of -10, so for a range of e.g. (0,1) we end up with
        // (-10,0) as the mapped range.
        // 
        // But in other contexts we could end up with a mapped range
        // much larger than that if we have a small non-zero minimum
        // value (less than 1e-10), or a particularly large
        // maximum. That's unlikely to give us good results, so let's
        // insist that the mapped log range has no more than 10
        // difference between min and max, to match the behaviour when
        // min == 0 at the input.
        //
        double threshold = -10.0;
        LogRange::mapRange(m_mappedMin, m_mappedMax, threshold);
        if (m_mappedMin < m_mappedMax + threshold) {
            m_mappedMin = m_mappedMax + threshold;
        }
        
    } else if (m_params.scaleType == ColourScaleType::PlusMinusOne) {
        
        m_mappedMin = -1.0;
        m_mappedMax =  1.0;

    } else if (m_params.scaleType == ColourScaleType::Absolute) {

        m_mappedMin = fabs(m_mappedMin);
        m_mappedMax = fabs(m_mappedMax);
        if (m_mappedMin >= m_mappedMax) {
            std::swap(m_mappedMin, m_mappedMax);
        }
    }

    if (m_mappedMin >= m_mappedMax) {
        SVCERR << "ERROR: ColourScale::ColourScale: minValue = " << m_params.minValue
             << ", maxValue = " << m_params.maxValue
             << ", threshold = " << m_params.threshold
             << ", scale = " << int(m_params.scaleType)
             << " resulting in mapped minValue = " << m_mappedMin
             << ", mapped maxValue = " << m_mappedMax << endl;
        throw std::logic_error("maxValue must be greater than minValue [after mapping]");
    }
}

ColourScale::~ColourScale()
{
}

ColourScaleType
ColourScale::getScale() const
{
    return m_params.scaleType;
}

int
ColourScale::getPixelGeneral(double value) const
{
    double maxPixF = m_maxPixel;

    if (m_params.scaleType == ColourScaleType::Phase) {
        double half = (maxPixF - 1.f) / 2.f;
        int pixel = 1 + int((value * half) / M_PI + half);
//        SVCERR << "phase = " << value << " pixel = " << pixel << endl;
        return pixel;
    }
    
    value *= m_params.gain;
    
    if (value < m_params.threshold) return 0;

    double mapped = value;

    if (m_params.scaleType == ColourScaleType::Log) {
        mapped = LogRange::map(value);
    } else if (m_params.scaleType == ColourScaleType::PlusMinusOne) {
        if (mapped < -1.f) mapped = -1.f;
        if (mapped > 1.f) mapped = 1.f;
    } else if (m_params.scaleType == ColourScaleType::Absolute) {
        if (mapped < 0.f) mapped = -mapped;
    }

    mapped *= m_params.multiple;
    
    if (mapped < m_mappedMin) {
        mapped = m_mappedMin;
    }
    if (mapped > m_mappedMax) {
        mapped = m_mappedMax;
    }

    double proportion = (mapped - m_mappedMin) / (m_mappedMax - m_mappedMin);

    int pixel = 0;

    if (m_params.scaleType == ColourScaleType::Meter) {
        pixel = AudioLevel::voltage_to_fader(proportion, m_maxPixel-1,
                                             AudioLevel::Scale::Preview) + 1;
    } else {
        pixel = int(proportion * maxPixF) + 1;
    }

    if (pixel < 0) {
        pixel = 0;
    }
    if (pixel > m_maxPixel) {
        pixel = m_maxPixel;
    }
    return pixel;
}

QColor
ColourScale::getColourForPixel(int pixel, int rotation) const
{
    if (pixel < 0) {
        pixel = 0;
    }
    if (pixel > m_maxPixel) {
        pixel = m_maxPixel;
    }
    if (pixel == 0) {
        if (m_mapper.hasLightBackground()) {
            return Qt::white;
        } else {
            return Qt::black;
        }
    } else {
        int target = int(pixel) + rotation;
        while (target < 1) target += m_maxPixel;
        while (target > m_maxPixel) target -= m_maxPixel;
        return m_mapper.map(double(target));
    }
}
} // end namespace sv