JUCE-7.0.12-0-g4f43011b96 JUCE-7.0.12-0-g4f43011b96
JUCE — C++ application framework with suport for VST, VST3, LV2 audio plug-ins

« « « Anklang Documentation
Loading...
Searching...
No Matches
juce_AudioVisualiserComponent.cpp
Go to the documentation of this file.
1 /*
2 ==============================================================================
3
4 This file is part of the JUCE library.
5 Copyright (c) 2022 - Raw Material Software Limited
6
7 JUCE is an open source library subject to commercial or open-source
8 licensing.
9
10 By using JUCE, you agree to the terms of both the JUCE 7 End-User License
11 Agreement and JUCE Privacy Policy.
12
13 End User License Agreement: www.juce.com/juce-7-licence
14 Privacy Policy: www.juce.com/juce-privacy-policy
15
16 Or: You may also use this code under the terms of the GPL v3 (see
17 www.gnu.org/licenses).
18
19 JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
20 EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
21 DISCLAIMED.
22
23 ==============================================================================
24*/
25
26namespace juce
27{
28
30{
31 ChannelInfo (AudioVisualiserComponent& o, int bufferSize) : owner (o)
32 {
33 setBufferSize (bufferSize);
34 clear();
35 }
36
37 void clear() noexcept
38 {
39 levels.fill ({});
40 value = {};
41 subSample = 0;
42 }
43
44 void pushSamples (const float* inputSamples, int num) noexcept
45 {
46 for (int i = 0; i < num; ++i)
47 pushSample (inputSamples[i]);
48 }
49
50 void pushSample (float newSample) noexcept
51 {
52 if (--subSample <= 0)
53 {
54 if (++nextSample == levels.size())
55 nextSample = 0;
56
57 levels.getReference (nextSample) = value;
58 subSample = owner.getSamplesPerBlock();
60 }
61 else
62 {
63 value = value.getUnionWith (newSample);
64 }
65 }
66
67 void setBufferSize (int newSize)
68 {
69 levels.removeRange (newSize, levels.size());
70 levels.insertMultiple (-1, {}, newSize - levels.size());
71
72 if (nextSample >= newSize)
73 nextSample = 0;
74 }
75
77 Array<Range<float>> levels;
78 Range<float> value;
79 std::atomic<int> nextSample { 0 }, subSample { 0 };
80
82};
83
84//==============================================================================
86 : numSamples (1024),
87 inputSamplesPerBlock (256),
88 backgroundColour (Colours::black),
89 waveformColour (Colours::white)
90{
91 setOpaque (true);
93 setRepaintRate (60);
94}
95
99
101{
102 channels.clear();
103
104 for (int i = 0; i < numChannels; ++i)
105 channels.add (new ChannelInfo (*this, numSamples));
106}
107
109{
110 numSamples = newNumSamples;
111
112 for (auto* c : channels)
113 c->setBufferSize (newNumSamples);
114}
115
117{
118 for (auto* c : channels)
119 c->clear();
120}
121
122void AudioVisualiserComponent::pushBuffer (const float* const* d, int numChannels, int num)
123{
124 numChannels = jmin (numChannels, channels.size());
125
126 for (int i = 0; i < numChannels; ++i)
127 channels.getUnchecked (i)->pushSamples (d[i], num);
128}
129
131{
133 buffer.getNumChannels(),
134 buffer.getNumSamples());
135}
136
138{
139 auto numChannels = jmin (buffer.buffer->getNumChannels(), channels.size());
140
141 for (int i = 0; i < numChannels; ++i)
142 channels.getUnchecked (i)->pushSamples (buffer.buffer->getReadPointer (i, buffer.startSample),
143 buffer.numSamples);
144}
145
146void AudioVisualiserComponent::pushSample (const float* d, int numChannels)
147{
148 numChannels = jmin (numChannels, channels.size());
149
150 for (int i = 0; i < numChannels; ++i)
151 channels.getUnchecked (i)->pushSample (d[i]);
152}
153
154void AudioVisualiserComponent::setSamplesPerBlock (int newSamplesPerPixel) noexcept
155{
156 inputSamplesPerBlock = newSamplesPerPixel;
157}
158
163
164void AudioVisualiserComponent::timerCallback()
165{
166 repaint();
167}
168
170{
171 backgroundColour = bk;
172 waveformColour = fg;
173 repaint();
174}
175
177{
178 g.fillAll (backgroundColour);
179
180 auto r = getLocalBounds().toFloat();
181 auto channelHeight = r.getHeight() / (float) channels.size();
182
183 g.setColour (waveformColour);
184
185 for (auto* c : channels)
186 paintChannel (g, r.removeFromTop (channelHeight),
187 c->levels.begin(), c->levels.size(), c->nextSample);
188}
189
191 int numLevels, int nextSample)
192{
193 path.preallocateSpace (4 * numLevels + 8);
194
195 for (int i = 0; i < numLevels; ++i)
196 {
197 auto level = -(levels[(nextSample + i) % numLevels].getEnd());
198
199 if (i == 0)
200 path.startNewSubPath (0.0f, level);
201 else
202 path.lineTo ((float) i, level);
203 }
204
205 for (int i = numLevels; --i >= 0;)
206 path.lineTo ((float) i, -(levels[(nextSample + i) % numLevels].getStart()));
207
208 path.closeSubPath();
209}
210
212 const Range<float>* levels, int numLevels, int nextSample)
213{
214 Path p;
215 getChannelAsPath (p, levels, numLevels, nextSample);
216
217 g.fillPath (p, AffineTransform::fromTargetPoints (0.0f, -1.0f, area.getX(), area.getY(),
218 0.0f, 1.0f, area.getX(), area.getBottom(),
219 (float) numLevels, -1.0f, area.getRight(), area.getY()));
220}
221
222} // namespace juce
static AffineTransform fromTargetPoints(float x00, float y00, float x10, float y10, float x01, float y01) noexcept
Returns the transform that will map three known points onto three coordinates that are supplied.
Holds a resizable array of primitive or copy-by-value objects.
Definition juce_Array.h:56
A multi-channel buffer containing floating point audio samples.
int getNumChannels() const noexcept
Returns the number of channels of audio data that this buffer contains.
int getNumSamples() const noexcept
Returns the number of samples allocated in each of the buffer's channels.
const Type * getReadPointer(int channelNumber) const noexcept
Returns a pointer to an array of read-only samples in one of the buffer's channels.
const Type *const * getArrayOfReadPointers() const noexcept
Returns an array of pointers to the channels in the buffer.
A simple component that can be used to show a scrolling waveform of audio data.
virtual void paintChannel(Graphics &, Rectangle< float > bounds, const Range< float > *levels, int numLevels, int nextSample)
Draws a channel of audio data in the given bounds.
void setNumChannels(int numChannels)
Changes the number of channels that the visualiser stores.
void clear()
Clears the contents of the buffers.
AudioVisualiserComponent(int initialNumChannels)
Creates a visualiser with the given number of channels.
void setBufferSize(int bufferSize)
Changes the number of samples that the visualiser keeps in its history.
void getChannelAsPath(Path &result, const Range< float > *levels, int numLevels, int nextSample)
Creates a path which contains the waveform shape of a given set of range data.
void paint(Graphics &) override
Components can override this method to draw their content.
void pushBuffer(const AudioBuffer< float > &bufferToPush)
Pushes a buffer of channels data.
void setRepaintRate(int frequencyInHz)
Sets the frequency at which the component repaints itself.
void setColours(Colour backgroundColour, Colour waveformColour) noexcept
Sets the colours used to paint the.
void pushSample(const float *samplesForEachChannel, int numChannels)
Pushes a single sample (per channel).
Represents a colour, also including a transparency value.
Definition juce_Colour.h:38
void setOpaque(bool shouldBeOpaque)
Indicates whether any parts of the component might be transparent.
void repaint()
Marks the whole component as needing to be redrawn.
Rectangle< int > getLocalBounds() const noexcept
Returns the component's bounds, relative to its own origin.
A graphics context, used for drawing a component or image.
void fillPath(const Path &path) const
Fills a path using the currently selected colour or brush.
void setColour(Colour newColour)
Changes the current drawing colour.
void fillAll() const
Fills the context's entire clip region with the current colour or brush.
A path is a sequence of lines and curves that may either form a closed shape or be open-ended.
Definition juce_Path.h:65
void startNewSubPath(float startX, float startY)
Begins a new subpath with a given starting position.
void preallocateSpace(int numExtraCoordsToMakeSpaceFor)
Preallocates enough space for adding the given number of coordinates to the path.
void closeSubPath()
Closes a the current sub-path with a line back to its start-point.
void lineTo(float endX, float endY)
Adds a line from the shape's last position to a new end-point.
A general-purpose range object, that simply represents any linear range with a start and end point.
Definition juce_Range.h:40
constexpr Range getUnionWith(Range other) const noexcept
Returns the smallest range that contains both this one and the other one.
Definition juce_Range.h:246
Manages a rectangle and allows geometric operations to be performed on it.
ValueType getRight() const noexcept
Returns the x coordinate of the rectangle's right-hand-side.
Rectangle< float > toFloat() const noexcept
Casts this rectangle to a Rectangle<float>.
ValueType getX() const noexcept
Returns the x coordinate of the rectangle's left-hand-side.
ValueType getBottom() const noexcept
Returns the y coordinate of the rectangle's bottom edge.
ValueType getY() const noexcept
Returns the y coordinate of the rectangle's top edge.
ValueType getHeight() const noexcept
Returns the height of the rectangle.
void startTimerHz(int timerFrequencyHz) noexcept
Starts the timer with an interval specified in Hertz.
#define JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(className)
This is a shorthand way of writing both a JUCE_DECLARE_NON_COPYABLE and JUCE_LEAK_DETECTOR macro for ...
typedef float
JUCE Namespace.
constexpr Type jmin(Type a, Type b)
Returns the smaller of two values.
Type unalignedPointerCast(void *ptr) noexcept
Casts a pointer to another type via void*, which suppresses the cast-align warning which sometimes ar...
Definition juce_Memory.h:88
Used by AudioSource::getNextAudioBlock().
int numSamples
The number of samples in the buffer which the callback is expected to fill with data.
AudioBuffer< float > * buffer
The destination buffer to fill with audio data.
int startSample
The first sample in the buffer from which the callback is expected to write data.