Browse Source

More cleanups

tags/2021-05-28
jules 10 years ago
parent
commit
efb30265da
6 changed files with 65 additions and 73 deletions
  1. +1
    -1
      .gitignore
  2. +0
    -1
      examples/AnimationAppExample/Source/Main.cpp
  3. +7
    -6
      examples/AnimationAppExample/Source/MainComponent.cpp
  4. +54
    -62
      examples/AudioAppExample/Source/MainComponent.cpp
  5. +1
    -1
      extras/Demo/Source/Demos/AnimationDemo.cpp
  6. +2
    -2
      extras/Introjucer/Source/BinaryData/jucer_AudioComponentTemplate.cpp

+ 1
- 1
.gitignore View File

@@ -108,4 +108,4 @@ extras/example projects/Builds/VisualStudio2013/Debug
extras/example projects/Builds/VisualStudio2013/Release
extras/windows dll/Builds/VisualStudio2008/Debug
extras/windows dll/Builds/VisualStudio2008/Release
examples/*/*.app
examples/**/*.app

+ 0
- 1
examples/AnimationAppExample/Source/Main.cpp View File

@@ -71,7 +71,6 @@ public:
centreWithSize (getWidth(), getHeight());
setVisible (true);
}
void closeButtonPressed() override


+ 7
- 6
examples/AnimationAppExample/Source/MainComponent.cpp View File

@@ -45,20 +45,21 @@ public:
for (int i = 0; i < fishLength; ++i)
{
const float radius = 100 + 10 * std::sin (getFrameCounter() * 0.1f + i * 0.5f);
const float x = getWidth() / 2.0f + 1.5f * radius * std::sin (getFrameCounter() * 0.02f + i * 0.12f);
const float y = getHeight() / 2.0f + radius * std::cos (getFrameCounter() * 0.04f + i * 0.12f);
Point<float> p (getWidth() / 2.0f + 1.5f * radius * std::sin (getFrameCounter() * 0.02f + i * 0.12f),
getHeight() / 2.0f + 1.0f * radius * std::cos (getFrameCounter() * 0.04f + i * 0.12f));
// draw the circles along the fish
g.fillEllipse (x - i, y - i, 2.0f + 2.0f * i, 2.0f + 2.0f * i);
g.fillEllipse (p.x - i, p.y - i, 2.0f + 2.0f * i, 2.0f + 2.0f * i);
if (i == 0)
spinePath.startNewSubPath (x, y); // if this is the first point, start a new path..
spinePath.startNewSubPath (p); // if this is the first point, start a new path..
else
spinePath.lineTo (x, y); // ...otherwise add the next point
spinePath.lineTo (p); // ...otherwise add the next point
}
// draw an outline around the path that we have created
g.strokePath (spinePath, PathStrokeType (4));
g.strokePath (spinePath, PathStrokeType (4.0f));
}
void resized()


+ 54
- 62
examples/AudioAppExample/Source/MainComponent.cpp View File

@@ -11,27 +11,23 @@
#include "../JuceLibraryCode/JuceHeader.h"
//==============================================================================
/*
This component lives inside our window, and this is where you should put all
your controls and content.
*/
class MainContentComponent : public AudioAppComponent
{
public:
//==============================================================================
MainContentComponent() : phase (0.0f),
delta (0.0f),
frequency (5000.0f),
amplitude (0.2f),
sampleRate (0.0)
MainContentComponent()
: phase (0.0f),
phaseDelta (0.0f),
frequency (5000.0f),
amplitude (0.2f),
sampleRate (0.0)
{
setSize (500, 400);
// the the input and output channels (currently Mono in and out)
setAudioChannels (1, 1);
// specify the number of input and output channels that we want to open
setAudioChannels (2, 2);
}
~MainContentComponent()
@@ -40,33 +36,33 @@ public:
}
//=======================================================================
// HANDLE AUDIO
void prepareToPlay (int samplesPerBlockExpected, double newSampleRate) override
{
sampleRate = newSampleRate;
}
/* This is where the audio is created. In this example we
fill the audio buffer with a sine wave whose frequency is
controlled by the mouse Y position and whose volume is
controlled by the mouse X potition.
/* This method generates the actual audio samples.
In this example the buffer is filled with a sine wave whose frequency and
amplitude are controlled by the mouse position.
*/
void getNextAudioBlock (const AudioSourceChannelInfo& bufferToFill)
void getNextAudioBlock (const AudioSourceChannelInfo& bufferToFill) override
{
bufferToFill.clearActiveBufferRegion();
const float originalPhase = phase;
// iterate over each sample of the sample buffer
for (int i = bufferToFill.startSample; i < bufferToFill.numSamples + bufferToFill.startSample; ++i)
for (int chan = 0; chan < bufferToFill.buffer->getNumChannels(); ++chan)
{
bufferToFill.buffer->getWritePointer (0)[i] = amplitude * sinf (phase);
phase = originalPhase;
// increment the phase step for the next sample
phase += delta;
float* const channelData = bufferToFill.buffer->getWritePointer (chan, bufferToFill.startSample);
// reset the phase when it reaches 2PI to avoid large numbers
while (phase >= 2.0f * float_Pi) phase -= 2.0f * float_Pi;
for (int i = 0; i < bufferToFill.numSamples ; ++i)
{
channelData[i] = amplitude * std::sin (phase);
// increment the phase step for the next sample
phase = std::fmod (phase + phaseDelta, float_Pi * 2.0f);
}
}
}
@@ -78,37 +74,33 @@ public:
//=======================================================================
// HANDLE DRAWING
void paint (Graphics& g)
void paint (Graphics& g) override
{
// fill background
// (Our component is opaque, so we must completely fill the background with a solid colour)
g.fillAll (Colours::black);
// Set the drawing colour to white
g.setColour (Colours::white);
const float centreY = getHeight() / 2.0f;
const float radius = amplitude * 200.0f;
// Draw an ellipse based on the mouse position and audio volume
int radius = amplitude * 200;
g.fillEllipse (mouse.x - radius/2, mouse.y - radius/2, radius, radius);
g.setColour (Colours::lightgreen);
g.fillEllipse (lastMousePosition.x - radius / 2.0f,
lastMousePosition.y - radius / 2.0f,
radius, radius);
// draw a representative sinewave
Path wave;
for (int i = 0; i < getWidth(); i++)
{
if (i == 0) wave.startNewSubPath (0, getHeight()/2);
else wave.lineTo (i, getHeight()/2 + amplitude * getHeight() * 2.0f * sin (i*frequency*0.0001f));
}
g.strokePath (wave, PathStrokeType (2));
Path wavePath;
wavePath.startNewSubPath (0, centreY);
}
for (float x = 1.0f; x < getWidth(); ++x)
wavePath.lineTo (x, centreY + amplitude * getHeight() * 2.0f
* std::sin (x * frequency * 0.0001f));
// Mouse handling
void mouseUp(const MouseEvent& e) override
{
amplitude = 0.0f;
g.setColour (Colours::grey);
g.strokePath (wavePath, PathStrokeType (2.0f));
}
// Mouse handling..
void mouseDown (const MouseEvent& e) override
{
mouseDrag (e);
@@ -116,18 +108,23 @@ public:
void mouseDrag (const MouseEvent& e) override
{
// Update the mouse position variable
mouse.setXY (e.x, e.y);
repaint();
lastMousePosition = e.position;
frequency = (getHeight() - e.y) * 10.0f;
amplitude = e.x/float(getWidth()) * 0.2f;
amplitude = jmin (0.9f, 0.2f * e.position.x / getWidth());
phaseDelta = 2.0f * float_Pi * frequency / sampleRate;
delta = 2.0f * float_Pi * frequency / sampleRate;
repaint();
}
void mouseUp (const MouseEvent&) override
{
amplitude = 0.0f;
repaint();
}
void resized()
void resized() override
{
// This is called when the MainContentComponent is resized.
// If you add any child components, this is where you should
@@ -137,18 +134,13 @@ public:
private:
//==============================================================================
// private member variables
float phase;
float delta;
float phaseDelta;
float frequency;
float amplitude;
double sampleRate;
Point<int> mouse;
Point<float> lastMousePosition;
JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (MainContentComponent)
};


+ 1
- 1
extras/Demo/Source/Demos/AnimationDemo.cpp View File

@@ -266,7 +266,7 @@ private:
animator.animateComponent (componentsToAnimate.getUnchecked(i),
r.reduced (10),
1.0f,
900 + 300 * sin (angle),
900 + 300 * std::sin (angle),
false,
0.0,
0.0);


+ 2
- 2
extras/Introjucer/Source/BinaryData/jucer_AudioComponentTemplate.cpp View File

@@ -24,8 +24,8 @@ public:
{
setSize (500, 400);
// specify the number of input and output channels needed
setAudioChannels (1, 1);
// specify the number of input and output channels that we want to open
setAudioChannels (2, 2);
}
~MainContentComponent()


Loading…
Cancel
Save