Created
December 9, 2018 19:35
-
-
Save Xenakios/a3924f7a24dbb89eb8041bf894672110 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#pragma once | |
#include <random> | |
#include "../JuceLibraryCode/JuceHeader.h" | |
class NoiseGenerator | |
{ | |
public: | |
NoiseGenerator() {} | |
void toggleSound(bool isOn, int chan, double sampleRate, double gain) | |
{ | |
if (isOn) | |
{ | |
m_outchan = chan; | |
m_gain = gain; | |
m_env.setSampleRate(sampleRate); | |
m_env.setParameters({ 0.1f,0.5f,0.1f,5.0f }); | |
m_env.noteOn(); | |
} | |
else | |
{ | |
m_env.noteOff(); | |
} | |
} | |
void processAudio(AudioBuffer<float>& buf, int startSample, int numSamples) | |
{ | |
if (m_gain == 0.0) | |
return; | |
for (int i = 0; i < numSamples; ++i) | |
{ | |
float sample = m_gain * m_dist(m_rng) * m_env.getNextSample(); | |
buf.addSample(m_outchan, i+startSample, sample); | |
} | |
if (m_env.isActive() == false) | |
m_gain = 0.0; | |
} | |
private: | |
ADSR m_env; | |
std::mt19937 m_rng; | |
std::uniform_real_distribution<float> m_dist{ -1.0f,1.0f }; | |
int m_outchan = 0; | |
double m_gain = 0.0; | |
}; | |
class MainComponent : public AudioAppComponent | |
{ | |
public: | |
//============================================================================== | |
MainComponent() | |
{ | |
addAndMakeVisible(leftNoiseButton); | |
addAndMakeVisible(rightNoiseButton); | |
leftNoiseButton.setButtonText("Left noise"); | |
rightNoiseButton.setButtonText("Right noise"); | |
leftNoiseButton.onClick = [this]() | |
{ m_leftGenerator.toggleSound(leftNoiseButton.getToggleState(), 0, getSamplerate(),0.2); }; | |
rightNoiseButton.onClick = [this]() | |
{ m_rightGenerator.toggleSound(rightNoiseButton.getToggleState(), 1, getSamplerate(),0.2); }; | |
// Some platforms require permissions to open input channels so request that here | |
if (RuntimePermissions::isRequired (RuntimePermissions::recordAudio) | |
&& ! RuntimePermissions::isGranted (RuntimePermissions::recordAudio)) | |
{ | |
RuntimePermissions::request (RuntimePermissions::recordAudio, | |
[&] (bool granted) { if (granted) setAudioChannels (2, 2); }); | |
} | |
else | |
{ | |
// Specify the number of input and output channels that we want to open | |
setAudioChannels (0, 2); | |
} | |
setSize(800, 100); | |
} | |
double getSamplerate() | |
{ | |
if (deviceManager.getCurrentAudioDevice()) | |
return deviceManager.getCurrentAudioDevice()->getCurrentSampleRate(); | |
return 1.0; | |
} | |
~MainComponent() | |
{ | |
shutdownAudio(); | |
} | |
void prepareToPlay (int samplesPerBlockExpected, double sampleRate) override | |
{ | |
} | |
void getNextAudioBlock (const AudioSourceChannelInfo& bufferToFill) override | |
{ | |
bufferToFill.clearActiveBufferRegion(); | |
m_leftGenerator.processAudio(*bufferToFill.buffer, bufferToFill.startSample, bufferToFill.numSamples); | |
m_rightGenerator.processAudio(*bufferToFill.buffer, bufferToFill.startSample, bufferToFill.numSamples); | |
} | |
void releaseResources() override | |
{ | |
} | |
void paint (Graphics& g) override | |
{ | |
g.fillAll (getLookAndFeel().findColour (ResizableWindow::backgroundColourId)); | |
} | |
void resized() override | |
{ | |
leftNoiseButton.setBounds(0, 0, getWidth() / 2 -2 , 30); | |
rightNoiseButton.setBounds(getWidth()/2, 0, getWidth() / 2 - 2, 30); | |
} | |
private: | |
ToggleButton leftNoiseButton, rightNoiseButton; | |
NoiseGenerator m_leftGenerator, m_rightGenerator; | |
JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (MainComponent) | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment