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
|
/* -*- 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 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 "Colour3DPlotExporter.h"
#include "data/model/EditableDenseThreeDimensionalModel.h"
#include "data/model/FFTModel.h"
#include "VerticalBinLayer.h"
namespace sv {
Colour3DPlotExporter::Colour3DPlotExporter(Sources sources, Parameters params) :
m_sources(sources),
m_params(params)
{
// SVCERR << "Colour3DPlotExporter::Colour3DPlotExporter: constructed at "
// << this << endl;
}
Colour3DPlotExporter::~Colour3DPlotExporter()
{
// SVCERR << "Colour3DPlotExporter[" << this << "]::~Colour3DPlotExporter"
// << endl;
}
void
Colour3DPlotExporter::discardSources()
{
// SVCERR << "Colour3DPlotExporter[" << this << "]::discardSources"
// << endl;
QMutexLocker locker(&m_mutex);
m_sources.verticalBinLayer = nullptr;
m_sources.source = {};
m_sources.fft = {};
m_sources.provider = nullptr;
}
QVector<QString>
Colour3DPlotExporter::getStringExportHeaders(DataExportOptions opts) const
{
auto model =
ModelById::getAs<DenseThreeDimensionalModel>(m_sources.source);
auto layer = m_sources.verticalBinLayer;
auto provider = m_sources.provider;
if (!model || !layer) {
SVCERR << "ERROR: Colour3DPlotExporter::getDelimitedDataHeaderLine: Source model and layer required" << endl;
return {};
}
int minbin = 0;
int sh = model->getHeight();
int nbins = sh;
if (provider) {
minbin = layer->getIBinForY(provider, provider->getPaintHeight());
if (minbin >= sh) minbin = sh - 1;
if (minbin < 0) minbin = 0;
nbins = layer->getIBinForY(provider, 0) - minbin + 1;
if (minbin + nbins > sh) nbins = sh - minbin;
}
QVector<QString> headers;
if (opts & DataExportAlwaysIncludeTimestamp) {
if (opts & DataExportWriteTimeInFrames) {
headers << "FRAME";
} else {
headers << "TIME";
}
}
if (m_params.binDisplay == BinDisplay::PeakFrequencies) {
for (int i = 0; i < nbins/4; ++i) {
headers << QString("FREQ %1").arg(i+1)
<< QString("MAG %1").arg(i+1);
}
} else {
bool hasValues = model->hasBinValues();
QString unit = (hasValues ? model->getBinValueUnit() : "");
for (int i = minbin; i < minbin + nbins; ++i) {
QString name = model->getBinName(i);
if (name == "") {
if (hasValues) {
if (unit != "") {
name = QString("BIN %1: %2 %3")
.arg(i+1)
.arg(model->getBinValue(i))
.arg(unit);
} else {
name = QString("BIN %1: %2")
.arg(i+1)
.arg(model->getBinValue(i));
}
} else {
name = QString("BIN %1")
.arg(i+1);
}
}
headers << name;
}
}
return headers;
}
QVector<QVector<QString>>
Colour3DPlotExporter::toStringExportRows(DataExportOptions opts,
sv_frame_t startFrame,
sv_frame_t duration) const
{
QMutexLocker locker(&m_mutex);
BinDisplay binDisplay = m_params.binDisplay;
auto model =
ModelById::getAs<DenseThreeDimensionalModel>(m_sources.source);
auto fftModel =
ModelById::getAs<FFTModel>(m_sources.fft);
auto layer = m_sources.verticalBinLayer;
auto provider = m_sources.provider;
if (!model || !layer) {
SVCERR << "ERROR: Colour3DPlotExporter::toDelimitedDataString: Source model and layer required" << endl;
return {};
}
if ((binDisplay == BinDisplay::PeakFrequencies) && !fftModel) {
SVCERR << "ERROR: Colour3DPlotExporter::toDelimitedDataString: FFT model required in peak frequencies mode" << endl;
return {};
}
int minbin = 0;
int sh = model->getHeight();
int nbins = sh;
if (provider) {
minbin = layer->getIBinForY(provider, provider->getPaintHeight());
if (minbin >= sh) minbin = sh - 1;
if (minbin < 0) minbin = 0;
nbins = layer->getIBinForY(provider, 0) - minbin + 1;
if (minbin + nbins > sh) nbins = sh - minbin;
}
int w = model->getWidth();
QVector<QVector<QString>> rows;
for (int i = 0; i < w; ++i) {
sv_frame_t fr = model->getStartFrame() + i * model->getResolution();
if (fr < startFrame || fr >= startFrame + duration) {
continue;
}
//!!! (+ phase layer type)
auto column = model->getColumn(i);
column = ColumnOp::Column(column.data() + minbin,
column.data() + minbin + nbins);
// The scale factor is always applied
column = ColumnOp::applyGain(column, m_params.scaleFactor);
QVector<QString> row;
if (opts & DataExportAlwaysIncludeTimestamp) {
if (opts & DataExportWriteTimeInFrames) {
row << QString("%1").arg(fr);
} else {
row << RealTime::frame2RealTime(fr, model->getSampleRate())
.toString().c_str();
}
}
if (binDisplay == BinDisplay::PeakFrequencies) {
FFTModel::Peaks peaks = fftModel->getPeakFrequencies
(FFTModel::AllPeaks, i, minbin, minbin + nbins - 1);
// We don't apply normalisation or gain to the output, but
// we *do* perform thresholding when exporting the
// peak-frequency spectrogram, to give the user an
// opportunity to cut irrelevant peaks. And to make that
// match the display, we have to apply both normalisation
// and gain locally for thresholding
auto toTest = ColumnOp::normalize(column, m_params.normalization);
toTest = ColumnOp::applyGain(toTest, m_params.gain);
for (const auto &p: peaks) {
int bin = p.first;
if (toTest[bin - minbin] < m_params.threshold) {
continue;
}
double freq = p.second;
double value = column[bin - minbin];
row << QString("%1").arg(freq) << QString("%1").arg(value);
}
} else {
if (binDisplay == BinDisplay::PeakBins) {
column = ColumnOp::peakPick(column);
}
for (auto value: column) {
row << QString("%1").arg(value);
}
}
if (!row.empty()) {
rows.push_back(row);
}
}
return rows;
}
} // end namespace sv
|