CPU usage for unison with spread of voices is too high (70%)


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?

Thanks in advance

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.

Im generating the waves like this:

double maxiOsc::saw(double frequency) {
	//Sawtooth generator. This is like a phasor but goes between -1 and 1
	if ( phase >= 1.0 ) phase -= 2.0;
	phase += (1./(maxiSettings::sampleRate/(frequency)));

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…)

Edit 1: found something https://docs.juce.com/master/tutorial_wavetable_synth.html

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.

Hope that basic explanation helps.

1 Like

You are testing the performance with a release build, right?

I tested it with both, it was a 4% difference, wich isnt really that much
so, kinda

it helped me alot, thank you :slight_smile:

That sounds suspicious


How are you measuring the CPU load, to begin with?

In my DAW (Fl-Studio)

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 :confused:
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.


Have a look at this and the next few vids after that…that should help you get on your way https://www.youtube.com/watch?v=kxKEFTQRZPI&t=20s

And a classic from Timur (shared here a few times before) about cache coherency (TL;DR 7mins):

1 Like

Thanks, it actually worked good for me :slight_smile:
I also made the Synth monophonic (since its for basses) and now the CPU usage is on 4%, which is perfect :smiley:

Thanks for helping everyone