From efb30265da69b2f3ff9e826c1eb8b293a128d7b7 Mon Sep 17 00:00:00 2001 From: jules Date: Tue, 4 Nov 2014 15:59:20 +0000 Subject: [PATCH] More cleanups --- .gitignore | 2 +- examples/AnimationAppExample/Source/Main.cpp | 1 - .../Source/MainComponent.cpp | 13 +- .../AudioAppExample/Source/MainComponent.cpp | 116 ++++++++---------- extras/Demo/Source/Demos/AnimationDemo.cpp | 2 +- .../jucer_AudioComponentTemplate.cpp | 4 +- 6 files changed, 65 insertions(+), 73 deletions(-) diff --git a/.gitignore b/.gitignore index f08200fbb4..2c4aff5aec 100644 --- a/.gitignore +++ b/.gitignore @@ -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 diff --git a/examples/AnimationAppExample/Source/Main.cpp b/examples/AnimationAppExample/Source/Main.cpp index 5bb6d5bab8..b6100305a6 100644 --- a/examples/AnimationAppExample/Source/Main.cpp +++ b/examples/AnimationAppExample/Source/Main.cpp @@ -71,7 +71,6 @@ public: centreWithSize (getWidth(), getHeight()); setVisible (true); - } void closeButtonPressed() override diff --git a/examples/AnimationAppExample/Source/MainComponent.cpp b/examples/AnimationAppExample/Source/MainComponent.cpp index 9b64b48ad1..e39794c0de 100644 --- a/examples/AnimationAppExample/Source/MainComponent.cpp +++ b/examples/AnimationAppExample/Source/MainComponent.cpp @@ -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 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() diff --git a/examples/AudioAppExample/Source/MainComponent.cpp b/examples/AudioAppExample/Source/MainComponent.cpp index 99ad1debb4..a7a4b63c1c 100644 --- a/examples/AudioAppExample/Source/MainComponent.cpp +++ b/examples/AudioAppExample/Source/MainComponent.cpp @@ -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 mouse; - + Point lastMousePosition; JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (MainContentComponent) }; diff --git a/extras/Demo/Source/Demos/AnimationDemo.cpp b/extras/Demo/Source/Demos/AnimationDemo.cpp index 34a8e86a1f..3aff6d8f39 100644 --- a/extras/Demo/Source/Demos/AnimationDemo.cpp +++ b/extras/Demo/Source/Demos/AnimationDemo.cpp @@ -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); diff --git a/extras/Introjucer/Source/BinaryData/jucer_AudioComponentTemplate.cpp b/extras/Introjucer/Source/BinaryData/jucer_AudioComponentTemplate.cpp index 763f3134db..9464432549 100644 --- a/extras/Introjucer/Source/BinaryData/jucer_AudioComponentTemplate.cpp +++ b/extras/Introjucer/Source/BinaryData/jucer_AudioComponentTemplate.cpp @@ -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()