annotate FreqView.h @ 1:2ca5d7440b5c tip

added README
author Fiore Martin <f.martin@qmul.ac.uk>
date Fri, 26 Feb 2016 16:11:20 +0000
parents 3004dd663202
children
rev   line source
f@0 1 #pragma once
f@0 2 #include "IControl.h"
f@0 3
f@0 4 #include <vector>
f@0 5
f@0 6 class FreqView : public IControl
f@0 7 {
f@0 8
f@0 9 int mNumBins;
f@0 10 std::vector<float> mBins;
f@0 11 std::vector<float> mPeaks;
f@0 12 double mSampleRate;
f@0 13 double mHalfSampleRate;
f@0 14 double mDecayAmount;
f@0 15 double mSelectionStart;
f@0 16 double mSelectionSize;
f@0 17 double mThreshold;
f@0 18
f@0 19 IRECT mPad;
f@0 20
f@0 21 public:
f@0 22
f@0 23 static const double kPeakDecayRate;
f@0 24 static const double kMaxPeakDB;
f@0 25 static const double kZeroDbY;
f@0 26 static const IColor kGridColor;
f@0 27 static const IColor kBgColor;
f@0 28 static const IColor kPadColor;
f@0 29 static const IChannelBlend kSelectionBlend;
f@0 30
f@0 31 FreqView(IPlugBase *pPlug, IRECT pR, int numBins) :
f@0 32 IControl(pPlug, pR),
f@0 33 mGrid(*this),
f@0 34 mNumBins(numBins),
f@0 35 mBins(numBins, 0.0f),
f@0 36 mPeaks(numBins, 0.0f),
f@0 37 mSelectionStart(0),
f@0 38 mSelectionSize(1),
f@0 39 mSampleRate(44100),
f@0 40 mHalfSampleRate(44100/2),
f@0 41 mPad(pR.GetPadded(4))
f@0 42
f@0 43 { }
f@0 44
f@0 45 ~FreqView(){}
f@0 46
f@0 47 bool Draw(IGraphics* pGraphics) override
f@0 48 {
f@0 49
f@0 50 pGraphics->FillIRect(&kPadColor, &mPad);
f@0 51 pGraphics->FillIRect(&kBgColor, &mRECT);
f@0 52
f@0 53 /* draw grid */
f@0 54 mGrid.Draw(pGraphics);
f@0 55
f@0 56 /* draw the frequency scope */
f@0 57 for (int i = 1; i < mNumBins; i++){
f@0 58 //DrawBin(pGraphics, i);
f@0 59
f@0 60 int x1 = mRECT.L + (float(i-1) / mNumBins * mRECT.W());
f@0 61 int x2 = mRECT.L + (float(i) / mNumBins * mRECT.W());
f@0 62
f@0 63 /* max between new peak and old peak decay wins */
f@0 64 mPeaks[i] = IPMAX(AmpToMeter(mBins[i], kMaxPeakDB) , mPeaks[i] - mDecayAmount);
f@0 65
f@0 66 int y1 = mRECT.T + (mRECT.H() * (1 - mPeaks[i-1]));
f@0 67 int y2 = mRECT.T + (mRECT.H() * (1 - mPeaks[i]));
f@0 68
f@0 69
f@0 70 pGraphics->DrawLine(&COLOR_WHITE, x1, y1, x2, y2 );
f@0 71 }
f@0 72
f@0 73 /* draw the selection */
f@0 74 //pGraphics->DrawVerticalLine(&COLOR_GREEN, mRECT.L + (mRECT.W() * (mSelectionStart/mHalfSampleRate)) , mRECT.B, mRECT.T);
f@0 75 int left = mRECT.L + (mRECT.W() * (mSelectionStart/mHalfSampleRate));
f@0 76 int right = left + (mRECT.W() * (mSelectionSize/mHalfSampleRate));
f@0 77 int top = mRECT.T + (mRECT.H() * (1 - DbToMeter(mThreshold, kMaxPeakDB)));
f@0 78
f@0 79 IRECT selRect(left , top, right, mRECT.B);
f@0 80 IRECT peakRect(left, mRECT.T, right, top);
f@0 81
f@0 82 pGraphics->FillIRect(&COLOR_GREEN, &selRect, &kSelectionBlend);
f@0 83 pGraphics->FillIRect(&COLOR_RED, &peakRect, &kSelectionBlend);
f@0 84
f@0 85 return true;
f@0 86 }
f@0 87
f@0 88
f@0 89
f@0 90 bool IsDirty() override
f@0 91 {
f@0 92 return true;
f@0 93 }
f@0 94
f@0 95 static double AmpToMeter(double amplitudeVal, double maxDB)
f@0 96 {
f@0 97 double db;
f@0 98 if (amplitudeVal > 0)
f@0 99 db = ::AmpToDB(amplitudeVal);
f@0 100 else
f@0 101 db = -999;
f@0 102 return BOUNDED((db + 60) / maxDB, 0, 1);
f@0 103 }
f@0 104
f@0 105 static double DbToMeter(double dbVal, double maxDB)
f@0 106 {
f@0 107 return BOUNDED((dbVal + 60) / maxDB, 0, 1);
f@0 108 }
f@0 109
f@0 110 void setBins(float * compBins)
f@0 111 {
f@0 112 for (int i = 0; i < mNumBins; i++){
f@0 113 mBins[i] = compBins[i];
f@0 114 }
f@0 115 SetDirty(false);
f@0 116 Redraw();
f@0 117 }
f@0 118
f@0 119 inline void setSelectionStart(double startHz)
f@0 120 {
f@0 121 mSelectionStart = startHz;
f@0 122 }
f@0 123
f@0 124 inline double getSelectionStart() const
f@0 125 {
f@0 126 return mSelectionStart;
f@0 127 }
f@0 128
f@0 129 inline void setSelectionSize(double sizeHz)
f@0 130 {
f@0 131 mSelectionSize = sizeHz;
f@0 132 }
f@0 133
f@0 134 inline double getSelectionSize() const
f@0 135 {
f@0 136 return mSelectionSize;
f@0 137 }
f@0 138
f@0 139 inline void setThreshold(double thresDb)
f@0 140 {
f@0 141 mThreshold = thresDb;
f@0 142 }
f@0 143
f@0 144 inline void setSampleRate(double sampleRate)
f@0 145 {
f@0 146 mSampleRate = sampleRate;
f@0 147 mHalfSampleRate = sampleRate/2;
f@0 148 const double deltaT = mNumBins / sampleRate;
f@0 149 mDecayAmount = deltaT * kPeakDecayRate;
f@0 150
f@0 151 /* reset selection with the new sampling rate */
f@0 152 setSelectionStart(mSelectionStart);
f@0 153 setSelectionSize(mSelectionSize);
f@0 154 }
f@0 155
f@0 156
f@0 157 class Grid
f@0 158 {
f@0 159 /* const reference to enclosing class */
f@0 160 const FreqView & mView;
f@0 161 std::vector<double> mHlines;
f@0 162 public:
f@0 163
f@0 164 static const IColor kZeroDbLineColor;
f@0 165
f@0 166 Grid(const FreqView &v ): mView(v)
f@0 167 {
f@0 168 for (int db=-60; db<kMaxPeakDB; db += 6){
f@0 169 if(db == 0){
f@0 170 continue; // 0 db has already the colored line
f@0 171 }
f@0 172
f@0 173 const IRECT & r = mView.mRECT;
f@0 174 double lineRatio = DbToMeter(db, FreqView::kMaxPeakDB);
f@0 175 mHlines.push_back(r.B - r.H() * lineRatio);
f@0 176 }
f@0 177 }
f@0 178
f@0 179
f@0 180 void Draw(IGraphics* pGraphics)
f@0 181 {
f@0 182 const IRECT & r = mView.mRECT;
f@0 183
f@0 184 /* draw horizontal lines */
f@0 185 for(double & l : mHlines){
f@0 186 pGraphics->DrawHorizontalLine(&FreqView::kGridColor, l, r.L, r.R);
f@0 187 }
f@0 188
f@0 189 /* draw vertical lines */
f@0 190 for(int i=1000; i<mView.mHalfSampleRate; i += 1000){
f@0 191 double ratio = i/mView.mHalfSampleRate;
f@0 192 double x = r.L + r.W()*ratio;
f@0 193
f@0 194 pGraphics->DrawVerticalLine(&kGridColor, x , r.T, r.B);
f@0 195 }
f@0 196
f@0 197 /* draw the 0 dB horiz line */
f@0 198 pGraphics->DrawHorizontalLine(&kZeroDbLineColor, r.B - (r.H() * FreqView::kZeroDbY), r.L, r.R );
f@0 199
f@0 200 }
f@0 201
f@0 202 } mGrid;
f@0 203 };
f@0 204