annotate layer/ColourScale.cpp @ 1072:76b50b48e1e4 spectrogram-minor-refactor

Comments
author Chris Cannam
date Mon, 27 Jun 2016 16:01:42 +0100
parents 65b183494331
children 7ebfb61b1701
rev   line source
Chris@1068 1 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */
Chris@1068 2
Chris@1068 3 /*
Chris@1068 4 Sonic Visualiser
Chris@1068 5 An audio file viewer and annotation editor.
Chris@1068 6 Centre for Digital Music, Queen Mary, University of London.
Chris@1068 7 This file copyright 2006-2016 Chris Cannam and QMUL.
Chris@1068 8
Chris@1068 9 This program is free software; you can redistribute it and/or
Chris@1068 10 modify it under the terms of the GNU General Public License as
Chris@1068 11 published by the Free Software Foundation; either version 2 of the
Chris@1068 12 License, or (at your option) any later version. See the file
Chris@1068 13 COPYING included with this distribution for more information.
Chris@1068 14 */
Chris@1068 15
Chris@1068 16 #include "ColourScale.h"
Chris@1068 17
Chris@1068 18 #include "base/AudioLevel.h"
Chris@1068 19 #include "base/LogRange.h"
Chris@1068 20
Chris@1068 21 #include <cmath>
Chris@1068 22
Chris@1068 23 int ColourScale::m_maxPixel = 255;
Chris@1068 24
Chris@1070 25 ColourScale::ColourScale(Parameters parameters) :
Chris@1070 26 m_params(parameters),
Chris@1070 27 m_mapper(m_params.colourMap, 1.f, double(m_maxPixel))
Chris@1068 28 {
Chris@1070 29 if (m_params.minValue >= m_params.maxValue) {
Chris@1068 30 throw std::logic_error("maxValue must be greater than minValue");
Chris@1068 31 }
Chris@1068 32
Chris@1070 33 m_mappedMin = m_params.minValue;
Chris@1070 34 m_mappedMax = m_params.maxValue;
Chris@1068 35
Chris@1070 36 if (m_params.scale == LogColourScale) {
Chris@1068 37
Chris@1068 38 LogRange::mapRange(m_mappedMin, m_mappedMax);
Chris@1068 39
Chris@1070 40 } else if (m_params.scale == PlusMinusOneScale) {
Chris@1068 41
Chris@1068 42 m_mappedMin = -1.0;
Chris@1068 43 m_mappedMax = 1.0;
Chris@1068 44
Chris@1070 45 } else if (m_params.scale == AbsoluteScale) {
Chris@1068 46
Chris@1068 47 m_mappedMin = fabs(m_mappedMin);
Chris@1068 48 m_mappedMax = fabs(m_mappedMax);
Chris@1068 49 if (m_mappedMin >= m_mappedMax) {
Chris@1068 50 std::swap(m_mappedMin, m_mappedMax);
Chris@1068 51 }
Chris@1068 52 }
Chris@1068 53
Chris@1068 54 if (m_mappedMin >= m_mappedMax) {
Chris@1068 55 throw std::logic_error("maxValue must be greater than minValue [after mapping]");
Chris@1068 56 }
Chris@1068 57 }
Chris@1068 58
Chris@1071 59 ColourScale::~ColourScale()
Chris@1071 60 {
Chris@1071 61 }
Chris@1071 62
Chris@1068 63 int
Chris@1068 64 ColourScale::getPixel(double value)
Chris@1068 65 {
Chris@1068 66 double maxPixF = m_maxPixel;
Chris@1068 67
Chris@1070 68 if (m_params.scale == PhaseColourScale) {
Chris@1068 69 double half = (maxPixF - 1.f) / 2.f;
Chris@1068 70 return 1 + int((value * half) / M_PI + half);
Chris@1068 71 }
Chris@1068 72
Chris@1070 73 value *= m_params.gain;
Chris@1068 74
Chris@1070 75 if (value < m_params.threshold) return 0;
Chris@1068 76
Chris@1068 77 double mapped = value;
Chris@1068 78
Chris@1070 79 if (m_params.scale == LogColourScale) {
Chris@1068 80 mapped = LogRange::map(value);
Chris@1070 81 } else if (m_params.scale == PlusMinusOneScale) {
Chris@1068 82 if (mapped < -1.f) mapped = -1.f;
Chris@1068 83 if (mapped > 1.f) mapped = 1.f;
Chris@1070 84 } else if (m_params.scale == AbsoluteScale) {
Chris@1068 85 if (mapped < 0.f) mapped = -mapped;
Chris@1068 86 }
Chris@1068 87
Chris@1068 88 if (mapped < m_mappedMin) {
Chris@1068 89 mapped = m_mappedMin;
Chris@1068 90 }
Chris@1068 91 if (mapped > m_mappedMax) {
Chris@1068 92 mapped = m_mappedMax;
Chris@1068 93 }
Chris@1068 94
Chris@1068 95 double proportion = (mapped - m_mappedMin) / (m_mappedMax - m_mappedMin);
Chris@1068 96
Chris@1068 97 int pixel = 0;
Chris@1068 98
Chris@1070 99 if (m_params.scale == MeterColourScale) {
Chris@1068 100 pixel = AudioLevel::multiplier_to_preview(proportion, m_maxPixel-1) + 1;
Chris@1068 101 } else {
Chris@1068 102 pixel = int(proportion * maxPixF) + 1;
Chris@1068 103 }
Chris@1068 104
Chris@1070 105 if (pixel < 0) {
Chris@1070 106 pixel = 0;
Chris@1070 107 }
Chris@1070 108 if (pixel > m_maxPixel) {
Chris@1070 109 pixel = m_maxPixel;
Chris@1070 110 }
Chris@1068 111 return pixel;
Chris@1068 112 }
Chris@1068 113
Chris@1068 114 QColor
Chris@1068 115 ColourScale::getColourForPixel(int pixel, int rotation)
Chris@1068 116 {
Chris@1068 117 if (pixel < 0) {
Chris@1068 118 pixel = 0;
Chris@1068 119 }
Chris@1068 120 if (pixel > m_maxPixel) {
Chris@1068 121 pixel = m_maxPixel;
Chris@1068 122 }
Chris@1068 123 if (pixel == 0) {
Chris@1068 124 if (m_mapper.hasLightBackground()) {
Chris@1068 125 return Qt::white;
Chris@1068 126 } else {
Chris@1068 127 return Qt::black;
Chris@1068 128 }
Chris@1068 129 } else {
Chris@1068 130 int target = int(pixel) + rotation;
Chris@1068 131 while (target < 1) target += m_maxPixel;
Chris@1068 132 while (target > m_maxPixel) target -= m_maxPixel;
Chris@1068 133 return m_mapper.map(double(target));
Chris@1068 134 }
Chris@1068 135 }