annotate layer/WaveformLayer.h @ 1338:6d091348e4e9 zoom

Get oversampled rendering almost working, but I think we need to take more care with the oversampling itself
author Chris Cannam
date Tue, 25 Sep 2018 16:38:50 +0100
parents 43296804c473
children b0555b434575
rev   line source
Chris@58 1 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */
Chris@0 2
Chris@0 3 /*
Chris@59 4 Sonic Visualiser
Chris@59 5 An audio file viewer and annotation editor.
Chris@59 6 Centre for Digital Music, Queen Mary, University of London.
Chris@182 7 This file copyright 2006 Chris Cannam and QMUL.
Chris@0 8
Chris@59 9 This program is free software; you can redistribute it and/or
Chris@59 10 modify it under the terms of the GNU General Public License as
Chris@59 11 published by the Free Software Foundation; either version 2 of the
Chris@59 12 License, or (at your option) any later version. See the file
Chris@59 13 COPYING included with this distribution for more information.
Chris@0 14 */
Chris@0 15
Chris@30 16 #ifndef _WAVEFORM_LAYER_H_
Chris@30 17 #define _WAVEFORM_LAYER_H_
Chris@0 18
Chris@0 19 #include <QRect>
Chris@0 20
Chris@287 21 #include "SingleColourLayer.h"
Chris@0 22
Chris@128 23 #include "data/model/RangeSummarisableTimeValueModel.h"
Chris@0 24
Chris@0 25 class View;
Chris@0 26 class QPainter;
Chris@0 27 class QPixmap;
Chris@0 28
Chris@1336 29 namespace breakfastquay { class Resampler; }
Chris@1336 30
Chris@287 31 class WaveformLayer : public SingleColourLayer
Chris@0 32 {
Chris@0 33 Q_OBJECT
Chris@0 34
Chris@0 35 public:
Chris@44 36 WaveformLayer();
Chris@0 37 ~WaveformLayer();
Chris@0 38
Chris@156 39 virtual const ZoomConstraint *getZoomConstraint() const {
Chris@156 40 return m_model ? m_model->getZoomConstraint() : 0;
Chris@156 41 }
Chris@0 42 virtual const Model *getModel() const { return m_model; }
Chris@916 43 virtual void paint(LayerGeometryProvider *v, QPainter &paint, QRect rect) const;
Chris@0 44
Chris@918 45 virtual QString getFeatureDescription(LayerGeometryProvider *v, QPoint &) const;
Chris@25 46
Chris@287 47 virtual ColourSignificance getLayerColourSignificance() const {
Chris@287 48 return ColourAndBackgroundSignificant;
Chris@287 49 }
Chris@287 50
Chris@918 51 virtual int getVerticalScaleWidth(LayerGeometryProvider *v, bool detailed, QPainter &) const;
Chris@918 52 virtual void paintVerticalScale(LayerGeometryProvider *v, bool detailed, QPainter &paint, QRect rect) const;
Chris@0 53
Chris@0 54 void setModel(const RangeSummarisableTimeValueModel *model);
Chris@0 55
Chris@0 56 virtual PropertyList getProperties() const;
Chris@87 57 virtual QString getPropertyLabel(const PropertyName &) const;
Chris@335 58 virtual QString getPropertyIconName(const PropertyName &) const;
Chris@0 59 virtual PropertyType getPropertyType(const PropertyName &) const;
Chris@0 60 virtual QString getPropertyGroupName(const PropertyName &) const;
Chris@0 61 virtual int getPropertyRangeAndValue(const PropertyName &,
Chris@216 62 int *min, int *max, int *deflt) const;
Chris@0 63 virtual QString getPropertyValueLabel(const PropertyName &,
Chris@1266 64 int value) const;
Chris@167 65 virtual RangeMapper *getNewPropertyRangeMapper(const PropertyName &) const;
Chris@0 66 virtual void setProperty(const PropertyName &, int value);
Chris@0 67
Chris@0 68 /**
Chris@0 69 * Set the gain multiplier for sample values in this view.
Chris@0 70 *
Chris@0 71 * The default is 1.0.
Chris@0 72 */
Chris@0 73 void setGain(float gain);
Chris@0 74 float getGain() const { return m_gain; }
Chris@0 75
Chris@0 76 /**
Chris@67 77 * Toggle automatic normalization of the currently visible waveform.
Chris@67 78 */
Chris@67 79 void setAutoNormalize(bool);
Chris@67 80 bool getAutoNormalize() const { return m_autoNormalize; }
Chris@67 81
Chris@67 82 /**
Chris@0 83 * Set whether to display mean values as a lighter-coloured area
Chris@0 84 * beneath the peaks. Rendering will be slightly faster without
Chris@0 85 * but arguably prettier with.
Chris@0 86 *
Chris@0 87 * The default is to display means.
Chris@0 88 */
Chris@0 89 void setShowMeans(bool);
Chris@0 90 bool getShowMeans() const { return m_showMeans; }
Chris@0 91
Chris@0 92 /**
Chris@0 93 * Set whether to use shades of grey (or of the base colour) to
Chris@0 94 * provide additional perceived vertical resolution (i.e. using
Chris@0 95 * half-filled pixels to represent levels that only just meet the
Chris@0 96 * pixel unit boundary). This provides a small improvement in
Chris@0 97 * waveform quality at a small cost in rendering speed.
Chris@0 98 *
Chris@0 99 * The default is to use greyscale.
Chris@0 100 */
Chris@0 101 void setUseGreyscale(bool);
Chris@0 102 bool getUseGreyscale() const { return m_greyscale; }
Chris@0 103
Chris@0 104
Chris@67 105 enum ChannelMode { SeparateChannels, MixChannels, MergeChannels };
Chris@0 106
Chris@0 107 /**
Chris@0 108 * Specify whether multi-channel audio data should be displayed
Chris@67 109 * with a separate axis per channel (SeparateChannels), with a
Chris@0 110 * single synthetic axis showing channel 0 above the axis and
Chris@67 111 * channel 1 below (MergeChannels), or with a single axis showing
Chris@67 112 * the average of the channels (MixChannels).
Chris@0 113 *
Chris@0 114 * MergeChannels does not work for files with more than 2
Chris@0 115 * channels.
Chris@0 116 *
Chris@0 117 * The default is SeparateChannels.
Chris@0 118 */
Chris@0 119 void setChannelMode(ChannelMode);
Chris@0 120 ChannelMode getChannelMode() const { return m_channelMode; }
Chris@0 121
Chris@0 122
Chris@0 123 /**
Chris@0 124 * Specify the channel to use from the source model. A value of
Chris@0 125 * -1 means to show all available channels (laid out to the
Chris@0 126 * channel mode). The default is -1.
Chris@0 127 */
Chris@0 128 void setChannel(int);
Chris@0 129 int getChannel() const { return m_channel; }
Chris@0 130
Chris@0 131
Chris@0 132 enum Scale { LinearScale, MeterScale, dBScale };
Chris@0 133
Chris@0 134 /**
Chris@0 135 * Specify the vertical scale for sample levels. With LinearScale,
Chris@0 136 * the scale is directly proportional to the raw [-1, +1)
Chris@0 137 * floating-point audio sample values. With dBScale the
Chris@0 138 * vertical scale is proportional to dB level (truncated at
Chris@0 139 * -50dB). MeterScale provides a hybrid variable scale based on
Chris@0 140 * IEC meter scale, intended to provide a clear overview at
Chris@0 141 * relatively small heights.
Chris@0 142 *
Chris@0 143 * Note that the effective gain (see setGain()) is applied before
Chris@0 144 * vertical scaling.
Chris@0 145 *
Chris@0 146 * The default is LinearScale.
Chris@0 147 */
Chris@0 148 void setScale(Scale);
Chris@0 149 Scale getScale() const { return m_scale; }
Chris@0 150
Chris@0 151 /**
Chris@709 152 * Specify the height of the middle of the waveform track or
Chris@709 153 * tracks within the layer, from 0.0 to 1.0.
Chris@709 154 *
Chris@709 155 * A value of 0.0 would indicate that the waveform occupies
Chris@709 156 * effectively no space at the very top of the layer; 1.0 would
Chris@709 157 * indicate that the waveform occupies no space at the very
Chris@709 158 * bottom; the default value of 0.5 indicates that it occupies the
Chris@709 159 * whole layer, centred at the middle.
Chris@709 160 */
Chris@905 161 void setMiddleLineHeight(double);
Chris@905 162 double getMiddleLineHeight() const { return m_middleLineHeight; }
Chris@709 163
Chris@709 164 /**
Chris@0 165 * Enable or disable aggressive pixmap cacheing. If enabled,
Chris@0 166 * waveforms will be rendered to an off-screen pixmap and
Chris@0 167 * refreshed from there instead of being redrawn from the peak
Chris@0 168 * data each time. This may be faster if the data and zoom level
Chris@0 169 * do not change often, but it may be slower for frequently zoomed
Chris@0 170 * data and it will only work if the waveform is the "bottom"
Chris@0 171 * layer on the displayed widget, as each refresh will erase
Chris@0 172 * anything beneath the waveform.
Chris@0 173 *
Chris@0 174 * This is intended specifically for a panner widget display in
Chris@0 175 * which the waveform never moves, zooms, or changes, but some
Chris@0 176 * graphic such as a panner outline is frequently redrawn over the
Chris@0 177 * waveform. This situation would necessitate a lot of waveform
Chris@0 178 * refresh if the default cacheing strategy was used.
Chris@0 179 *
Chris@0 180 * The default is not to use aggressive cacheing.
Chris@0 181 */
Chris@0 182 void setAggressiveCacheing(bool);
Chris@0 183 bool getAggressiveCacheing() const { return m_aggressive; }
Chris@0 184
Chris@918 185 virtual bool isLayerScrollable(const LayerGeometryProvider *) const;
Chris@67 186
Chris@918 187 virtual int getCompletion(LayerGeometryProvider *) const;
Chris@0 188
Chris@905 189 virtual bool getValueExtents(double &min, double &max,
Chris@101 190 bool &log, QString &unit) const;
Chris@79 191
Chris@918 192 virtual bool getYScaleValue(const LayerGeometryProvider *v, int y,
Chris@905 193 double &value, QString &unit) const;
Chris@274 194
Chris@918 195 virtual bool getYScaleDifference(const LayerGeometryProvider *v, int y0, int y1,
Chris@905 196 double &diff, QString &unit) const;
Chris@261 197
Chris@316 198 virtual void toXml(QTextStream &stream, QString indent = "",
Chris@316 199 QString extraAttributes = "") const;
Chris@6 200
Chris@287 201 virtual void setProperties(const QXmlAttributes &attributes);
Chris@11 202
Chris@133 203 virtual int getVerticalZoomSteps(int &defaultStep) const;
Chris@133 204 virtual int getCurrentVerticalZoomStep() const;
Chris@133 205 virtual void setVerticalZoomStep(int);
Chris@133 206
Chris@947 207 virtual bool canExistWithoutModel() const { return true; }
Chris@947 208
Chris@0 209 protected:
Chris@908 210 int dBscale(double sample, int m) const;
Chris@0 211
Chris@0 212 const RangeSummarisableTimeValueModel *m_model; // I do not own this
Chris@0 213
Chris@1338 214 typedef std::vector<RangeSummarisableTimeValueModel::RangeBlock> RangeVec;
Chris@1338 215
Chris@0 216 /// Return value is number of channels displayed
Chris@805 217 int getChannelArrangement(int &min, int &max,
Chris@1332 218 bool &merging, bool &mixing) const;
Chris@0 219
Chris@1338 220 void paintChannel
Chris@1336 221 (LayerGeometryProvider *, QPainter *paint, QRect rect, int channel,
Chris@1338 222 const RangeVec &ranges,
Chris@1336 223 int blockSize, sv_frame_t frame0, sv_frame_t frame1) const;
Chris@1336 224
Chris@1335 225 void paintChannelScaleGuides(LayerGeometryProvider *, QPainter *paint,
Chris@1335 226 QRect rect, int channel) const;
Chris@1338 227
Chris@1338 228 void getSummaryRanges(int minChannel, int maxChannel,
Chris@1338 229 bool mixingOrMerging,
Chris@1338 230 sv_frame_t f0, sv_frame_t f1,
Chris@1338 231 int blockSize, RangeVec &ranges) const;
Chris@1338 232
Chris@1338 233 void getOversampledRanges(int minChannel, int maxChannel,
Chris@1338 234 bool mixingOrMerging,
Chris@1338 235 sv_frame_t f0, sv_frame_t f1,
Chris@1338 236 int oversampleBy, RangeVec &ranges) const;
Chris@1332 237
Chris@918 238 int getYForValue(const LayerGeometryProvider *v, double value, int channel) const;
Chris@68 239
Chris@918 240 double getValueForY(const LayerGeometryProvider *v, int y, int &channel) const;
Chris@261 241
Chris@918 242 bool getSourceFramesForX(LayerGeometryProvider *v, int x, int modelZoomLevel,
Chris@908 243 sv_frame_t &f0, sv_frame_t &f1) const;
Chris@365 244
Chris@918 245 float getNormalizeGain(LayerGeometryProvider *v, int channel) const;
Chris@365 246
Chris@287 247 virtual void flagBaseColourChanged() { m_cacheValid = false; }
Chris@287 248
Chris@6 249 float m_gain;
Chris@67 250 bool m_autoNormalize;
Chris@6 251 bool m_showMeans;
Chris@6 252 bool m_greyscale;
Chris@6 253 ChannelMode m_channelMode;
Chris@6 254 int m_channel;
Chris@6 255 Scale m_scale;
Chris@905 256 double m_middleLineHeight;
Chris@6 257 bool m_aggressive;
Chris@0 258
Chris@67 259 mutable std::vector<float> m_effectiveGains;
Chris@67 260
Chris@1336 261 mutable breakfastquay::Resampler *m_oversampler;
Chris@1336 262
Chris@0 263 mutable QPixmap *m_cache;
Chris@0 264 mutable bool m_cacheValid;
Chris@1325 265 mutable ZoomLevel m_cacheZoomLevel;
Chris@0 266 };
Chris@0 267
Chris@0 268 #endif