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@0: Chris@0: #include Chris@0: #include Chris@0: Chris@0: #include Chris@0: #include Chris@0: Chris@4: //#define DEBUG_WAVEFORM_PAINT 1 Chris@4: Chris@0: using std::cerr; Chris@0: using std::endl; Chris@0: Chris@44: WaveformLayer::WaveformLayer() : Chris@44: Layer(), Chris@0: m_model(0), Chris@0: m_gain(1.0f), Chris@67: m_autoNormalize(false), Chris@0: m_colour(Qt::black), Chris@189: // m_colour(QColor(84, 177, 248)), 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@0: m_aggressive(false), Chris@0: m_cache(0), Chris@0: m_cacheValid(false) Chris@0: { Chris@44: 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@0: connect(m_model, SIGNAL(modelChanged()), this, SIGNAL(modelChanged())); Chris@0: connect(m_model, SIGNAL(modelChanged(size_t, size_t)), Chris@0: this, SIGNAL(modelChanged(size_t, size_t))); Chris@0: Chris@0: connect(m_model, SIGNAL(completionChanged()), Chris@0: this, SIGNAL(modelCompletionChanged())); Chris@0: 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@0: PropertyList list; Chris@87: list.push_back("Colour"); 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 == "Colour") return tr("Colour"); 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@87: return ""; Chris@87: } Chris@87: 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 == "Colour") return ValueProperty; Chris@87: if (name == "Channels") return ValueProperty; Chris@87: if (name == "Scale") return ValueProperty; Chris@0: return InvalidProperty; 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@87: name == "Scale") return tr("Scale"); Chris@0: return QString(); Chris@0: } Chris@0: Chris@0: int Chris@0: WaveformLayer::getPropertyRangeAndValue(const PropertyName &name, Chris@67: int *min, int *max) const Chris@0: { Chris@0: int deft = 0; Chris@0: Chris@56: int garbage0, garbage1; Chris@56: if (!min) min = &garbage0; Chris@56: if (!max) max = &garbage1; Chris@10: Chris@87: if (name == "Gain") { Chris@0: Chris@0: *min = -50; Chris@0: *max = 50; Chris@0: Chris@34: deft = lrint(log10(m_gain) * 20.0); Chris@0: if (deft < *min) deft = *min; Chris@0: if (deft > *max) deft = *max; Chris@0: Chris@87: } else if (name == "Normalize Visible Area") { Chris@67: Chris@67: deft = (m_autoNormalize ? 1 : 0); Chris@67: Chris@87: } else if (name == "Colour") { Chris@0: Chris@0: *min = 0; Chris@0: *max = 5; Chris@0: Chris@0: if (m_colour == Qt::black) deft = 0; Chris@0: else if (m_colour == Qt::darkRed) deft = 1; Chris@189: else if (m_colour == Qt::darkBlue || Chris@189: m_colour == QColor(84, 177, 248)) deft = 2; Chris@0: else if (m_colour == Qt::darkGreen) deft = 3; Chris@0: else if (m_colour == QColor(200, 50, 255)) deft = 4; Chris@0: else if (m_colour == QColor(255, 150, 50)) deft = 5; Chris@0: Chris@87: } else if (name == "Channels") { Chris@0: Chris@67: *min = 0; Chris@67: *max = 2; Chris@67: if (m_channelMode == MixChannels) deft = 1; Chris@67: else if (m_channelMode == MergeChannels) deft = 2; Chris@67: else deft = 0; Chris@0: Chris@87: } else if (name == "Scale") { Chris@0: Chris@0: *min = 0; Chris@0: *max = 2; Chris@0: Chris@0: deft = (int)m_scale; Chris@0: Chris@0: } else { Chris@0: deft = Layer::getPropertyRangeAndValue(name, min, max); Chris@0: } Chris@0: Chris@0: return deft; Chris@0: } Chris@0: Chris@0: QString Chris@0: WaveformLayer::getPropertyValueLabel(const PropertyName &name, Chris@0: int value) const Chris@0: { Chris@87: if (name == "Colour") { Chris@0: switch (value) { Chris@0: default: Chris@0: case 0: return tr("Black"); Chris@0: case 1: return tr("Red"); Chris@0: case 2: return tr("Blue"); Chris@0: case 3: return tr("Green"); Chris@0: case 4: return tr("Purple"); Chris@0: case 5: return tr("Orange"); Chris@0: } Chris@0: } Chris@87: if (name == "Scale") { Chris@0: switch (value) { Chris@0: default: Chris@0: case 0: return tr("Linear"); Chris@0: case 1: return tr("Meter"); Chris@0: case 2: return tr("dB"); Chris@0: } 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@0: return tr(""); 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@0: setGain(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 == "Colour") { Chris@0: switch (value) { Chris@0: default: Chris@0: case 0: setBaseColour(Qt::black); break; Chris@0: case 1: setBaseColour(Qt::darkRed); break; Chris@189: // case 2: setBaseColour(QColor(84, 177, 248)); break; Chris@0: case 2: setBaseColour(Qt::darkBlue); break; Chris@0: case 3: setBaseColour(Qt::darkGreen); break; Chris@0: case 4: setBaseColour(QColor(200, 50, 255)); break; Chris@0: case 5: setBaseColour(QColor(255, 150, 50)); break; Chris@0: } 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@0: switch (value) { Chris@0: default: Chris@0: case 0: setScale(LinearScale); break; Chris@0: case 1: setScale(MeterScale); break; Chris@0: case 2: setScale(dBScale); break; Chris@0: } 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::setBaseColour(QColor colour) Chris@0: { Chris@0: if (m_colour == colour) return; Chris@0: m_colour = colour; Chris@0: m_cacheValid = false; Chris@0: emit layerParametersChanged(); Chris@0: } Chris@0: Chris@0: 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@101: // std::cerr << "WaveformLayer::setChannel(" << channel << ")" << std::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@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@115: WaveformLayer::getCompletion(View *) 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@101: WaveformLayer::getValueExtents(float &min, float &max, Chris@101: bool &log, 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@0: WaveformLayer::dBscale(float sample, int m) const Chris@0: { Chris@67: //!!! if (sample < 0.0) return -dBscale(-sample, m); Chris@67: if (sample < 0.0) return dBscale(-sample, m); Chris@0: float 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@0: size_t Chris@67: WaveformLayer::getChannelArrangement(size_t &min, size_t &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@0: size_t channels = m_model->getChannelCount(); Chris@0: if (channels == 0) return 0; Chris@0: Chris@0: size_t rawChannels = channels; Chris@0: Chris@0: if (m_channel == -1) { Chris@0: min = 0; Chris@67: if (m_channelMode == MergeChannels || Chris@67: m_channelMode == MixChannels) { Chris@0: max = 0; Chris@0: channels = 1; Chris@0: } else { Chris@0: max = channels - 1; Chris@0: } Chris@0: } else { Chris@0: min = m_channel; Chris@0: max = m_channel; Chris@0: rawChannels = 1; Chris@0: 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@0: // std::cerr << "WaveformLayer::getChannelArrangement: min " << min << ", max " << max << ", merging " << merging << ", channels " << channels << std::endl; Chris@0: Chris@0: return channels; Chris@0: } Chris@0: Chris@67: bool Chris@67: WaveformLayer::isLayerScrollable(const View *) 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@0: void Chris@44: WaveformLayer::paint(View *v, QPainter &viewPainter, QRect rect) const Chris@0: { Chris@0: if (!m_model || !m_model->isOK()) { Chris@0: return; Chris@0: } Chris@0: Chris@44: long startFrame = v->getStartFrame(); Chris@44: int zoomLevel = v->getZoomLevel(); Chris@0: Chris@2: #ifdef DEBUG_WAVEFORM_PAINT Chris@0: Profiler profiler("WaveformLayer::paint", true); Chris@0: std::cerr << "WaveformLayer::paint (" << rect.x() << "," << rect.y() Chris@0: << ") [" << rect.width() << "x" << rect.height() << "]: zoom " << zoomLevel << ", start " << startFrame << std::endl; Chris@2: #endif Chris@0: Chris@0: size_t 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@44: int w = v->width(); Chris@44: int h = v->height(); Chris@0: Chris@0: bool ready = m_model->isReady(); Chris@0: QPainter *paint; Chris@0: Chris@0: if (m_aggressive) { Chris@0: Chris@0: if (m_cacheValid && (zoomLevel != m_cacheZoomLevel)) { Chris@0: m_cacheValid = false; Chris@0: } Chris@0: Chris@0: if (m_cacheValid) { Chris@0: viewPainter.drawPixmap(rect, *m_cache, rect); Chris@0: return; Chris@0: } Chris@0: Chris@0: if (!m_cache || m_cache->width() != w || m_cache->height() != h) { Chris@0: delete m_cache; Chris@0: m_cache = new QPixmap(w, h); Chris@0: } Chris@0: Chris@0: paint = new QPainter(m_cache); Chris@0: Chris@0: paint->setPen(Qt::NoPen); Chris@44: paint->setBrush(v->palette().background()); Chris@0: paint->drawRect(rect); Chris@0: Chris@0: paint->setPen(Qt::black); Chris@0: paint->setBrush(Qt::NoBrush); Chris@0: Chris@0: } else { Chris@0: paint = &viewPainter; Chris@0: } Chris@0: Chris@28: paint->setRenderHint(QPainter::Antialiasing, false); Chris@28: Chris@0: int x0 = 0, x1 = w - 1; Chris@0: int y0 = 0, y1 = h - 1; Chris@0: Chris@0: x0 = rect.left(); Chris@0: x1 = rect.right(); Chris@0: y0 = rect.top(); Chris@0: y1 = rect.bottom(); Chris@0: Chris@28: if (x0 > 0) --x0; Chris@44: if (x1 < v->width()) ++x1; Chris@28: Chris@44: long frame0 = v->getFrameForX(x0); Chris@44: long frame1 = v->getFrameForX(x1 + 1); Chris@0: Chris@4: #ifdef DEBUG_WAVEFORM_PAINT Chris@4: std::cerr << "Painting waveform from " << frame0 << " to " << frame1 << " (" << (x1-x0+1) << " pixels at zoom " << zoomLevel << ")" << std::endl; Chris@4: #endif Chris@0: Chris@0: RangeSummarisableTimeValueModel::RangeBlock ranges; Chris@0: RangeSummarisableTimeValueModel::RangeBlock otherChannelRanges; Chris@0: RangeSummarisableTimeValueModel::Range range; Chris@0: Chris@0: QColor greys[3]; Chris@0: if (m_colour == Qt::black) { Chris@27: for (int i = 0; i < 3; ++i) { // 0 lightest, 2 darkest Chris@0: int level = 192 - 64 * i; Chris@0: greys[i] = QColor(level, level, level); Chris@0: } Chris@0: } else { Chris@44: int hue, sat, val; Chris@44: m_colour.getHsv(&hue, &sat, &val); Chris@27: for (int i = 0; i < 3; ++i) { // 0 lightest, 2 darkest Chris@44: if (v->hasLightBackground()) { Chris@44: greys[i] = QColor::fromHsv(hue, sat * (i + 1) / 4, val); Chris@27: } else { Chris@44: greys[i] = QColor::fromHsv(hue, sat * (3 - i) / 4, val); Chris@27: } Chris@27: } Chris@0: } Chris@0: Chris@0: QColor midColour = m_colour; Chris@0: if (midColour == Qt::black) { Chris@0: midColour = Qt::gray; Chris@44: } else if (v->hasLightBackground()) { Chris@0: midColour = midColour.light(150); Chris@0: } else { Chris@0: midColour = midColour.light(50); Chris@0: } Chris@0: Chris@67: while (m_effectiveGains.size() <= maxChannel) { Chris@67: m_effectiveGains.push_back(m_gain); Chris@67: } Chris@67: Chris@0: for (size_t ch = minChannel; ch <= maxChannel; ++ch) { Chris@0: Chris@0: int prevRangeBottom = -1, prevRangeTop = -1; Chris@28: QColor prevRangeBottomColour = m_colour, prevRangeTopColour = m_colour; Chris@0: Chris@67: m_effectiveGains[ch] = m_gain; Chris@67: Chris@67: if (m_autoNormalize) { Chris@67: RangeSummarisableTimeValueModel::Range range = Chris@67: m_model->getRange(ch, startFrame < 0 ? 0 : startFrame, Chris@67: v->getEndFrame()); Chris@67: if (mergingChannels || mixingChannels) { Chris@67: RangeSummarisableTimeValueModel::Range otherRange = Chris@67: m_model->getRange(1, startFrame < 0 ? 0 : startFrame, Chris@67: v->getEndFrame()); Chris@67: range.max = std::max(range.max, otherRange.max); Chris@67: range.min = std::min(range.min, otherRange.min); Chris@67: range.absmean = std::min(range.absmean, otherRange.absmean); Chris@67: } Chris@67: m_effectiveGains[ch] = 1.0 / std::max(fabsf(range.max), Chris@67: fabsf(range.min)); Chris@67: } Chris@67: Chris@67: float gain = m_effectiveGains[ch]; Chris@67: Chris@68: int m = (h / channels) / 2; Chris@68: int my = m + (((ch - minChannel) * h) / channels); Chris@68: Chris@68: // std::cerr << "ch = " << ch << ", channels = " << channels << ", m = " << m << ", my = " << my << ", h = " << h << std::endl; Chris@68: Chris@68: if (my - m > y1 || my + m < y0) continue; Chris@68: Chris@68: if ((m_scale == dBScale || m_scale == MeterScale) && Chris@68: m_channelMode != MergeChannels) { Chris@68: m = (h / channels); Chris@68: my = m + (((ch - minChannel) * h) / channels); Chris@68: } Chris@68: Chris@68: paint->setPen(greys[0]); Chris@68: paint->drawLine(x0, my, x1, my); Chris@68: Chris@68: int n = 10; Chris@68: int py = -1; Chris@68: Chris@195: if (v->hasLightBackground() && Chris@195: v->getViewManager() && Chris@195: v->getViewManager()->shouldShowScaleGuides()) { Chris@68: Chris@68: paint->setPen(QColor(240, 240, 240)); Chris@68: Chris@68: for (int i = 1; i < n; ++i) { Chris@68: Chris@68: float val = 0.0, nval = 0.0; Chris@68: Chris@68: switch (m_scale) { Chris@68: Chris@68: case LinearScale: Chris@68: val = (i * gain) / n; Chris@68: if (i > 0) nval = -val; Chris@68: break; Chris@68: Chris@68: case MeterScale: Chris@68: val = AudioLevel::dB_to_multiplier(meterdbs[i]) * gain; Chris@68: break; Chris@68: Chris@68: case dBScale: Chris@68: val = AudioLevel::dB_to_multiplier(-(10*n) + i * 10) * gain; Chris@68: break; Chris@68: } Chris@68: Chris@68: if (val < -1.0 || val > 1.0) continue; Chris@68: Chris@68: int y = getYForValue(v, m_scale, val, ch, minChannel, maxChannel); Chris@68: Chris@68: if (py >= 0 && abs(y - py) < 10) continue; Chris@68: else py = y; Chris@68: Chris@68: int ny = y; Chris@68: if (nval != 0.0) { Chris@68: ny = getYForValue(v, m_scale, nval, ch, minChannel, maxChannel); Chris@68: } Chris@68: Chris@68: paint->drawLine(x0, y, x1, y); Chris@68: if (ny != y) { Chris@68: paint->drawLine(x0, ny, x1, ny); Chris@68: } Chris@68: } Chris@68: } Chris@68: Chris@68: if (frame1 <= 0) continue; Chris@68: Chris@68: size_t modelZoomLevel = zoomLevel; Chris@68: Chris@68: ranges = m_model->getRanges Chris@68: (ch, frame0 < 0 ? 0 : frame0, frame1, modelZoomLevel); Chris@68: Chris@68: if (mergingChannels || mixingChannels) { Chris@71: if (m_model->getChannelCount() > 1) { Chris@71: otherChannelRanges = m_model->getRanges Chris@71: (1, frame0 < 0 ? 0 : frame0, frame1, modelZoomLevel); Chris@71: } else { Chris@71: otherChannelRanges = ranges; Chris@71: } Chris@68: } Chris@68: Chris@0: for (int x = x0; x <= x1; ++x) { Chris@0: Chris@0: range = RangeSummarisableTimeValueModel::Range(); Chris@0: size_t index = x - x0; Chris@0: size_t maxIndex = index; Chris@0: Chris@0: if (frame0 < 0) { Chris@0: if (index < size_t(-frame0 / zoomLevel)) { Chris@0: continue; Chris@0: } else { Chris@0: index -= -frame0 / zoomLevel; Chris@0: maxIndex = index; Chris@0: } Chris@0: } Chris@0: Chris@0: if (int(modelZoomLevel) != zoomLevel) { Chris@0: Chris@0: index = size_t((double(index) * zoomLevel) / modelZoomLevel); Chris@0: Chris@0: if (int(modelZoomLevel) < zoomLevel) { Chris@0: // Peaks may be missed! The model should avoid Chris@0: // this by rounding zoom levels up rather than Chris@0: // down, but we'd better cope in case it doesn't Chris@0: maxIndex = index; Chris@0: } else { Chris@0: maxIndex = size_t((double(index + 1) * zoomLevel) Chris@0: / modelZoomLevel) - 1; Chris@0: } Chris@0: } Chris@0: Chris@0: if (index < ranges.size()) { Chris@0: Chris@0: range = ranges[index]; Chris@0: Chris@0: if (maxIndex > index && maxIndex < ranges.size()) { Chris@0: range.max = std::max(range.max, ranges[maxIndex].max); Chris@0: range.min = std::min(range.min, ranges[maxIndex].min); Chris@0: range.absmean = (range.absmean + Chris@0: ranges[maxIndex].absmean) / 2; Chris@0: } Chris@0: Chris@0: } else { Chris@0: continue; Chris@0: } Chris@0: Chris@0: int rangeBottom = 0, rangeTop = 0, meanBottom = 0, meanTop = 0; Chris@0: Chris@0: if (mergingChannels) { Chris@0: Chris@0: if (index < otherChannelRanges.size()) { Chris@0: Chris@0: range.max = fabsf(range.max); Chris@0: range.min = -fabsf(otherChannelRanges[index].max); Chris@0: range.absmean = (range.absmean + Chris@0: otherChannelRanges[index].absmean) / 2; Chris@0: Chris@0: if (maxIndex > index && maxIndex < ranges.size()) { Chris@0: // let's not concern ourselves about the mean Chris@0: range.min = std::min Chris@0: (range.min, Chris@0: -fabsf(otherChannelRanges[maxIndex].max)); Chris@0: } Chris@0: } Chris@67: Chris@67: } else if (mixingChannels) { Chris@67: Chris@67: if (index < otherChannelRanges.size()) { Chris@67: Chris@67: range.max = (range.max + otherChannelRanges[index].max) / 2; Chris@67: range.min = (range.min + otherChannelRanges[index].min) / 2; Chris@67: range.absmean = (range.absmean + otherChannelRanges[index].absmean) / 2; Chris@67: } Chris@67: } Chris@0: Chris@0: int greyLevels = 1; Chris@0: if (m_greyscale && (m_scale == LinearScale)) greyLevels = 4; Chris@0: Chris@0: switch (m_scale) { Chris@0: Chris@0: case LinearScale: Chris@67: rangeBottom = int( m * greyLevels * range.min * gain); Chris@67: rangeTop = int( m * greyLevels * range.max * gain); Chris@67: meanBottom = int(-m * range.absmean * gain); Chris@67: meanTop = int( m * range.absmean * gain); Chris@0: break; Chris@0: Chris@0: case dBScale: Chris@67: if (!mergingChannels) { Chris@67: int db0 = dBscale(range.min * gain, m); Chris@67: int db1 = dBscale(range.max * gain, m); Chris@67: rangeTop = std::max(db0, db1); Chris@67: meanTop = std::min(db0, db1); Chris@67: if (mixingChannels) rangeBottom = meanTop; Chris@67: else rangeBottom = dBscale(range.absmean * gain, m); Chris@67: meanBottom = rangeBottom; Chris@67: } else { Chris@67: rangeBottom = -dBscale(range.min * gain, m * greyLevels); Chris@67: rangeTop = dBscale(range.max * gain, m * greyLevels); Chris@67: meanBottom = -dBscale(range.absmean * gain, m); Chris@67: meanTop = dBscale(range.absmean * gain, m); Chris@67: } Chris@0: break; Chris@0: Chris@0: case MeterScale: Chris@67: if (!mergingChannels) { Chris@67: int r0 = abs(AudioLevel::multiplier_to_preview(range.min * gain, m)); Chris@67: int r1 = abs(AudioLevel::multiplier_to_preview(range.max * gain, m)); Chris@67: rangeTop = std::max(r0, r1); Chris@67: meanTop = std::min(r0, r1); Chris@67: if (mixingChannels) rangeBottom = meanTop; Chris@67: else rangeBottom = AudioLevel::multiplier_to_preview(range.absmean * gain, m); Chris@67: meanBottom = rangeBottom; Chris@67: } else { Chris@67: rangeBottom = AudioLevel::multiplier_to_preview(range.min * gain, m * greyLevels); Chris@67: rangeTop = AudioLevel::multiplier_to_preview(range.max * gain, m * greyLevels); Chris@67: meanBottom = -AudioLevel::multiplier_to_preview(range.absmean * gain, m); Chris@67: meanTop = AudioLevel::multiplier_to_preview(range.absmean * gain, m); Chris@67: } Chris@67: break; Chris@0: } Chris@0: Chris@27: rangeBottom = my * greyLevels - rangeBottom; Chris@27: rangeTop = my * greyLevels - rangeTop; Chris@27: meanBottom = my - meanBottom; Chris@27: meanTop = my - meanTop; Chris@27: Chris@27: int topFill = (rangeTop % greyLevels); Chris@27: if (topFill > 0) topFill = greyLevels - topFill; Chris@27: Chris@27: int bottomFill = (rangeBottom % greyLevels); Chris@27: Chris@0: rangeTop = rangeTop / greyLevels; Chris@0: rangeBottom = rangeBottom / greyLevels; Chris@0: Chris@0: bool clipped = false; Chris@27: Chris@27: if (rangeTop < my - m) { rangeTop = my - m; } Chris@27: if (rangeTop > my + m) { rangeTop = my + m; } Chris@27: if (rangeBottom < my - m) { rangeBottom = my - m; } Chris@27: if (rangeBottom > my + m) { rangeBottom = my + m; } Chris@27: Chris@67: if (range.max <= -1.0 || Chris@67: range.max >= 1.0) clipped = true; Chris@0: Chris@0: if (meanBottom > rangeBottom) meanBottom = rangeBottom; Chris@0: if (meanTop < rangeTop) meanTop = rangeTop; Chris@0: Chris@0: bool drawMean = m_showMeans; Chris@0: if (meanTop == rangeTop) { Chris@0: if (meanTop < meanBottom) ++meanTop; Chris@0: else drawMean = false; Chris@0: } Chris@67: if (meanBottom == rangeBottom && m_scale == LinearScale) { Chris@0: if (meanBottom > meanTop) --meanBottom; Chris@0: else drawMean = false; Chris@0: } Chris@0: Chris@0: if (x != x0 && prevRangeBottom != -1) { Chris@0: if (prevRangeBottom > rangeBottom && Chris@0: prevRangeTop > rangeBottom) { Chris@28: // paint->setPen(midColour); Chris@28: paint->setPen(m_colour); Chris@0: paint->drawLine(x-1, prevRangeTop, x, rangeBottom); Chris@28: paint->setPen(prevRangeTopColour); Chris@0: paint->drawPoint(x-1, prevRangeTop); Chris@0: } else if (prevRangeBottom < rangeTop && Chris@0: prevRangeTop < rangeTop) { Chris@28: // paint->setPen(midColour); Chris@28: paint->setPen(m_colour); Chris@0: paint->drawLine(x-1, prevRangeBottom, x, rangeTop); Chris@28: paint->setPen(prevRangeBottomColour); Chris@0: paint->drawPoint(x-1, prevRangeBottom); Chris@0: } Chris@0: } Chris@0: Chris@0: if (ready) { Chris@67: if (clipped /*!!! || Chris@67: range.min * gain <= -1.0 || Chris@67: range.max * gain >= 1.0 */) { Chris@0: paint->setPen(Qt::red); Chris@0: } else { Chris@0: paint->setPen(m_colour); Chris@0: } Chris@0: } else { Chris@0: paint->setPen(midColour); Chris@0: } Chris@0: Chris@0: paint->drawLine(x, rangeBottom, x, rangeTop); Chris@0: Chris@28: prevRangeTopColour = m_colour; Chris@28: prevRangeBottomColour = m_colour; Chris@28: Chris@0: if (m_greyscale && (m_scale == LinearScale) && ready) { Chris@0: if (!clipped) { Chris@0: if (rangeTop < rangeBottom) { Chris@0: if (topFill > 0 && Chris@0: (!drawMean || (rangeTop < meanTop - 1))) { Chris@0: paint->setPen(greys[topFill - 1]); Chris@27: paint->drawPoint(x, rangeTop); Chris@28: prevRangeTopColour = greys[topFill - 1]; Chris@0: } Chris@0: if (bottomFill > 0 && Chris@0: (!drawMean || (rangeBottom > meanBottom + 1))) { Chris@0: paint->setPen(greys[bottomFill - 1]); Chris@27: paint->drawPoint(x, rangeBottom); Chris@28: prevRangeBottomColour = greys[bottomFill - 1]; Chris@0: } Chris@0: } Chris@0: } Chris@0: } Chris@0: Chris@0: if (drawMean) { Chris@0: paint->setPen(midColour); Chris@0: paint->drawLine(x, meanBottom, x, meanTop); Chris@0: } Chris@0: Chris@0: prevRangeBottom = rangeBottom; Chris@0: prevRangeTop = rangeTop; Chris@0: } Chris@0: } Chris@0: Chris@0: if (m_aggressive) { Chris@41: Chris@44: if (ready && rect == v->rect()) { Chris@0: m_cacheValid = true; Chris@0: m_cacheZoomLevel = zoomLevel; Chris@0: } Chris@0: paint->end(); Chris@0: delete paint; Chris@0: viewPainter.drawPixmap(rect, *m_cache, rect); Chris@0: } Chris@0: } Chris@0: Chris@25: QString Chris@44: WaveformLayer::getFeatureDescription(View *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@44: long f0 = v->getFrameForX(x); Chris@44: long f1 = v->getFrameForX(x + 1); Chris@25: Chris@25: if (f0 < 0) f0 = 0; Chris@25: if (f1 <= f0) 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@25: text += tr("Time:\t%1 - %2") Chris@25: .arg(rt0.toText(true).c_str()) Chris@25: .arg(rt1.toText(true).c_str()); Chris@25: } else { Chris@25: text += tr("Time:\t%1") Chris@25: .arg(rt0.toText(true).c_str()); Chris@25: } Chris@25: Chris@25: size_t 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@25: for (size_t ch = minChannel; ch <= maxChannel; ++ch) { Chris@25: Chris@44: size_t blockSize = v->getZoomLevel(); Chris@25: RangeSummarisableTimeValueModel::RangeBlock ranges = Chris@25: m_model->getRanges(ch, f0, f1, blockSize); Chris@25: Chris@25: if (ranges.empty()) continue; Chris@25: Chris@25: RangeSummarisableTimeValueModel::Range range = ranges[0]; Chris@25: Chris@25: QString label = tr("Level:"); Chris@25: if (minChannel != maxChannel) { Chris@25: if (ch == 0) label = tr("Left:"); Chris@25: else if (ch == 1) label = tr("Right:"); Chris@25: else label = tr("Channel %1").arg(ch + 1); Chris@25: } Chris@25: Chris@76: bool singleValue = false; Chris@76: float min, max; Chris@76: Chris@76: if (fabs(range.min) < 0.01) { Chris@76: min = range.min; Chris@76: max = range.max; Chris@76: singleValue = (min == max); Chris@76: } else { Chris@76: int imin = int(range.min * 1000); Chris@76: int imax = int(range.max * 1000); Chris@76: singleValue = (imin == imax); Chris@76: min = float(imin)/1000; Chris@76: max = float(imax)/1000; Chris@76: } Chris@76: Chris@25: int db = int(AudioLevel::multiplier_to_dB(std::max(fabsf(range.min), Chris@25: fabsf(range.max))) Chris@25: * 100); Chris@25: Chris@76: if (!singleValue) { Chris@25: text += tr("\n%1\t%2 - %3 (%4 dB peak)") Chris@76: .arg(label).arg(min).arg(max).arg(float(db)/100); Chris@25: } else { Chris@25: text += tr("\n%1\t%2 (%3 dB peak)") Chris@76: .arg(label).arg(min).arg(float(db)/100); Chris@25: } Chris@25: } Chris@25: Chris@25: return text; Chris@25: } Chris@25: Chris@0: int Chris@68: WaveformLayer::getYForValue(View *v, Scale scale, float value, size_t channel, Chris@68: size_t minChannel, size_t maxChannel) const Chris@68: { Chris@68: if (maxChannel < minChannel || channel < minChannel) return 0; Chris@68: Chris@68: int w = v->width(); Chris@68: int h = v->height(); Chris@68: Chris@68: int channels = maxChannel - minChannel + 1; Chris@68: int m = (h / channels) / 2; Chris@68: int my = m + (((channel - minChannel) * h) / channels); Chris@68: Chris@68: if ((m_scale == dBScale || m_scale == MeterScale) && Chris@68: m_channelMode != MergeChannels) { Chris@68: m = (h / channels); Chris@68: my = m + (((channel - minChannel) * h) / channels); Chris@68: } Chris@68: Chris@68: int vy = 0; Chris@68: Chris@68: switch (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@68: return my - vy; Chris@68: } Chris@68: Chris@68: int Chris@44: WaveformLayer::getVerticalScaleWidth(View *v, QPainter &paint) const Chris@0: { Chris@0: if (m_scale == LinearScale) { Chris@0: return paint.fontMetrics().width("0.0") + 13; Chris@0: } else { Chris@0: return std::max(paint.fontMetrics().width(tr("0dB")), Chris@0: paint.fontMetrics().width(tr("-Inf"))) + 13; Chris@0: } Chris@0: } Chris@0: Chris@0: void Chris@44: WaveformLayer::paintVerticalScale(View *v, QPainter &paint, QRect rect) const Chris@0: { Chris@0: if (!m_model || !m_model->isOK()) { Chris@0: return; Chris@0: } Chris@0: Chris@0: size_t 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@67: float gain = m_gain; Chris@67: Chris@0: for (size_t ch = minChannel; ch <= maxChannel; ++ch) { Chris@0: Chris@68: int lastLabelledY = -1; Chris@0: Chris@67: if (ch < m_effectiveGains.size()) gain = m_effectiveGains[ch]; Chris@67: Chris@68: int n = 10; Chris@0: Chris@68: for (int i = 0; i <= n; ++i) { Chris@68: Chris@68: float val = 0.0, nval = 0.0; Chris@0: QString text = ""; Chris@0: Chris@68: switch (m_scale) { Chris@68: Chris@68: case LinearScale: Chris@68: val = (i * gain) / n; Chris@68: text = QString("%1").arg(float(i) / n); Chris@68: 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@68: text = QString("%1").arg(meterdbs[i]); Chris@68: if (i == n) text = tr("0dB"); Chris@68: if (i == 0) { Chris@68: text = tr("-Inf"); Chris@68: val = 0.0; Chris@68: } Chris@68: break; Chris@0: Chris@68: case dBScale: Chris@68: val = AudioLevel::dB_to_multiplier(-(10*n) + i * 10) * gain; Chris@68: text = QString("%1").arg(-(10*n) + i * 10); Chris@68: if (i == n) text = tr("0dB"); Chris@68: if (i == 0) { Chris@68: text = tr("-Inf"); Chris@68: val = 0.0; Chris@68: } Chris@68: break; Chris@68: } Chris@0: Chris@68: if (val < -1.0 || val > 1.0) continue; Chris@0: Chris@68: int y = getYForValue(v, m_scale, val, ch, minChannel, maxChannel); Chris@0: Chris@68: int ny = y; Chris@68: if (nval != 0.0) { Chris@68: ny = getYForValue(v, m_scale, nval, ch, minChannel, maxChannel); 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@0: } Chris@0: } Chris@0: } Chris@0: Chris@6: QString Chris@6: WaveformLayer::toXmlString(QString indent, QString extraAttributes) const Chris@6: { Chris@6: QString s; Chris@6: Chris@6: s += QString("gain=\"%1\" " Chris@6: "colour=\"%2\" " Chris@6: "showMeans=\"%3\" " Chris@6: "greyscale=\"%4\" " Chris@6: "channelMode=\"%5\" " Chris@6: "channel=\"%6\" " Chris@6: "scale=\"%7\" " Chris@67: "aggressive=\"%8\" " Chris@67: "autoNormalize=\"%9\"") Chris@6: .arg(m_gain) Chris@6: .arg(encodeColour(m_colour)) Chris@6: .arg(m_showMeans) Chris@6: .arg(m_greyscale) Chris@6: .arg(m_channelMode) Chris@6: .arg(m_channel) Chris@6: .arg(m_scale) Chris@67: .arg(m_aggressive) Chris@67: .arg(m_autoNormalize); Chris@6: Chris@6: return Layer::toXmlString(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@11: float gain = attributes.value("gain").toFloat(&ok); Chris@11: if (ok) setGain(gain); Chris@11: Chris@11: QString colourSpec = attributes.value("colour"); Chris@11: if (colourSpec != "") { Chris@11: QColor colour(colourSpec); Chris@11: if (colour.isValid()) { Chris@11: setBaseColour(QColor(colourSpec)); Chris@11: } Chris@11: } Chris@11: Chris@11: bool showMeans = (attributes.value("showMeans") == "1" || Chris@11: attributes.value("showMeans") == "true"); Chris@11: setShowMeans(showMeans); Chris@11: Chris@11: bool greyscale = (attributes.value("greyscale") == "1" || Chris@11: attributes.value("greyscale") == "true"); Chris@11: setUseGreyscale(greyscale); Chris@11: Chris@11: ChannelMode channelMode = (ChannelMode) Chris@11: 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@11: Scale scale = (Scale) Chris@11: attributes.value("scale").toInt(&ok); Chris@11: if (ok) setScale(scale); Chris@11: Chris@11: bool aggressive = (attributes.value("aggressive") == "1" || Chris@11: 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@133: int val = 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@133: setGain(pow(10, float(step - 50) / 20.0)); Chris@133: } Chris@133: