Chris@58: /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */ Chris@0: Chris@0: /* Chris@59: Sonic Visualiser Chris@59: An audio file viewer and annotation editor. Chris@59: Centre for Digital Music, Queen Mary, University of London. Chris@182: This file copyright 2006 Chris Cannam and QMUL. Chris@0: Chris@59: This program is free software; you can redistribute it and/or Chris@59: modify it under the terms of the GNU General Public License as Chris@59: published by the Free Software Foundation; either version 2 of the Chris@59: License, or (at your option) any later version. See the file Chris@59: COPYING included with this distribution for more information. Chris@0: */ Chris@0: Chris@0: #include "WaveformLayer.h" Chris@0: Chris@0: #include "base/AudioLevel.h" Chris@128: #include "view/View.h" Chris@0: #include "base/Profiler.h" Chris@167: #include "base/RangeMapper.h" Chris@1147: #include "base/Strings.h" Chris@1078: Chris@376: #include "ColourDatabase.h" Chris@1078: #include "PaintAssistant.h" Chris@1340: Chris@1340: #include "data/model/WaveformOversampler.h" Chris@0: Chris@0: #include Chris@0: #include Chris@316: #include Chris@0: Chris@0: #include Chris@0: #include Chris@0: Chris@1352: //#define DEBUG_WAVEFORM_PAINT 1 Chris@1338: //#define DEBUG_WAVEFORM_PAINT_BY_PIXEL 1 Chris@4: Chris@1333: using std::vector; Chris@682: Chris@0: Chris@44: WaveformLayer::WaveformLayer() : Chris@287: SingleColourLayer(), Chris@0: m_model(0), Chris@0: m_gain(1.0f), Chris@67: m_autoNormalize(false), Chris@0: m_showMeans(true), Chris@0: m_greyscale(true), Chris@0: m_channelMode(SeparateChannels), Chris@0: m_channel(-1), Chris@0: m_scale(LinearScale), Chris@709: m_middleLineHeight(0.5), Chris@0: m_aggressive(false), Chris@0: m_cache(0), Chris@1325: m_cacheValid(false) Chris@0: { Chris@0: } Chris@0: Chris@0: WaveformLayer::~WaveformLayer() Chris@0: { Chris@0: delete m_cache; Chris@0: } Chris@0: Chris@0: void Chris@0: WaveformLayer::setModel(const RangeSummarisableTimeValueModel *model) Chris@0: { Chris@69: bool channelsChanged = false; Chris@69: if (m_channel == -1) { Chris@69: if (!m_model) { Chris@69: if (model) { Chris@69: channelsChanged = true; Chris@69: } Chris@69: } else { Chris@69: if (model && Chris@69: m_model->getChannelCount() != model->getChannelCount()) { Chris@69: channelsChanged = true; Chris@69: } Chris@69: } Chris@69: } Chris@69: Chris@0: m_model = model; Chris@0: m_cacheValid = false; Chris@0: if (!m_model || !m_model->isOK()) return; Chris@0: Chris@320: connectSignals(m_model); Chris@301: Chris@0: emit modelReplaced(); Chris@69: Chris@69: if (channelsChanged) emit layerParametersChanged(); Chris@0: } Chris@0: Chris@0: Layer::PropertyList Chris@0: WaveformLayer::getProperties() const Chris@0: { Chris@287: PropertyList list = SingleColourLayer::getProperties(); Chris@87: list.push_back("Scale"); Chris@87: list.push_back("Gain"); Chris@87: list.push_back("Normalize Visible Area"); Chris@68: Chris@68: if (m_model && m_model->getChannelCount() > 1 && m_channel == -1) { Chris@87: list.push_back("Channels"); Chris@68: } Chris@68: Chris@0: return list; Chris@0: } Chris@0: Chris@87: QString Chris@87: WaveformLayer::getPropertyLabel(const PropertyName &name) const Chris@87: { Chris@87: if (name == "Scale") return tr("Scale"); Chris@87: if (name == "Gain") return tr("Gain"); Chris@87: if (name == "Normalize Visible Area") return tr("Normalize Visible Area"); Chris@87: if (name == "Channels") return tr("Channels"); Chris@287: return SingleColourLayer::getPropertyLabel(name); Chris@87: } Chris@87: Chris@335: QString Chris@335: WaveformLayer::getPropertyIconName(const PropertyName &name) const Chris@335: { Chris@335: if (name == "Normalize Visible Area") return "normalise"; Chris@335: return ""; Chris@335: } Chris@335: Chris@0: Layer::PropertyType Chris@0: WaveformLayer::getPropertyType(const PropertyName &name) const Chris@0: { Chris@87: if (name == "Gain") return RangeProperty; Chris@87: if (name == "Normalize Visible Area") return ToggleProperty; Chris@87: if (name == "Channels") return ValueProperty; Chris@87: if (name == "Scale") return ValueProperty; Chris@287: return SingleColourLayer::getPropertyType(name); Chris@0: } Chris@0: Chris@0: QString Chris@0: WaveformLayer::getPropertyGroupName(const PropertyName &name) const Chris@0: { Chris@87: if (name == "Gain" || Chris@87: name == "Normalize Visible Area" || Chris@1266: name == "Scale") return tr("Scale"); Chris@0: return QString(); Chris@0: } Chris@0: Chris@0: int Chris@0: WaveformLayer::getPropertyRangeAndValue(const PropertyName &name, Chris@216: int *min, int *max, int *deflt) const Chris@0: { Chris@216: int val = 0; Chris@0: Chris@216: int garbage0, garbage1, garbage2; Chris@56: if (!min) min = &garbage0; Chris@56: if (!max) max = &garbage1; Chris@216: if (!deflt) deflt = &garbage2; Chris@10: Chris@87: if (name == "Gain") { Chris@0: Chris@1266: *min = -50; Chris@1266: *max = 50; Chris@216: *deflt = 0; Chris@0: Chris@1266: val = int(lrint(log10(m_gain) * 20.0)); Chris@1266: if (val < *min) val = *min; Chris@1266: if (val > *max) val = *max; Chris@0: Chris@87: } else if (name == "Normalize Visible Area") { Chris@67: Chris@216: val = (m_autoNormalize ? 1 : 0); Chris@216: *deflt = 0; Chris@67: Chris@87: } else if (name == "Channels") { Chris@0: Chris@67: *min = 0; Chris@67: *max = 2; Chris@216: *deflt = 0; Chris@216: if (m_channelMode == MixChannels) val = 1; Chris@216: else if (m_channelMode == MergeChannels) val = 2; Chris@216: else val = 0; Chris@0: Chris@87: } else if (name == "Scale") { Chris@0: Chris@1266: *min = 0; Chris@1266: *max = 2; Chris@216: *deflt = 0; Chris@0: Chris@1266: val = (int)m_scale; Chris@0: Chris@0: } else { Chris@1266: val = SingleColourLayer::getPropertyRangeAndValue(name, min, max, deflt); Chris@0: } Chris@0: Chris@216: return val; Chris@0: } Chris@0: Chris@0: QString Chris@0: WaveformLayer::getPropertyValueLabel(const PropertyName &name, Chris@1266: int value) const Chris@0: { Chris@87: if (name == "Scale") { Chris@1266: switch (value) { Chris@1266: default: Chris@1266: case 0: return tr("Linear"); Chris@1266: case 1: return tr("Meter"); Chris@1266: case 2: return tr("dB"); Chris@1266: } Chris@0: } Chris@87: if (name == "Channels") { Chris@67: switch (value) { Chris@67: default: Chris@67: case 0: return tr("Separate"); Chris@67: case 1: return tr("Mean"); Chris@67: case 2: return tr("Butterfly"); Chris@67: } Chris@67: } Chris@287: return SingleColourLayer::getPropertyValueLabel(name, value); Chris@0: } Chris@0: Chris@167: RangeMapper * Chris@167: WaveformLayer::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@167: return 0; Chris@167: } Chris@167: Chris@0: void Chris@0: WaveformLayer::setProperty(const PropertyName &name, int value) Chris@0: { Chris@87: if (name == "Gain") { Chris@1266: setGain(float(pow(10, float(value)/20.0))); Chris@87: } else if (name == "Normalize Visible Area") { Chris@67: setAutoNormalize(value ? true : false); Chris@87: } else if (name == "Channels") { Chris@67: if (value == 1) setChannelMode(MixChannels); Chris@67: else if (value == 2) setChannelMode(MergeChannels); Chris@67: else setChannelMode(SeparateChannels); Chris@87: } else if (name == "Scale") { Chris@1266: switch (value) { Chris@1266: default: Chris@1266: case 0: setScale(LinearScale); break; Chris@1266: case 1: setScale(MeterScale); break; Chris@1266: case 2: setScale(dBScale); break; Chris@1266: } Chris@287: } else { Chris@287: SingleColourLayer::setProperty(name, value); Chris@0: } Chris@0: } Chris@0: Chris@0: void Chris@67: WaveformLayer::setGain(float gain) Chris@0: { Chris@0: if (m_gain == gain) return; Chris@0: m_gain = gain; Chris@0: m_cacheValid = false; Chris@0: emit layerParametersChanged(); Chris@133: emit verticalZoomChanged(); Chris@0: } Chris@0: Chris@0: void Chris@67: WaveformLayer::setAutoNormalize(bool autoNormalize) Chris@67: { Chris@67: if (m_autoNormalize == autoNormalize) return; Chris@67: m_autoNormalize = autoNormalize; Chris@67: m_cacheValid = false; Chris@67: emit layerParametersChanged(); Chris@67: } Chris@67: Chris@67: void Chris@0: WaveformLayer::setShowMeans(bool showMeans) Chris@0: { Chris@0: if (m_showMeans == showMeans) return; Chris@0: m_showMeans = showMeans; Chris@0: m_cacheValid = false; Chris@0: emit layerParametersChanged(); Chris@0: } Chris@0: Chris@0: void Chris@0: WaveformLayer::setUseGreyscale(bool useGreyscale) Chris@0: { Chris@0: if (m_greyscale == useGreyscale) return; Chris@0: m_greyscale = useGreyscale; Chris@0: m_cacheValid = false; Chris@0: emit layerParametersChanged(); Chris@0: } Chris@0: Chris@0: void Chris@0: WaveformLayer::setChannelMode(ChannelMode channelMode) Chris@0: { Chris@0: if (m_channelMode == channelMode) return; Chris@0: m_channelMode = channelMode; Chris@0: m_cacheValid = false; Chris@0: emit layerParametersChanged(); Chris@0: } Chris@0: Chris@0: void Chris@0: WaveformLayer::setChannel(int channel) Chris@0: { Chris@587: // SVDEBUG << "WaveformLayer::setChannel(" << channel << ")" << endl; Chris@0: Chris@0: if (m_channel == channel) return; Chris@0: m_channel = channel; Chris@0: m_cacheValid = false; Chris@0: emit layerParametersChanged(); Chris@0: } Chris@0: Chris@0: void Chris@0: WaveformLayer::setScale(Scale scale) Chris@0: { Chris@0: if (m_scale == scale) return; Chris@0: m_scale = scale; Chris@0: m_cacheValid = false; Chris@0: emit layerParametersChanged(); Chris@0: } Chris@0: Chris@0: void Chris@908: WaveformLayer::setMiddleLineHeight(double height) Chris@709: { Chris@709: if (m_middleLineHeight == height) return; Chris@709: m_middleLineHeight = height; Chris@709: m_cacheValid = false; Chris@709: emit layerParametersChanged(); Chris@709: } Chris@709: Chris@709: void Chris@0: WaveformLayer::setAggressiveCacheing(bool aggressive) Chris@0: { Chris@0: if (m_aggressive == aggressive) return; Chris@0: m_aggressive = aggressive; Chris@0: m_cacheValid = false; Chris@0: emit layerParametersChanged(); Chris@0: } Chris@0: Chris@0: int Chris@918: WaveformLayer::getCompletion(LayerGeometryProvider *) const Chris@0: { Chris@0: int completion = 100; Chris@0: if (!m_model || !m_model->isOK()) return completion; Chris@0: if (m_model->isReady(&completion)) return 100; Chris@0: return completion; Chris@0: } Chris@0: Chris@79: bool Chris@908: WaveformLayer::getValueExtents(double &min, double &max, Chris@248: bool &, QString &unit) const Chris@79: { Chris@79: if (m_scale == LinearScale) { Chris@79: min = 0.0; Chris@79: max = 1.0; Chris@79: unit = "V"; Chris@79: } else if (m_scale == MeterScale) { Chris@79: return false; //!!! Chris@79: } else { Chris@79: min = AudioLevel::multiplier_to_dB(0.0); Chris@79: max = AudioLevel::multiplier_to_dB(1.0); Chris@79: unit = "dB"; Chris@79: } Chris@79: return true; Chris@79: } Chris@79: Chris@0: int Chris@908: WaveformLayer::dBscale(double sample, int m) const Chris@0: { Chris@67: if (sample < 0.0) return dBscale(-sample, m); Chris@908: double dB = AudioLevel::multiplier_to_dB(sample); Chris@0: if (dB < -50.0) return 0; Chris@0: if (dB > 0.0) return m; Chris@0: return int(((dB + 50.0) * m) / 50.0 + 0.1); Chris@0: } Chris@0: Chris@805: int Chris@805: WaveformLayer::getChannelArrangement(int &min, int &max, Chris@67: bool &merging, bool &mixing) Chris@0: const Chris@0: { Chris@0: if (!m_model || !m_model->isOK()) return 0; Chris@0: Chris@805: int channels = m_model->getChannelCount(); Chris@0: if (channels == 0) return 0; Chris@0: Chris@805: int rawChannels = channels; Chris@0: Chris@0: if (m_channel == -1) { Chris@1266: min = 0; Chris@1266: if (m_channelMode == MergeChannels || Chris@67: m_channelMode == MixChannels) { Chris@1266: max = 0; Chris@1266: channels = 1; Chris@1266: } else { Chris@1266: max = channels - 1; Chris@1266: } Chris@0: } else { Chris@1266: min = m_channel; Chris@1266: max = m_channel; Chris@1266: rawChannels = 1; Chris@1266: channels = 1; Chris@0: } Chris@0: Chris@0: merging = (m_channelMode == MergeChannels && rawChannels > 1); Chris@67: mixing = (m_channelMode == MixChannels && rawChannels > 1); Chris@0: Chris@587: // SVDEBUG << "WaveformLayer::getChannelArrangement: min " << min << ", max " << max << ", merging " << merging << ", channels " << channels << endl; Chris@0: Chris@0: return channels; Chris@0: } Chris@0: Chris@67: bool Chris@918: WaveformLayer::isLayerScrollable(const LayerGeometryProvider *) const Chris@67: { Chris@67: return !m_autoNormalize; Chris@67: } Chris@67: Chris@68: static float meterdbs[] = { -40, -30, -20, -15, -10, Chris@68: -5, -3, -2, -1, -0.5, 0 }; Chris@68: Chris@365: bool Chris@1336: WaveformLayer::getSourceFramesForX(LayerGeometryProvider *v, Chris@1336: int x, int modelZoomLevel, Chris@908: sv_frame_t &f0, sv_frame_t &f1) const Chris@365: { Chris@908: sv_frame_t viewFrame = v->getFrameForX(x); Chris@365: if (viewFrame < 0) { Chris@365: f0 = 0; Chris@365: f1 = 0; Chris@365: return false; Chris@365: } Chris@365: Chris@365: f0 = viewFrame; Chris@365: f0 = f0 / modelZoomLevel; Chris@365: f0 = f0 * modelZoomLevel; Chris@365: Chris@1341: if (v->getZoomLevel().zone == ZoomLevel::PixelsPerFrame) { Chris@1341: f1 = f0 + 1; Chris@1341: } else { Chris@1341: viewFrame = v->getFrameForX(x + 1); Chris@1341: f1 = viewFrame; Chris@1341: f1 = f1 / modelZoomLevel; Chris@1341: f1 = f1 * modelZoomLevel; Chris@1341: } Chris@365: Chris@365: return (f0 < m_model->getEndFrame()); Chris@365: } Chris@365: Chris@365: float Chris@918: WaveformLayer::getNormalizeGain(LayerGeometryProvider *v, int channel) const Chris@365: { Chris@908: sv_frame_t startFrame = v->getStartFrame(); Chris@908: sv_frame_t endFrame = v->getEndFrame(); Chris@365: Chris@908: sv_frame_t modelStart = m_model->getStartFrame(); Chris@908: sv_frame_t modelEnd = m_model->getEndFrame(); Chris@365: Chris@908: sv_frame_t rangeStart, rangeEnd; Chris@365: Chris@365: if (startFrame < modelStart) rangeStart = modelStart; Chris@365: else rangeStart = startFrame; Chris@365: Chris@365: if (endFrame < 0) rangeEnd = 0; Chris@365: else if (endFrame > modelEnd) rangeEnd = modelEnd; Chris@365: else rangeEnd = endFrame; Chris@365: Chris@365: if (rangeEnd < rangeStart) rangeEnd = rangeStart; Chris@365: Chris@365: RangeSummarisableTimeValueModel::Range range = Chris@365: m_model->getSummary(channel, rangeStart, rangeEnd - rangeStart); Chris@365: Chris@805: int minChannel = 0, maxChannel = 0; Chris@365: bool mergingChannels = false, mixingChannels = false; Chris@365: Chris@859: (void)getChannelArrangement(minChannel, maxChannel, Chris@859: mergingChannels, mixingChannels); Chris@365: Chris@365: if (mergingChannels || mixingChannels) { Chris@365: RangeSummarisableTimeValueModel::Range otherRange = Chris@365: m_model->getSummary(1, rangeStart, rangeEnd - rangeStart); Chris@386: range.setMax(std::max(range.max(), otherRange.max())); Chris@386: range.setMin(std::min(range.min(), otherRange.min())); Chris@386: range.setAbsmean(std::min(range.absmean(), otherRange.absmean())); Chris@365: } Chris@365: Chris@908: return float(1.0 / std::max(fabs(range.max()), fabs(range.min()))); Chris@365: } Chris@365: Chris@0: void Chris@916: WaveformLayer::paint(LayerGeometryProvider *v, QPainter &viewPainter, QRect rect) const Chris@0: { Chris@0: if (!m_model || !m_model->isOK()) { Chris@1266: return; Chris@0: } Chris@0: Chris@1325: ZoomLevel zoomLevel = v->getZoomLevel(); Chris@0: Chris@2: #ifdef DEBUG_WAVEFORM_PAINT Chris@0: Profiler profiler("WaveformLayer::paint", true); Chris@1338: SVCERR << "WaveformLayer::paint (" << rect.x() << "," << rect.y() Chris@1266: << ") [" << rect.width() << "x" << rect.height() << "]: zoom " << zoomLevel << endl; Chris@2: #endif Chris@0: Chris@805: int channels = 0, minChannel = 0, maxChannel = 0; Chris@67: bool mergingChannels = false, mixingChannels = false; Chris@0: Chris@67: channels = getChannelArrangement(minChannel, maxChannel, Chris@67: mergingChannels, mixingChannels); Chris@0: if (channels == 0) return; Chris@0: Chris@915: int w = v->getPaintWidth(); Chris@915: int h = v->getPaintHeight(); Chris@0: Chris@0: QPainter *paint; Chris@0: Chris@0: if (m_aggressive) { Chris@0: Chris@214: #ifdef DEBUG_WAVEFORM_PAINT Chris@1338: SVCERR << "WaveformLayer::paint: aggressive is true" << endl; Chris@214: #endif Chris@214: Chris@1325: using namespace std::rel_ops; Chris@1325: Chris@1266: if (m_cacheValid && (zoomLevel != m_cacheZoomLevel)) { Chris@1266: m_cacheValid = false; Chris@1266: } Chris@0: Chris@1266: if (!m_cache || m_cache->width() != w || m_cache->height() != h) { Chris@214: #ifdef DEBUG_WAVEFORM_PAINT Chris@214: if (m_cache) { Chris@1338: SVCERR << "WaveformLayer::paint: cache size " << m_cache->width() << "x" << m_cache->height() << " differs from view size " << w << "x" << h << ": regenerating aggressive cache" << endl; Chris@214: } Chris@214: #endif Chris@1266: delete m_cache; Chris@1266: m_cache = new QPixmap(w, h); Chris@214: m_cacheValid = false; Chris@1266: } Chris@214: Chris@1266: if (m_cacheValid) { Chris@1266: viewPainter.drawPixmap(rect, *m_cache, rect); Chris@1266: return; Chris@1266: } Chris@0: Chris@1266: paint = new QPainter(m_cache); Chris@0: Chris@1266: paint->setPen(Qt::NoPen); Chris@1266: paint->setBrush(getBackgroundQColor(v)); Chris@1266: paint->drawRect(rect); Chris@0: Chris@1266: paint->setPen(getForegroundQColor(v)); Chris@1266: paint->setBrush(Qt::NoBrush); Chris@0: Chris@0: } else { Chris@1266: paint = &viewPainter; Chris@0: } Chris@0: Chris@28: paint->setRenderHint(QPainter::Antialiasing, false); Chris@28: Chris@709: if (m_middleLineHeight != 0.5) { Chris@709: paint->save(); Chris@908: double space = m_middleLineHeight * 2; Chris@709: if (space > 1.0) space = 2.0 - space; Chris@908: double yt = h * (m_middleLineHeight - space/2); Chris@709: paint->translate(QPointF(0, yt)); Chris@709: paint->scale(1.0, space); Chris@709: } Chris@709: Chris@0: int x0 = 0, x1 = w - 1; Chris@0: Chris@0: x0 = rect.left(); Chris@0: x1 = rect.right(); Chris@0: Chris@1334: if (x0 > 0) { Chris@1334: rect.adjust(-1, 0, 0, 0); Chris@1334: x0 = rect.left(); Chris@1334: } Chris@1334: Chris@1334: if (x1 < w) { Chris@1334: rect.adjust(0, 0, 1, 0); Chris@1334: x1 = rect.right(); Chris@1334: } Chris@28: Chris@365: // Our zoom level may differ from that at which the underlying Chris@365: // model has its blocks. Chris@302: Chris@365: // Each pixel within our visible range must always draw from Chris@365: // exactly the same set of underlying audio frames, no matter what Chris@365: // the range being drawn is. And that set of underlying frames Chris@365: // must remain the same when we scroll one or more pixels left or Chris@365: // right. Chris@1325: Chris@1325: int desiredBlockSize = 1; Chris@1325: if (zoomLevel.zone == ZoomLevel::FramesPerPixel) { Chris@1325: desiredBlockSize = zoomLevel.level; Chris@1325: } Chris@1325: int blockSize = m_model->getSummaryBlockSize(desiredBlockSize); Chris@365: Chris@908: sv_frame_t frame0; Chris@908: sv_frame_t frame1; Chris@908: sv_frame_t spare; Chris@365: Chris@1325: getSourceFramesForX(v, x0, blockSize, frame0, spare); Chris@1325: getSourceFramesForX(v, x1, blockSize, spare, frame1); Chris@365: Chris@4: #ifdef DEBUG_WAVEFORM_PAINT Chris@1338: SVCERR << "Painting waveform from " << frame0 << " to " << frame1 << " (" << (x1-x0+1) << " pixels at zoom " << zoomLevel << " and model zoom " << blockSize << ")" << endl; Chris@4: #endif Chris@0: Chris@1338: m_effectiveGains.clear(); Chris@805: while ((int)m_effectiveGains.size() <= maxChannel) { Chris@67: m_effectiveGains.push_back(m_gain); Chris@67: } Chris@1337: if (m_autoNormalize) { Chris@1337: for (int ch = minChannel; ch <= maxChannel; ++ch) { Chris@1337: m_effectiveGains[ch] = getNormalizeGain(v, ch); Chris@1337: } Chris@1336: } Chris@67: Chris@1338: RangeVec ranges; Chris@1338: Chris@1336: if (v->getZoomLevel().zone == ZoomLevel::FramesPerPixel) { Chris@1338: getSummaryRanges(minChannel, maxChannel, Chris@1338: mixingChannels || mergingChannels, Chris@1338: frame0, frame1, Chris@1338: blockSize, ranges); Chris@1338: } else { Chris@1338: getOversampledRanges(minChannel, maxChannel, Chris@1338: mixingChannels || mergingChannels, Chris@1338: frame0, frame1, Chris@1338: v->getZoomLevel().level, ranges); Chris@1338: } Chris@1333: Chris@1343: if (!ranges.empty()) { Chris@1343: for (int ch = minChannel; ch <= maxChannel; ++ch) { Chris@1343: paintChannel(v, paint, rect, ch, ranges, blockSize, Chris@1343: frame0, frame1); Chris@1343: } Chris@1333: } Chris@1333: Chris@709: if (m_middleLineHeight != 0.5) { Chris@709: paint->restore(); Chris@709: } Chris@709: Chris@0: if (m_aggressive) { Chris@1332: if (m_model->isReady() && rect == v->getPaintRect()) { Chris@1266: m_cacheValid = true; Chris@1266: m_cacheZoomLevel = zoomLevel; Chris@1266: } Chris@1266: paint->end(); Chris@1266: delete paint; Chris@1266: viewPainter.drawPixmap(rect, *m_cache, rect); Chris@0: } Chris@0: } Chris@0: Chris@1332: void Chris@1338: WaveformLayer::getSummaryRanges(int minChannel, int maxChannel, Chris@1338: bool mixingOrMerging, Chris@1338: sv_frame_t frame0, sv_frame_t frame1, Chris@1338: int blockSize, RangeVec &ranges) Chris@1338: const Chris@1338: { Chris@1338: for (int ch = minChannel; ch <= maxChannel; ++ch) { Chris@1338: ranges.push_back({}); Chris@1338: m_model->getSummaries(ch, frame0, frame1 - frame0, Chris@1338: ranges[ch - minChannel], blockSize); Chris@1338: #ifdef DEBUG_WAVEFORM_PAINT Chris@1338: SVCERR << "channel " << ch << ": " << ranges[ch - minChannel].size() << " ranges from " << frame0 << " to " << frame1 << " at zoom level " << blockSize << endl; Chris@1338: #endif Chris@1338: } Chris@1338: Chris@1338: if (mixingOrMerging) { Chris@1338: if (minChannel != 0 || maxChannel != 0) { Chris@1338: SVCERR << "Internal error: min & max channels should be 0 when merging or mixing all channels" << endl; Chris@1338: } else if (m_model->getChannelCount() > 1) { Chris@1338: ranges.push_back({}); Chris@1338: m_model->getSummaries Chris@1338: (1, frame0, frame1 - frame0, ranges[1], blockSize); Chris@1338: } Chris@1338: } Chris@1338: } Chris@1338: Chris@1338: void Chris@1338: WaveformLayer::getOversampledRanges(int minChannel, int maxChannel, Chris@1343: bool /* mixingOrMerging */, Chris@1338: sv_frame_t frame0, sv_frame_t frame1, Chris@1338: int oversampleBy, RangeVec &ranges) Chris@1338: const Chris@1338: { Chris@1338: // These frame values, tail length, etc variables are at the model Chris@1338: // sample rate, not the oversampled rate Chris@1338: Chris@1338: sv_frame_t tail = 16; Chris@1338: sv_frame_t startFrame = m_model->getStartFrame(); Chris@1338: sv_frame_t endFrame = m_model->getEndFrame(); Chris@1338: Chris@1338: sv_frame_t rf0 = frame0 - tail; Chris@1338: if (rf0 < startFrame) { Chris@1338: rf0 = 0; Chris@1338: } Chris@1338: Chris@1338: sv_frame_t rf1 = frame1 + tail; Chris@1338: if (rf1 >= endFrame) { Chris@1338: rf1 = endFrame - 1; Chris@1338: } Chris@1338: if (rf1 <= rf0) { Chris@1338: SVCERR << "WARNING: getOversampledRanges: rf1 (" << rf1 << ") <= rf0 (" Chris@1338: << rf0 << ")" << endl; Chris@1338: return; Chris@1338: } Chris@1338: Chris@1338: for (int ch = minChannel; ch <= maxChannel; ++ch) { Chris@1339: floatvec_t oversampled = WaveformOversampler::getOversampledData Chris@1339: (m_model, ch, frame0, frame1 - frame0, oversampleBy); Chris@1338: RangeSummarisableTimeValueModel::RangeBlock rr; Chris@1339: for (float v: oversampled) { Chris@1338: RangeSummarisableTimeValueModel::Range r; Chris@1339: r.sample(v); Chris@1338: rr.push_back(r); Chris@1338: } Chris@1338: ranges.push_back(rr); Chris@1338: Chris@1338: #ifdef DEBUG_WAVEFORM_PAINT Chris@1338: SVCERR << "getOversampledRanges: " << frame0 << " -> " << frame1 Chris@1338: << " (" << frame1 - frame0 << "-frame range) at ratio " Chris@1338: << oversampleBy << " with tail " << tail Chris@1338: << " -> got " << oversampled.size() Chris@1338: << " oversampled values for channel " << ch Chris@1338: << ", from which returning " << rr.size() << " ranges" << endl; Chris@1338: #endif Chris@1338: } Chris@1338: Chris@1338: //!!! + channel modes Chris@1338: Chris@1338: return; Chris@1338: } Chris@1338: Chris@1338: void Chris@1338: WaveformLayer::paintChannel(LayerGeometryProvider *v, Chris@1338: QPainter *paint, Chris@1338: QRect rect, int ch, Chris@1338: const RangeVec &ranges, Chris@1338: int blockSize, Chris@1352: sv_frame_t frame0, Chris@1352: sv_frame_t frame1) Chris@1332: const Chris@1332: { Chris@1334: int x0 = rect.left(); Chris@1334: int y0 = rect.top(); Chris@1334: Chris@1334: int x1 = rect.right(); Chris@1334: int y1 = rect.bottom(); Chris@1334: Chris@1332: int h = v->getPaintHeight(); Chris@1332: Chris@1332: int channels = 0, minChannel = 0, maxChannel = 0; Chris@1332: bool mergingChannels = false, mixingChannels = false; Chris@1332: Chris@1332: channels = getChannelArrangement(minChannel, maxChannel, Chris@1332: mergingChannels, mixingChannels); Chris@1332: if (channels == 0) return; Chris@1332: Chris@1332: QColor baseColour = getBaseQColor(); Chris@1333: vector greys = getPartialShades(v); Chris@1332: Chris@1332: QColor midColour = baseColour; Chris@1332: if (midColour == Qt::black) { Chris@1332: midColour = Qt::gray; Chris@1332: } else if (v->hasLightBackground()) { Chris@1332: midColour = midColour.light(150); Chris@1332: } else { Chris@1332: midColour = midColour.light(50); Chris@1332: } Chris@1332: Chris@1332: int prevRangeBottom = -1, prevRangeTop = -1; Chris@1332: QColor prevRangeBottomColour = baseColour, prevRangeTopColour = baseColour; Chris@1332: Chris@1332: double gain = m_effectiveGains[ch]; Chris@1332: Chris@1332: int m = (h / channels) / 2; Chris@1332: int my = m + (((ch - minChannel) * h) / channels); Chris@1332: Chris@1332: #ifdef DEBUG_WAVEFORM_PAINT Chris@1338: SVCERR << "ch = " << ch << ", channels = " << channels << ", m = " << m << ", my = " << my << ", h = " << h << endl; Chris@1332: #endif Chris@1332: Chris@1332: if (my - m > y1 || my + m < y0) return; Chris@1332: Chris@1332: if ((m_scale == dBScale || m_scale == MeterScale) && Chris@1332: m_channelMode != MergeChannels) { Chris@1332: m = (h / channels); Chris@1332: my = m + (((ch - minChannel) * h) / channels); Chris@1332: } Chris@1332: Chris@1332: paint->setPen(greys[1]); Chris@1332: paint->drawLine(x0, my, x1, my); Chris@1332: Chris@1335: paintChannelScaleGuides(v, paint, rect, ch); Chris@1332: Chris@1333: int rangeix = ch - minChannel; Chris@1338: Chris@1338: #ifdef DEBUG_WAVEFORM_PAINT Chris@1338: SVCERR << "paint channel " << ch << ": frame0 = " << frame0 << ", frame1 = " << frame1 << ", blockSize = " << blockSize << ", have " << ranges.size() << " range blocks of which ours is index " << rangeix << " with " << ranges[rangeix].size() << " ranges in it" << endl; Chris@1352: #else Chris@1352: (void)frame1; // not actually used Chris@1338: #endif Chris@1338: Chris@1332: for (int x = x0; x <= x1; ++x) { Chris@1332: Chris@1332: sv_frame_t f0, f1; Chris@1338: sv_frame_t i0, i1; Chris@1338: Chris@1338: bool showIndividualSample = false; Chris@1338: Chris@1338: if (v->getZoomLevel().zone == ZoomLevel::FramesPerPixel) { Chris@1338: if (!getSourceFramesForX(v, x, blockSize, f0, f1)) { Chris@1338: continue; Chris@1338: } Chris@1338: f1 = f1 - 1; Chris@1338: i0 = (f0 - frame0) / blockSize; Chris@1338: i1 = (f1 - frame0) / blockSize; Chris@1338: } else { Chris@1338: int oversampleBy = v->getZoomLevel().level; Chris@1338: f0 = f1 = v->getFrameForX(x); Chris@1338: int xf0 = v->getXForFrame(f0); Chris@1338: showIndividualSample = (x == xf0); Chris@1338: i0 = i1 = (f0 - frame0) * oversampleBy + (x - xf0); Chris@1338: } Chris@1332: Chris@1332: if (f0 < frame0) { Chris@1338: SVCERR << "ERROR: WaveformLayer::paint: pixel " << x << " has f0 = " << f0 << " which is less than range frame0 " << frame0 << " for x0 = " << x0 << endl; Chris@1332: continue; Chris@1332: } Chris@1332: Chris@1338: #ifdef DEBUG_WAVEFORM_PAINT_BY_PIXEL Chris@1338: SVCERR << "WaveformLayer::paint: pixel " << x << ": i0 " << i0 << " (f " << f0 << "), i1 " << i1 << " (f " << f1 << ")" << endl; Chris@1332: #endif Chris@1332: Chris@1332: if (i1 > i0 + 1) { Chris@1338: SVCERR << "WaveformLayer::paint: ERROR: i1 " << i1 << " > i0 " << i0 << " plus one (zoom = " << v->getZoomLevel() << ", model zoom = " << blockSize << ")" << endl; Chris@1332: } Chris@1332: Chris@1333: const auto &r = ranges[rangeix]; Chris@1333: RangeSummarisableTimeValueModel::Range range; Chris@1333: Chris@1333: if (in_range_for(r, i0)) { Chris@1332: Chris@1333: range = r[i0]; Chris@1332: Chris@1333: if (i1 > i0 && in_range_for(r, i1)) { Chris@1333: range.setMax(std::max(range.max(), r[i1].max())); Chris@1333: range.setMin(std::min(range.min(), r[i1].min())); Chris@1333: range.setAbsmean((range.absmean() + r[i1].absmean()) / 2); Chris@1332: } Chris@1332: Chris@1332: } else { Chris@1332: #ifdef DEBUG_WAVEFORM_PAINT Chris@1338: SVCERR << "No (or not enough) ranges for index i0 = " << i0 << " (there are " << r.size() << " range(s))" << endl; Chris@1332: #endif Chris@1332: continue; Chris@1332: } Chris@1332: Chris@1332: int rangeBottom = 0, rangeTop = 0, meanBottom = 0, meanTop = 0; Chris@1332: Chris@1333: if (mergingChannels && ranges.size() > 1) { Chris@1332: Chris@1333: const auto &other = ranges[1]; Chris@1333: Chris@1333: if (in_range_for(other, i0)) { Chris@1332: Chris@1332: range.setMax(fabsf(range.max())); Chris@1333: range.setMin(-fabsf(other[i0].max())); Chris@1332: range.setAbsmean Chris@1333: ((range.absmean() + other[i0].absmean()) / 2); Chris@1332: Chris@1333: if (i1 > i0 && in_range_for(other, i1)) { Chris@1332: // let's not concern ourselves about the mean Chris@1333: range.setMin(std::min(range.min(), Chris@1333: -fabsf(other[i1].max()))); Chris@1332: } Chris@1332: } Chris@1332: Chris@1333: } else if (mixingChannels && ranges.size() > 1) { Chris@1332: Chris@1333: const auto &other = ranges[1]; Chris@1333: Chris@1333: if (in_range_for(other, i0)) { Chris@1332: Chris@1333: range.setMax((range.max() + other[i0].max()) / 2); Chris@1333: range.setMin((range.min() + other[i0].min()) / 2); Chris@1333: range.setAbsmean((range.absmean() + other[i0].absmean()) / 2); Chris@1332: } Chris@1332: } Chris@1332: Chris@1332: int greyLevels = 1; Chris@1332: if (m_greyscale && (m_scale == LinearScale)) greyLevels = 4; Chris@1332: Chris@1332: switch (m_scale) { Chris@1332: Chris@1332: case LinearScale: Chris@1332: rangeBottom = int(double(m * greyLevels) * range.min() * gain); Chris@1332: rangeTop = int(double(m * greyLevels) * range.max() * gain); Chris@1332: meanBottom = int(double(-m) * range.absmean() * gain); Chris@1332: meanTop = int(double(m) * range.absmean() * gain); Chris@1332: break; Chris@1332: Chris@1332: case dBScale: Chris@1332: if (!mergingChannels) { Chris@1332: int db0 = dBscale(range.min() * gain, m); Chris@1332: int db1 = dBscale(range.max() * gain, m); Chris@1332: rangeTop = std::max(db0, db1); Chris@1332: meanTop = std::min(db0, db1); Chris@1332: if (mixingChannels) rangeBottom = meanTop; Chris@1332: else rangeBottom = dBscale(range.absmean() * gain, m); Chris@1332: meanBottom = rangeBottom; Chris@1332: } else { Chris@1332: rangeBottom = -dBscale(range.min() * gain, m * greyLevels); Chris@1332: rangeTop = dBscale(range.max() * gain, m * greyLevels); Chris@1332: meanBottom = -dBscale(range.absmean() * gain, m); Chris@1332: meanTop = dBscale(range.absmean() * gain, m); Chris@1332: } Chris@1332: break; Chris@1332: Chris@1332: case MeterScale: Chris@1332: if (!mergingChannels) { Chris@1332: int r0 = abs(AudioLevel::multiplier_to_preview(range.min() * gain, m)); Chris@1332: int r1 = abs(AudioLevel::multiplier_to_preview(range.max() * gain, m)); Chris@1332: rangeTop = std::max(r0, r1); Chris@1332: meanTop = std::min(r0, r1); Chris@1332: if (mixingChannels) rangeBottom = meanTop; Chris@1332: else rangeBottom = AudioLevel::multiplier_to_preview(range.absmean() * gain, m); Chris@1332: meanBottom = rangeBottom; Chris@1332: } else { Chris@1332: rangeBottom = -AudioLevel::multiplier_to_preview(range.min() * gain, m * greyLevels); Chris@1332: rangeTop = AudioLevel::multiplier_to_preview(range.max() * gain, m * greyLevels); Chris@1332: meanBottom = -AudioLevel::multiplier_to_preview(range.absmean() * gain, m); Chris@1332: meanTop = AudioLevel::multiplier_to_preview(range.absmean() * gain, m); Chris@1332: } Chris@1332: break; Chris@1332: } Chris@1332: Chris@1332: rangeBottom = my * greyLevels - rangeBottom; Chris@1332: rangeTop = my * greyLevels - rangeTop; Chris@1332: meanBottom = my - meanBottom; Chris@1332: meanTop = my - meanTop; Chris@1332: Chris@1332: int topFill = (rangeTop % greyLevels); Chris@1332: if (topFill > 0) topFill = greyLevels - topFill; Chris@1332: Chris@1332: int bottomFill = (rangeBottom % greyLevels); Chris@1332: Chris@1332: rangeTop = rangeTop / greyLevels; Chris@1332: rangeBottom = rangeBottom / greyLevels; Chris@1332: Chris@1332: bool clipped = false; Chris@1332: Chris@1332: if (rangeTop < my - m) { rangeTop = my - m; } Chris@1332: if (rangeTop > my + m) { rangeTop = my + m; } Chris@1332: if (rangeBottom < my - m) { rangeBottom = my - m; } Chris@1332: if (rangeBottom > my + m) { rangeBottom = my + m; } Chris@1332: Chris@1332: if (range.max() <= -1.0 || Chris@1332: range.max() >= 1.0) clipped = true; Chris@1332: Chris@1332: if (meanBottom > rangeBottom) meanBottom = rangeBottom; Chris@1332: if (meanTop < rangeTop) meanTop = rangeTop; Chris@1332: Chris@1332: bool drawMean = m_showMeans; Chris@1332: if (meanTop == rangeTop) { Chris@1332: if (meanTop < meanBottom) ++meanTop; Chris@1332: else drawMean = false; Chris@1332: } Chris@1332: if (meanBottom == rangeBottom && m_scale == LinearScale) { Chris@1332: if (meanBottom > meanTop) --meanBottom; Chris@1332: else drawMean = false; Chris@1332: } Chris@1332: Chris@1338: if (showIndividualSample) { Chris@1338: paint->setPen(baseColour); Chris@1344: paint->drawRect(x-1, rangeTop-1, 2, 2); Chris@1338: } Chris@1338: Chris@1332: if (x != x0 && prevRangeBottom != -1) { Chris@1332: if (prevRangeBottom > rangeBottom + 1 && Chris@1332: prevRangeTop > rangeBottom + 1) { Chris@1332: // paint->setPen(midColour); Chris@1332: paint->setPen(baseColour); Chris@1332: paint->drawLine(x-1, prevRangeTop, x, rangeBottom + 1); Chris@1332: paint->setPen(prevRangeTopColour); Chris@1332: paint->drawPoint(x-1, prevRangeTop); Chris@1332: } else if (prevRangeBottom < rangeTop - 1 && Chris@1332: prevRangeTop < rangeTop - 1) { Chris@1332: // paint->setPen(midColour); Chris@1332: paint->setPen(baseColour); Chris@1332: paint->drawLine(x-1, prevRangeBottom, x, rangeTop - 1); Chris@1332: paint->setPen(prevRangeBottomColour); Chris@1332: paint->drawPoint(x-1, prevRangeBottom); Chris@1332: } Chris@1332: } Chris@1332: Chris@1332: if (m_model->isReady()) { Chris@1332: if (clipped /*!!! || Chris@1332: range.min() * gain <= -1.0 || Chris@1332: range.max() * gain >= 1.0 */) { Chris@1332: paint->setPen(Qt::red); //!!! getContrastingColour Chris@1332: } else { Chris@1332: paint->setPen(baseColour); Chris@1332: } Chris@1332: } else { Chris@1332: paint->setPen(midColour); Chris@1332: } Chris@1332: Chris@1338: #ifdef DEBUG_WAVEFORM_PAINT_BY_PIXEL Chris@1338: SVCERR << "range " << rangeBottom << " -> " << rangeTop << ", means " << meanBottom << " -> " << meanTop << ", raw range " << range.min() << " -> " << range.max() << endl; Chris@1332: #endif Chris@1332: Chris@1332: if (rangeTop == rangeBottom) { Chris@1332: paint->drawPoint(x, rangeTop); Chris@1332: } else { Chris@1332: paint->drawLine(x, rangeBottom, x, rangeTop); Chris@1332: } Chris@1332: Chris@1332: prevRangeTopColour = baseColour; Chris@1332: prevRangeBottomColour = baseColour; Chris@1332: Chris@1332: if (m_greyscale && (m_scale == LinearScale) && m_model->isReady()) { Chris@1332: if (!clipped) { Chris@1332: if (rangeTop < rangeBottom) { Chris@1332: if (topFill > 0 && Chris@1332: (!drawMean || (rangeTop < meanTop - 1))) { Chris@1332: paint->setPen(greys[topFill - 1]); Chris@1332: paint->drawPoint(x, rangeTop); Chris@1332: prevRangeTopColour = greys[topFill - 1]; Chris@1332: } Chris@1332: if (bottomFill > 0 && Chris@1332: (!drawMean || (rangeBottom > meanBottom + 1))) { Chris@1332: paint->setPen(greys[bottomFill - 1]); Chris@1332: paint->drawPoint(x, rangeBottom); Chris@1332: prevRangeBottomColour = greys[bottomFill - 1]; Chris@1332: } Chris@1332: } Chris@1332: } Chris@1332: } Chris@1332: Chris@1332: if (drawMean) { Chris@1332: paint->setPen(midColour); Chris@1332: paint->drawLine(x, meanBottom, x, meanTop); Chris@1332: } Chris@1332: Chris@1332: prevRangeBottom = rangeBottom; Chris@1332: prevRangeTop = rangeTop; Chris@1332: } Chris@1332: } Chris@1332: Chris@1335: void Chris@1335: WaveformLayer::paintChannelScaleGuides(LayerGeometryProvider *v, Chris@1335: QPainter *paint, Chris@1335: QRect rect, Chris@1335: int ch) const Chris@1335: { Chris@1335: int x0 = rect.left(); Chris@1335: int x1 = rect.right(); Chris@1335: Chris@1335: int n = 10; Chris@1335: int py = -1; Chris@1335: Chris@1335: double gain = m_effectiveGains[ch]; Chris@1335: Chris@1335: if (v->hasLightBackground() && Chris@1335: v->getViewManager() && Chris@1335: v->getViewManager()->shouldShowScaleGuides()) { Chris@1335: Chris@1335: paint->setPen(QColor(240, 240, 240)); Chris@1335: Chris@1335: for (int i = 1; i < n; ++i) { Chris@1335: Chris@1335: double val = 0.0, nval = 0.0; Chris@1335: Chris@1335: switch (m_scale) { Chris@1335: Chris@1335: case LinearScale: Chris@1335: val = (i * gain) / n; Chris@1335: if (i > 0) nval = -val; Chris@1335: break; Chris@1335: Chris@1335: case MeterScale: Chris@1335: val = AudioLevel::dB_to_multiplier(meterdbs[i]) * gain; Chris@1335: break; Chris@1335: Chris@1335: case dBScale: Chris@1335: val = AudioLevel::dB_to_multiplier(-(10*n) + i * 10) * gain; Chris@1335: break; Chris@1335: } Chris@1335: Chris@1335: if (val < -1.0 || val > 1.0) continue; Chris@1335: Chris@1335: int y = getYForValue(v, val, ch); Chris@1335: Chris@1335: if (py >= 0 && abs(y - py) < 10) continue; Chris@1335: else py = y; Chris@1335: Chris@1335: int ny = y; Chris@1335: if (nval != 0.0) { Chris@1335: ny = getYForValue(v, nval, ch); Chris@1335: } Chris@1335: Chris@1335: paint->drawLine(x0, y, x1, y); Chris@1335: if (ny != y) { Chris@1335: paint->drawLine(x0, ny, x1, ny); Chris@1335: } Chris@1335: } Chris@1335: } Chris@1335: } Chris@1335: Chris@25: QString Chris@918: WaveformLayer::getFeatureDescription(LayerGeometryProvider *v, QPoint &pos) const Chris@25: { Chris@25: int x = pos.x(); Chris@25: Chris@25: if (!m_model || !m_model->isOK()) return ""; Chris@25: Chris@1325: ZoomLevel zoomLevel = v->getZoomLevel(); Chris@25: Chris@1325: int desiredBlockSize = 1; Chris@1325: if (zoomLevel.zone == ZoomLevel::FramesPerPixel) { Chris@1325: desiredBlockSize = zoomLevel.level; Chris@1325: } Chris@1325: Chris@1325: int blockSize = m_model->getSummaryBlockSize(desiredBlockSize); Chris@365: Chris@908: sv_frame_t f0, f1; Chris@1325: if (!getSourceFramesForX(v, x, blockSize, f0, f1)) return ""; Chris@25: Chris@25: QString text; Chris@25: Chris@25: RealTime rt0 = RealTime::frame2RealTime(f0, m_model->getSampleRate()); Chris@25: RealTime rt1 = RealTime::frame2RealTime(f1, m_model->getSampleRate()); Chris@25: Chris@25: if (f1 != f0 + 1 && (rt0.sec != rt1.sec || rt0.msec() != rt1.msec())) { Chris@1266: text += tr("Time:\t%1 - %2") Chris@1266: .arg(rt0.toText(true).c_str()) Chris@1266: .arg(rt1.toText(true).c_str()); Chris@25: } else { Chris@1266: text += tr("Time:\t%1") Chris@1266: .arg(rt0.toText(true).c_str()); Chris@25: } Chris@25: Chris@805: int channels = 0, minChannel = 0, maxChannel = 0; Chris@67: bool mergingChannels = false, mixingChannels = false; Chris@25: Chris@67: channels = getChannelArrangement(minChannel, maxChannel, Chris@67: mergingChannels, mixingChannels); Chris@25: if (channels == 0) return ""; Chris@25: Chris@805: for (int ch = minChannel; ch <= maxChannel; ++ch) { Chris@25: Chris@1266: RangeSummarisableTimeValueModel::RangeBlock ranges; Chris@302: m_model->getSummaries(ch, f0, f1 - f0, ranges, blockSize); Chris@25: Chris@1266: if (ranges.empty()) continue; Chris@1266: Chris@1266: RangeSummarisableTimeValueModel::Range range = ranges[0]; Chris@1266: Chris@1266: QString label = tr("Level:"); Chris@1266: if (minChannel != maxChannel) { Chris@1266: if (ch == 0) label = tr("Left:"); Chris@1266: else if (ch == 1) label = tr("Right:"); Chris@1266: else label = tr("Channel %1").arg(ch + 1); Chris@1266: } Chris@25: Chris@76: bool singleValue = false; Chris@908: double min, max; Chris@76: Chris@386: if (fabs(range.min()) < 0.01) { Chris@386: min = range.min(); Chris@386: max = range.max(); Chris@76: singleValue = (min == max); Chris@76: } else { Chris@908: int imin = int(lrint(range.min() * 10000)); Chris@908: int imax = int(lrint(range.max() * 10000)); Chris@76: singleValue = (imin == imax); Chris@908: min = double(imin)/10000; Chris@908: max = double(imax)/10000; Chris@76: } Chris@76: Chris@1266: int db = int(AudioLevel::multiplier_to_dB(std::max(fabsf(range.min()), Chris@1266: fabsf(range.max()))) Chris@1266: * 100); Chris@25: Chris@1266: if (!singleValue) { Chris@1266: text += tr("\n%1\t%2 - %3 (%4 dB peak)") Chris@1266: .arg(label).arg(min).arg(max).arg(double(db)/100); Chris@1266: } else { Chris@1266: text += tr("\n%1\t%2 (%3 dB peak)") Chris@1266: .arg(label).arg(min).arg(double(db)/100); Chris@1266: } Chris@25: } Chris@25: Chris@25: return text; Chris@25: } Chris@25: Chris@0: int Chris@918: WaveformLayer::getYForValue(const LayerGeometryProvider *v, double value, int channel) const Chris@68: { Chris@805: int channels = 0, minChannel = 0, maxChannel = 0; Chris@274: bool mergingChannels = false, mixingChannels = false; Chris@274: Chris@274: channels = getChannelArrangement(minChannel, maxChannel, Chris@274: mergingChannels, mixingChannels); Chris@853: if (channels == 0) return 0; Chris@68: if (maxChannel < minChannel || channel < minChannel) return 0; Chris@68: Chris@918: int h = v->getPaintHeight(); Chris@68: int m = (h / channels) / 2; Chris@1266: Chris@68: if ((m_scale == dBScale || m_scale == MeterScale) && Chris@68: m_channelMode != MergeChannels) { Chris@68: m = (h / channels); Chris@68: } Chris@68: Chris@274: int my = m + (((channel - minChannel) * h) / channels); Chris@274: Chris@68: int vy = 0; Chris@68: Chris@274: switch (m_scale) { Chris@68: Chris@68: case LinearScale: Chris@68: vy = int(m * value); Chris@68: break; Chris@68: Chris@68: case MeterScale: Chris@68: vy = AudioLevel::multiplier_to_preview(value, m); Chris@68: break; Chris@68: Chris@68: case dBScale: Chris@68: vy = dBscale(value, m); Chris@68: break; Chris@68: } Chris@68: Chris@1338: // SVCERR << "mergingChannels= " << mergingChannels << ", channel = " << channel << ", value = " << value << ", vy = " << vy << endl; Chris@324: Chris@68: return my - vy; Chris@68: } Chris@68: Chris@908: double Chris@918: WaveformLayer::getValueForY(const LayerGeometryProvider *v, int y, int &channel) const Chris@261: { Chris@805: int channels = 0, minChannel = 0, maxChannel = 0; Chris@274: bool mergingChannels = false, mixingChannels = false; Chris@274: Chris@274: channels = getChannelArrangement(minChannel, maxChannel, Chris@274: mergingChannels, mixingChannels); Chris@853: if (channels == 0) return 0; Chris@261: if (maxChannel < minChannel) return 0; Chris@261: Chris@918: int h = v->getPaintHeight(); Chris@261: int m = (h / channels) / 2; Chris@261: Chris@261: if ((m_scale == dBScale || m_scale == MeterScale) && Chris@261: m_channelMode != MergeChannels) { Chris@261: m = (h / channels); Chris@261: } Chris@274: Chris@274: channel = (y * channels) / h + minChannel; Chris@261: Chris@261: int my = m + (((channel - minChannel) * h) / channels); Chris@261: Chris@262: int vy = my - y; Chris@908: double value = 0; Chris@908: double thresh = -50.f; Chris@261: Chris@274: switch (m_scale) { Chris@261: Chris@261: case LinearScale: Chris@908: value = double(vy) / m; Chris@261: break; Chris@261: Chris@261: case MeterScale: Chris@261: value = AudioLevel::preview_to_multiplier(vy, m); Chris@261: break; Chris@261: Chris@261: case dBScale: Chris@908: value = (-thresh * double(vy)) / m + thresh; Chris@274: value = AudioLevel::dB_to_multiplier(value); Chris@261: break; Chris@261: } Chris@261: Chris@274: return value / m_gain; Chris@261: } Chris@261: Chris@261: bool Chris@918: WaveformLayer::getYScaleValue(const LayerGeometryProvider *v, int y, Chris@908: double &value, QString &unit) const Chris@261: { Chris@805: int channel; Chris@261: Chris@274: value = getValueForY(v, y, channel); Chris@261: Chris@274: if (m_scale == dBScale || m_scale == MeterScale) { Chris@261: Chris@908: double thresh = -50.f; Chris@274: Chris@908: if (value > 0.0) { Chris@908: value = 10.0 * log10(value); Chris@274: if (value < thresh) value = thresh; Chris@274: } else value = thresh; Chris@274: Chris@274: unit = "dBV"; Chris@274: Chris@274: } else { Chris@274: unit = "V"; Chris@274: } Chris@274: Chris@274: return true; Chris@274: } Chris@274: Chris@274: bool Chris@918: WaveformLayer::getYScaleDifference(const LayerGeometryProvider *v, int y0, int y1, Chris@908: double &diff, QString &unit) const Chris@274: { Chris@805: int c0, c1; Chris@908: double v0 = getValueForY(v, y0, c0); Chris@908: double v1 = getValueForY(v, y1, c1); Chris@274: Chris@274: if (c0 != c1) { Chris@274: // different channels, not comparable Chris@908: diff = 0.0; Chris@274: unit = ""; Chris@274: return false; Chris@274: } Chris@274: Chris@274: if (m_scale == dBScale || m_scale == MeterScale) { Chris@274: Chris@908: double thresh = -50.0; Chris@274: Chris@274: if (v1 == v0) diff = thresh; Chris@274: else { Chris@274: if (v1 > v0) diff = v0 / v1; Chris@274: else diff = v1 / v0; Chris@274: Chris@908: diff = 10.0 * log10(diff); Chris@274: if (diff < thresh) diff = thresh; Chris@274: } Chris@274: Chris@274: unit = "dBV"; Chris@274: Chris@274: } else { Chris@908: diff = fabs(v1 - v0); Chris@274: unit = "V"; Chris@274: } Chris@274: Chris@261: return true; Chris@261: } Chris@261: Chris@68: int Chris@918: WaveformLayer::getVerticalScaleWidth(LayerGeometryProvider *, bool, QPainter &paint) const Chris@0: { Chris@0: if (m_scale == LinearScale) { Chris@1266: return paint.fontMetrics().width("0.0") + 13; Chris@0: } else { Chris@1266: return std::max(paint.fontMetrics().width(tr("0dB")), Chris@1266: paint.fontMetrics().width(Strings::minus_infinity)) + 13; Chris@0: } Chris@0: } Chris@0: Chris@0: void Chris@918: WaveformLayer::paintVerticalScale(LayerGeometryProvider *v, bool, QPainter &paint, QRect rect) const Chris@0: { Chris@0: if (!m_model || !m_model->isOK()) { Chris@1266: return; Chris@0: } Chris@0: Chris@805: int channels = 0, minChannel = 0, maxChannel = 0; Chris@67: bool mergingChannels = false, mixingChannels = false; Chris@0: Chris@67: channels = getChannelArrangement(minChannel, maxChannel, Chris@67: mergingChannels, mixingChannels); Chris@0: if (channels == 0) return; Chris@0: Chris@0: int h = rect.height(), w = rect.width(); Chris@0: int textHeight = paint.fontMetrics().height(); Chris@0: int toff = -textHeight/2 + paint.fontMetrics().ascent() + 1; Chris@0: Chris@908: double gain = m_gain; Chris@67: Chris@805: for (int ch = minChannel; ch <= maxChannel; ++ch) { Chris@0: Chris@1266: int lastLabelledY = -1; Chris@0: Chris@805: if (ch < (int)m_effectiveGains.size()) gain = m_effectiveGains[ch]; Chris@67: Chris@68: int n = 10; Chris@0: Chris@1266: for (int i = 0; i <= n; ++i) { Chris@68: Chris@908: double val = 0.0, nval = 0.0; Chris@1266: QString text = ""; Chris@0: Chris@68: switch (m_scale) { Chris@68: Chris@68: case LinearScale: Chris@68: val = (i * gain) / n; Chris@1266: text = QString("%1").arg(double(i) / n); Chris@1266: if (i == 0) text = "0.0"; Chris@68: else { Chris@68: nval = -val; Chris@68: if (i == n) text = "1.0"; Chris@68: } Chris@68: break; Chris@0: Chris@68: case MeterScale: Chris@68: val = AudioLevel::dB_to_multiplier(meterdbs[i]) * gain; Chris@1266: text = QString("%1").arg(meterdbs[i]); Chris@1266: if (i == n) text = tr("0dB"); Chris@1266: if (i == 0) { Chris@1147: text = Strings::minus_infinity; Chris@68: val = 0.0; Chris@1266: } Chris@68: break; Chris@0: Chris@68: case dBScale: Chris@68: val = AudioLevel::dB_to_multiplier(-(10*n) + i * 10) * gain; Chris@1266: text = QString("%1").arg(-(10*n) + i * 10); Chris@1266: if (i == n) text = tr("0dB"); Chris@1266: if (i == 0) { Chris@1147: text = Strings::minus_infinity; Chris@68: val = 0.0; Chris@1266: } Chris@68: break; Chris@68: } Chris@0: Chris@68: if (val < -1.0 || val > 1.0) continue; Chris@0: Chris@274: int y = getYForValue(v, val, ch); Chris@0: Chris@68: int ny = y; Chris@68: if (nval != 0.0) { Chris@274: ny = getYForValue(v, nval, ch); Chris@68: } Chris@0: Chris@68: bool spaceForLabel = (i == 0 || Chris@68: abs(y - lastLabelledY) >= textHeight - 1); Chris@0: Chris@68: if (spaceForLabel) { Chris@0: Chris@68: int tx = 3; Chris@68: if (m_scale != LinearScale) { Chris@68: tx = w - 10 - paint.fontMetrics().width(text); Chris@68: } Chris@68: Chris@68: int ty = y; Chris@68: if (ty < paint.fontMetrics().ascent()) { Chris@68: ty = paint.fontMetrics().ascent(); Chris@68: } else if (ty > h - paint.fontMetrics().descent()) { Chris@68: ty = h - paint.fontMetrics().descent(); Chris@68: } else { Chris@68: ty += toff; Chris@68: } Chris@68: paint.drawText(tx, ty, text); Chris@0: Chris@68: lastLabelledY = ty - toff; Chris@67: Chris@68: if (ny != y) { Chris@68: ty = ny; Chris@68: if (ty < paint.fontMetrics().ascent()) { Chris@68: ty = paint.fontMetrics().ascent(); Chris@68: } else if (ty > h - paint.fontMetrics().descent()) { Chris@68: ty = h - paint.fontMetrics().descent(); Chris@68: } else { Chris@68: ty += toff; Chris@68: } Chris@68: paint.drawText(tx, ty, text); Chris@68: } Chris@68: Chris@68: paint.drawLine(w - 7, y, w, y); Chris@68: if (ny != y) paint.drawLine(w - 7, ny, w, ny); Chris@68: Chris@68: } else { Chris@68: Chris@68: paint.drawLine(w - 4, y, w, y); Chris@68: if (ny != y) paint.drawLine(w - 4, ny, w, ny); Chris@68: } Chris@1266: } Chris@0: } Chris@0: } Chris@0: Chris@316: void Chris@316: WaveformLayer::toXml(QTextStream &stream, Chris@316: QString indent, QString extraAttributes) const Chris@6: { Chris@6: QString s; Chris@6: Chris@285: QString colourName, colourSpec, darkbg; Chris@285: ColourDatabase::getInstance()->getStringValues Chris@285: (m_colour, colourName, colourSpec, darkbg); Chris@285: Chris@6: s += QString("gain=\"%1\" " Chris@1266: "showMeans=\"%2\" " Chris@1266: "greyscale=\"%3\" " Chris@1266: "channelMode=\"%4\" " Chris@1266: "channel=\"%5\" " Chris@287: "scale=\"%6\" " Chris@709: "middleLineHeight=\"%7\" " Chris@1266: "aggressive=\"%8\" " Chris@709: "autoNormalize=\"%9\"") Chris@1266: .arg(m_gain) Chris@1266: .arg(m_showMeans) Chris@1266: .arg(m_greyscale) Chris@1266: .arg(m_channelMode) Chris@1266: .arg(m_channel) Chris@1266: .arg(m_scale) Chris@709: .arg(m_middleLineHeight) Chris@1266: .arg(m_aggressive) Chris@67: .arg(m_autoNormalize); Chris@6: Chris@316: SingleColourLayer::toXml(stream, indent, extraAttributes + " " + s); Chris@6: } Chris@6: Chris@11: void Chris@11: WaveformLayer::setProperties(const QXmlAttributes &attributes) Chris@11: { Chris@11: bool ok = false; Chris@11: Chris@287: SingleColourLayer::setProperties(attributes); Chris@287: Chris@11: float gain = attributes.value("gain").toFloat(&ok); Chris@11: if (ok) setGain(gain); Chris@11: Chris@11: bool showMeans = (attributes.value("showMeans") == "1" || Chris@1266: attributes.value("showMeans") == "true"); Chris@11: setShowMeans(showMeans); Chris@11: Chris@11: bool greyscale = (attributes.value("greyscale") == "1" || Chris@1266: attributes.value("greyscale") == "true"); Chris@11: setUseGreyscale(greyscale); Chris@11: Chris@11: ChannelMode channelMode = (ChannelMode) Chris@1266: attributes.value("channelMode").toInt(&ok); Chris@11: if (ok) setChannelMode(channelMode); Chris@11: Chris@11: int channel = attributes.value("channel").toInt(&ok); Chris@11: if (ok) setChannel(channel); Chris@11: Chris@709: Scale scale = (Scale)attributes.value("scale").toInt(&ok); Chris@11: if (ok) setScale(scale); Chris@11: Chris@709: float middleLineHeight = attributes.value("middleLineHeight").toFloat(&ok); Chris@709: if (ok) setMiddleLineHeight(middleLineHeight); Chris@709: Chris@11: bool aggressive = (attributes.value("aggressive") == "1" || Chris@1266: attributes.value("aggressive") == "true"); Chris@11: setUseGreyscale(aggressive); Chris@67: Chris@67: bool autoNormalize = (attributes.value("autoNormalize") == "1" || Chris@67: attributes.value("autoNormalize") == "true"); Chris@67: setAutoNormalize(autoNormalize); Chris@11: } Chris@11: Chris@133: int Chris@133: WaveformLayer::getVerticalZoomSteps(int &defaultStep) const Chris@133: { Chris@133: defaultStep = 50; Chris@133: return 100; Chris@133: } Chris@0: Chris@133: int Chris@133: WaveformLayer::getCurrentVerticalZoomStep() const Chris@133: { Chris@908: int val = int(lrint(log10(m_gain) * 20.0) + 50); Chris@133: if (val < 0) val = 0; Chris@133: if (val > 100) val = 100; Chris@133: return val; Chris@133: } Chris@133: Chris@133: void Chris@133: WaveformLayer::setVerticalZoomStep(int step) Chris@133: { Chris@908: setGain(powf(10, float(step - 50) / 20.f)); Chris@133: } Chris@133: