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@59: This file copyright 2006 Chris Cannam. 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 "Colour3DPlotLayer.h" Chris@0: Chris@0: #include "base/View.h" Chris@0: #include "base/Profiler.h" Chris@0: Chris@0: #include Chris@0: #include Chris@0: #include Chris@0: Chris@0: #include Chris@0: Chris@0: #include Chris@0: Chris@0: Chris@44: Colour3DPlotLayer::Colour3DPlotLayer() : Chris@44: Layer(), Chris@0: m_model(0), Chris@0: m_cache(0) Chris@0: { Chris@44: Chris@0: } Chris@0: Chris@0: Colour3DPlotLayer::~Colour3DPlotLayer() Chris@0: { Chris@0: } Chris@0: Chris@0: void Chris@0: Colour3DPlotLayer::setModel(const DenseThreeDimensionalModel *model) Chris@0: { Chris@0: m_model = model; 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: connect(m_model, SIGNAL(modelChanged()), this, SLOT(cacheInvalid())); Chris@0: connect(m_model, SIGNAL(modelChanged(size_t, size_t)), Chris@0: this, SLOT(cacheInvalid(size_t, size_t))); Chris@0: Chris@0: emit modelReplaced(); Chris@0: } Chris@0: Chris@0: void Chris@0: Colour3DPlotLayer::cacheInvalid() Chris@0: { Chris@0: delete m_cache; Chris@0: m_cache = 0; Chris@0: } Chris@0: Chris@0: void Chris@0: Colour3DPlotLayer::cacheInvalid(size_t, size_t) Chris@0: { Chris@0: cacheInvalid(); Chris@0: } Chris@0: Chris@25: bool Chris@44: Colour3DPlotLayer::isLayerScrollable(const View *v) const Chris@25: { Chris@25: QPoint discard; Chris@44: return !v->shouldIlluminateLocalFeatures(this, discard); Chris@25: } Chris@25: Chris@25: QString Chris@44: Colour3DPlotLayer::getFeatureDescription(View *v, QPoint &pos) const Chris@25: { Chris@25: if (!m_model) return ""; Chris@25: Chris@25: int x = pos.x(); Chris@25: int y = pos.y(); Chris@25: Chris@25: size_t modelStart = m_model->getStartFrame(); Chris@25: size_t modelWindow = m_model->getWindowSize(); Chris@25: Chris@25: int sx0 = modelWindow * Chris@44: int((v->getFrameForX(x) - long(modelStart)) / long(modelWindow)); Chris@25: int sx1 = sx0 + modelWindow; Chris@25: Chris@44: float binHeight = float(v->height()) / m_model->getYBinCount(); Chris@44: int sy = (v->height() - y) / binHeight; Chris@25: Chris@25: float value = m_model->getBinValue(sx0, sy); Chris@25: Chris@25: QString binName = m_model->getBinName(sy); Chris@25: if (binName == "") binName = QString("[%1]").arg(sy + 1); Chris@25: else binName = QString("%1 [%2]").arg(binName).arg(sy + 1); Chris@25: Chris@25: QString text = tr("Time:\t%1 - %2\nBin:\t%3\nValue:\t%4") Chris@25: .arg(RealTime::frame2RealTime(sx0, m_model->getSampleRate()) Chris@25: .toText(true).c_str()) Chris@25: .arg(RealTime::frame2RealTime(sx1, m_model->getSampleRate()) Chris@25: .toText(true).c_str()) Chris@25: .arg(binName) Chris@25: .arg(value); Chris@25: Chris@25: return text; Chris@25: } Chris@25: Chris@25: int Chris@44: Colour3DPlotLayer::getVerticalScaleWidth(View *v, QPainter &paint) const Chris@25: { Chris@25: if (!m_model) return 0; Chris@25: Chris@98: QString sampleText = QString("[%1]").arg(m_model->getYBinCount()); Chris@25: int tw = paint.fontMetrics().width(sampleText); Chris@98: bool another = false; Chris@25: Chris@25: for (size_t i = 0; i < m_model->getYBinCount(); ++i) { Chris@25: if (m_model->getBinName(i).length() > sampleText.length()) { Chris@25: sampleText = m_model->getBinName(i); Chris@98: another = true; Chris@25: } Chris@25: } Chris@98: if (another) { Chris@25: tw = std::max(tw, paint.fontMetrics().width(sampleText)); Chris@25: } Chris@25: Chris@25: return tw + 13; Chris@25: } Chris@25: Chris@25: void Chris@44: Colour3DPlotLayer::paintVerticalScale(View *v, QPainter &paint, QRect rect) const Chris@25: { Chris@25: if (!m_model) return; Chris@25: Chris@25: int h = rect.height(), w = rect.width(); Chris@44: float binHeight = float(v->height()) / m_model->getYBinCount(); Chris@25: Chris@98: int count = v->height() / paint.fontMetrics().height(); Chris@98: int step = m_model->getYBinCount() / count; Chris@98: if (step == 0) step = 1; Chris@25: Chris@25: for (size_t i = 0; i < m_model->getYBinCount(); ++i) { Chris@25: Chris@98: if ((i % step) != 0) continue; Chris@98: Chris@44: int y0 = v->height() - (i * binHeight) - 1; Chris@25: Chris@25: QString text = m_model->getBinName(i); Chris@25: if (text == "") text = QString("[%1]").arg(i + 1); Chris@25: Chris@25: paint.drawLine(0, y0, w, y0); Chris@25: Chris@98: int cy = y0 - (step * binHeight)/2; Chris@25: int ty = cy + paint.fontMetrics().ascent()/2; Chris@25: Chris@25: paint.drawText(10, ty, text); Chris@25: } Chris@25: } Chris@25: Chris@0: void Chris@44: Colour3DPlotLayer::paint(View *v, QPainter &paint, QRect rect) const Chris@0: { Chris@0: // Profiler profiler("Colour3DPlotLayer::paint"); Chris@44: // std::cerr << "Colour3DPlotLayer::paint(): m_model is " << m_model << ", zoom level is " << v->getZoomLevel() << std::endl; Chris@0: Chris@0: //!!! This doesn't yet accommodate the fact that the model may Chris@0: //have a different sample rate from an underlying model. At the Chris@0: //moment our paint mechanism assumes all models have the same Chris@0: //sample rate. If that isn't the case, they won't align and the Chris@0: //time ruler will match whichever model was used to construct it. Chris@0: //Obviously it is not going to be the case in general that models Chris@0: //will have the same samplerate, so we need a pane samplerate as Chris@0: //well which we trivially realign to. (We can probably require Chris@0: //the waveform and spectrogram layers to display at the pane Chris@0: //samplerate.) Chris@0: Chris@0: int completion = 0; Chris@0: if (!m_model || !m_model->isOK() || !m_model->isReady(&completion)) { Chris@0: if (completion > 0) { Chris@44: paint.fillRect(0, 10, v->width() * completion / 100, Chris@0: 10, QColor(120, 120, 120)); Chris@0: } Chris@0: return; Chris@0: } Chris@0: Chris@0: size_t modelStart = m_model->getStartFrame(); Chris@0: size_t modelEnd = m_model->getEndFrame(); Chris@0: size_t modelWindow = m_model->getWindowSize(); Chris@0: Chris@12: size_t cacheWidth = (modelEnd - modelStart) / modelWindow + 1; Chris@12: size_t cacheHeight = m_model->getYBinCount(); Chris@12: Chris@12: if (m_cache && Chris@12: (m_cache->width() != cacheWidth || Chris@12: m_cache->height() != cacheHeight)) { Chris@12: Chris@12: delete m_cache; Chris@12: m_cache = 0; Chris@12: } Chris@12: Chris@0: if (!m_cache) { Chris@0: Chris@12: m_cache = new QImage(cacheWidth, cacheHeight, QImage::Format_Indexed8); Chris@0: Chris@0: m_cache->setNumColors(256); Chris@0: DenseThreeDimensionalModel::BinValueSet values; Chris@0: Chris@0: float min = m_model->getMinimumLevel(); Chris@0: float max = m_model->getMaximumLevel(); Chris@0: Chris@0: if (max == min) max = min + 1.0; Chris@0: Chris@0: for (int value = 0; value < 256; ++value) { Chris@0: int hue = 256 - value; Chris@26: QColor colour = QColor::fromHsv(hue, value/2 + 128, value); Chris@26: m_cache->setColor(value, qRgba(colour.red(), colour.green(), colour.blue(), 80)); Chris@0: } Chris@0: Chris@0: m_cache->fill(min); Chris@0: Chris@0: for (size_t f = modelStart; f <= modelEnd; f += modelWindow) { Chris@0: Chris@0: values.clear(); Chris@0: m_model->getBinValues(f, values); Chris@0: Chris@0: for (size_t y = 0; y < m_model->getYBinCount(); ++y) { Chris@0: Chris@0: float value = min; Chris@0: if (y < values.size()) value = values[y]; Chris@0: Chris@0: int pixel = int(((value - min) * 256) / (max - min)); Chris@98: if (pixel < 0) pixel = 0; Chris@98: if (pixel > 255) pixel = 255; Chris@24: Chris@0: m_cache->setPixel(f / modelWindow, y, pixel); Chris@0: } Chris@0: } Chris@0: } Chris@0: Chris@98: if (m_model->getYBinCount() >= v->height()) { Chris@98: paintDense(v, paint, rect); Chris@98: return; Chris@98: } Chris@98: Chris@0: int x0 = rect.left(); Chris@0: int x1 = rect.right() + 1; Chris@0: Chris@0: int w = x1 - x0; Chris@44: int h = v->height(); Chris@0: Chris@0: // The cache is from the model's start frame to the model's end Chris@0: // frame at the model's window increment frames per pixel. We Chris@0: // want to draw from our start frame + x0 * zoomLevel to our start Chris@0: // frame + x1 * zoomLevel at zoomLevel frames per pixel. Chris@0: Chris@0: //!!! Strictly speaking we want quite different paint mechanisms Chris@0: //for models that have more than one bin per pixel in either Chris@0: //direction. This one is only really appropriate for models with Chris@0: //far fewer bins in both directions. Chris@0: Chris@44: int sx0 = int((v->getFrameForX(x0) - long(modelStart)) / long(modelWindow)); Chris@44: int sx1 = int((v->getFrameForX(x1) - long(modelStart)) / long(modelWindow)); Chris@0: int sw = sx1 - sx0; Chris@0: int sh = m_model->getYBinCount(); Chris@0: Chris@0: /* Chris@0: std::cerr << "Colour3DPlotLayer::paint: w " << w << ", h " << h << ", sx0 " << sx0 << ", sx1 " << sx1 << ", sw " << sw << ", sh " << sh << std::endl; Chris@0: std::cerr << "Colour3DPlotLayer: sample rate is " << m_model->getSampleRate() << ", window size " << m_model->getWindowSize() << std::endl; Chris@0: */ Chris@0: Chris@25: QPoint illuminatePos; Chris@44: bool illuminate = v->shouldIlluminateLocalFeatures(this, illuminatePos); Chris@54: char labelbuf[10]; Chris@25: Chris@0: for (int sx = sx0 - 1; sx <= sx1; ++sx) { Chris@0: Chris@0: int fx = sx * int(modelWindow); Chris@0: Chris@0: if (fx + modelWindow < int(modelStart) || Chris@0: fx > int(modelEnd)) continue; Chris@0: Chris@54: int rx0 = v->getXForFrame(fx + int(modelStart)); Chris@54: int rx1 = v->getXForFrame(fx + int(modelStart) + int(modelWindow)); Chris@54: Chris@54: int w = rx1 - rx0; Chris@54: if (w < 1) w = 1; Chris@54: Chris@54: bool showLabel = (w > 10 && Chris@54: paint.fontMetrics().width("0.000000") < w - 3 && Chris@54: paint.fontMetrics().height() < (h / sh)); Chris@98: Chris@0: for (int sy = 0; sy < sh; ++sy) { Chris@0: Chris@0: int ry0 = h - (sy * h) / sh - 1; Chris@0: QRgb pixel = qRgb(255, 255, 255); Chris@0: if (sx >= 0 && sx < m_cache->width() && Chris@0: sy >= 0 && sy < m_cache->height()) { Chris@0: pixel = m_cache->pixel(sx, sy); Chris@0: } Chris@0: Chris@0: QColor pen(255, 255, 255, 80); Chris@0: QColor brush(pixel); Chris@0: brush.setAlpha(160); Chris@0: paint.setPen(Qt::NoPen); Chris@0: paint.setBrush(brush); Chris@0: Chris@25: QRect r(rx0, ry0 - h / sh - 1, w, h / sh + 1); Chris@25: Chris@25: if (illuminate) { Chris@25: if (r.contains(illuminatePos)) { Chris@25: paint.setPen(Qt::black);//!!! Chris@25: } Chris@25: } Chris@76: Chris@76: // std::cout << "rect " << rx0 << "," << (ry0 - h / sh - 1) << " " Chris@76: // << w << "x" << (h / sh + 1) << std::endl; Chris@25: Chris@25: paint.drawRect(r); Chris@0: Chris@54: if (showLabel) { Chris@54: if (sx >= 0 && sx < m_cache->width() && Chris@54: sy >= 0 && sy < m_cache->height()) { Chris@54: float value = m_model->getBinValue(fx, sy); Chris@54: sprintf(labelbuf, "%06f", value); Chris@54: QString text(labelbuf); Chris@54: paint.setPen(Qt::white); Chris@54: paint.drawText(rx0 + 2, Chris@54: ry0 - h / sh - 1 + 2 + paint.fontMetrics().ascent(), Chris@54: text); Chris@0: } Chris@0: } Chris@0: } Chris@0: } Chris@98: } Chris@0: Chris@98: void Chris@98: Colour3DPlotLayer::paintDense(View *v, QPainter &paint, QRect rect) const Chris@98: { Chris@98: long startFrame = v->getStartFrame(); Chris@98: int zoomLevel = v->getZoomLevel(); Chris@0: Chris@98: size_t modelStart = m_model->getStartFrame(); Chris@98: size_t modelEnd = m_model->getEndFrame(); Chris@98: size_t modelWindow = m_model->getWindowSize(); Chris@0: Chris@98: int x0 = rect.left(); Chris@98: int x1 = rect.right() + 1; Chris@0: Chris@98: int w = x1 - x0; Chris@98: int h = v->height(); Chris@98: int sh = m_model->getYBinCount(); Chris@98: Chris@98: QImage img(w, h, QImage::Format_RGB32); Chris@98: Chris@98: for (int x = x0; x < x1; ++x) { Chris@98: Chris@98: float sx0 = (float(v->getFrameForX(x)) - modelStart) / modelWindow; Chris@98: float sx1 = (float(v->getFrameForX(x+1)) - modelStart) / modelWindow; Chris@98: Chris@98: int sx0i = int(sx0 + 0.001); Chris@98: int sx1i = int(sx1); Chris@98: Chris@98: for (int y = 0; y < h; ++y) { Chris@98: Chris@98: float sy0 = (float(h - y - 1) * sh) / h; Chris@98: float sy1 = (float(h - y) * sh) / h; Chris@98: Chris@98: int sy0i = int(sy0 + 0.001); Chris@98: int sy1i = int(sy1); Chris@98: Chris@98: float mag = 0.0, div = 0.0; Chris@98: Chris@98: for (int sx = sx0i; sx <= sx1i; ++sx) { Chris@98: Chris@98: if (sx < 0 || sx >= m_cache->width()) continue; Chris@98: Chris@98: for (int sy = sy0i; sy <= sy1i; ++sy) { Chris@98: Chris@98: if (sy < 0 || sy >= m_cache->height()) continue; Chris@98: Chris@98: float prop = 1.0; Chris@98: if (sx == sx0i) prop *= (sx + 1) - sx0; Chris@98: if (sx == sx1i) prop *= sx1 - sx; Chris@98: if (sy == sy0i) prop *= (sy + 1) - sy0; Chris@98: if (sy == sy1i) prop *= sy1 - sy; Chris@98: Chris@98: mag += prop * m_cache->pixelIndex(sx, sy); Chris@98: div += prop; Chris@98: } Chris@98: } Chris@98: Chris@98: if (div != 0) mag /= div; Chris@98: if (mag < 0) mag = 0; Chris@98: if (mag > 255) mag = 255; Chris@98: Chris@98: img.setPixel(x - x0, y, m_cache->color(int(mag + 0.001))); Chris@98: } Chris@0: } Chris@0: Chris@98: paint.drawImage(x0, 0, img); Chris@0: } Chris@0: Chris@28: bool Chris@44: Colour3DPlotLayer::snapToFeatureFrame(View *v, int &frame, Chris@28: size_t &resolution, Chris@28: SnapType snap) const Chris@24: { Chris@24: if (!m_model) { Chris@44: return Layer::snapToFeatureFrame(v, frame, resolution, snap); Chris@24: } Chris@24: Chris@24: resolution = m_model->getWindowSize(); Chris@28: int left = (frame / resolution) * resolution; Chris@28: int right = left + resolution; Chris@28: Chris@28: switch (snap) { Chris@28: case SnapLeft: frame = left; break; Chris@28: case SnapRight: frame = right; break; Chris@28: case SnapNearest: Chris@28: case SnapNeighbouring: Chris@28: if (frame - left > right - frame) frame = right; Chris@28: else frame = left; Chris@28: break; Chris@28: } Chris@24: Chris@28: return true; Chris@24: } Chris@24: Chris@0: #ifdef INCLUDE_MOCFILES Chris@0: #include "Colour3DPlotLayer.moc.cpp" Chris@0: #endif Chris@0: Chris@0: