Cabbage Logo
Back to Cabbage Site

CPU overload, how to optimize this code?

Hi,
I’m trying to make a synth, 4osc with up to 8 voices each to make nice supersaws.
I achieved it, but now I’m facing an issue : When I export the VST and load it into LMMS, when I play, note the CPU bar goes in the red… The more notes I play, the worst it is, and it finally freeze LMMS.

I understand that having 32 voices is a lot but many synth have this, so I think it’s not the problem here, it’s more that I don’t know how to optimize the code, to avoid CPU spikes !

Here is a short version of the code, to show the problem ! hugeCPU.csd (4.2 KB)

Thanks in advance for your help !

I haven’t a csound computer right now to test but:

First thing I’d increase ksmps and setksmps values

Or you could try to see if instrument 1 doesen’t get turned off by adding an always on instr to check:’
instr 2 kinstr init 1 kactive active kinstr printk2 kactive endin
and in the score
i 2 0 -1
If the printed number keep increasing (and never decrease) could be a problem of missing turnoff.

Hey,
Thanks for the tips !
I tried to increase the ksmps value and I removed the setksmps (as I didn’t even know why it was here, I took this part from a research paper)
From 32 to 256 (512 results in a distorted sound)
And now the problem is less obvious (but still, lots of CPU usage…) now the GUI reacts slowly (on the big project I created a tabbed oscillator view, but it’s not a big deal)

And I tried your code, the counter reset to 0 when I release notes, so no problems on this side.

If you have other ideas let me know !

A supersaw is just a bunch of slightly detuned sawtooth waves combined, right? Why does your orchestra need to be this complex? Provided that I do understand what you’re going for, I think I could provide a simpler example that will probably be more performant, hold on just a moment…

1 Like

I love HUGE supersaw, so for me, 4 osc with 8 voices is a must have, but yeah I recognize it’s a lot !
So yeah it’s the way it works ! you can have a look at this paper section 2, it’s where I found the opcode, which I modified to suit my needs.

Thanks a lot, I don’t see how this could be done in other ways, so if you have suggestion I would be grateful !

let me know !

How’s this? supersaw.csd (6.8 KB) I originally had 7 oscillators, but after I saw your reply, I scaled it up to 32. I don’t think it really sounds very different though. :stuck_out_tongue: I don’t think there’s really that much point in having that many oscillators unless they’re oscillating over different waveforms. If they’re all sawtooths, 7 or so slightly detuned and at different phases sounds basically the same, and the performance is better. You could probably get away with even less than that, especially with some reverb.

The CPU usage in my version still gets sort of high if you play a lot of voices at once, but if you remove the spread feature (or use a smaller number of oscillators) it’s pretty light.

The reason the example you’re working from in that whitepaper is as complex as it is is because they want a wider range of waveforms than just sawtooths. The synthesizer they’re trying to model has the ability to morph between a variety of different waveforms, so they needed infrastructure to accommodate that. As they note, they would’ve used vco2 as I did, but its morphing capabilities are more limited than the synthesizer they were trying to model—it only does saw/triangle/ramp and separately does PWM, whereas they wanted to morph between sine, triangle, saw, square and “spectral” waves. But that seems like a greater range of sounds than just the “supersaw” sound you’re seeking.

1 Like

Thanks a lot for your example ! I tried it as a VST and it has exactly the same CPU usage than mine (when I set my ksmps to 256)

So I think it’s internal in CSound and maybe there is no way to change it :frowning:

Well, here, try this version with 8 oscillators. I think it sounds just as good, maybe even better, and the performance is significantly improved. supersaw.csd (2.6 KB) Like I said, I think even 8 oscillators is maybe more than is necessary.

I managed to improve performance a little more by removing the calls to a custom opcode: supersaw.csd (2.6 KB). I tried some other potential optimizations but they didn’t seem to make a significant difference. When playing 6-note chords on my system, the CPU usage is around 11%, which seems pretty usable to me, although ymmv. Eliminating the spread feature improves performance further, but that feature seems pretty desirable to me for a synth like this. It’s possible that there are other ways to make this faster that I’m not aware of.