Chris@1554: /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */ Chris@1554: Chris@1554: /* Chris@1554: Sonic Visualiser Chris@1554: An audio file viewer and annotation editor. Chris@1554: Centre for Digital Music, Queen Mary, University of London. Chris@1554: Chris@1554: This program is free software; you can redistribute it and/or Chris@1554: modify it under the terms of the GNU General Public License as Chris@1554: published by the Free Software Foundation; either version 2 of the Chris@1554: License, or (at your option) any later version. See the file Chris@1554: COPYING included with this distribution for more information. Chris@1554: */ Chris@1554: Chris@1554: #include "Colour3DPlotExporter.h" Chris@1554: Chris@1554: #include "data/model/EditableDenseThreeDimensionalModel.h" Chris@1554: #include "data/model/FFTModel.h" Chris@1554: Chris@1554: #include "VerticalBinLayer.h" Chris@1554: Chris@1556: Colour3DPlotExporter::Colour3DPlotExporter(Sources sources, Parameters params) : Chris@1556: m_sources(sources), Chris@1556: m_params(params) Chris@1556: { Chris@1556: SVCERR << "Colour3DPlotExporter::Colour3DPlotExporter: constructed at " Chris@1556: << this << endl; Chris@1556: } Chris@1556: Chris@1556: Colour3DPlotExporter::~Colour3DPlotExporter() Chris@1556: { Chris@1556: SVCERR << "Colour3DPlotExporter[" << this << "]::~Colour3DPlotExporter" Chris@1556: << endl; Chris@1556: } Chris@1556: Chris@1556: void Chris@1556: Colour3DPlotExporter::discardSources() Chris@1556: { Chris@1556: SVCERR << "Colour3DPlotExporter[" << this << "]::discardSources" Chris@1556: << endl; Chris@1556: QMutexLocker locker(&m_mutex); Chris@1556: m_sources.verticalBinLayer = nullptr; Chris@1556: m_sources.source = {}; Chris@1556: m_sources.fft = {}; Chris@1556: m_sources.provider = nullptr; Chris@1556: } Chris@1556: Chris@1554: QString Chris@1554: Colour3DPlotExporter::toDelimitedDataString(QString delimiter, Chris@1554: DataExportOptions options, Chris@1554: sv_frame_t startFrame, Chris@1554: sv_frame_t duration) const Chris@1554: { Chris@1554: QMutexLocker locker(&m_mutex); Chris@1554: Chris@1554: BinDisplay binDisplay = m_params.binDisplay; Chris@1554: Chris@1556: (void)options; //!!! Chris@1556: Chris@1554: auto model = Chris@1554: ModelById::getAs(m_sources.source); Chris@1554: auto fftModel = Chris@1554: ModelById::getAs(m_sources.fft); Chris@1554: Chris@1554: auto layer = m_sources.verticalBinLayer; Chris@1554: auto provider = m_sources.provider; Chris@1554: Chris@1554: if (!model || !layer) { Chris@1554: SVCERR << "ERROR: Colour3DPlotExporter::toDelimitedDataString: Source model and layer required" << endl; Chris@1554: return {}; Chris@1554: } Chris@1556: if ((binDisplay == BinDisplay::PeakFrequencies) && !fftModel) { Chris@1556: SVCERR << "ERROR: Colour3DPlotExporter::toDelimitedDataString: FFT model required in peak frequencies mode" << endl; Chris@1556: return {}; Chris@1556: } Chris@1554: Chris@1554: int minbin = 0; Chris@1554: int sh = model->getHeight(); Chris@1554: int nbins = sh; Chris@1554: Chris@1554: //!!! todo: consider what to do about the actual Colour 3D Plot Chris@1554: //!!! Layer. In the existing application, this is exported full Chris@1554: //!!! height. If we switch to using this code, we will be Chris@1554: //!!! exporting only the displayed height. This is backward Chris@1554: //!!! incompatible, but also not directly interpretable without Chris@1554: //!!! any guide in the exported file as to what the bin indices Chris@1554: //!!! are. Perhaps we should have a flag to export full height, Chris@1554: //!!! and default to using it. Chris@1554: Chris@1554: //!!! todo: what about the other export types besides Chris@1554: //!!! delimited-data-string ? Chris@1556: Chris@1556: //!!! todo: scripted regression tests for layer exports (of all Chris@1556: //!!! types) Chris@1556: Chris@1556: //!!! todo: export selected region only (we have the necessaries Chris@1556: //!!! here, but it needs support higher up) Chris@1556: Chris@1556: //!!! todo: option to include timestamps for columns Chris@1554: Chris@1554: if (provider) { Chris@1554: Chris@1554: minbin = layer->getIBinForY(provider, provider->getPaintHeight()); Chris@1554: if (minbin >= sh) minbin = sh - 1; Chris@1554: if (minbin < 0) minbin = 0; Chris@1554: Chris@1554: nbins = layer->getIBinForY(provider, 0) - minbin + 1; Chris@1554: if (minbin + nbins > sh) nbins = sh - minbin; Chris@1554: } Chris@1554: Chris@1554: int w = model->getWidth(); Chris@1554: Chris@1554: QString s; Chris@1554: Chris@1554: for (int i = 0; i < w; ++i) { Chris@1556: Chris@1554: sv_frame_t fr = model->getStartFrame() + i * model->getResolution(); Chris@1554: if (fr < startFrame || fr >= startFrame + duration) { Chris@1554: continue; Chris@1554: } Chris@1556: Chris@1556: // Unlike Colour3DPlotRenderer, we don't want to scale or Chris@1556: // normalise Chris@1556: Chris@1556: //!!! (should we be handling phase layer type?) Chris@1556: Chris@1556: auto column = model->getColumn(i); Chris@1556: column = ColumnOp::Column(column.data() + minbin, Chris@1556: column.data() + minbin + nbins); Chris@1556: Chris@1556: //!!! todo: peaks, frequencies (the things we came here for) Chris@1556: Chris@1554: QStringList list; Chris@1554: Chris@1556: if (binDisplay == BinDisplay::PeakFrequencies) { Chris@1556: Chris@1556: FFTModel::PeakSet peaks = fftModel->getPeakFrequencies Chris@1556: (FFTModel::AllPeaks, i, minbin, minbin + nbins + 1); Chris@1554: Chris@1556: for (const auto &p: peaks) { Chris@1556: Chris@1556: int bin = p.first; Chris@1556: double freq = p.second; Chris@1556: float mag = column[bin - minbin]; Chris@1556: Chris@1556: list << QString("%1").arg(freq) << QString("%1").arg(mag); Chris@1556: } Chris@1556: Chris@1556: } else { Chris@1556: Chris@1556: if (binDisplay == BinDisplay::PeakBins) { Chris@1556: column = ColumnOp::peakPick(column); Chris@1556: } Chris@1556: Chris@1556: for (auto value: column) { Chris@1556: list << QString("%1").arg(value); Chris@1556: } Chris@1556: } Chris@1556: Chris@1554: s += list.join(delimiter) + "\n"; Chris@1554: } Chris@1554: Chris@1554: return s; Chris@1554: Chris@1554: Chris@1554: //!!! For reference, this is the body of Chris@1554: //!!! EditableDenseThreeDimensionalModel::toDelimitedDataString Chris@1554: /* Chris@1554: QString s; Chris@1554: for (int i = 0; in_range_for(m_data, i); ++i) { Chris@1554: sv_frame_t fr = m_startFrame + i * m_resolution; Chris@1554: if (fr >= startFrame && fr < startFrame + duration) { Chris@1554: QStringList list; Chris@1554: for (int j = 0; in_range_for(m_data.at(i), j); ++j) { Chris@1554: list << QString("%1").arg(m_data.at(i).at(j)); Chris@1554: } Chris@1554: s += list.join(delimiter) + "\n"; Chris@1554: } Chris@1554: } Chris@1554: return s; Chris@1554: */ Chris@1554: } Chris@1554: