2017-01-02 21:07:43 -05:00
|
|
|
// Copyright (c) Charles J. Cliffe
|
|
|
|
// SPDX-License-Identifier: GPL-2.0+
|
|
|
|
|
2015-08-09 23:00:51 -04:00
|
|
|
#include "SpectrumPanel.h"
|
|
|
|
|
2015-08-10 00:33:31 -04:00
|
|
|
#include <iostream>
|
2015-08-17 21:52:38 -04:00
|
|
|
#include <iomanip>
|
2016-10-20 21:44:33 -04:00
|
|
|
#include "CubicSDR.h"
|
2015-08-10 00:33:31 -04:00
|
|
|
#include "ColorTheme.h"
|
2015-09-09 17:31:55 -04:00
|
|
|
#include "CubicSDRDefs.h"
|
|
|
|
|
|
|
|
SpectrumPanel::SpectrumPanel() {
|
|
|
|
floorValue = 0;
|
|
|
|
ceilValue = 1;
|
2017-01-27 15:38:48 -05:00
|
|
|
showDb = true;
|
|
|
|
useDbOfs = true;
|
2015-09-09 17:31:55 -04:00
|
|
|
fftSize = DEFAULT_FFT_SIZE;
|
|
|
|
bandwidth = DEFAULT_DEMOD_BW;
|
|
|
|
freq = 0;
|
|
|
|
|
2015-08-10 00:33:31 -04:00
|
|
|
setFill(GLPANEL_FILL_GRAD_Y);
|
|
|
|
setFillColor(ThemeMgr::mgr.currentTheme->fftBackground * 2.0, ThemeMgr::mgr.currentTheme->fftBackground);
|
2015-08-17 21:52:38 -04:00
|
|
|
|
|
|
|
dbPanelCeil.setMarginPx(0);
|
|
|
|
dbPanelCeil.setFill(GLPanel::GLPANEL_FILL_GRAD_X);
|
2016-06-01 19:42:34 -04:00
|
|
|
dbPanelCeil.setFillColor(RGBA4f(0.2f,0.2f,0.2f,5.0f), RGBA4f(0.2f,0.2f,0.2f,0.0f));
|
2015-08-17 21:52:38 -04:00
|
|
|
|
|
|
|
dbPanelFloor.setMarginPx(0);
|
|
|
|
dbPanelFloor.setFill(GLPanel::GLPANEL_FILL_GRAD_X);
|
2016-06-01 19:42:34 -04:00
|
|
|
dbPanelFloor.setFillColor(RGBA4f(0.2f,0.2f,0.2f,5.0f), RGBA4f(0.2f,0.2f,0.2f,0.0f));
|
2015-08-10 00:33:31 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-04-04 22:17:30 -04:00
|
|
|
float SpectrumPanel::getFloorValue() const {
|
2015-08-10 00:33:31 -04:00
|
|
|
return floorValue;
|
|
|
|
}
|
|
|
|
|
2021-04-04 22:17:30 -04:00
|
|
|
void SpectrumPanel::setFloorValue(float floorValue_in) {
|
|
|
|
floorValue = floorValue_in;
|
2015-08-10 00:33:31 -04:00
|
|
|
}
|
|
|
|
|
2021-04-04 22:17:30 -04:00
|
|
|
float SpectrumPanel::getCeilValue() const {
|
2015-08-10 00:33:31 -04:00
|
|
|
return ceilValue;
|
|
|
|
}
|
|
|
|
|
2021-04-04 22:17:30 -04:00
|
|
|
void SpectrumPanel::setCeilValue(float ceilValue_in) {
|
|
|
|
ceilValue = ceilValue_in;
|
2015-08-10 00:33:31 -04:00
|
|
|
}
|
|
|
|
|
2021-04-04 22:17:30 -04:00
|
|
|
void SpectrumPanel::setFreq(long long freq_in) {
|
|
|
|
freq = freq_in;
|
2015-08-10 00:33:31 -04:00
|
|
|
}
|
|
|
|
|
2021-04-04 22:17:30 -04:00
|
|
|
long long SpectrumPanel::getFreq() const {
|
2015-08-10 00:33:31 -04:00
|
|
|
return freq;
|
|
|
|
}
|
|
|
|
|
2021-04-04 22:17:30 -04:00
|
|
|
void SpectrumPanel::setBandwidth(long long bandwidth_in) {
|
|
|
|
bandwidth = bandwidth_in;
|
2015-08-10 00:33:31 -04:00
|
|
|
}
|
|
|
|
|
2021-04-04 22:17:30 -04:00
|
|
|
long long SpectrumPanel::getBandwidth() const {
|
2015-08-10 00:33:31 -04:00
|
|
|
return bandwidth;
|
|
|
|
}
|
|
|
|
|
2015-08-24 01:31:37 -04:00
|
|
|
void SpectrumPanel::setFFTSize(int fftSize_in) {
|
|
|
|
this->fftSize = fftSize_in;
|
|
|
|
}
|
|
|
|
|
2021-04-04 22:17:30 -04:00
|
|
|
int SpectrumPanel::getFFTSize() const {
|
2015-08-24 01:31:37 -04:00
|
|
|
return fftSize;
|
|
|
|
}
|
|
|
|
|
2021-04-04 22:17:30 -04:00
|
|
|
void SpectrumPanel::setShowDb(bool showDb_in) {
|
|
|
|
showDb = showDb_in;
|
|
|
|
if (showDb_in) {
|
2015-08-17 21:52:38 -04:00
|
|
|
addChild(&dbPanelCeil);
|
|
|
|
addChild(&dbPanelFloor);
|
|
|
|
} else {
|
|
|
|
removeChild(&dbPanelCeil);
|
|
|
|
removeChild(&dbPanelFloor);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2021-04-04 22:17:30 -04:00
|
|
|
bool SpectrumPanel::getShowDb() const {
|
2015-08-17 21:52:38 -04:00
|
|
|
return showDb;
|
|
|
|
}
|
|
|
|
|
2016-10-20 21:44:33 -04:00
|
|
|
void SpectrumPanel::setUseDBOffset(bool useOfs) {
|
|
|
|
this->useDbOfs = useOfs;
|
|
|
|
}
|
|
|
|
|
2021-04-04 22:17:30 -04:00
|
|
|
bool SpectrumPanel::getUseDBOffset() const {
|
2016-10-20 21:44:33 -04:00
|
|
|
return useDbOfs;
|
|
|
|
}
|
|
|
|
|
2015-08-17 21:52:38 -04:00
|
|
|
|
2021-04-04 22:17:30 -04:00
|
|
|
void SpectrumPanel::setPoints(std::vector<float> &points_in) {
|
|
|
|
points.assign(points_in.begin(), points_in.end());
|
2015-08-10 00:33:31 -04:00
|
|
|
}
|
|
|
|
|
2021-04-04 22:17:30 -04:00
|
|
|
void SpectrumPanel::setPeakPoints(std::vector<float> &points_in) {
|
|
|
|
peak_points.assign(points_in.begin(), points_in.end());
|
2015-12-31 20:44:39 -05:00
|
|
|
}
|
|
|
|
|
2015-08-10 00:33:31 -04:00
|
|
|
|
|
|
|
void SpectrumPanel::drawPanelContents() {
|
|
|
|
glDisable(GL_TEXTURE_2D);
|
|
|
|
|
|
|
|
glEnable(GL_BLEND);
|
|
|
|
glEnable(GL_LINE_SMOOTH);
|
|
|
|
glHint( GL_LINE_SMOOTH_HINT, GL_NICEST );
|
|
|
|
|
2017-05-28 06:28:19 -04:00
|
|
|
glLoadMatrixf((transform * (CubicVR::mat4::translate(-1.0f, -0.75f, 0.0f) * CubicVR::mat4::scale(2.0f, 1.5f, 1.0f))).to_ptr());
|
2015-08-10 00:33:31 -04:00
|
|
|
|
2021-04-04 22:17:30 -04:00
|
|
|
if (!points.empty()) {
|
2015-08-10 00:33:31 -04:00
|
|
|
glBlendFunc(GL_SRC_ALPHA, GL_ONE);
|
2015-08-11 21:49:42 -04:00
|
|
|
double range = ceilValue-floorValue;
|
|
|
|
double ranges[3][4] = { { 90.0, 5000.0, 10.0, 100.0 }, { 20.0, 150.0, 10.0, 10.0 }, { -20.0, 30.0, 10.0, 1.0 } };
|
2015-08-10 00:33:31 -04:00
|
|
|
|
2021-04-04 22:17:30 -04:00
|
|
|
for (auto & i : ranges) {
|
2015-08-11 21:49:42 -04:00
|
|
|
double p = 0;
|
2021-04-04 22:17:30 -04:00
|
|
|
double rangeMin = i[0];
|
|
|
|
double rangeMax = i[1];
|
|
|
|
double rangeTrans = i[2];
|
|
|
|
double rangeStep = i[3];
|
2015-08-10 00:33:31 -04:00
|
|
|
|
|
|
|
if (range >= rangeMin && range <= rangeMax) {
|
2015-08-11 21:49:42 -04:00
|
|
|
double a = 1.0;
|
2015-08-10 00:33:31 -04:00
|
|
|
|
|
|
|
if (range <= rangeMin+rangeTrans) {
|
|
|
|
a *= (range-rangeMin)/rangeTrans;
|
|
|
|
}
|
|
|
|
if (range >= rangeMax-rangeTrans) {
|
|
|
|
a *= (rangeTrans-(range-(rangeMax-rangeTrans)))/rangeTrans;
|
|
|
|
}
|
|
|
|
|
2016-06-01 19:42:34 -04:00
|
|
|
glColor4f(0.12f, 0.12f, 0.12f, a);
|
2015-08-10 00:33:31 -04:00
|
|
|
glBegin(GL_LINES);
|
2015-08-11 21:49:42 -04:00
|
|
|
for (double l = floorValue; l<=ceilValue+rangeStep; l+=rangeStep) {
|
2015-08-10 00:33:31 -04:00
|
|
|
p += rangeStep/range;
|
|
|
|
glVertex2f(0,p); glVertex2f(1,p);
|
|
|
|
}
|
|
|
|
glEnd();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
|
|
|
|
glColor3f(ThemeMgr::mgr.currentTheme->fftLine.r, ThemeMgr::mgr.currentTheme->fftLine.g, ThemeMgr::mgr.currentTheme->fftLine.b);
|
|
|
|
glEnableClientState(GL_VERTEX_ARRAY);
|
|
|
|
glVertexPointer(2, GL_FLOAT, 0, &points[0]);
|
|
|
|
glDrawArrays(GL_LINE_STRIP, 0, points.size() / 2);
|
2021-04-04 22:17:30 -04:00
|
|
|
if (!peak_points.empty()) {
|
2015-12-31 20:44:39 -05:00
|
|
|
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
|
|
|
|
glColor4f(0, 1.0, 0, 0.5);
|
|
|
|
glVertexPointer(2, GL_FLOAT, 0, &peak_points[0]);
|
|
|
|
glDrawArrays(GL_LINE_STRIP, 0, peak_points.size() / 2);
|
|
|
|
}
|
2015-08-10 00:33:31 -04:00
|
|
|
glDisableClientState(GL_VERTEX_ARRAY);
|
|
|
|
}
|
2015-08-17 21:52:38 -04:00
|
|
|
|
2015-08-10 00:33:31 -04:00
|
|
|
GLint vp[4];
|
|
|
|
glGetIntegerv( GL_VIEWPORT, vp);
|
|
|
|
|
|
|
|
float viewHeight = (float) vp[3];
|
|
|
|
float viewWidth = (float) vp[2];
|
2017-05-28 06:28:19 -04:00
|
|
|
glLoadMatrixf(transform.to_ptr());
|
2015-08-17 21:52:38 -04:00
|
|
|
|
2015-08-10 00:33:31 -04:00
|
|
|
|
2015-08-11 21:49:42 -04:00
|
|
|
long long leftFreq = (double) freq - ((double) bandwidth / 2.0);
|
|
|
|
long long rightFreq = leftFreq + (double) bandwidth;
|
2015-08-23 17:51:20 -04:00
|
|
|
|
2015-08-24 08:58:08 -04:00
|
|
|
long long hzStep = 1000000;
|
2015-08-10 00:33:31 -04:00
|
|
|
|
|
|
|
long double mhzStep = (100000.0 / (long double) (rightFreq - leftFreq)) * 2.0;
|
2015-08-23 17:51:20 -04:00
|
|
|
double mhzVisualStep = 0.1;
|
|
|
|
|
|
|
|
std::stringstream label;
|
|
|
|
label.precision(1);
|
|
|
|
|
2016-06-23 20:01:32 -04:00
|
|
|
double fontScale = GLFont::getScaleFactor();
|
|
|
|
|
|
|
|
if (mhzStep * 0.5 * viewWidth < 40 * fontScale) {
|
2015-08-11 21:49:42 -04:00
|
|
|
mhzStep = (250000.0 / (long double) (rightFreq - leftFreq)) * 2.0;
|
2015-08-23 17:51:20 -04:00
|
|
|
mhzVisualStep = 0.25;
|
2015-08-11 21:49:42 -04:00
|
|
|
|
2016-06-23 20:01:32 -04:00
|
|
|
if (mhzStep * 0.5 * viewWidth < 40 * fontScale) {
|
2015-08-24 01:31:37 -04:00
|
|
|
mhzStep = (500000.0 / (long double) (rightFreq - leftFreq)) * 2.0;
|
|
|
|
mhzVisualStep = 0.5;
|
|
|
|
}
|
|
|
|
|
2016-06-23 20:01:32 -04:00
|
|
|
if (mhzStep * 0.5 * viewWidth < 40 * fontScale) {
|
2015-08-24 01:31:37 -04:00
|
|
|
mhzStep = (1000000.0 / (long double) (rightFreq - leftFreq)) * 2.0;
|
|
|
|
mhzVisualStep = 1.0;
|
|
|
|
}
|
|
|
|
|
2016-06-23 20:01:32 -04:00
|
|
|
if (mhzStep * 0.5 * viewWidth < 40 * fontScale) {
|
2015-08-24 01:31:37 -04:00
|
|
|
mhzStep = (2500000.0 / (long double) (rightFreq - leftFreq)) * 2.0;
|
|
|
|
mhzVisualStep = 2.5;
|
|
|
|
}
|
|
|
|
|
2016-06-23 20:01:32 -04:00
|
|
|
if (mhzStep * 0.5 * viewWidth < 40 * fontScale) {
|
2015-08-24 01:31:37 -04:00
|
|
|
mhzStep = (5000000.0 / (long double) (rightFreq - leftFreq)) * 2.0;
|
|
|
|
mhzVisualStep = 5.0;
|
|
|
|
}
|
|
|
|
|
2016-06-23 20:01:32 -04:00
|
|
|
if (mhzStep * 0.5 * viewWidth < 40 * fontScale) {
|
2015-08-24 01:31:37 -04:00
|
|
|
mhzStep = (10000000.0 / (long double) (rightFreq - leftFreq)) * 2.0;
|
|
|
|
mhzVisualStep = 10.0;
|
|
|
|
}
|
|
|
|
|
2016-06-23 20:01:32 -04:00
|
|
|
if (mhzStep * 0.5 * viewWidth < 40 * fontScale) {
|
2015-08-24 01:31:37 -04:00
|
|
|
mhzStep = (50000000.0 / (long double) (rightFreq - leftFreq)) * 2.0;
|
|
|
|
mhzVisualStep = 50.0;
|
|
|
|
}
|
2016-06-23 20:01:32 -04:00
|
|
|
} else if (mhzStep * 0.5 * viewWidth > 350 * fontScale) {
|
2015-08-11 23:52:03 -04:00
|
|
|
mhzStep = (10000.0 / (long double) (rightFreq - leftFreq)) * 2.0;
|
2015-08-23 17:51:20 -04:00
|
|
|
mhzVisualStep = 0.01;
|
|
|
|
label.precision(2);
|
2015-08-11 23:52:03 -04:00
|
|
|
}
|
2015-08-10 00:33:31 -04:00
|
|
|
|
2015-08-23 17:51:20 -04:00
|
|
|
long long firstMhz = (leftFreq / hzStep) * hzStep;
|
|
|
|
long double mhzStart = ((long double) (firstMhz - leftFreq) / (long double) (rightFreq - leftFreq)) * 2.0;
|
|
|
|
long double currentMhz = trunc(floor(firstMhz / (long double)1000000.0));
|
|
|
|
|
2015-08-10 00:33:31 -04:00
|
|
|
|
2016-06-23 20:01:32 -04:00
|
|
|
double hPos = 1.0 - (16.0 / viewHeight) * GLFont::getScaleFactor();
|
2015-08-11 21:49:42 -04:00
|
|
|
double lMhzPos = 1.0 - (5.0 / viewHeight);
|
2015-08-10 00:33:31 -04:00
|
|
|
|
2016-06-23 15:17:02 -04:00
|
|
|
int fontSize = 12;
|
2016-01-13 23:07:21 -05:00
|
|
|
|
|
|
|
if (viewHeight > 135) {
|
2016-06-21 13:13:48 -04:00
|
|
|
|
2016-06-23 15:17:02 -04:00
|
|
|
fontSize = 16;
|
2016-06-23 20:01:32 -04:00
|
|
|
hPos = 1.0 - (18.0 / viewHeight) * GLFont::getScaleFactor();
|
2016-01-13 23:07:21 -05:00
|
|
|
}
|
2016-03-14 19:27:34 -04:00
|
|
|
|
2016-06-23 15:17:02 -04:00
|
|
|
GLFont::Drawer refDrawingFont = GLFont::getFont(fontSize, GLFont::getScaleFactor());
|
2016-06-21 13:40:02 -04:00
|
|
|
|
2015-08-11 21:49:42 -04:00
|
|
|
for (double m = -1.0 + mhzStart, mMax = 1.0 + ((mhzStart>0)?mhzStart:-mhzStart); m <= mMax; m += mhzStep) {
|
2016-03-14 19:27:34 -04:00
|
|
|
if (m < -1.0) {
|
|
|
|
currentMhz += mhzVisualStep;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (m > 1.0) {
|
|
|
|
break;
|
|
|
|
}
|
2015-08-10 00:33:31 -04:00
|
|
|
label << std::fixed << currentMhz;
|
|
|
|
|
|
|
|
double fractpart, intpart;
|
|
|
|
|
|
|
|
fractpart = modf(currentMhz, &intpart);
|
|
|
|
|
|
|
|
if (fractpart < 0.001) {
|
|
|
|
glLineWidth(4.0);
|
|
|
|
glColor3f(ThemeMgr::mgr.currentTheme->freqLine.r, ThemeMgr::mgr.currentTheme->freqLine.g, ThemeMgr::mgr.currentTheme->freqLine.b);
|
|
|
|
} else {
|
|
|
|
glLineWidth(1.0);
|
|
|
|
glColor3f(ThemeMgr::mgr.currentTheme->freqLine.r * 0.65, ThemeMgr::mgr.currentTheme->freqLine.g * 0.65,
|
|
|
|
ThemeMgr::mgr.currentTheme->freqLine.b * 0.65);
|
|
|
|
}
|
|
|
|
|
|
|
|
glDisable(GL_TEXTURE_2D);
|
|
|
|
glBegin(GL_LINES);
|
|
|
|
glVertex2f(m, lMhzPos);
|
|
|
|
glVertex2f(m, 1);
|
|
|
|
glEnd();
|
|
|
|
|
|
|
|
glColor4f(ThemeMgr::mgr.currentTheme->text.r, ThemeMgr::mgr.currentTheme->text.g, ThemeMgr::mgr.currentTheme->text.b,1.0);
|
2016-01-13 23:07:21 -05:00
|
|
|
|
2016-06-21 13:40:02 -04:00
|
|
|
refDrawingFont.drawString(label.str(), m, hPos, GLFont::GLFONT_ALIGN_CENTER, GLFont::GLFONT_ALIGN_CENTER, 0, 0, true);
|
2015-08-10 00:33:31 -04:00
|
|
|
|
|
|
|
label.str(std::string());
|
|
|
|
|
|
|
|
currentMhz += mhzVisualStep;
|
|
|
|
}
|
|
|
|
|
|
|
|
glLineWidth(1.0);
|
2015-08-17 21:52:38 -04:00
|
|
|
|
|
|
|
if (showDb) {
|
2016-06-24 14:44:35 -04:00
|
|
|
float dbPanelWidth = (1.0 / viewWidth)*88.0 * GLFont::getScaleFactor();
|
2016-06-22 13:04:59 -04:00
|
|
|
float dbPanelHeight = (1.0/viewHeight)*14.0 * GLFont::getScaleFactor();
|
2016-10-20 21:44:33 -04:00
|
|
|
float dbOfs = useDbOfs?wxGetApp().getConfig()->getDBOffset():0;
|
2015-08-17 21:52:38 -04:00
|
|
|
|
2015-09-09 17:31:55 -04:00
|
|
|
std::stringstream ssLabel("");
|
|
|
|
if (getCeilValue() != getFloorValue() && fftSize) {
|
2016-10-20 21:44:33 -04:00
|
|
|
ssLabel << std::fixed << std::setprecision(1) << (dbOfs + 20.0 * log10(2.0*(getCeilValue())/(double)fftSize)) << "dB";
|
2015-09-09 17:31:55 -04:00
|
|
|
}
|
2015-08-17 21:52:38 -04:00
|
|
|
dbPanelCeil.setText(ssLabel.str(), GLFont::GLFONT_ALIGN_RIGHT);
|
|
|
|
dbPanelCeil.setSize(dbPanelWidth, dbPanelHeight);
|
|
|
|
dbPanelCeil.setPosition(-1.0 + dbPanelWidth, 1.0 - dbPanelHeight);
|
2016-06-22 13:04:59 -04:00
|
|
|
|
2015-08-17 21:52:38 -04:00
|
|
|
|
|
|
|
ssLabel.str("");
|
2015-09-09 17:31:55 -04:00
|
|
|
if (getCeilValue() != getFloorValue() && fftSize) {
|
2016-10-20 21:44:33 -04:00
|
|
|
ssLabel << (dbOfs + 20.0 * log10(2.0*(getFloorValue())/(double)fftSize)) << "dB";
|
2015-09-09 17:31:55 -04:00
|
|
|
}
|
2015-08-17 21:52:38 -04:00
|
|
|
|
|
|
|
dbPanelFloor.setText(ssLabel.str(), GLFont::GLFONT_ALIGN_RIGHT);
|
|
|
|
dbPanelFloor.setSize(dbPanelWidth, dbPanelHeight);
|
|
|
|
dbPanelFloor.setPosition(-1.0 + dbPanelWidth, - 1.0 + dbPanelHeight);
|
|
|
|
}
|
2015-08-10 00:33:31 -04:00
|
|
|
}
|