Chris@133: /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */ Chris@133: Chris@133: /* Chris@133: Sonic Visualiser Chris@133: An audio file viewer and annotation editor. Chris@133: Centre for Digital Music, Queen Mary, University of London. Chris@195: This file copyright 2006-2007 QMUL. Chris@133: Chris@133: This program is free software; you can redistribute it and/or Chris@133: modify it under the terms of the GNU General Public License as Chris@133: published by the Free Software Foundation; either version 2 of the Chris@133: License, or (at your option) any later version. See the file Chris@133: COPYING included with this distribution for more information. Chris@133: */ Chris@133: Chris@193: #include "SliceLayer.h" Chris@133: Chris@133: #include "view/View.h" Chris@153: #include "base/AudioLevel.h" Chris@167: #include "base/RangeMapper.h" Chris@195: #include "base/RealTime.h" Chris@376: #include "ColourMapper.h" Chris@376: #include "ColourDatabase.h" Chris@195: Chris@195: #include "PaintAssistant.h" Chris@133: Chris@133: #include Chris@133: #include Chris@316: #include Chris@316: Chris@133: Chris@193: SliceLayer::SliceLayer() : Chris@193: m_sliceableModel(0), Chris@197: m_colourMap(0), Chris@153: m_energyScale(dBScale), Chris@198: m_samplingMode(SampleMean), Chris@195: m_plotStyle(PlotSteps), Chris@193: m_binScale(LinearBins), Chris@153: m_normalize(false), Chris@284: m_threshold(0.0), Chris@284: m_initialThreshold(0.0), Chris@198: m_gain(1.0), Chris@198: m_currentf0(0), Chris@198: m_currentf1(0) Chris@133: { Chris@133: } Chris@133: Chris@193: SliceLayer::~SliceLayer() Chris@133: { Chris@193: Chris@133: } Chris@133: Chris@133: void Chris@193: SliceLayer::setSliceableModel(const Model *model) Chris@133: { Chris@193: const DenseThreeDimensionalModel *sliceable = Chris@193: dynamic_cast(model); Chris@193: Chris@193: if (model && !sliceable) { Chris@682: cerr << "WARNING: SliceLayer::setSliceableModel(" << model Chris@682: << "): model is not a DenseThreeDimensionalModel" << endl; Chris@193: } Chris@193: Chris@193: if (m_sliceableModel == sliceable) return; Chris@193: Chris@193: m_sliceableModel = sliceable; Chris@193: Chris@320: connectSignals(m_sliceableModel); Chris@193: Chris@193: emit modelReplaced(); Chris@153: } Chris@153: Chris@153: void Chris@193: SliceLayer::sliceableModelReplaced(const Model *orig, const Model *replacement) Chris@153: { Chris@587: SVDEBUG << "SliceLayer::sliceableModelReplaced(" << orig << ", " << replacement << ")" << endl; Chris@153: Chris@193: if (orig == m_sliceableModel) { Chris@193: setSliceableModel Chris@193: (dynamic_cast(replacement)); Chris@153: } Chris@153: } Chris@153: Chris@153: void Chris@193: SliceLayer::modelAboutToBeDeleted(Model *m) Chris@153: { Chris@587: SVDEBUG << "SliceLayer::modelAboutToBeDeleted(" << m << ")" << endl; Chris@153: Chris@193: if (m == m_sliceableModel) { Chris@193: setSliceableModel(0); Chris@153: } Chris@133: } Chris@133: Chris@198: QString Chris@918: SliceLayer::getFeatureDescription(LayerGeometryProvider *v, QPoint &p) const Chris@198: { Chris@199: int minbin, maxbin, range; Chris@805: return getFeatureDescriptionAux(v, p, true, minbin, maxbin, range); Chris@199: } Chris@199: Chris@199: QString Chris@918: SliceLayer::getFeatureDescriptionAux(LayerGeometryProvider *v, QPoint &p, Chris@805: bool includeBinDescription, Chris@805: int &minbin, int &maxbin, int &range) const Chris@199: { Chris@199: minbin = 0; Chris@199: maxbin = 0; Chris@198: if (!m_sliceableModel) return ""; Chris@198: Chris@198: int xorigin = m_xorigins[v]; Chris@918: int w = v->getPaintWidth() - xorigin - 1; Chris@198: Chris@198: int mh = m_sliceableModel->getHeight(); Chris@199: minbin = getBinForX(p.x() - xorigin, mh, w); Chris@199: maxbin = getBinForX(p.x() - xorigin + 1, mh, w); Chris@199: Chris@199: if (minbin >= mh) minbin = mh - 1; Chris@199: if (maxbin >= mh) maxbin = mh - 1; Chris@199: if (minbin < 0) minbin = 0; Chris@199: if (maxbin < 0) maxbin = 0; Chris@198: Chris@906: sv_samplerate_t sampleRate = m_sliceableModel->getSampleRate(); Chris@198: Chris@906: sv_frame_t f0 = m_currentf0; Chris@906: sv_frame_t f1 = m_currentf1; Chris@198: Chris@198: RealTime rt0 = RealTime::frame2RealTime(f0, sampleRate); Chris@198: RealTime rt1 = RealTime::frame2RealTime(f1, sampleRate); Chris@198: Chris@906: range = int(f1 - f0 + 1); Chris@198: Chris@280: QString rtrangestr = QString("%1 s").arg((rt1 - rt0).toText().c_str()); Chris@280: Chris@199: if (includeBinDescription) { Chris@198: Chris@906: float minvalue = 0.0; Chris@248: if (minbin < int(m_values.size())) minvalue = m_values[minbin]; Chris@198: Chris@199: float maxvalue = minvalue; Chris@248: if (maxbin < int(m_values.size())) maxvalue = m_values[maxbin]; Chris@199: Chris@199: if (minvalue > maxvalue) std::swap(minvalue, maxvalue); Chris@199: Chris@199: QString binstr; Chris@199: if (maxbin != minbin) { Chris@199: binstr = tr("%1 - %2").arg(minbin+1).arg(maxbin+1); Chris@199: } else { Chris@199: binstr = QString("%1").arg(minbin+1); Chris@199: } Chris@199: Chris@199: QString valuestr; Chris@199: if (maxvalue != minvalue) { Chris@199: valuestr = tr("%1 - %2").arg(minvalue).arg(maxvalue); Chris@199: } else { Chris@199: valuestr = QString("%1").arg(minvalue); Chris@199: } Chris@199: Chris@280: QString description = tr("Time:\t%1 - %2\nRange:\t%3 samples (%4)\nBin:\t%5\n%6 value:\t%7") Chris@199: .arg(QString::fromStdString(rt0.toText(true))) Chris@199: .arg(QString::fromStdString(rt1.toText(true))) Chris@199: .arg(range) Chris@280: .arg(rtrangestr) Chris@199: .arg(binstr) Chris@199: .arg(m_samplingMode == NearestSample ? tr("First") : Chris@199: m_samplingMode == SampleMean ? tr("Mean") : tr("Peak")) Chris@199: .arg(valuestr); Chris@199: Chris@199: return description; Chris@199: Chris@199: } else { Chris@199: Chris@280: QString description = tr("Time:\t%1 - %2\nRange:\t%3 samples (%4)") Chris@199: .arg(QString::fromStdString(rt0.toText(true))) Chris@199: .arg(QString::fromStdString(rt1.toText(true))) Chris@280: .arg(range) Chris@280: .arg(rtrangestr); Chris@199: Chris@199: return description; Chris@199: } Chris@198: } Chris@198: Chris@906: double Chris@906: SliceLayer::getXForBin(int bin, int count, double w) const Chris@198: { Chris@906: double x = 0; Chris@198: Chris@198: switch (m_binScale) { Chris@198: Chris@198: case LinearBins: Chris@906: x = (w * bin) / count; Chris@198: break; Chris@198: Chris@198: case LogBins: Chris@906: x = (w * log10(bin + 1)) / log10(count + 1); Chris@198: break; Chris@198: Chris@198: case InvertedLogBins: Chris@906: x = w - (w * log10(count - bin - 1)) / log10(count); Chris@198: break; Chris@198: } Chris@198: Chris@198: return x; Chris@198: } Chris@198: Chris@198: int Chris@906: SliceLayer::getBinForX(double x, int count, double w) const Chris@198: { Chris@198: int bin = 0; Chris@198: Chris@198: switch (m_binScale) { Chris@198: Chris@198: case LinearBins: Chris@198: bin = int((x * count) / w + 0.0001); Chris@198: break; Chris@198: Chris@198: case LogBins: Chris@906: bin = int(pow(10.0, (x * log10(count + 1)) / w) - 1 + 0.0001); Chris@198: break; Chris@198: Chris@198: case InvertedLogBins: Chris@906: bin = count + 1 - int(pow(10.0, (log10(count) * (w - x)) / double(w)) + 0.0001); Chris@198: break; Chris@198: } Chris@198: Chris@198: return bin; Chris@198: } Chris@198: Chris@906: double Chris@918: SliceLayer::getYForValue(double value, const LayerGeometryProvider *v, double &norm) const Chris@274: { Chris@906: norm = 0.0; Chris@274: Chris@274: if (m_yorigins.find(v) == m_yorigins.end()) return 0; Chris@274: Chris@274: value *= m_gain; Chris@274: Chris@274: int yorigin = m_yorigins[v]; Chris@274: int h = m_heights[v]; Chris@906: double thresh = getThresholdDb(); Chris@274: Chris@906: double y = 0.0; Chris@274: Chris@274: if (h <= 0) return y; Chris@274: Chris@274: switch (m_energyScale) { Chris@274: Chris@274: case dBScale: Chris@274: { Chris@906: double db = thresh; Chris@906: if (value > 0.0) db = 10.0 * log10(fabs(value)); Chris@274: if (db < thresh) db = thresh; Chris@274: norm = (db - thresh) / -thresh; Chris@906: y = yorigin - (double(h) * norm); Chris@274: break; Chris@274: } Chris@274: Chris@274: case MeterScale: Chris@274: y = AudioLevel::multiplier_to_preview(value, h); Chris@906: norm = double(y) / double(h); Chris@274: y = yorigin - y; Chris@274: break; Chris@274: Chris@538: case AbsoluteScale: Chris@906: value = fabs(value); Chris@538: // and fall through Chris@538: Chris@805: case LinearScale: Chris@274: default: Chris@538: norm = (value - m_threshold); Chris@284: if (norm < 0) norm = 0; Chris@906: y = yorigin - (double(h) * norm); Chris@274: break; Chris@274: } Chris@274: Chris@274: return y; Chris@274: } Chris@274: Chris@906: double Chris@918: SliceLayer::getValueForY(double y, const LayerGeometryProvider *v) const Chris@274: { Chris@906: double value = 0.0; Chris@274: Chris@274: if (m_yorigins.find(v) == m_yorigins.end()) return value; Chris@274: Chris@274: int yorigin = m_yorigins[v]; Chris@274: int h = m_heights[v]; Chris@906: double thresh = getThresholdDb(); Chris@274: Chris@274: if (h <= 0) return value; Chris@274: Chris@274: y = yorigin - y; Chris@274: Chris@274: switch (m_energyScale) { Chris@274: Chris@274: case dBScale: Chris@274: { Chris@906: double db = ((y / h) * -thresh) + thresh; Chris@906: value = pow(10.0, db/10.0); Chris@274: break; Chris@274: } Chris@274: Chris@274: case MeterScale: Chris@906: value = AudioLevel::preview_to_multiplier(int(lrint(y)), h); Chris@274: break; Chris@805: Chris@805: case LinearScale: Chris@805: case AbsoluteScale: Chris@274: default: Chris@284: value = y / h + m_threshold; Chris@274: } Chris@274: Chris@274: return value / m_gain; Chris@274: } Chris@274: Chris@133: void Chris@916: SliceLayer::paint(LayerGeometryProvider *v, QPainter &paint, QRect rect) const Chris@133: { Chris@254: if (!m_sliceableModel || !m_sliceableModel->isOK() || Chris@254: !m_sliceableModel->isReady()) return; Chris@133: Chris@195: paint.save(); Chris@195: paint.setRenderHint(QPainter::Antialiasing, false); Chris@538: paint.setBrush(Qt::NoBrush); Chris@195: Chris@195: if (v->getViewManager() && v->getViewManager()->shouldShowScaleGuides()) { Chris@195: if (!m_scalePoints.empty()) { Chris@195: paint.setPen(QColor(240, 240, 240)); //!!! and dark background? Chris@805: for (int i = 0; i < (int)m_scalePoints.size(); ++i) { Chris@195: paint.drawLine(0, m_scalePoints[i], rect.width(), m_scalePoints[i]); Chris@195: } Chris@195: } Chris@195: } Chris@195: Chris@287: paint.setPen(getBaseQColor()); Chris@195: Chris@607: int xorigin = getVerticalScaleWidth(v, true, paint) + 1; Chris@918: int w = v->getPaintWidth() - xorigin - 1; Chris@198: Chris@198: m_xorigins[v] = xorigin; // for use in getFeatureDescription Chris@133: Chris@918: int yorigin = v->getPaintHeight() - 20 - paint.fontMetrics().height() - 7; Chris@195: int h = yorigin - paint.fontMetrics().height() - 8; Chris@133: Chris@274: m_yorigins[v] = yorigin; // for getYForValue etc Chris@274: m_heights[v] = h; Chris@274: Chris@274: if (h <= 0) return; Chris@274: Chris@133: QPainterPath path; Chris@133: Chris@805: int mh = m_sliceableModel->getHeight(); Chris@133: Chris@193: int divisor = 0; Chris@193: Chris@198: m_values.clear(); Chris@805: for (int bin = 0; bin < mh; ++bin) { Chris@906: m_values.push_back(0.0); Chris@193: } Chris@193: Chris@906: sv_frame_t f0 = v->getCentreFrame(); Chris@193: int f0x = v->getXForFrame(f0); Chris@195: f0 = v->getFrameForX(f0x); Chris@906: sv_frame_t f1 = v->getFrameForX(f0x + 1); Chris@195: if (f1 > f0) --f1; Chris@193: Chris@682: // cerr << "centre frame " << v->getCentreFrame() << ", x " << f0x << ", f0 " << f0 << ", f1 " << f1 << endl; Chris@274: Chris@805: int res = m_sliceableModel->getResolution(); Chris@906: int col0 = int(f0 / res); Chris@805: int col1 = col0; Chris@906: if (m_samplingMode != NearestSample) col1 = int(f1 / res); Chris@274: f0 = col0 * res; Chris@274: f1 = (col1 + 1) * res - 1; Chris@274: Chris@682: // cerr << "resolution " << res << ", col0 " << col0 << ", col1 " << col1 << ", f0 " << f0 << ", f1 " << f1 << endl; Chris@193: Chris@198: m_currentf0 = f0; Chris@198: m_currentf1 = f1; Chris@198: Chris@254: BiasCurve curve; Chris@254: getBiasCurve(curve); Chris@906: int cs = int(curve.size()); Chris@254: Chris@805: for (int col = col0; col <= col1; ++col) { Chris@805: for (int bin = 0; bin < mh; ++bin) { Chris@193: float value = m_sliceableModel->getValueAt(col, bin); Chris@254: if (bin < cs) value *= curve[bin]; Chris@193: if (m_samplingMode == SamplePeak) { Chris@198: if (value > m_values[bin]) m_values[bin] = value; Chris@193: } else { Chris@198: m_values[bin] += value; Chris@193: } Chris@153: } Chris@193: ++divisor; Chris@193: } Chris@193: Chris@906: float max = 0.0; Chris@805: for (int bin = 0; bin < mh; ++bin) { Chris@847: if (m_samplingMode == SampleMean && divisor > 0) { Chris@906: m_values[bin] /= float(divisor); Chris@847: } Chris@198: if (m_values[bin] > max) max = m_values[bin]; Chris@193: } Chris@906: if (max != 0.0 && m_normalize) { Chris@805: for (int bin = 0; bin < mh; ++bin) { Chris@198: m_values[bin] /= max; Chris@193: } Chris@193: } Chris@193: Chris@906: double nx = xorigin; Chris@193: Chris@197: ColourMapper mapper(m_colourMap, 0, 1); Chris@197: Chris@805: for (int bin = 0; bin < mh; ++bin) { Chris@193: Chris@906: double x = nx; Chris@198: nx = xorigin + getXForBin(bin + 1, mh, w); Chris@193: Chris@906: double value = m_values[bin]; Chris@906: double norm = 0.0; Chris@906: double y = getYForValue(value, v, norm); Chris@133: Chris@193: if (m_plotStyle == PlotLines) { Chris@193: Chris@193: if (bin == 0) { Chris@193: path.moveTo(x, y); Chris@193: } else { Chris@193: path.lineTo(x, y); Chris@193: } Chris@193: Chris@193: } else if (m_plotStyle == PlotSteps) { Chris@193: Chris@193: if (bin == 0) { Chris@193: path.moveTo(x, y); Chris@193: } else { Chris@193: path.lineTo(x, y); Chris@193: } Chris@193: path.lineTo(nx, y); Chris@193: Chris@193: } else if (m_plotStyle == PlotBlocks) { Chris@193: Chris@193: path.moveTo(x, yorigin); Chris@133: path.lineTo(x, y); Chris@193: path.lineTo(nx, y); Chris@193: path.lineTo(nx, yorigin); Chris@193: path.lineTo(x, yorigin); Chris@197: Chris@197: } else if (m_plotStyle == PlotFilledBlocks) { Chris@197: Chris@197: paint.fillRect(QRectF(x, y, nx - x, yorigin - y), mapper.map(norm)); Chris@133: } Chris@193: Chris@133: } Chris@133: Chris@197: if (m_plotStyle != PlotFilledBlocks) { Chris@197: paint.drawPath(path); Chris@197: } Chris@133: paint.restore(); Chris@198: /* Chris@197: QPoint discard; Chris@197: Chris@197: if (v->getViewManager() && v->getViewManager()->shouldShowFrameCount() && Chris@197: v->shouldIlluminateLocalFeatures(this, discard)) { Chris@195: Chris@195: int sampleRate = m_sliceableModel->getSampleRate(); Chris@195: Chris@195: QString startText = QString("%1 / %2") Chris@195: .arg(QString::fromStdString Chris@195: (RealTime::frame2RealTime Chris@195: (f0, sampleRate).toText(true))) Chris@195: .arg(f0); Chris@195: Chris@195: QString endText = QString(" %1 / %2") Chris@195: .arg(QString::fromStdString Chris@195: (RealTime::frame2RealTime Chris@195: (f1, sampleRate).toText(true))) Chris@195: .arg(f1); Chris@195: Chris@195: QString durationText = QString("(%1 / %2) ") Chris@195: .arg(QString::fromStdString Chris@195: (RealTime::frame2RealTime Chris@195: (f1 - f0 + 1, sampleRate).toText(true))) Chris@195: .arg(f1 - f0 + 1); Chris@195: Chris@195: v->drawVisibleText Chris@195: (paint, xorigin + 5, Chris@195: paint.fontMetrics().ascent() + 5, Chris@195: startText, View::OutlinedText); Chris@195: Chris@195: v->drawVisibleText Chris@195: (paint, xorigin + 5, Chris@195: paint.fontMetrics().ascent() + paint.fontMetrics().height() + 10, Chris@195: endText, View::OutlinedText); Chris@195: Chris@195: v->drawVisibleText Chris@195: (paint, xorigin + 5, Chris@195: paint.fontMetrics().ascent() + 2*paint.fontMetrics().height() + 15, Chris@195: durationText, View::OutlinedText); Chris@195: } Chris@195: */ Chris@195: } Chris@195: Chris@195: int Chris@918: SliceLayer::getVerticalScaleWidth(LayerGeometryProvider *, bool, QPainter &paint) const Chris@195: { Chris@538: if (m_energyScale == LinearScale || m_energyScale == AbsoluteScale) { Chris@220: return std::max(paint.fontMetrics().width("0.0") + 13, Chris@220: paint.fontMetrics().width("x10-10")); Chris@195: } else { Chris@195: return std::max(paint.fontMetrics().width(tr("0dB")), Chris@195: paint.fontMetrics().width(tr("-Inf"))) + 13; Chris@195: } Chris@195: } Chris@195: Chris@195: void Chris@918: SliceLayer::paintVerticalScale(LayerGeometryProvider *v, bool, QPainter &paint, QRect rect) const Chris@195: { Chris@906: double thresh = m_threshold; Chris@538: if (m_energyScale != LinearScale && m_energyScale != AbsoluteScale) { Chris@284: thresh = AudioLevel::dB_to_multiplier(getThresholdDb()); Chris@195: } Chris@195: Chris@195: // int h = (rect.height() * 3) / 4; Chris@195: // int y = (rect.height() / 2) - (h / 2); Chris@195: Chris@918: int yorigin = v->getPaintHeight() - 20 - paint.fontMetrics().height() - 6; Chris@195: int h = yorigin - paint.fontMetrics().height() - 8; Chris@195: if (h < 0) return; Chris@195: Chris@195: QRect actual(rect.x(), rect.y() + yorigin - h, rect.width(), h); Chris@195: Chris@220: int mult = 1; Chris@220: Chris@195: PaintAssistant::paintVerticalLevelScale Chris@195: (paint, actual, thresh, 1.0 / m_gain, Chris@195: PaintAssistant::Scale(m_energyScale), Chris@220: mult, Chris@195: const_cast *>(&m_scalePoints)); Chris@220: Chris@220: if (mult != 1 && mult != 0) { Chris@906: int log = int(lrint(log10(mult))); Chris@220: QString a = tr("x10"); Chris@220: QString b = QString("%1").arg(-log); Chris@220: paint.drawText(3, 8 + paint.fontMetrics().ascent(), a); Chris@220: paint.drawText(3 + paint.fontMetrics().width(a), Chris@220: 3 + paint.fontMetrics().ascent(), b); Chris@220: } Chris@133: } Chris@133: Chris@153: Layer::PropertyList Chris@193: SliceLayer::getProperties() const Chris@153: { Chris@287: PropertyList list = SingleColourLayer::getProperties(); Chris@538: list.push_back("Bin Scale"); Chris@193: list.push_back("Plot Type"); Chris@153: list.push_back("Scale"); Chris@153: list.push_back("Normalize"); Chris@284: list.push_back("Threshold"); Chris@153: list.push_back("Gain"); Chris@153: Chris@153: return list; Chris@153: } Chris@153: Chris@153: QString Chris@193: SliceLayer::getPropertyLabel(const PropertyName &name) const Chris@153: { Chris@193: if (name == "Plot Type") return tr("Plot Type"); Chris@290: if (name == "Scale") return tr("Scale"); Chris@153: if (name == "Normalize") return tr("Normalize"); Chris@284: if (name == "Threshold") return tr("Threshold"); Chris@153: if (name == "Gain") return tr("Gain"); Chris@193: if (name == "Sampling Mode") return tr("Sampling Mode"); Chris@538: if (name == "Bin Scale") return tr("Bin Scale"); Chris@287: return SingleColourLayer::getPropertyLabel(name); Chris@153: } Chris@153: Chris@335: QString Chris@335: SliceLayer::getPropertyIconName(const PropertyName &name) const Chris@335: { Chris@335: if (name == "Normalize") return "normalise"; Chris@335: return ""; Chris@335: } Chris@335: Chris@153: Layer::PropertyType Chris@193: SliceLayer::getPropertyType(const PropertyName &name) const Chris@153: { Chris@153: if (name == "Gain") return RangeProperty; Chris@153: if (name == "Normalize") return ToggleProperty; Chris@284: if (name == "Threshold") return RangeProperty; Chris@287: if (name == "Plot Type") return ValueProperty; Chris@290: if (name == "Scale") return ValueProperty; Chris@287: if (name == "Sampling Mode") return ValueProperty; Chris@287: if (name == "Bin Scale") return ValueProperty; Chris@287: if (name == "Colour" && m_plotStyle == PlotFilledBlocks) return ValueProperty; Chris@287: return SingleColourLayer::getPropertyType(name); Chris@153: } Chris@153: Chris@153: QString Chris@193: SliceLayer::getPropertyGroupName(const PropertyName &name) const Chris@153: { Chris@153: if (name == "Scale" || Chris@153: name == "Normalize" || Chris@193: name == "Sampling Mode" || Chris@284: name == "Threshold" || Chris@193: name == "Gain") return tr("Scale"); Chris@193: if (name == "Plot Type" || Chris@538: name == "Bin Scale") return tr("Bins"); Chris@287: return SingleColourLayer::getPropertyGroupName(name); Chris@153: } Chris@153: Chris@153: int Chris@193: SliceLayer::getPropertyRangeAndValue(const PropertyName &name, Chris@216: int *min, int *max, int *deflt) const Chris@153: { Chris@216: int val = 0; Chris@153: Chris@216: int garbage0, garbage1, garbage2; Chris@153: if (!min) min = &garbage0; Chris@153: if (!max) max = &garbage1; Chris@248: if (!deflt) deflt = &garbage2; Chris@153: Chris@153: if (name == "Gain") { Chris@153: Chris@153: *min = -50; Chris@153: *max = 50; Chris@216: *deflt = 0; Chris@153: Chris@682: cerr << "gain is " << m_gain << ", mode is " << m_samplingMode << endl; Chris@193: Chris@906: val = int(lrint(log10(m_gain) * 20.0)); Chris@216: if (val < *min) val = *min; Chris@216: if (val > *max) val = *max; Chris@153: Chris@284: } else if (name == "Threshold") { Chris@284: Chris@284: *min = -80; Chris@284: *max = 0; Chris@284: Chris@906: *deflt = int(lrint(AudioLevel::multiplier_to_dB(m_initialThreshold))); Chris@284: if (*deflt < *min) *deflt = *min; Chris@284: if (*deflt > *max) *deflt = *max; Chris@284: Chris@906: val = int(lrint(AudioLevel::multiplier_to_dB(m_threshold))); Chris@284: if (val < *min) val = *min; Chris@284: if (val > *max) val = *max; Chris@284: Chris@153: } else if (name == "Normalize") { Chris@153: Chris@216: val = (m_normalize ? 1 : 0); Chris@216: *deflt = 0; Chris@153: Chris@287: } else if (name == "Colour" && m_plotStyle == PlotFilledBlocks) { Chris@197: Chris@287: *min = 0; Chris@287: *max = ColourMapper::getColourMapCount() - 1; Chris@287: *deflt = 0; Chris@287: Chris@287: val = m_colourMap; Chris@153: Chris@153: } else if (name == "Scale") { Chris@153: Chris@153: *min = 0; Chris@538: *max = 3; Chris@216: *deflt = (int)dBScale; Chris@153: Chris@216: val = (int)m_energyScale; Chris@153: Chris@193: } else if (name == "Sampling Mode") { Chris@153: Chris@153: *min = 0; Chris@193: *max = 2; Chris@216: *deflt = (int)SampleMean; Chris@193: Chris@216: val = (int)m_samplingMode; Chris@153: Chris@193: } else if (name == "Plot Type") { Chris@193: Chris@193: *min = 0; Chris@197: *max = 3; Chris@216: *deflt = (int)PlotSteps; Chris@193: Chris@216: val = (int)m_plotStyle; Chris@193: Chris@193: } else if (name == "Bin Scale") { Chris@193: Chris@193: *min = 0; Chris@198: *max = 2; Chris@216: *deflt = (int)LinearBins; Chris@198: // *max = 1; // I don't think we really do want to offer inverted log Chris@193: Chris@216: val = (int)m_binScale; Chris@193: Chris@153: } else { Chris@287: val = SingleColourLayer::getPropertyRangeAndValue(name, min, max, deflt); Chris@153: } Chris@153: Chris@216: return val; Chris@153: } Chris@153: Chris@153: QString Chris@193: SliceLayer::getPropertyValueLabel(const PropertyName &name, Chris@153: int value) const Chris@153: { Chris@287: if (name == "Colour" && m_plotStyle == PlotFilledBlocks) { Chris@287: return ColourMapper::getColourMapName(value); Chris@153: } Chris@153: if (name == "Scale") { Chris@153: switch (value) { Chris@153: default: Chris@153: case 0: return tr("Linear"); Chris@153: case 1: return tr("Meter"); Chris@538: case 2: return tr("Log"); Chris@538: case 3: return tr("Absolute"); Chris@153: } Chris@153: } Chris@193: if (name == "Sampling Mode") { Chris@153: switch (value) { Chris@153: default: Chris@193: case 0: return tr("Any"); Chris@193: case 1: return tr("Mean"); Chris@193: case 2: return tr("Peak"); Chris@193: } Chris@193: } Chris@193: if (name == "Plot Type") { Chris@193: switch (value) { Chris@193: default: Chris@193: case 0: return tr("Lines"); Chris@193: case 1: return tr("Steps"); Chris@193: case 2: return tr("Blocks"); Chris@197: case 3: return tr("Colours"); Chris@193: } Chris@193: } Chris@193: if (name == "Bin Scale") { Chris@193: switch (value) { Chris@193: default: Chris@538: case 0: return tr("Linear"); Chris@538: case 1: return tr("Log"); Chris@538: case 2: return tr("Rev Log"); Chris@153: } Chris@153: } Chris@287: return SingleColourLayer::getPropertyValueLabel(name, value); Chris@153: } Chris@153: Chris@167: RangeMapper * Chris@193: SliceLayer::getNewPropertyRangeMapper(const PropertyName &name) const Chris@167: { Chris@167: if (name == "Gain") { Chris@167: return new LinearRangeMapper(-50, 50, -25, 25, tr("dB")); Chris@167: } Chris@284: if (name == "Threshold") { Chris@284: return new LinearRangeMapper(-80, 0, -80, 0, tr("dB")); Chris@284: } Chris@287: return SingleColourLayer::getNewPropertyRangeMapper(name); Chris@167: } Chris@167: Chris@133: void Chris@193: SliceLayer::setProperty(const PropertyName &name, int value) Chris@133: { Chris@153: if (name == "Gain") { Chris@906: setGain(powf(10, float(value)/20.0f)); Chris@284: } else if (name == "Threshold") { Chris@906: if (value == -80) setThreshold(0.0f); Chris@906: else setThreshold(float(AudioLevel::dB_to_multiplier(value))); Chris@287: } else if (name == "Colour" && m_plotStyle == PlotFilledBlocks) { Chris@287: setFillColourMap(value); Chris@153: } else if (name == "Scale") { Chris@153: switch (value) { Chris@153: default: Chris@153: case 0: setEnergyScale(LinearScale); break; Chris@153: case 1: setEnergyScale(MeterScale); break; Chris@153: case 2: setEnergyScale(dBScale); break; Chris@538: case 3: setEnergyScale(AbsoluteScale); break; Chris@153: } Chris@193: } else if (name == "Plot Type") { Chris@193: setPlotStyle(PlotStyle(value)); Chris@193: } else if (name == "Sampling Mode") { Chris@193: switch (value) { Chris@193: default: Chris@193: case 0: setSamplingMode(NearestSample); break; Chris@193: case 1: setSamplingMode(SampleMean); break; Chris@193: case 2: setSamplingMode(SamplePeak); break; Chris@193: } Chris@193: } else if (name == "Bin Scale") { Chris@193: switch (value) { Chris@193: default: Chris@193: case 0: setBinScale(LinearBins); break; Chris@193: case 1: setBinScale(LogBins); break; Chris@193: case 2: setBinScale(InvertedLogBins); break; Chris@193: } Chris@153: } else if (name == "Normalize") { Chris@153: setNormalize(value ? true : false); Chris@287: } else { Chris@287: SingleColourLayer::setProperty(name, value); Chris@153: } Chris@153: } Chris@153: Chris@153: void Chris@197: SliceLayer::setFillColourMap(int map) Chris@197: { Chris@197: if (m_colourMap == map) return; Chris@197: m_colourMap = map; Chris@197: emit layerParametersChanged(); Chris@197: } Chris@197: Chris@197: void Chris@193: SliceLayer::setEnergyScale(EnergyScale scale) Chris@153: { Chris@153: if (m_energyScale == scale) return; Chris@153: m_energyScale = scale; Chris@153: emit layerParametersChanged(); Chris@153: } Chris@153: Chris@153: void Chris@193: SliceLayer::setSamplingMode(SamplingMode mode) Chris@153: { Chris@193: if (m_samplingMode == mode) return; Chris@193: m_samplingMode = mode; Chris@153: emit layerParametersChanged(); Chris@153: } Chris@153: Chris@153: void Chris@193: SliceLayer::setPlotStyle(PlotStyle style) Chris@153: { Chris@193: if (m_plotStyle == style) return; Chris@197: bool colourTypeChanged = (style == PlotFilledBlocks || Chris@197: m_plotStyle == PlotFilledBlocks); Chris@193: m_plotStyle = style; Chris@197: if (colourTypeChanged) { Chris@197: emit layerParameterRangesChanged(); Chris@197: } Chris@153: emit layerParametersChanged(); Chris@153: } Chris@153: Chris@153: void Chris@193: SliceLayer::setBinScale(BinScale scale) Chris@153: { Chris@193: if (m_binScale == scale) return; Chris@193: m_binScale = scale; Chris@153: emit layerParametersChanged(); Chris@153: } Chris@153: Chris@153: void Chris@193: SliceLayer::setNormalize(bool n) Chris@153: { Chris@153: if (m_normalize == n) return; Chris@153: m_normalize = n; Chris@153: emit layerParametersChanged(); Chris@153: } Chris@153: Chris@153: void Chris@284: SliceLayer::setThreshold(float thresh) Chris@284: { Chris@284: if (m_threshold == thresh) return; Chris@284: m_threshold = thresh; Chris@284: emit layerParametersChanged(); Chris@284: } Chris@284: Chris@284: void Chris@193: SliceLayer::setGain(float gain) Chris@153: { Chris@153: if (m_gain == gain) return; Chris@153: m_gain = gain; Chris@153: emit layerParametersChanged(); Chris@153: } Chris@153: Chris@284: float Chris@284: SliceLayer::getThresholdDb() const Chris@284: { Chris@284: if (m_threshold == 0.0) return -80.f; Chris@906: float db = float(AudioLevel::multiplier_to_dB(m_threshold)); Chris@284: return db; Chris@284: } Chris@284: Chris@287: int Chris@287: SliceLayer::getDefaultColourHint(bool darkbg, bool &impose) Chris@287: { Chris@287: impose = false; Chris@287: return ColourDatabase::getInstance()->getColourIndex Chris@287: (QString(darkbg ? "Bright Blue" : "Blue")); Chris@287: } Chris@287: Chris@316: void Chris@316: SliceLayer::toXml(QTextStream &stream, Chris@316: QString indent, QString extraAttributes) const Chris@153: { Chris@153: QString s; Chris@153: Chris@287: s += QString("colourScheme=\"%1\" " Chris@287: "energyScale=\"%2\" " Chris@287: "samplingMode=\"%3\" " Chris@598: "plotStyle=\"%4\" " Chris@598: "binScale=\"%5\" " Chris@598: "gain=\"%6\" " Chris@598: "threshold=\"%7\" " Chris@598: "normalize=\"%8\"") Chris@197: .arg(m_colourMap) Chris@153: .arg(m_energyScale) Chris@193: .arg(m_samplingMode) Chris@598: .arg(m_plotStyle) Chris@598: .arg(m_binScale) Chris@153: .arg(m_gain) Chris@598: .arg(m_threshold) Chris@153: .arg(m_normalize ? "true" : "false"); Chris@153: Chris@316: SingleColourLayer::toXml(stream, indent, extraAttributes + " " + s); Chris@153: } Chris@153: Chris@153: void Chris@193: SliceLayer::setProperties(const QXmlAttributes &attributes) Chris@153: { Chris@153: bool ok = false; Chris@153: Chris@287: SingleColourLayer::setProperties(attributes); Chris@153: Chris@153: EnergyScale scale = (EnergyScale) Chris@153: attributes.value("energyScale").toInt(&ok); Chris@153: if (ok) setEnergyScale(scale); Chris@153: Chris@193: SamplingMode mode = (SamplingMode) Chris@193: attributes.value("samplingMode").toInt(&ok); Chris@193: if (ok) setSamplingMode(mode); Chris@153: Chris@197: int colourMap = attributes.value("colourScheme").toInt(&ok); Chris@197: if (ok) setFillColourMap(colourMap); Chris@197: Chris@598: PlotStyle s = (PlotStyle) Chris@598: attributes.value("plotStyle").toInt(&ok); Chris@598: if (ok) setPlotStyle(s); Chris@598: Chris@598: BinScale b = (BinScale) Chris@598: attributes.value("binScale").toInt(&ok); Chris@598: if (ok) setBinScale(b); Chris@598: Chris@153: float gain = attributes.value("gain").toFloat(&ok); Chris@153: if (ok) setGain(gain); Chris@153: Chris@598: float threshold = attributes.value("threshold").toFloat(&ok); Chris@598: if (ok) setThreshold(threshold); Chris@598: Chris@153: bool normalize = (attributes.value("normalize").trimmed() == "true"); Chris@153: setNormalize(normalize); Chris@133: } Chris@133: Chris@133: bool Chris@906: SliceLayer::getValueExtents(double &, double &, bool &, QString &) const Chris@133: { Chris@133: return false; Chris@133: } Chris@133: