Hello,
I’ve built my own parameter smoothing function because for some reason in my install of JUCE it can’t see SmoothedValue (even though the file is there in my JUCE folder).
So I call it glide()
and it looks like this:
void glide(float& parameter, float target, float scalar)
{
parameter = parameter - scalar * (parameter - target);
}
I’m testing it in AudioDeviceIOCallback()
with only 1 channel to output a sinewave:
for (int i = 0; i < numSamples; ++i)
{
glide(freq, newFreq, 0.0005);
outputChannelData[0][i] = std::sin (2.0f * pi * freq * static_cast<float>(pos++) / sampleRate);
}
I change the frequency by setting the variable using std::cin
in main()
(I’m running a console app).
The complete code looks like this:
#include "../JuceLibraryCode/JuceHeader.h"
#define PI 3.1415926535897932384626433832795028841971693993751058209749445923078164062
//==============================================================================
class AudioApp : public AudioIODeviceCallback
{
private:
float freq, newFreq;
int pos;
float sampleRate;
AudioDeviceManager dm;
public:
AudioApp()
{
dm.addAudioCallback(this);
dm.initialiseWithDefaultDevices(0,1);
}
void audioDeviceIOCallback (const float** inputChannelData, int numInputChannels,
float** outputChannelData, int numOutputChannels,
int numSamples) override
{
///********** Barebones way of creating a sineWave (with glide)
for (int i = 0; i < numSamples; ++i)
{
glide(freq, newFreq, 0.0005);
outputChannelData[0][i] = std::sin (2.0f * PI * freq * static_cast<float>(pos++) / sampleRate);
}
//*****************************************************************************************/
}
void audioDeviceAboutToStart(AudioIODevice* device) override
{
sampleRate = device->getCurrentSampleRate();
}
void audioDeviceStopped() override
{
}
void glide(float& parameter, float target, float scalar)
{
parameter = parameter - scalar * (parameter - target);
}
void setFreq(double value)
{
newFreq = value;
}
};
int main (int argc, char* argv[])
{
AudioApp iLoveTheJUCEForums;
while (true)
{
float userFreq;
std::cout << "Enter frequency: ";
std::cin >> userFreq;
iLoveTheJUCEForums.setFreq(userFreq);
}
return 0;
}
2 Questions:
1: Why does my glide function always appear to glide to the destination from a very high number, no matter what frequency I set, or where the previous frequency was. (The sinewave sounds like I’m trying to create a sci-fi laser sound)
2: Why can’t I just plonk glide()
in place of freq
in my sinewave generating algorithm, modifying it to return a float, and either remove (or not) the pass by reference?.. i.e.:
float glide(float parameter, float target, float scalar)
{
return parameter = parameter - scalar * (parameter - target);
}
Thank you so much