-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathrenderer.h
More file actions
263 lines (222 loc) · 6.75 KB
/
Copy pathrenderer.h
File metadata and controls
263 lines (222 loc) · 6.75 KB
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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
/*
* Copyright (C) 2019 - 2026 Judd Niemann - All Rights Reserved.
* You may use, distribute and modify this code under the
* terms of the GNU Lesser General Public License, version 2.1
*
* You should have received a copy of GNU Lesser General Public License v2.1
* with this file. If not, please refer to: https://github.com/jniemann66/ReSampler
*/
#ifndef RENDERER_H
#define RENDERER_H
#include "parameters.h"
#include "spectrogram.h"
#include <cairo.h>
#include <cstdint>
#include <iomanip>
#include <map>
#include <sstream>
#include <string>
extern "C" {
#include <cairo.h>
}
namespace Sndspec {
struct Rgb
{
double red;
double green;
double blue;
};
struct Marker
{
size_t index{0};
double freq{0.0};
double mag{0.0};
double x{0.0};
double y{0.0};
Rgb color;
bool visible{true};
std::string displayText() const
{
std::ostringstream oss;
oss << std::fixed << std::setprecision(1) << freq /*<< " Hz"*/;
return oss.str();
}
};
class Renderer
{
public:
Renderer(int width, int height);
~Renderer();
// spectrogram
void renderSpectrogram(const Parameters& parameters, const SpectrogramResults<double>& spectrogramData);
// renderSpectrum() : returns <final plotted values, vertical scaling factor used>
std::pair<std::vector<std::vector<double>>, double> renderSpectrum(const Parameters& parameters, const std::vector<std::vector<double>>& spectrumData);
// plot the actual window function
void renderWindowFunction(const Parameters& parameters, const std::vector<double> &data);
std::vector<Marker> getTopNFrequencyMarkers(const Parameters& parameters, const std::vector<double>& spectrumData, double vScaling, size_t n, int channel);
void drawMarkers(const std::vector<Marker>& markers);
void makeNegativeImage();
bool writeToFile(const std::string &filename);
void clear();
// enum to control the formatting of text on Horiz axis tickmarks
enum FreqAxisFormat
{
FreqAxisFormat_ZeroToNyquist,
FreqAxisFormat_PlusMinusNormalisedFreq
};
// setters
void setHeatMapPalette(const std::vector<int32_t> &value);
void setNyquist(double value); // required for frequency axis
void setFreqStep(double value); // required for frequency axis
void setNumTimeDivs(int value); // required for time axis
void setStartTime(double value); // required for time axis
void setFinishTime(double value); // required for time axis
void setInputFilename(const std::string &value); // required to display input filename
void setDynRange(double value); // required for showing heatmap dB values
void setTitle(const std::string &value);
void setHorizAxisLabel(const std::string &value);
void setVertAxisLabel(const std::string &value);
void setChannelsEnabled(const std::vector<bool> &value);
void setFreqAxisFormat(FreqAxisFormat newFreqAxisFormat);
void setHorizZoomFactor(double newHorizZoomFactor);
// getters
std::vector<int32_t> getHeatMapPalette() const;
int getPlotWidth() const;
int getPlotHeight() const;
double getNyquist() const;
double getFreqStep() const;
int getNumTimeDivs() const;
double getStartTime() const;
double getFinishTime() const;
std::string getInputFilename() const;
std::string getTitle() const;
std::string getHorizAxisLabel() const;
std::string getVertAxisLabel() const;
double getDynRange() const;
std::vector<uint32_t> getPixelBuffer() const;
std::vector<bool> getChannelsEnabled() const;
FreqAxisFormat getFreqAxisFormat() const;
double getHorizZoomFactor() const;
private:
void drawBorder();
void drawSpectrogramGrid();
void drawSpectrogramTickmarks();
void drawSpectrogramText();
void drawSpectrogramHeatMap(bool linearMag = false);
void drawSpectrumGrid();
void drawSpectrumTickmarks(bool linearMag = false);
void drawSpectrumText();
// typedef for map of <magnitude, index> with largest peak-value first
using LocalPeakData_t = std::map<double, size_t, std::greater<double>>;
LocalPeakData_t getRankedLocalMaxima(const std::vector<double>& data);
// vector indicating which channels to plot or not plot
std::vector<bool> channelsEnabled;
// dimensions of whole image
int width;
int height;
// properties required for labelling the chart
double nyquist;
double freqStep;
int numTimeDivs{5};
double startTime{0.0};
double finishTime{0.0};
std::string title{"Spectrogram"};
std::string inputFilename;
std::string channelMode;
std::string horizAxisLabel{"Time (s)"};
std::string vertAxisLabel{"Frequency (Hz)"};
std::string windowFunctionLabel;
bool showWindowFunctionLabel{false};
FreqAxisFormat freqAxisStyle{FreqAxisFormat_ZeroToNyquist};
double horizZoomFactor{1.0};
double dynRange{};
// font sizes
const double fontSizeNormal{13.0};
const double fontSizeHeading{16.0};
// heatmap origin
const int hmOriginX{10};
int hmOriginY; // depends on marginTop
// heatmap width
const int hmWidth{10};
int hmLabelWidth{};
// plot origin
int plotOriginX;
int plotOriginY;
// plot dimensions
int plotWidth;
int plotHeight;
// tick Width
const double tickWidth{10.0};
// margins
double marginLeft{};
double marginTop{};
double marginRight{};
double marginBottom{};
std::vector<uint32_t> pixelBuffer;
int stride32;
cairo_surface_t* surface;
cairo_t* cr;
std::vector<int32_t> heatMapPalette {
0x00ffffff,
0x00f0fed8,
0x00f2fbb9,
0x00fdf58f,
0x00fdc866,
0x00fc9042,
0x00fc4b20,
0x00ed1c29,
0x00d60340,
0x00b70365,
0x009d037a,
0x007a037e,
0x0050026e,
0x002d0259,
0x00130246,
0x00010335,
0x00010325,
0x00010213,
0x00000000
};
// todo: how to handle other palettes ?
// (experimental)
// std::vector<int32_t> heatMapPalette {
// 0x00440154,
// 0x00481567,
// 0x00482677,
// 0x00453781,
// 0x00404788,
// 0x0039568C,
// 0x0033638D,
// 0x002D708E,
// 0x00287D8E,
// 0x00238A8D,
// 0x001F968B,
// 0x0020A387,
// 0x0029AF7F,
// 0x003CBB75,
// 0x0055C667,
// 0x0073D055,
// 0x0095D840, size_t i = 0;
// 0x00B8DE29,
// 0x00DCE319,
// 0x00FDE725
// };
std::vector<Rgb> spectrumChannelColors
{
{0.5, 1.0, 1.0},
{1.0, 0.5, 1.0},
{1.0, 1.0, 0.5},
{1.0, 0.5, 0.5},
{0.5, 1.0, 0.5},
{0.5, 0.5, 1.0}
};
void setMargins();
// resolveEnabledChannels() : determines which channels should be enabled / disabled based on:
// channel mode, parameters, and existing enabled / disabled state of each channel
// sets Renderer::enabledChannels accordingly
// also sets the description string Renderer::channelMode
void resolveEnabledChannels(const Parameters ¶meters, int numChannels);
static std::string formatTimeRange(const double startSecs, const double finishSecs);
};
} // namespace Sndspec
#endif // RENDERER_H