Fixes to the UI
This commit is contained in:
@@ -0,0 +1,208 @@
|
||||
/*
|
||||
==============================================================================
|
||||
|
||||
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: WavetableSynthTutorial
|
||||
version: 4.0.0
|
||||
vendor: JUCE
|
||||
website: http://juce.com
|
||||
description: Wavetable synthesiser.
|
||||
|
||||
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 WavetableOscillator
|
||||
{
|
||||
public:
|
||||
WavetableOscillator (const juce::AudioSampleBuffer& wavetableToUse)
|
||||
: wavetable (wavetableToUse),
|
||||
tableSize (wavetable.getNumSamples() - 1)
|
||||
{
|
||||
jassert (wavetable.getNumChannels() == 1);
|
||||
}
|
||||
|
||||
void setFrequency (float frequency, float sampleRate)
|
||||
{
|
||||
auto tableSizeOverSampleRate = (float) tableSize / sampleRate;
|
||||
tableDelta = frequency * tableSizeOverSampleRate;
|
||||
}
|
||||
|
||||
forcedinline float getNextSample() noexcept
|
||||
{
|
||||
auto index0 = (unsigned int) currentIndex;
|
||||
auto index1 = index0 + 1;
|
||||
|
||||
auto frac = currentIndex - (float) index0;
|
||||
|
||||
auto* table = wavetable.getReadPointer (0);
|
||||
auto value0 = table[index0];
|
||||
auto value1 = table[index1];
|
||||
|
||||
auto currentSample = value0 + frac * (value1 - value0);
|
||||
|
||||
if ((currentIndex += tableDelta) > (float) tableSize)
|
||||
currentIndex -= (float) tableSize;
|
||||
|
||||
return currentSample;
|
||||
}
|
||||
|
||||
private:
|
||||
const juce::AudioSampleBuffer& wavetable;
|
||||
const int tableSize;
|
||||
float currentIndex = 0.0f, tableDelta = 0.0f;
|
||||
};
|
||||
|
||||
//==============================================================================
|
||||
class MainContentComponent : public juce::AudioAppComponent,
|
||||
public juce::Timer
|
||||
{
|
||||
public:
|
||||
MainContentComponent()
|
||||
{
|
||||
cpuUsageLabel.setText ("CPU Usage", juce::dontSendNotification);
|
||||
cpuUsageText.setJustificationType (juce::Justification::right);
|
||||
addAndMakeVisible (cpuUsageLabel);
|
||||
addAndMakeVisible (cpuUsageText);
|
||||
|
||||
createWavetable();
|
||||
|
||||
setSize (400, 200);
|
||||
setAudioChannels (0, 2); // no inputs, two outputs
|
||||
startTimer (50);
|
||||
}
|
||||
|
||||
~MainContentComponent() override
|
||||
{
|
||||
shutdownAudio();
|
||||
}
|
||||
|
||||
void resized() override
|
||||
{
|
||||
cpuUsageLabel.setBounds (10, 10, getWidth() - 20, 20);
|
||||
cpuUsageText .setBounds (10, 10, getWidth() - 20, 20);
|
||||
}
|
||||
|
||||
void timerCallback() override
|
||||
{
|
||||
auto cpu = deviceManager.getCpuUsage() * 100;
|
||||
cpuUsageText.setText (juce::String (cpu, 6) + " %", juce::dontSendNotification);
|
||||
}
|
||||
|
||||
void createWavetable()
|
||||
{
|
||||
sineTable.setSize (1, (int) tableSize + 1);
|
||||
sineTable.clear();
|
||||
|
||||
auto* samples = sineTable.getWritePointer (0);
|
||||
|
||||
int harmonics[] = { 1, 3, 5, 6, 7, 9, 13, 15 };
|
||||
float harmonicWeights[] = { 0.5f, 0.1f, 0.05f, 0.125f, 0.09f, 0.005f, 0.002f, 0.001f }; // [1]
|
||||
|
||||
jassert (juce::numElementsInArray (harmonics) == juce::numElementsInArray (harmonicWeights));
|
||||
|
||||
for (auto harmonic = 0; harmonic < juce::numElementsInArray (harmonics); ++harmonic)
|
||||
{
|
||||
auto angleDelta = juce::MathConstants<double>::twoPi / (double) (tableSize - 1) * harmonics[harmonic]; // [2]
|
||||
auto currentAngle = 0.0;
|
||||
|
||||
for (unsigned int i = 0; i < tableSize; ++i)
|
||||
{
|
||||
auto sample = std::sin (currentAngle);
|
||||
samples[i] += (float) sample * harmonicWeights[harmonic]; // [3]
|
||||
currentAngle += angleDelta;
|
||||
}
|
||||
}
|
||||
|
||||
samples[tableSize] = samples[0];
|
||||
}
|
||||
|
||||
void prepareToPlay (int, double sampleRate) override
|
||||
{
|
||||
auto numberOfOscillators = 10;
|
||||
|
||||
for (auto i = 0; i < numberOfOscillators; ++i)
|
||||
{
|
||||
auto* oscillator = new WavetableOscillator (sineTable);
|
||||
|
||||
auto midiNote = juce::Random::getSystemRandom().nextDouble() * 36.0 + 48.0;
|
||||
auto frequency = 440.0 * pow (2.0, (midiNote - 69.0) / 12.0);
|
||||
|
||||
oscillator->setFrequency ((float) frequency, (float) sampleRate);
|
||||
oscillators.add (oscillator);
|
||||
}
|
||||
|
||||
level = 0.25f / (float) numberOfOscillators;
|
||||
}
|
||||
|
||||
void releaseResources() override {}
|
||||
|
||||
void getNextAudioBlock (const juce::AudioSourceChannelInfo& bufferToFill) override
|
||||
{
|
||||
auto* leftBuffer = bufferToFill.buffer->getWritePointer (0, bufferToFill.startSample);
|
||||
auto* rightBuffer = bufferToFill.buffer->getWritePointer (1, bufferToFill.startSample);
|
||||
|
||||
bufferToFill.clearActiveBufferRegion();
|
||||
|
||||
for (auto oscillatorIndex = 0; oscillatorIndex < oscillators.size(); ++oscillatorIndex)
|
||||
{
|
||||
auto* oscillator = oscillators.getUnchecked (oscillatorIndex);
|
||||
|
||||
for (auto sample = 0; sample < bufferToFill.numSamples; ++sample)
|
||||
{
|
||||
auto levelSample = oscillator->getNextSample() * level;
|
||||
|
||||
leftBuffer[sample] += levelSample;
|
||||
rightBuffer[sample] += levelSample;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private:
|
||||
juce::Label cpuUsageLabel;
|
||||
juce::Label cpuUsageText;
|
||||
|
||||
const unsigned int tableSize = 1 << 7;
|
||||
float level = 0.0f;
|
||||
|
||||
juce::AudioSampleBuffer sineTable;
|
||||
juce::OwnedArray<WavetableOscillator> oscillators;
|
||||
|
||||
JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (MainContentComponent)
|
||||
};
|
||||
Reference in New Issue
Block a user