Chris@0: /* -*- c-basic-offset: 4 -*- vi:set ts=8 sts=4 sw=4: */ Chris@0: Chris@0: /* Chris@0: A waveform viewer and audio annotation editor. Chris@4: Chris Cannam, Queen Mary University of London, 2005-2006 Chris@0: Chris@0: This is experimental software. Not for distribution. Chris@0: */ Chris@0: Chris@0: #include "WaveformLayer.h" Chris@0: Chris@0: #include "base/AudioLevel.h" Chris@0: #include "base/View.h" Chris@0: #include "base/Profiler.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@0: WaveformLayer::WaveformLayer(View *w) : Chris@0: Layer(w), Chris@0: m_model(0), Chris@0: m_gain(1.0f), Chris@0: m_colour(Qt::black), 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@0: m_view->addLayer(this); 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@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@0: } Chris@0: Chris@0: Layer::PropertyList Chris@0: WaveformLayer::getProperties() const Chris@0: { Chris@0: PropertyList list; Chris@0: list.push_back(tr("Colour")); Chris@0: list.push_back(tr("Scale")); Chris@0: list.push_back(tr("Gain")); Chris@0: list.push_back(tr("Merge Channels")); Chris@0: return list; Chris@0: } Chris@0: Chris@0: Layer::PropertyType Chris@0: WaveformLayer::getPropertyType(const PropertyName &name) const Chris@0: { Chris@0: if (name == tr("Gain")) return RangeProperty; Chris@0: if (name == tr("Colour")) return ValueProperty; Chris@0: if (name == tr("Merge Channels")) return ToggleProperty; Chris@0: if (name == tr("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@0: if (name == tr("Gain") || Chris@0: name == tr("Scale")) return tr("Scale"); Chris@0: return QString(); Chris@0: } Chris@0: Chris@0: int Chris@0: WaveformLayer::getPropertyRangeAndValue(const PropertyName &name, Chris@0: int *min, int *max) const Chris@0: { Chris@0: int deft = 0; Chris@0: Chris@10: int throwaway; Chris@10: if (!min) min = &throwaway; Chris@10: if (!max) max = &throwaway; Chris@10: Chris@0: if (name == tr("Gain")) { Chris@0: Chris@0: *min = -50; Chris@0: *max = 50; Chris@0: Chris@0: deft = int(nearbyint(log10(m_gain) * 20.0)); Chris@0: if (deft < *min) deft = *min; Chris@0: if (deft > *max) deft = *max; Chris@0: Chris@0: } else if (name == tr("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@0: else if (m_colour == Qt::darkBlue) 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@0: } else if (name == tr("Merge Channels")) { Chris@0: Chris@0: deft = ((m_channelMode == MergeChannels) ? 1 : 0); Chris@0: Chris@0: } else if (name == tr("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@0: if (name == tr("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@0: if (name == tr("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@0: return tr(""); Chris@0: } Chris@0: Chris@0: void Chris@0: WaveformLayer::setProperty(const PropertyName &name, int value) Chris@0: { Chris@0: if (name == tr("Gain")) { Chris@0: setGain(pow(10, float(value)/20.0)); Chris@0: } else if (name == tr("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@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@0: } else if (name == tr("Merge Channels")) { Chris@0: setChannelMode(value ? MergeChannels : SeparateChannels); Chris@0: } else if (name == tr("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: /* Chris@0: Chris@0: int Chris@0: WaveformLayer::getProperty(const PropertyName &name) Chris@0: { Chris@0: if (name == "Gain") { Chris@0: return int((getGain() - 1.0) * 10.0 + 0.01); Chris@0: } Chris@0: if (name == "Colour") { Chris@0: Chris@0: */ Chris@0: Chris@0: void Chris@0: WaveformLayer::setGain(float gain) //!!! inadequate for floats! Chris@0: { Chris@0: if (m_gain == gain) return; Chris@0: m_gain = gain; Chris@0: m_cacheValid = false; Chris@0: emit layerParametersChanged(); Chris@0: } Chris@0: Chris@0: 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@0: 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@0: WaveformLayer::getCompletion() 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@0: int Chris@0: WaveformLayer::dBscale(float sample, int m) const Chris@0: { Chris@0: 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@0: WaveformLayer::getChannelArrangement(size_t &min, size_t &max, bool &merging) 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@0: if (m_channelMode == MergeChannels) { 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@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@0: void Chris@0: WaveformLayer::paint(QPainter &viewPainter, QRect rect) const Chris@0: { Chris@0: if (!m_model || !m_model->isOK()) { Chris@0: return; Chris@0: } Chris@0: Chris@0: long startFrame = m_view->getStartFrame(); Chris@0: int zoomLevel = m_view->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@0: bool mergingChannels = false; Chris@0: Chris@0: channels = getChannelArrangement(minChannel, maxChannel, mergingChannels); Chris@0: if (channels == 0) return; Chris@0: Chris@0: int w = m_view->width(); Chris@0: int h = m_view->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@0: paint->setBrush(m_view->palette().background()); Chris@0: paint->drawRect(rect); Chris@0: Chris@0: paint->setPen(Qt::black); Chris@0: paint->setBrush(Qt::NoBrush); Chris@0: Chris@13: paint->setRenderHint(QPainter::Antialiasing, false); Chris@13: Chris@0: } else { Chris@0: paint = &viewPainter; Chris@0: } Chris@0: 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@0: long frame0 = startFrame + x0 * zoomLevel; Chris@0: long frame1 = startFrame + (x1 + 1) * zoomLevel; 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@0: for (int i = 0; i < 3; ++i) { Chris@0: int level = 192 - 64 * i; Chris@0: greys[i] = QColor(level, level, level); Chris@0: } Chris@0: } else { Chris@0: int factor = (m_view->hasLightBackground() ? 120 : 80); Chris@0: greys[2] = m_colour.light(factor); Chris@0: greys[1] = greys[2].light(factor); Chris@0: greys[0] = greys[1].light(factor); Chris@0: } Chris@0: Chris@0: QColor midColour = m_colour; Chris@0: if (midColour == Qt::black) { Chris@0: midColour = Qt::gray; Chris@0: } else if (m_view->hasLightBackground()) { Chris@0: midColour = midColour.light(150); Chris@0: } else { Chris@0: midColour = midColour.light(50); Chris@0: } Chris@0: Chris@0: for (size_t ch = minChannel; ch <= maxChannel; ++ch) { Chris@0: Chris@0: int prevRangeBottom = -1, prevRangeTop = -1; Chris@0: Chris@0: int m = (h / channels) / 2; Chris@0: int my = m + (((ch - minChannel) * h) / channels); Chris@0: Chris@0: // std::cerr << "ch = " << ch << ", channels = " << channels << ", m = " << m << ", my = " << my << ", h = " << h << std::endl; Chris@0: Chris@0: if (my - m > y1 || my + m < y0) continue; Chris@0: Chris@0: paint->setPen(greys[0]); Chris@0: paint->drawLine(x0, my, x1, my); Chris@0: Chris@0: if (frame1 <= 0) continue; Chris@0: Chris@0: size_t modelZoomLevel = zoomLevel; Chris@0: Chris@0: ranges = m_model->getRanges Chris@0: (ch, frame0 < 0 ? 0 : frame0, frame1, modelZoomLevel); Chris@0: Chris@0: if (mergingChannels) { Chris@0: otherChannelRanges = m_model->getRanges Chris@0: (1, frame0 < 0 ? 0 : frame0, frame1, modelZoomLevel); Chris@0: } Chris@0: 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@0: } 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@0: rangeBottom = int( m * greyLevels * range.min * m_gain); Chris@0: rangeTop = int( m * greyLevels * range.max * m_gain); Chris@0: meanBottom = int(-m * range.absmean * m_gain); Chris@0: meanTop = int( m * range.absmean * m_gain); Chris@0: break; Chris@0: Chris@0: case dBScale: Chris@0: rangeBottom = dBscale(range.min * m_gain, m * greyLevels); Chris@0: rangeTop = dBscale(range.max * m_gain, m * greyLevels); Chris@0: meanBottom = -dBscale(range.absmean * m_gain, m); Chris@0: meanTop = dBscale(range.absmean * m_gain, m); Chris@0: break; Chris@0: Chris@0: case MeterScale: Chris@0: rangeBottom = AudioLevel::multiplier_to_preview(range.min * m_gain, m * greyLevels); Chris@0: rangeTop = AudioLevel::multiplier_to_preview(range.max * m_gain, m * greyLevels); Chris@0: meanBottom = -AudioLevel::multiplier_to_preview(range.absmean * m_gain, m); Chris@0: meanTop = AudioLevel::multiplier_to_preview(range.absmean * m_gain, m); Chris@0: } Chris@0: Chris@0: int topFill = (rangeTop < 0 ? -rangeTop : rangeTop) % greyLevels; Chris@0: int bottomFill = (rangeBottom < 0 ? -rangeBottom : rangeBottom) % greyLevels; Chris@0: rangeTop = rangeTop / greyLevels; Chris@0: rangeBottom = rangeBottom / greyLevels; Chris@0: Chris@0: bool clipped = false; Chris@0: if (rangeTop < -m) { rangeTop = -m; clipped = true; } Chris@0: if (rangeTop > m) { rangeTop = m; clipped = true; } Chris@0: if (rangeBottom < -m) { rangeBottom = -m; clipped = true; } Chris@0: if (rangeBottom > m) { rangeBottom = m; clipped = true; } Chris@0: Chris@0: rangeBottom = my - rangeBottom; Chris@0: rangeTop = my - rangeTop; Chris@0: meanBottom = my - meanBottom; Chris@0: meanTop = my - meanTop; 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@0: if (meanBottom == rangeBottom) { 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@0: paint->setPen(midColour); Chris@0: paint->drawLine(x-1, prevRangeTop, x, rangeBottom); Chris@0: paint->setPen(m_colour); Chris@0: paint->drawPoint(x-1, prevRangeTop); Chris@0: } else if (prevRangeBottom < rangeTop && Chris@0: prevRangeTop < rangeTop) { Chris@0: paint->setPen(midColour); Chris@0: paint->drawLine(x-1, prevRangeBottom, x, rangeTop); Chris@0: paint->setPen(m_colour); Chris@0: paint->drawPoint(x-1, prevRangeBottom); Chris@0: } Chris@0: } Chris@0: Chris@0: if (ready) { Chris@0: if (clipped || Chris@0: range.min * m_gain <= -1.0 || Chris@0: range.max * m_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@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@0: paint->drawPoint(x, rangeTop - 1); Chris@0: } Chris@0: if (bottomFill > 0 && Chris@0: (!drawMean || (rangeBottom > meanBottom + 1))) { Chris@0: paint->setPen(greys[bottomFill - 1]); Chris@0: paint->drawPoint(x, rangeBottom + 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@0: if (ready && rect == m_view->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@0: int Chris@0: WaveformLayer::getVerticalScaleWidth(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@0: WaveformLayer::paintVerticalScale(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@0: bool mergingChannels = false; Chris@0: Chris@0: channels = getChannelArrangement(minChannel, maxChannel, mergingChannels); 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@0: for (size_t ch = minChannel; ch <= maxChannel; ++ch) { Chris@0: Chris@0: int m = (h / channels) / 2; Chris@0: int my = m + (((ch - minChannel) * h) / channels); Chris@0: int py = -1; Chris@0: Chris@0: for (int i = 0; i <= 10; ++i) { Chris@0: Chris@0: int vy = 0; Chris@0: QString text = ""; Chris@0: Chris@0: if (m_scale == LinearScale) { Chris@0: Chris@0: vy = int((m * i * m_gain) / 10); Chris@0: Chris@0: text = QString("%1").arg(float(i) / 10.0); Chris@0: if (i == 0) text = "0.0"; Chris@0: if (i == 10) text = "1.0"; Chris@0: Chris@0: } else { Chris@0: Chris@0: int db; Chris@0: bool minvalue = false; Chris@0: Chris@0: if (m_scale == MeterScale) { Chris@0: static int dbs[] = { -50, -40, -30, -20, -15, Chris@0: -10, -5, -3, -2, -1, 0 }; Chris@0: db = dbs[i]; Chris@0: if (db == -50) minvalue = true; Chris@0: vy = AudioLevel::multiplier_to_preview Chris@0: (AudioLevel::dB_to_multiplier(db) * m_gain, m); Chris@0: } else { Chris@0: db = -100 + i * 10; Chris@0: if (db == -100) minvalue = true; Chris@0: vy = dBscale Chris@0: (AudioLevel::dB_to_multiplier(db) * m_gain, m); Chris@0: } Chris@0: Chris@0: text = QString("%1").arg(db); Chris@0: if (db == 0) text = tr("0dB"); Chris@0: if (minvalue) { Chris@0: text = tr("-Inf"); Chris@0: vy = 0; Chris@0: } Chris@0: } Chris@0: Chris@0: if (vy < 0) vy = -vy; Chris@0: if (vy >= m - 1) continue; Chris@0: Chris@0: if (py >= 0 && (vy - py) < textHeight - 1) { Chris@0: paint.drawLine(w - 4, my - vy, w, my - vy); Chris@0: if (vy > 0) paint.drawLine(w - 4, my + vy, w, my + vy); Chris@0: continue; Chris@0: } Chris@0: Chris@0: paint.drawLine(w - 7, my - vy, w, my - vy); Chris@0: if (vy > 0) paint.drawLine(w - 7, my + vy, w, my + vy); Chris@0: Chris@0: int tx = 3; Chris@0: if (m_scale != LinearScale) { Chris@0: tx = w - 10 - paint.fontMetrics().width(text); Chris@0: } Chris@0: Chris@0: paint.drawText(tx, my - vy + toff, text); Chris@0: if (vy > 0) paint.drawText(tx, my + vy + toff, text); Chris@0: Chris@0: py = vy; 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@6: "aggressive=\"%8\"") 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@6: .arg(m_aggressive); 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@11: } Chris@11: Chris@0: #ifdef INCLUDE_MOCFILES Chris@0: #include "WaveformLayer.moc.cpp" Chris@0: #endif Chris@0: