Pitch issue with example JUCE code

Hi all,

First post so go easy on me! :slight_smile:

I have encountered an issue when using JUCE demo code for creating audio waveforms. It is that the pitch of the waveform appears to become inaccurate at higher frequencies.

I am using as my waveform starting point the code from the standard JUCE tutorial project SynthUsingMidiInputTutorial:

https://docs.juce.com/master/tutorial_synth_using_midi_input.html

I won’t reproduce the code here as it can easily be seen in the segments of this tutorial, specifically the ‘Starting A Voice’ and ‘Rendering A Voice’ code blocks.

To give an example: the sine wave notes C6 and C7 are not octaves of each other when played. The C7 is flat by about a semitone. (C#7 is actually closer to being an octave of C6.)

I am guessing that the issue relates somehow to the fact that at higher frequencies there are fewer sample points for a cycle and therefore accuracy is attenuated.

I’m not expecting anyone to write code to solve this problem for me, all I would like is some directional pointers so I can fix this issue.

FWIW, though I doubt it is too important here, I am running JUCE on Apple Silicone with macOS Ventura.

Thanks in advance,

theSurfDoc

this error typically occuring when you make an error in folding:

if( phase > 2.*M_PI ) phase=0; // this is an error, the phase error is “lost”

if( phase > 2.*M_PI ) phase-= 2.*M_PI ; // this is correct, the phaseerror is distributed

1 Like

Thanks for the reply, Paul!

I actually implemented that very same line of code a few days ago, and unfortunately it didn’t fix the problem.

Do you use this for getting the frequncy?
float myFreq = 440 * pow( 2. , (myNote-69.) /12.)

float myPhaseProg = 2.*M_PI / (getSamplerate() / myFreq);

If myPhaseProg is int or rounded you also get your error pattern

I read the demo now a bit
folding should work.

Thanks again for the replies.

If the issue is related to folding I will get to the bottom of it. I’m just a bit frustrated that the code used in the JUCE demo isn’t quite fit for purpose.

I’ve written similar sine wave code from scratch for other non-JUCE projects so I’ll go back and look at that, and just ditch the JUCE demo code if I have to.

1 Like