Skip to content

Instantly share code, notes, and snippets.

@Xenakios
Created October 2, 2020 19:40
Show Gist options
  • Save Xenakios/9f0c92755abc1e5839c15802901c64f0 to your computer and use it in GitHub Desktop.
Save Xenakios/9f0c92755abc1e5839c15802901c64f0 to your computer and use it in GitHub Desktop.
#include <JuceHeader.h>
class Noise : public juce::AudioSource
{
public:
void prepareToPlay(int samplesPerBlockExpected, double sampleRate) override
{
// initialise the filter object
juce::dsp::ProcessSpec spec;
spec.maximumBlockSize = samplesPerBlockExpected;
spec.sampleRate = sampleRate;
spec.numChannels = 1;
filter.prepare(spec);
filter.setCutoffFrequency(500);
// initialise LFO with a sine wave
auto wavetablefunc = [](float x)
{
return std::sin(x);
};
osc.initialise(wavetablefunc, 1024);
osc.setFrequency(0.5); // LFO runs at 0.5 Hz
osc.prepare(spec);
}
void releaseResources() override
{
// nothing to do here, we don't have anything to release
}
void getNextAudioBlock(const juce::AudioSourceChannelInfo& bufferToFill) override
{
for (int i = 0; i < bufferToFill.numSamples; ++i)
{
float noise = juce::jmap(rgen.nextFloat(),0.0f,1.0f,-1.0f,1.0f) * 0.1f; // generate output sample
float cutoff = juce::jmap(osc.processSample(0.0f), -1.0f, 1.0f, 500.0f, 8000.0f);
filter.setCutoffFrequency(cutoff);
float out = filter.processSample(0, noise);
// copy to all output channels
for (int j=0;j<bufferToFill.buffer->getNumChannels();++j)
{
bufferToFill.buffer->setSample(j, i, out);
}
}
}
private:
juce::Random rgen;
juce::dsp::StateVariableTPTFilter<float> filter;
juce::dsp::Oscillator<float> osc;
};
//==============================================================================
int main (int argc, char* argv[])
{
juce::ScopedJuceInitialiser_GUI gui_init; // even if this isn't a GUI app, the Juce message manager needs to be running
juce::AudioDeviceManager aman;
aman.initialiseWithDefaultDevices(0, 2); // 0 inputs, 2 outputs for the audio
juce::AudioSourcePlayer noisePlayer;
Noise noiseSource;
noisePlayer.setSource(&noiseSource);
aman.addAudioCallback(&noisePlayer);
juce::Thread::sleep(5000); // keep the audio playing for 5 seconds (5000 milliseconds)
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment