synthesizer
Posted: Sat Oct 12, 2024 3:55 pm
Hi, how to make knob that changes oscillator wave-form?
A place to discuss Cherry Audio and Voltage Modular
https://458485.trucktrade.tech/
It depends on whether you want to smoothly morph between waveforms, or use the knob as a rotary selector switch.
Firstly, select the style of knob you would like from the selection available in VMD.
If you want an example of how to generate switched waveforms in ProcessSample, you can check out the source code for the Pink Flight Divide-Down Oscillator. This has a three-position switch to choose between Sawtooth, Pulse and Triangle waveforms, but you can just as easily use a knob with discrete steps as described in my previous post.
Code: Select all
switch (currentWaveform)
{
case PULSE:
{
channelOscillator[i].SetPulseWidth(pulseWidth);
oscillatorOutput = channelOscillator[i].GetPulseValue();
}
break;
case TRIANGLE:
{
oscillatorOutput = channelOscillator[i].GetTriangleValue();
}
break;
default:
{
oscillatorOutput = channelOscillator[i].GetSawtoothValue();
}
break;
}
Code: Select all
// Add your own variables and functions here
private final float BASE_FREQUENCY = 65.4f;
private float previousPitchIn;
private AnalogOscillator mainOscillator = new AnalogOscillator();
Code: Select all
public void Initialize()
{
// add your own code here
previousPitchIn = -1000.0f; // Force recalculate on first ProcessSample
Code: Select all
public void ProcessSample()
{
// add your own code here
float tempFloat;
float pitchIn;
double tempDouble;
double pulseWidthIn;
double pulseWidth;
double outputValue;
int waveformNumber;
pitchIn = (float) inputJackPitch.GetValue();
if (pitchIn != previousPitchIn)
{
// Recalculate exponential frequency
tempFloat = BASE_FREQUENCY * Values.FastTwoToTheX(pitchIn);
mainOscillator.SetFrequency(tempFloat);
}
waveformNumber = (int) knobWaveform.GetValue();
if (waveformNumber == 2) // Pulse
{
pulseWidth = knobPulseWidth.GetValue();
if (inputJackPWM.IsConnected())
{
tempDouble = inputJackPWM.GetValue() *
knobPWM.GetValue() *
0.2;
pulseWidth += tempDouble;
}
mainOscillator.SetPulseWidth (pulseWidth);
}
mainOscillator.AdvanceSample();
switch (waveformNumber)
{
case 1:
{
outputValue = mainOscillator.GetSawtoothValue();
}
break;
case 2:
{
outputValue = mainOscillator.GetPulseValue();
}
break;
case 3:
{
outputValue = mainOscillator.GetTriangleValue();
}
break;
default:
{
outputValue = mainOscillator.GetPureSineValue();
}
break;
}
outputJack.SetValue(outputValue);
}
That's simple.
Code: Select all
private final float BASE_FREQUENCY = 65.4f;
private float previousPitchIn;
private AnalogOscillator mainOscillator = new AnalogOscillator();
private SmoothValue smoothVolume = new SmoothValue(); // NEW LINE ***
Code: Select all
case Knob_Changed: // doubleValue is the new VoltageKnob value
{
if (component == knobOutputLevel)
{
smoothVolume.SetValue(doubleValue);
}
}
break;
Code: Select all
previousPitchIn = -1000.0f; // Force recalculate on first ProcessSample
smoothVolume.SetValue(knobOutputLevel.GetValue()); // NEW LINE***
Code: Select all
default:
{
outputValue = mainOscillator.GetPureSineValue();
}
break;
}
outputValue *= smoothVolume.GetSmoothValue(); // NEW LINE ***
outputJack.SetValue(outputValue);