Created
July 31, 2023 23:10
-
-
Save Xenakios/63dec176e733c47d997b33bfe7f850b8 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
/* | |
============================================================================== | |
This file is part of the JUCE tutorials. | |
Copyright (c) 2020 - Raw Material Software Limited | |
The code included in this file is provided under the terms of the ISC license | |
http://www.isc.org/downloads/software-support-policy/isc-license. Permission | |
To use, copy, modify, and/or distribute this software for any purpose with or | |
without fee is hereby granted provided that the above copyright notice and | |
this permission notice appear in all copies. | |
THE SOFTWARE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, | |
WHETHER EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR | |
PURPOSE, ARE DISCLAIMED. | |
============================================================================== | |
*/ | |
/******************************************************************************* | |
The block below describes the properties of this PIP. A PIP is a short snippet | |
of code that can be read by the Projucer and used to generate a JUCE project. | |
BEGIN_JUCE_PIP_METADATA | |
name: SynthLevelControlTutorial | |
version: 2.0.0 | |
vendor: JUCE | |
website: http://juce.com | |
description: Synthesiser with level control. | |
dependencies: juce_audio_basics, juce_audio_devices, juce_audio_formats, | |
juce_audio_processors, juce_audio_utils, juce_core, | |
juce_data_structures, juce_events, juce_graphics, | |
juce_gui_basics, juce_gui_extra | |
exporters: xcode_mac, vs2019, linux_make | |
type: Component | |
mainClass: MainContentComponent | |
useLocalCopy: 1 | |
END_JUCE_PIP_METADATA | |
*******************************************************************************/ | |
#pragma once | |
//============================================================================== | |
class MainContentComponent : public juce::AudioAppComponent | |
{ | |
public: | |
MainContentComponent() | |
{ | |
levelSlider.setRange (0.0, 0.25); | |
levelSlider.onValueChange = [this]() | |
{ | |
gainLevel.store(levelSlider.getValue()); | |
}; | |
levelSlider.setTextBoxStyle (juce::Slider::TextBoxRight, false, 100, 20); | |
levelLabel.setText ("Noise Level", juce::dontSendNotification); | |
addAndMakeVisible (levelSlider); | |
addAndMakeVisible (levelLabel); | |
setSize (600, 100); | |
setAudioChannels (0, 2); | |
} | |
~MainContentComponent() override | |
{ | |
shutdownAudio(); | |
} | |
void prepareToPlay (int, double) override {} | |
//! [getNextAudioBlock full] | |
void getNextAudioBlock (const juce::AudioSourceChannelInfo& bufferToFill) override | |
{ | |
auto level = gainLevel.load(); | |
auto levelScale = level * 2.0f; | |
for (auto channel = 0; channel < bufferToFill.buffer->getNumChannels(); ++channel) | |
{ | |
auto* buffer = bufferToFill.buffer->getWritePointer (channel, bufferToFill.startSample); | |
for (auto sample = 0; sample < bufferToFill.numSamples; ++sample) | |
buffer[sample] = random.nextFloat() * levelScale - level; | |
} | |
} | |
//! [getNextAudioBlock full] | |
void releaseResources() override {} | |
void resized() override | |
{ | |
levelLabel .setBounds (10, 10, 90, 20); | |
levelSlider.setBounds (100, 10, getWidth() - 110, 20); | |
} | |
private: | |
juce::Random random; | |
juce::Slider levelSlider; | |
juce::Label levelLabel; | |
std::atomic<float> gainLevel{0.0f}; | |
JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (MainContentComponent) | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment