-
Notifications
You must be signed in to change notification settings - Fork 2
/
renderer.h
212 lines (180 loc) · 5.16 KB
/
renderer.h
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
/*
* Copyright (C) 2019 - 2023 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>
extern "C" {
#include <cairo.h>
}
namespace Sndspec {
struct Rgb
{
double red;
double green;
double blue;
};
class Renderer
{
public:
Renderer(int width, int height);
~Renderer();
void renderSpectrogram(const Parameters& parameters, const SpectrogramResults<double>& spectrogramData);
void renderSpectrum(const Parameters& parameters, const std::vector<std::vector<double> >& spectrumData);
void makeNegativeImage();
bool writeToFile(const std::string &filename);
void clear();
// setters
void setHeatMapPalette(const std::vector<int32_t> &value);
void setNyquist(int value); // required for frequency axis
void setFreqStep(int 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);
// getters
std::vector<int32_t> getHeatMapPalette() const;
int getPlotWidth() const;
int getPlotHeight() const;
int getNyquist() const;
int 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;
private:
void drawBorder();
void drawSpectrogramGrid();
void drawSpectrogramTickmarks();
void drawSpectrogramText();
void drawSpectrogramHeatMap(bool linearMag = false);
void drawSpectrumGrid();
void drawSpectrumTickmarks(bool linearMag = false);
void drawSpectrumText();
// 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
int nyquist{};
int freqStep{};
int numTimeDivs{5};
double startTime{};
double finishTime{};
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};
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,
// 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