mirror of
https://github.com/juce-framework/JUCE.git
synced 2026-01-16 00:34:19 +00:00
Added AudioAppExample file in examples
This commit is contained in:
parent
06b9bdefb6
commit
c81ee3b5be
1140 changed files with 442849 additions and 10 deletions
101
Examples/AudioAppExample/Source/Main.cpp
Normal file
101
Examples/AudioAppExample/Source/Main.cpp
Normal file
|
|
@ -0,0 +1,101 @@
|
|||
/*
|
||||
==============================================================================
|
||||
|
||||
This file was auto-generated by the Introjucer!
|
||||
|
||||
It contains the basic startup code for a Juce application.
|
||||
|
||||
==============================================================================
|
||||
*/
|
||||
|
||||
#include "../JuceLibraryCode/JuceHeader.h"
|
||||
|
||||
Component* createMainContentComponent();
|
||||
|
||||
//==============================================================================
|
||||
class AudioAppExampleApplication : public JUCEApplication
|
||||
{
|
||||
public:
|
||||
//==============================================================================
|
||||
AudioAppExampleApplication() {}
|
||||
|
||||
const String getApplicationName() override { return ProjectInfo::projectName; }
|
||||
const String getApplicationVersion() override { return ProjectInfo::versionString; }
|
||||
bool moreThanOneInstanceAllowed() override { return true; }
|
||||
|
||||
//==============================================================================
|
||||
void initialise (const String& commandLine) override
|
||||
{
|
||||
// This method is where you should put your application's initialisation code..
|
||||
|
||||
mainWindow = new MainWindow (getApplicationName());
|
||||
}
|
||||
|
||||
void shutdown() override
|
||||
{
|
||||
// Add your application's shutdown code here..
|
||||
|
||||
mainWindow = nullptr; // (deletes our window)
|
||||
}
|
||||
|
||||
//==============================================================================
|
||||
void systemRequestedQuit() override
|
||||
{
|
||||
// This is called when the app is being asked to quit: you can ignore this
|
||||
// request and let the app carry on running, or call quit() to allow the app to close.
|
||||
quit();
|
||||
}
|
||||
|
||||
void anotherInstanceStarted (const String& commandLine) override
|
||||
{
|
||||
// When another instance of the app is launched while this one is running,
|
||||
// this method is invoked, and the commandLine parameter tells you what
|
||||
// the other instance's command-line arguments were.
|
||||
}
|
||||
|
||||
//==============================================================================
|
||||
/*
|
||||
This class implements the desktop window that contains an instance of
|
||||
our MainContentComponent class.
|
||||
*/
|
||||
class MainWindow : public DocumentWindow
|
||||
{
|
||||
public:
|
||||
MainWindow (String name) : DocumentWindow (name,
|
||||
Colours::lightgrey,
|
||||
DocumentWindow::allButtons)
|
||||
{
|
||||
setUsingNativeTitleBar (true);
|
||||
setContentOwned (createMainContentComponent(), true);
|
||||
setResizable (true, true);
|
||||
|
||||
centreWithSize (getWidth(), getHeight());
|
||||
setVisible (true);
|
||||
}
|
||||
|
||||
void closeButtonPressed() override
|
||||
{
|
||||
// This is called when the user tries to close this window. Here, we'll just
|
||||
// ask the app to quit when this happens, but you can change this to do
|
||||
// whatever you need.
|
||||
JUCEApplication::getInstance()->systemRequestedQuit();
|
||||
}
|
||||
|
||||
/* Note: Be careful if you override any DocumentWindow methods - the base
|
||||
class uses a lot of them, so by overriding you might break its functionality.
|
||||
It's best to do all your work in your content component instead, but if
|
||||
you really have to override any DocumentWindow methods, make sure your
|
||||
subclass also calls the superclass's method.
|
||||
*/
|
||||
|
||||
private:
|
||||
JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (MainWindow)
|
||||
};
|
||||
|
||||
private:
|
||||
ScopedPointer<MainWindow> mainWindow;
|
||||
};
|
||||
|
||||
//==============================================================================
|
||||
// This macro generates the main() routine that launches the app.
|
||||
START_JUCE_APPLICATION (AudioAppExampleApplication)
|
||||
159
Examples/AudioAppExample/Source/MainComponent.cpp
Normal file
159
Examples/AudioAppExample/Source/MainComponent.cpp
Normal file
|
|
@ -0,0 +1,159 @@
|
|||
/*
|
||||
==============================================================================
|
||||
|
||||
This file was auto-generated!
|
||||
|
||||
==============================================================================
|
||||
*/
|
||||
|
||||
#ifndef MAINCOMPONENT_H_INCLUDED
|
||||
#define MAINCOMPONENT_H_INCLUDED
|
||||
|
||||
#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)
|
||||
|
||||
{
|
||||
setSize (500, 400);
|
||||
// the the input and output channels (currently Mono in and out)
|
||||
setAudioChannels (1, 1);
|
||||
}
|
||||
|
||||
~MainContentComponent()
|
||||
{
|
||||
shutdownAudio();
|
||||
}
|
||||
|
||||
//=======================================================================
|
||||
// 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.
|
||||
*/
|
||||
void getNextAudioBlock (const AudioSourceChannelInfo& bufferToFill)
|
||||
{
|
||||
bufferToFill.clearActiveBufferRegion();
|
||||
|
||||
// iterate over each sample of the sample buffer
|
||||
for (int i = bufferToFill.startSample; i < bufferToFill.numSamples + bufferToFill.startSample; ++i)
|
||||
{
|
||||
bufferToFill.buffer->getWritePointer (0)[i] = amplitude * sinf (phase);
|
||||
|
||||
// increment the phase step for the next sample
|
||||
phase += delta;
|
||||
|
||||
// reset the phase when it reaches 2PI to avoid large numbers
|
||||
while (phase >= 2.0f * float_Pi) phase -= 2.0f * float_Pi;
|
||||
}
|
||||
}
|
||||
|
||||
void releaseResources() override
|
||||
{
|
||||
// This gets automatically called when audio device paramters change
|
||||
// or device is restarted.
|
||||
}
|
||||
|
||||
|
||||
//=======================================================================
|
||||
// HANDLE DRAWING
|
||||
|
||||
void paint (Graphics& g)
|
||||
{
|
||||
// fill background
|
||||
g.fillAll (Colours::black);
|
||||
|
||||
// Set the drawing colour to white
|
||||
g.setColour (Colours::white);
|
||||
|
||||
// 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);
|
||||
|
||||
// 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));
|
||||
|
||||
}
|
||||
|
||||
// Mouse handling
|
||||
void mouseUp(const MouseEvent& e) override
|
||||
{
|
||||
amplitude = 0.0f;
|
||||
}
|
||||
|
||||
void mouseDown (const MouseEvent& e) override
|
||||
{
|
||||
mouseDrag (e);
|
||||
}
|
||||
|
||||
void mouseDrag (const MouseEvent& e) override
|
||||
{
|
||||
// Update the mouse position variable
|
||||
mouse.setXY (e.x, e.y);
|
||||
repaint();
|
||||
|
||||
frequency = (getHeight() - e.y) * 10.0f;
|
||||
amplitude = e.x/float(getWidth()) * 0.2f;
|
||||
|
||||
delta = 2.0f * float_Pi * frequency / sampleRate;
|
||||
}
|
||||
|
||||
|
||||
void resized()
|
||||
{
|
||||
// This is called when the MainContentComponent is resized.
|
||||
// If you add any child components, this is where you should
|
||||
// update their positions.
|
||||
}
|
||||
|
||||
|
||||
private:
|
||||
//==============================================================================
|
||||
|
||||
// private member variables
|
||||
|
||||
float phase;
|
||||
float delta;
|
||||
float frequency;
|
||||
float amplitude;
|
||||
|
||||
double sampleRate;
|
||||
|
||||
Point<int> mouse;
|
||||
|
||||
|
||||
JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (MainContentComponent)
|
||||
};
|
||||
|
||||
|
||||
Component* createMainContentComponent() { return new MainContentComponent(); };
|
||||
|
||||
#endif // MAINCOMPONENT_H_INCLUDED
|
||||
Loading…
Add table
Add a link
Reference in a new issue