I came up with a piece code that works perfectly fine for me, but sucks too much CPU (around 70%, which is far too much imo)
It’s code for using multiple voices in a synth (and detuning)
I’ve linked my code here, because else it would look a bit messy in this thread
Does anyone know how I could make this piece of code more organized or lower its CPU usage?
How are you generating the waves? If you’re using an equation vs a wavetable for example, that will greatly affect your cpu usage…that would be the first place I’d look.
double maxiOsc::saw(double frequency) {
//Sawtooth generator. This is like a phasor but goes between -1 and 1
output=phase;
if ( phase >= 1.0 ) phase -= 2.0;
phase += (1./(maxiSettings::sampleRate/(frequency)));
return(output);
}
So I’d say defently not wavetables (idk how to implement them to be honest, I’m new to DSP and I don’t know if it would work with a supersaw either…)
Yes, the code from above is generating the next index position in real time, which requires much more cpu power, as opposed to a wavetable, where (simplifying) you load a set of values into a vector or array and create a lookup table, which encompasses one cycle of the wave.
This one cycle of the wave is then played faster or slower depending on the frequency you’re playing the wave at. Once again this is a very watered down explanation, but basically since those values are already loaded into an array (in memory), you are only “looking up” what the next value needs to be, rather than needing to calculate it in real time.
Okay, I now understood how the basic concept of wavetables work
The only problem I am having now is that I don’t know how to implement it to work correctly
I can generate a sinewave or a saw tooth with it, but only In one frequency
I don’t know how to hook it up with MIDI input (I want my SynthVoice class to do that)
I had a quick look on the code. You will get much lower CPU usage if you process audio buffers and not single samples. And also processing voice after another will give you a boost. Outer loop would be the voices loop and the inner loop would process voice into a buffer. You can mix the audio output at the end of the voice loop or after all voices have been processed.