# Sine wave plugin randomly changes pitch

**URL:** https://forum.juce.com/t/sine-wave-plugin-randomly-changes-pitch/58016
**Category:** Audio Plugins
**Created:** [September 24, 2023, 2:37pm UTC](https://forum.juce.com/t/sine-wave-plugin-randomly-changes-pitch/58016 "2023-09-24T14:37:31Z")
**Posts on this page:** 8
**Page:** 1

<div class="post-metadata">

### Author: ![FernwehSmith](https://avatars.discourse-cdn.com/v4/letter/f/eada6e/32.png) [@FernwehSmith](https://forum.juce.com/u/FernwehSmith)
#### Post date: [September 24, 2023, 2:37pm UTC](https://forum.juce.com/t/sine-wave-plugin-randomly-changes-pitch/58016/1 "2023-09-24T14:37:31Z")

</div>

Hello all. I have a simple audio plugin that just produces a continuous sine wave at 261.63hz (C4). To achieve this I have the following members:

```auto
float currentPhase = 0.0;
float phasePerSample = 0.0;
float toneFunc (float phase){
  return std::sin(phase * juce::MathConstants<float>::twoPi) * 0.025f;
}

```

In the prepareToPlay function I set phasePerSample as:

```auto
phasePerSample = 261.63f / (float)sampleRate;

```

Then in the processBlock function I have:

```auto
for(auto i = 0; i < buffer.getNumSamples(); i++){
        const float sampleValue = toneFunc(currentPhase);
        for(auto chan = 0; chan < buffer.getNumChannels(); chan++){
            buffer.setSample(chan, i, sampleValue);
        }
        currentPhase += phasePerSample;
    }

```

This is basically my version of a HelloWorld project. Every plugin I’ve made so far (which isn’t many but still done a few) I start with this to verify that everything is building and outputting correctly. And for all the others everything has worked exactly as you’d expect.

However today I’ve run into an odd issue where the pitch of the tone randomly shifts. Usually its within about 2 semitones, but has jumped by as much as 5 semitones (Its never perfectly on note, just within those ranges). The length of time that it holds a particular tone varies from half a second to multiple seconds.

I am completely stumped. I’ve poured over both the editor and processor files, and checked my math. I’ve also made sure that i’m using the same AudioPluginHost to test the plugin as I have for the others. The only change that I have made compared to previous projects is that I’m now using CMake + CLion instead of Projucer + XCode. Surely that can’t be causing this though right? Any help would be massively appreciated!

---

<div class="post-metadata">

### Author: ![theSurfDoc](https://avatars.discourse-cdn.com/v4/letter/t/f6c823/32.png) [@theSurfDoc](https://forum.juce.com/u/theSurfDoc)
#### Post date: [September 24, 2023, 3:24pm UTC](https://forum.juce.com/t/sine-wave-plugin-randomly-changes-pitch/58016/2 "2023-09-24T15:24:04Z")

</div>

Just a guess, but it may be worth trying.

Code in the processBlock() routine is resource-sensitive. It could be that calling out to an external function, toneFunc(), has some impact. I doubt it’s that but you could write put your toneFunc() function code inline to test.

Again, I doubt this is an issue but you could change your ‘auto’ types to ‘int’ for more efficiency (very little gain, but best practice nevertheless is to keep everything as lean as possible in processBlock() ).

---

<div class="post-metadata">

### Author: ![oxxyyd](https://avatars.discourse-cdn.com/v4/letter/o/e47c2d/32.png) [@oxxyyd](https://forum.juce.com/u/oxxyyd)
#### Post date: [September 24, 2023, 5:20pm UTC](https://forum.juce.com/t/sine-wave-plugin-randomly-changes-pitch/58016/3 "2023-09-24T17:20:46Z")

</div>

It seems you keep on adding to currentPhase in all eternity. It will soon loose it’s precision if you keep on doing that. Better to limit the value you store in currentPhase to between 0…twoPi. You can do

```auto
if (currentPhase > twoPi)
    currentPhase -= twoPi;

```

Or you could at least change it to a double…

---

<div class="post-metadata">

### Author: ![PaulDriessen](https://avatars.discourse-cdn.com/v4/letter/p/a88e57/32.png) [@PaulDriessen](https://forum.juce.com/u/PaulDriessen)
#### Post date: [September 24, 2023, 7:44pm UTC](https://forum.juce.com/t/sine-wave-plugin-randomly-changes-pitch/58016/4 "2023-09-24T19:44:23Z")

</div>

correct, but In the OP the phase counts to 1, so you can use  
currentPhase = fmod( currentPhase, 1.f);

---

<div class="post-metadata">

### Author: ![FernwehSmith](https://avatars.discourse-cdn.com/v4/letter/f/eada6e/32.png) [@FernwehSmith](https://forum.juce.com/u/FernwehSmith)
#### Post date: [September 26, 2023, 10:08am UTC](https://forum.juce.com/t/sine-wave-plugin-randomly-changes-pitch/58016/5 "2023-09-26T10:08:01Z")

</div>

Changing the types to double did indeed fix the issue. Also I don’t how I’ve been getting away without looping the phase value.

---

<div class="post-metadata">

### Author: ![Nitsuj70](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/nitsuj70/32/12754_2.png) [@Nitsuj70](https://forum.juce.com/u/Nitsuj70)
#### Post date: [September 26, 2023, 1:08pm UTC](https://forum.juce.com/t/sine-wave-plugin-randomly-changes-pitch/58016/6 "2023-09-26T13:08:29Z")

</div>

In theory you’d have gotten away with it when the frequency was lower because there would be less distance between zero and the wrapped value. Higher frequencies would have suffered more.

---

<div class="post-metadata">

### Author: ![chrisboy2000](https://avatars.discourse-cdn.com/v4/letter/c/a3d4f5/32.png) [@chrisboy2000](https://forum.juce.com/u/chrisboy2000)
#### Post date: [September 27, 2023, 6:36pm UTC](https://forum.juce.com/t/sine-wave-plugin-randomly-changes-pitch/58016/7 "2023-09-27T18:36:02Z")

</div>

> [@theSurfDoc](#):
>
> I doubt it’s that but you could write put your toneFunc() function code inline to test.

You can safely assume that every compiler since 1970 will inline this function call if optimizations are enabled.

> Changing the types to double did indeed fix the issue

This. I’m not in the team `double` for audio signals, but filter coefficients and phase counters are the two things were single precision definitely isn’t enough.

---

<div class="post-metadata">

### Author: ![matt](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/matt/32/18340_2.png) [@matt](https://forum.juce.com/u/matt)
#### Post date: [September 27, 2023, 6:44pm UTC](https://forum.juce.com/t/sine-wave-plugin-randomly-changes-pitch/58016/8 "2023-09-27T18:44:14Z")

</div>

Two things to check out:

[ToneGeneratorAudioSource](https://docs.juce.com/master/classToneGeneratorAudioSource.html)

[dsp::Phase](https://docs.juce.com/master/structdsp_1_1Phase.html)

Matt
