annotate layer/Colour3DPlotExporter.cpp @ 1568:3943553b95b0 csv-export-dialog

Add CSV export dialog, + associated supporting changes
author Chris Cannam
date Tue, 14 Jan 2020 15:41:17 +0000
parents a6a31908bd13
children b33b0b06133e
rev   line source
Chris@1554 1 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */
Chris@1554 2
Chris@1554 3 /*
Chris@1554 4 Sonic Visualiser
Chris@1554 5 An audio file viewer and annotation editor.
Chris@1554 6 Centre for Digital Music, Queen Mary, University of London.
Chris@1554 7
Chris@1554 8 This program is free software; you can redistribute it and/or
Chris@1554 9 modify it under the terms of the GNU General Public License as
Chris@1554 10 published by the Free Software Foundation; either version 2 of the
Chris@1554 11 License, or (at your option) any later version. See the file
Chris@1554 12 COPYING included with this distribution for more information.
Chris@1554 13 */
Chris@1554 14
Chris@1554 15 #include "Colour3DPlotExporter.h"
Chris@1554 16
Chris@1554 17 #include "data/model/EditableDenseThreeDimensionalModel.h"
Chris@1554 18 #include "data/model/FFTModel.h"
Chris@1554 19
Chris@1554 20 #include "VerticalBinLayer.h"
Chris@1554 21
Chris@1556 22 Colour3DPlotExporter::Colour3DPlotExporter(Sources sources, Parameters params) :
Chris@1556 23 m_sources(sources),
Chris@1556 24 m_params(params)
Chris@1556 25 {
Chris@1556 26 SVCERR << "Colour3DPlotExporter::Colour3DPlotExporter: constructed at "
Chris@1556 27 << this << endl;
Chris@1556 28 }
Chris@1556 29
Chris@1556 30 Colour3DPlotExporter::~Colour3DPlotExporter()
Chris@1556 31 {
Chris@1556 32 SVCERR << "Colour3DPlotExporter[" << this << "]::~Colour3DPlotExporter"
Chris@1556 33 << endl;
Chris@1556 34 }
Chris@1556 35
Chris@1556 36 void
Chris@1556 37 Colour3DPlotExporter::discardSources()
Chris@1556 38 {
Chris@1556 39 SVCERR << "Colour3DPlotExporter[" << this << "]::discardSources"
Chris@1556 40 << endl;
Chris@1556 41 QMutexLocker locker(&m_mutex);
Chris@1556 42 m_sources.verticalBinLayer = nullptr;
Chris@1556 43 m_sources.source = {};
Chris@1556 44 m_sources.fft = {};
Chris@1556 45 m_sources.provider = nullptr;
Chris@1556 46 }
Chris@1556 47
Chris@1554 48 QString
Chris@1565 49 Colour3DPlotExporter::getDelimitedDataHeaderLine(QString delimiter,
Chris@1568 50 DataExportOptions opts) const
Chris@1565 51 {
Chris@1565 52 auto model =
Chris@1565 53 ModelById::getAs<DenseThreeDimensionalModel>(m_sources.source);
Chris@1565 54
Chris@1565 55 auto layer = m_sources.verticalBinLayer;
Chris@1565 56 auto provider = m_sources.provider;
Chris@1565 57
Chris@1565 58 if (!model || !layer) {
Chris@1565 59 SVCERR << "ERROR: Colour3DPlotExporter::getDelimitedDataHeaderLine: Source model and layer required" << endl;
Chris@1565 60 return {};
Chris@1565 61 }
Chris@1565 62
Chris@1565 63 int minbin = 0;
Chris@1565 64 int sh = model->getHeight();
Chris@1565 65 int nbins = sh;
Chris@1565 66
Chris@1565 67 if (provider) {
Chris@1565 68
Chris@1565 69 minbin = layer->getIBinForY(provider, provider->getPaintHeight());
Chris@1565 70 if (minbin >= sh) minbin = sh - 1;
Chris@1565 71 if (minbin < 0) minbin = 0;
Chris@1565 72
Chris@1565 73 nbins = layer->getIBinForY(provider, 0) - minbin + 1;
Chris@1565 74 if (minbin + nbins > sh) nbins = sh - minbin;
Chris@1565 75 }
Chris@1565 76
Chris@1565 77 QStringList list;
Chris@1565 78
Chris@1568 79 if (opts & DataExportAlwaysIncludeTimestamp) {
Chris@1568 80 if (opts & DataExportWriteTimeInFrames) {
Chris@1568 81 list << "FRAME";
Chris@1568 82 } else {
Chris@1568 83 list << "TIME";
Chris@1568 84 }
Chris@1565 85 }
Chris@1565 86
Chris@1565 87 if (m_params.binDisplay == BinDisplay::PeakFrequencies) {
Chris@1565 88 for (int i = 0; i < nbins/4; ++i) {
Chris@1565 89 list << QString("FREQ %1").arg(i+1)
Chris@1565 90 << QString("MAG %1").arg(i+1);
Chris@1565 91 }
Chris@1565 92 } else {
Chris@1565 93 bool hasValues = model->hasBinValues();
Chris@1565 94 QString unit = (hasValues ? model->getBinValueUnit() : "");
Chris@1565 95 for (int i = minbin; i < minbin + nbins; ++i) {
Chris@1565 96 QString name = model->getBinName(i);
Chris@1565 97 if (name == "") {
Chris@1565 98 if (hasValues) {
Chris@1565 99 if (unit != "") {
Chris@1565 100 name = QString("BIN %1: %2 %3")
Chris@1565 101 .arg(i+1)
Chris@1565 102 .arg(model->getBinValue(i))
Chris@1565 103 .arg(unit);
Chris@1565 104 } else {
Chris@1565 105 name = QString("BIN %1: %2")
Chris@1565 106 .arg(i+1)
Chris@1565 107 .arg(model->getBinValue(i));
Chris@1565 108 }
Chris@1565 109 } else {
Chris@1565 110 name = QString("BIN %1")
Chris@1565 111 .arg(i+1);
Chris@1565 112 }
Chris@1565 113 }
Chris@1565 114 list << name;
Chris@1565 115 }
Chris@1565 116 }
Chris@1565 117
Chris@1565 118 return list.join(delimiter);
Chris@1565 119 }
Chris@1565 120
Chris@1565 121 QString
Chris@1554 122 Colour3DPlotExporter::toDelimitedDataString(QString delimiter,
Chris@1568 123 DataExportOptions opts,
Chris@1554 124 sv_frame_t startFrame,
Chris@1554 125 sv_frame_t duration) const
Chris@1554 126 {
Chris@1554 127 QMutexLocker locker(&m_mutex);
Chris@1554 128
Chris@1554 129 BinDisplay binDisplay = m_params.binDisplay;
Chris@1554 130
Chris@1554 131 auto model =
Chris@1554 132 ModelById::getAs<DenseThreeDimensionalModel>(m_sources.source);
Chris@1554 133 auto fftModel =
Chris@1554 134 ModelById::getAs<FFTModel>(m_sources.fft);
Chris@1554 135
Chris@1554 136 auto layer = m_sources.verticalBinLayer;
Chris@1554 137 auto provider = m_sources.provider;
Chris@1554 138
Chris@1554 139 if (!model || !layer) {
Chris@1554 140 SVCERR << "ERROR: Colour3DPlotExporter::toDelimitedDataString: Source model and layer required" << endl;
Chris@1554 141 return {};
Chris@1554 142 }
Chris@1556 143 if ((binDisplay == BinDisplay::PeakFrequencies) && !fftModel) {
Chris@1556 144 SVCERR << "ERROR: Colour3DPlotExporter::toDelimitedDataString: FFT model required in peak frequencies mode" << endl;
Chris@1556 145 return {};
Chris@1556 146 }
Chris@1554 147
Chris@1554 148 int minbin = 0;
Chris@1554 149 int sh = model->getHeight();
Chris@1554 150 int nbins = sh;
Chris@1554 151
Chris@1554 152 if (provider) {
Chris@1554 153
Chris@1554 154 minbin = layer->getIBinForY(provider, provider->getPaintHeight());
Chris@1554 155 if (minbin >= sh) minbin = sh - 1;
Chris@1554 156 if (minbin < 0) minbin = 0;
Chris@1554 157
Chris@1554 158 nbins = layer->getIBinForY(provider, 0) - minbin + 1;
Chris@1554 159 if (minbin + nbins > sh) nbins = sh - minbin;
Chris@1554 160 }
Chris@1554 161
Chris@1554 162 int w = model->getWidth();
Chris@1554 163
Chris@1554 164 QString s;
Chris@1554 165
Chris@1554 166 for (int i = 0; i < w; ++i) {
Chris@1556 167
Chris@1554 168 sv_frame_t fr = model->getStartFrame() + i * model->getResolution();
Chris@1554 169 if (fr < startFrame || fr >= startFrame + duration) {
Chris@1554 170 continue;
Chris@1554 171 }
Chris@1556 172
Chris@1561 173 //!!! (+ phase layer type)
Chris@1556 174
Chris@1556 175 auto column = model->getColumn(i);
Chris@1556 176 column = ColumnOp::Column(column.data() + minbin,
Chris@1556 177 column.data() + minbin + nbins);
Chris@1561 178
Chris@1561 179 // The scale factor is always applied
Chris@1561 180 column = ColumnOp::applyGain(column, m_params.scaleFactor);
Chris@1556 181
Chris@1554 182 QStringList list;
Chris@1554 183
Chris@1568 184 if (opts & DataExportAlwaysIncludeTimestamp) {
Chris@1568 185 if (opts & DataExportWriteTimeInFrames) {
Chris@1568 186 list << QString("%1").arg(fr);
Chris@1568 187 } else {
Chris@1568 188 list << RealTime::frame2RealTime(fr, model->getSampleRate())
Chris@1568 189 .toString().c_str();
Chris@1568 190 }
Chris@1564 191 }
Chris@1564 192
Chris@1556 193 if (binDisplay == BinDisplay::PeakFrequencies) {
Chris@1556 194
Chris@1556 195 FFTModel::PeakSet peaks = fftModel->getPeakFrequencies
Chris@1558 196 (FFTModel::AllPeaks, i, minbin, minbin + nbins - 1);
Chris@1554 197
Chris@1561 198 // We don't apply normalisation or gain to the output, but
Chris@1561 199 // we *do* perform thresholding when exporting the
Chris@1561 200 // peak-frequency spectrogram, to give the user an
Chris@1561 201 // opportunity to cut irrelevant peaks. And to make that
Chris@1561 202 // match the display, we have to apply both normalisation
Chris@1561 203 // and gain locally for thresholding
Chris@1561 204
Chris@1561 205 auto toTest = ColumnOp::normalize(column, m_params.normalization);
Chris@1561 206 toTest = ColumnOp::applyGain(toTest, m_params.gain);
Chris@1561 207
Chris@1556 208 for (const auto &p: peaks) {
Chris@1556 209
Chris@1556 210 int bin = p.first;
Chris@1561 211
Chris@1561 212 if (toTest[bin - minbin] < m_params.threshold) {
Chris@1561 213 continue;
Chris@1561 214 }
Chris@1561 215
Chris@1556 216 double freq = p.second;
Chris@1561 217 double value = column[bin - minbin];
Chris@1561 218
Chris@1561 219 list << QString("%1").arg(freq) << QString("%1").arg(value);
Chris@1556 220 }
Chris@1556 221
Chris@1556 222 } else {
Chris@1556 223
Chris@1556 224 if (binDisplay == BinDisplay::PeakBins) {
Chris@1556 225 column = ColumnOp::peakPick(column);
Chris@1556 226 }
Chris@1556 227
Chris@1556 228 for (auto value: column) {
Chris@1556 229 list << QString("%1").arg(value);
Chris@1556 230 }
Chris@1556 231 }
Chris@1556 232
Chris@1554 233 s += list.join(delimiter) + "\n";
Chris@1554 234 }
Chris@1554 235
Chris@1554 236 return s;
Chris@1554 237 }
Chris@1554 238