CSL  6.0
JCSL_Widgets.h
Go to the documentation of this file.
1 //
2 // STP_Widgets.h - JUCE widgets for audio by stephen@heaveneverywhere.com
3 // What's here:
4 // AudioWaveformDisplay - simple oscilloscope based on the audio demo code
5 // AudioSpectrumDisplay - simple spectrogram
6 // RangeSlider - interprets its two markers as base value and range, rather than min/max
7 // i.e., the upper marker is the base value, and the lower one is the random range
8 // (This is removed for now since it requires a change to Slider, making some of the private members protected.)
9 // VUMeter (based on killerbobjr's MeterComponent)
10 // see http://www.rawmaterialsoftware.com/juceforum/viewtopic.php?t=608
11 // and http://mysite.verizon.net/mikews/vumeters.zip
12 //
13 
14 #include "JuceHeader.h"
15 #include "CSL_Types.h"
16 
17 // Note that defining RANGE_SLIDER requires a change to the privacy of class Slider's members;
18 // comment out line 705 of ~/Code/juce/src/juce_appframework/gui/components/controls/juce_Slider.h
19 // (This line says "private:")
20 
21 //#define RANGE_SLIDER
22 
23 //////////////////////////////////////////////////////////////////////////
24 //
25 // AudioWaveformDisplay = your basic oscilloscope component
26 // This can be zoomed in X and Y, and repaints itself at the selected rate (see initialise)
27 //
28 
29 class AudioWaveformDisplay : public juce::Component,
30  public juce::Timer,
31  public juce::AudioIODeviceCallback {
32 public:
33  // Constructor
36  // set up oscilloscope state (since constructor takes no args)
37  void initialise(int channel, unsigned rate, unsigned window, bool zeroX);
38  // start/stop display
39  void start();
40  void stop();
41  // callback accumulates samples
42  virtual void audioDeviceIOCallback (const float** inputChannelData, int totalNumInputChannels,
43  float** outputChannelData, int totalNumOutputChannels, int numSamples);
44  void audioDeviceAboutToStart (double sampleRate, int numSamplesPerBlock);
45  void audioDeviceAboutToStart (juce::AudioIODevice* device) { };
46  void audioDeviceStopped();
47  void setIndicator(unsigned value) { indicatorValue = value; }
48 
49  void timerCallback(); // timer redraws
50  virtual void paint (juce::Graphics& g); // main scope draw method
51 
52  void setSamplesToAverage(unsigned val) { samplesToAverage = val; }
53 
54 protected:
55  float * circularBuffer; // This is the sample buffer for the waveform display
56  // and the pixel buffer for the spectrum display
59  unsigned delayInMsec;
60  unsigned samplesToAverage;
61  unsigned indicatorValue;
63  bool zeroCross;
64 
65  void addSample (const float sample); // adding a sample accumulates values
66 };
67 
68 
69 //////////////////////////////////////////////////////////////////////////
70 //
71 // AudioSpectrumDisplay = your basic spectrogram component
72 // This can be zoomed in X and Y, and repaints itself at the selected rate (see initialise)
73 //
74 
76 public: // Constructor
79 
80  virtual void audioDeviceIOCallback (const float** inputChannelData, int totalNumInputChannels,
81  float** outputChannelData, int totalNumOutputChannels, int numSamples);
82  virtual void paint (juce::Graphics& g); // main scope draw method
83 
84  bool mLogDisplay; // if true, use log display, else linear
85  bool mSpectroDisplay; // if true, show full spectrogram, else single spectral slice
86  unsigned numWindows;
87 
88 protected:
89  float ** spectrumBuffer; // 2D spectral frames
90 };
91 
92 #ifdef RANGE_SLIDER
93 
94 //////////////////////////////////////////////////////////////////////////
95 //
96 // A RangeSlider interprets its two markers as base value and range, rather than min/max
97 //
98 
99 class RangeSlider : public Slider {
100 public:
101  RangeSlider (const String& componentName) : Slider(componentName) { };
102  ~RangeSlider() { };
103  // set/get new values
104  double getBaseValue();
105  double getRangeValue();
106  void setBaseValue (double newValue, const bool sendUpdateMessage = true, const bool sendMessageSynchronously = true);
107  void setRangeValue (double newValue, const bool sendUpdateMessage = true, const bool sendMessageSynchronously = true);
108 
109 protected: // work methods
110  void mouseDown (const MouseEvent& e);
111  void mouseDrag (const MouseEvent& e);
112  // data members
113  double baseValue, rangeValue;
114 };
115 
116 #endif
117 
118 //////////////////////////////////////////////////////////////////////////
119 //
120 // VU meters - horizontal bar, vertical bar, and analog meter
121 //
122 
123 class VUMeter : public juce::Component,
124  public juce::Timer,
125  public juce::AudioIODeviceCallback {
126 public:
127 
128  enum { /** The type of meters to use. */
129  Vertical = 1,
132  };
133 
134  enum { /** Needle drop shadow type */
135  None = 1,
140  };
141 
142  VUMeter(); // default constructor
143 
144  VUMeter(int channel, // digital c'tor
145  int type,
146  int segments=0,
147  int markerWidth=2,
148  const juce::Colour& minColour=juce::Colours::green,
149  const juce::Colour& thresholdColour=juce::Colours::yellow,
150  const juce::Colour& maxColour=juce::Colours::red,
151  const juce::Colour& back=juce::Colours::black,
152  float threshold=0.707f);
153 
154  VUMeter(int channel, // analog c'tor
155  juce::Image* background,
156  juce::Image* overlay,
157  float minPosition,
158  float maxPosition,
159  juce::Point<int>& needleCenter,
160  int needleLength,
161  int needleWidth=2,
162  int arrowLength=4,
163  int arrowWidth=4,
164  const juce::Colour& needleColour=juce::Colours::black,
165  int needleDropShadow=None,
166  int dropDistance=0);
167 
168  ~VUMeter();
169 
170  void setBounds (int x, int y, int width, int height);
171  void setFrame(int inset, bool raised=false);
172  void setColours(juce::Colour& min, juce::Colour& threshold, juce::Colour& max, juce::Colour& back);
173  void setDecay(int decay, int hold, float percent=0.707f);
174  void setChannel(unsigned channel) { m_channel = channel; }
175  void setValue(float v);
176  void setScale(float v) { m_scale = v; };
177  float getValue() { return m_value; }
178  void setSkewFactor(float skew) { m_skew = skew; }
179 
180  void audioDeviceIOCallback (const float** inputChannelData, int totalNumInputChannels,
181  float** outputChannelData, int totalNumOutputChannels, int numSamples);
182  void audioDeviceAboutToStart (double sampleRate, int numSamplesPerBlock) { };
183  void audioDeviceStopped() { };
184  void audioDeviceAboutToStart (juce::AudioIODevice* device) { };
185 
186  juce_UseDebuggingNewOperator
187 
188 protected:
189  void paint (juce::Graphics& g);
190 
191 private:
192  juce::Image* m_img;
193  float m_value;
194  float m_skew;
195  float m_threshold;
199  int m_inset;
200  unsigned m_channel;
201  bool m_raised;
202  juce::Colour m_minColour;
203  juce::Colour m_thresholdColour;
204  juce::Colour m_maxColour;
205  juce::Colour m_backgroundColour;
209  int m_hold;
211  juce::Image* m_background;
212  juce::Image* m_overlay;
215  juce::Point<int> m_needleCenter;
220  juce::Colour m_needleColour;
223  float m_scale = 1.0f;
224 
225  void buildImage(void);
226  virtual void timerCallback();
227 };
228 
229 // Bitmaps for fancy analog meters
230 
231 namespace bmps {
232  extern const char* vuback_png;
233  const int vuback_pngSize = 73824;
234 
235  extern const char* vufront_png;
236  const int vufront_pngSize = 58837;
237 };
#define max(a, b)
Definition: matrix.h:156
float m_skew
Definition: JCSL_Widgets.h:194
void audioDeviceAboutToStart(juce::AudioIODevice *device)
Definition: JCSL_Widgets.h:45
void audioDeviceIOCallback(const float **inputChannelData, int totalNumInputChannels, float **outputChannelData, int totalNumOutputChannels, int numSamples)
void setDecay(int decay, int hold, float percent=0.707f)
int m_needleLength
Definition: JCSL_Widgets.h:216
int m_decayTime
Definition: JCSL_Widgets.h:206
void audioDeviceAboutToStart(double sampleRate, int numSamplesPerBlock)
#define min(a, b)
Definition: matrix.h:157
void setIndicator(unsigned value)
Definition: JCSL_Widgets.h:47
int m_inset
Definition: JCSL_Widgets.h:199
float m_decayPercent
Definition: JCSL_Widgets.h:207
juce::Colour m_needleColour
Definition: JCSL_Widgets.h:220
unsigned m_channel
Definition: JCSL_Widgets.h:200
const int vufront_pngSize
Definition: JCSL_Widgets.h:236
void audioDeviceAboutToStart(juce::AudioIODevice *device)
Definition: JCSL_Widgets.h:184
virtual void audioDeviceIOCallback(const float **inputChannelData, int totalNumInputChannels, float **outputChannelData, int totalNumOutputChannels, int numSamples)
float m_threshold
Definition: JCSL_Widgets.h:195
const char * vufront_png
int m_markerWidth
Definition: JCSL_Widgets.h:198
void paint(juce::Graphics &g)
virtual void timerCallback()
void setSkewFactor(float skew)
Definition: JCSL_Widgets.h:178
const int vuback_pngSize
Definition: JCSL_Widgets.h:233
float getValue()
Definition: JCSL_Widgets.h:177
void addSample(const float sample)
juce::Colour m_minColour
Definition: JCSL_Widgets.h:202
int m_arrowLength
Definition: JCSL_Widgets.h:218
float m_decayToValue
Definition: JCSL_Widgets.h:208
juce::Image * m_background
Definition: JCSL_Widgets.h:211
void buildImage(void)
void setValue(float v)
float sample
(could be changed to int, or double)
Definition: CSL_Types.h:191
void setScale(float v)
Definition: JCSL_Widgets.h:176
int volatile bufferPos
Definition: JCSL_Widgets.h:58
void setSamplesToAverage(unsigned val)
Definition: JCSL_Widgets.h:52
void setChannel(unsigned channel)
Definition: JCSL_Widgets.h:174
void setBounds(int x, int y, int width, int height)
virtual void paint(juce::Graphics &g)
int m_hold
Definition: JCSL_Widgets.h:209
virtual void paint(juce::Graphics &g)
int m_meterType
Definition: JCSL_Widgets.h:196
int m_needleWidth
Definition: JCSL_Widgets.h:217
int volatile numSamplesIn
Definition: JCSL_Widgets.h:58
juce::Colour m_thresholdColour
Definition: JCSL_Widgets.h:203
juce::Colour m_backgroundColour
Definition: JCSL_Widgets.h:205
juce::Point< int > m_needleCenter
Definition: JCSL_Widgets.h:215
float m_maxPosition
Definition: JCSL_Widgets.h:214
void initialise(int channel, unsigned rate, unsigned window, bool zeroX)
void setColours(juce::Colour &min, juce::Colour &threshold, juce::Colour &max, juce::Colour &back)
float m_minPosition
Definition: JCSL_Widgets.h:213
bool m_raised
Definition: JCSL_Widgets.h:201
unsigned samplesToAverage
Definition: JCSL_Widgets.h:60
int m_arrowWidth
Definition: JCSL_Widgets.h:219
float m_value
Definition: JCSL_Widgets.h:193
juce::Colour m_maxColour
Definition: JCSL_Widgets.h:204
int m_dropDistance
Definition: JCSL_Widgets.h:222
juce::Image * m_img
Definition: JCSL_Widgets.h:192
void setFrame(int inset, bool raised=false)
float m_scale
Definition: JCSL_Widgets.h:223
const char * vuback_png
void audioDeviceAboutToStart(double sampleRate, int numSamplesPerBlock)
Definition: JCSL_Widgets.h:182
int m_needleDropShadow
Definition: JCSL_Widgets.h:221
int volatile bufferSize
Definition: JCSL_Widgets.h:58
int m_segments
Definition: JCSL_Widgets.h:197
int m_monostable
Definition: JCSL_Widgets.h:210
void audioDeviceStopped()
Definition: JCSL_Widgets.h:183
juce::Image * m_overlay
Definition: JCSL_Widgets.h:212
virtual void audioDeviceIOCallback(const float **inputChannelData, int totalNumInputChannels, float **outputChannelData, int totalNumOutputChannels, int numSamples)