# Popping per second with Sine oscillator

**URL:** <https://forum.juce.com/t/popping-per-second-with-sine-oscillator/56456>\
**Category:** Development\
**Created:** [May 30, 2023, 8:17pm UTC](https://forum.juce.com/t/popping-per-second-with-sine-oscillator/56456 "2023-05-30T20:17:33Z")\
**Posts on this page:** 16\
**Page:** 1

<div class="post-metadata">

**Author:** ![Worlingham](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/worlingham/32/15763_2.png) [@Worlingham](https://forum.juce.com/u/Worlingham)\
**Post date:** [May 30, 2023, 8:17pm UTC](https://forum.juce.com/t/popping-per-second-with-sine-oscillator/56456/1 "2023-05-30T20:17:33Z")

</div>

Hi all, made a sine oscillator as part of a larger project, have done this many times (even with JUCE) but this time I’m getting a ‘pop’ every second. I’ve checked all the usual issues (the thread by Jules etc) and can’t work it out. I’ve transplanted the sine class into an empty plugin and am still getting the same issue. Any help appreciated.

Below is a snippet of my buffer in the test setup, as well as the sine wave class.

```auto
#pragma once
#include "JuceHeader.h"
/ ***************************************************************************//**
 * Generates a sine wave oscillator. Call PrePlay to set internal sample rate
 *
 * 
 * 
 * 
 * 
 * 
 *
 * a
 * 
 ****************************************************************************** /

class Oscillators {
public:

	float master_phase_ = 1.0f;
	float phase_increment_ = 1.0f;
	float sample_rate_ = 1.0f;
	float oscillator1_freq_ = 1.0f;

	float two_pi_ = 6.28318530718f;

	void PrePlay(float sr) {
		sample_rate_ = sr;
		phase_increment_ = two_pi_ / sample_rate_;
		master_phase_ = 0;
	}
	float GetNext() {
		Phase();
		return DoSine(220.0f, 0);
	}

private:
	void Phase() {
		if (master_phase_ > two_pi_) {
			master_phase_ = master_phase_ - (2.0f * two_pi_);
			return;
		}
		master_phase_ += phase_increment_;
	
	}
	float DoSine(float f, float ph) {
		return sinf(two_pi_ * f * master_phase_ + ph);
	}
	
	

};

```

process block and buffers:

```auto
    for (auto i = totalNumInputChannels; i < totalNumOutputChannels; ++i)
        buffer.clear (i, 0, buffer.getNumSamples());

  
    for (int channel = 0; channel < totalNumOutputChannels; ++channel)
    {
        auto* channelData = buffer.getWritePointer (channel);
        for (int i = 0;i < numSamples ; i++) {
            channelData[i] = Osc[channel].GetNext();
        }

        
    }

```

---

<div class="post-metadata">

**Author:** ![xenakios](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/xenakios/32/4047_2.png) [@xenakios](https://forum.juce.com/u/xenakios)\
**Post date:** [May 30, 2023, 8:27pm UTC](https://forum.juce.com/t/popping-per-second-with-sine-oscillator/56456/2 "2023-05-30T20:27:06Z")

</div>

Does anything change if you change your oscillator phase variables to doubles instead of floats?

---

<div class="post-metadata">

**Author:** ![Worlingham](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/worlingham/32/15763_2.png) [@Worlingham](https://forum.juce.com/u/Worlingham)\
**Post date:** [May 30, 2023, 8:31pm UTC](https://forum.juce.com/t/popping-per-second-with-sine-oscillator/56456/3 "2023-05-30T20:31:01Z")

</div>

Sadly not! 🧐

---

<div class="post-metadata">

**Author:** ![xenakios](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/xenakios/32/4047_2.png) [@xenakios](https://forum.juce.com/u/xenakios)\
**Post date:** [May 30, 2023, 8:38pm UTC](https://forum.juce.com/t/popping-per-second-with-sine-oscillator/56456/4 "2023-05-30T20:38:59Z")

</div>

> [@Worlingham](#):
>
> `master_phase_ = master_phase_ - (2.0f * two_pi_);`

What about removing the 2.0f\* in that?

---

<div class="post-metadata">

**Author:** ![Worlingham](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/worlingham/32/15763_2.png) [@Worlingham](https://forum.juce.com/u/Worlingham)\
**Post date:** [May 30, 2023, 8:44pm UTC](https://forum.juce.com/t/popping-per-second-with-sine-oscillator/56456/5 "2023-05-30T20:44:35Z")

</div>

Still there! The only other parts I’ve changed are where I create the Oscillator objects (processor header) and where I pass the sample rate to the objects (preparetoplay). No idea what’s going on!

---

<div class="post-metadata">

**Author:** ![Marcusonic](https://avatars.discourse-cdn.com/v4/letter/m/f0a364/32.png) [@Marcusonic](https://forum.juce.com/u/Marcusonic)\
**Post date:** [May 31, 2023, 12:30am UTC](https://forum.juce.com/t/popping-per-second-with-sine-oscillator/56456/6 "2023-05-31T00:30:56Z")

</div>

the problem seems to be in the master phase, which is updated every second, which is the size of the loop. But I don’t know how it works.

I can provide you with a more intelligible version in which the frequency and phase are just the speed and angle of the wave in radians. when the phase makes a full turn it restarts.

```
class Oscillator {
     float phase = 0, freq = 440.0f, sampleRate = 48000;
     float two_pi_ = 6.28318530718f;
    
public:
     void prePlay(float sr) {
         sampleRate = sr;
     }
     void setFrequency(float freqHz) {
         freq = freqHz / sampleRate * two_pi_;
     }
     float GetNext() {
         phase+=freq;
         if (phase> two_pi_) phase-= two_pi_;
         return sinf(phase);
     }
};

```

---

<div class="post-metadata">

**Author:** ![Worlingham](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/worlingham/32/15763_2.png) [@Worlingham](https://forum.juce.com/u/Worlingham)\
**Post date:** [May 31, 2023, 12:53pm UTC](https://forum.juce.com/t/popping-per-second-with-sine-oscillator/56456/7 "2023-05-31T12:53:23Z")

</div>

That doesn’t click! Thank you, slightly unsatisfying still though as I’ve still no idea why my attempt was!

---

<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:** [May 31, 2023, 2:16pm UTC](https://forum.juce.com/t/popping-per-second-with-sine-oscillator/56456/8 "2023-05-31T14:16:20Z")

</div>

Your DoSine method should have been:

```auto
	float DoSine(float f) {
		return sinf(f * master_phase_);
	}

```

That is, you had an unnecessary multiply by two\_pi\_.

---

<div class="post-metadata">

**Author:** ![Worlingham](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/worlingham/32/15763_2.png) [@Worlingham](https://forum.juce.com/u/Worlingham)\
**Post date:** [May 31, 2023, 6:32pm UTC](https://forum.juce.com/t/popping-per-second-with-sine-oscillator/56456/9 "2023-05-31T18:32:30Z")

</div>

> [@Nitsuj70](#):
>
> That is, you had an unnecessary multiply by two\_pi\_

Aha! That’s it! Many thanks

---

<div class="post-metadata">

**Author:** ![Worlingham](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/worlingham/32/15763_2.png) [@Worlingham](https://forum.juce.com/u/Worlingham)\
**Post date:** [May 31, 2023, 9:41pm UTC](https://forum.juce.com/t/popping-per-second-with-sine-oscillator/56456/10 "2023-05-31T21:41:49Z")

</div>

Too fast of me! I carried on using it and found that now it still clicks, but only with decimal frequencies!  
I made another test setup, literally default plugin just with this in the process block:

```auto
    float inc = juce::MathConstants<float>::twoPi / getSampleRate();
//get increment for accumulator

    
        auto* channelData1 = buffer.getWritePointer (0);
        auto* channelData2 = buffer.getWritePointer(1);
        for (int spl = 0; spl < buffer.getNumSamples(); spl++) {

//accumulator
            phase = phase + inc;
            if (phase > juce::MathConstants<float>::twoPi) {
                phase = 0;
            }
//write to first channel, copy to second 
            channelData1[spl] = sinf(phase * 440.2399);
            channelData2[spl] = channelData1[spl];
        }

```

Only get a click with a decimal frequency (like 440.2399!!)

---

<div class="post-metadata">

**Author:** ![HowardAntares](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/howardantares/32/4629_2.png) [@HowardAntares](https://forum.juce.com/u/HowardAntares)\
**Post date:** [May 31, 2023, 9:58pm UTC](https://forum.juce.com/t/popping-per-second-with-sine-oscillator/56456/11 "2023-05-31T21:58:52Z")

</div>

> [@Worlingham](#):
>
> ```auto
> if (phase > juce::MathConstants<float>::twoPi) {
> phase = 0;
> }
> 
> ```

Shouldn’t that be

```auto
if (phase >= juce::MathConstants<float>::twoPi)
  phase -= juce::MathConstants<float>::twoPi;

```

Incrementing like this is highly unlikely to end up with an exact value for comparison, so maybe you’re just a bit out of phase every time it passes twoPi?

---

<div class="post-metadata">

**Author:** ![Worlingham](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/worlingham/32/15763_2.png) [@Worlingham](https://forum.juce.com/u/Worlingham)\
**Post date:** [May 31, 2023, 10:02pm UTC](https://forum.juce.com/t/popping-per-second-with-sine-oscillator/56456/12 "2023-05-31T22:02:19Z")

</div>

Still popping! Think I’ve tried every possible combo now! maybe I’ll have to redownload the JUCE library!

---

<div class="post-metadata">

**Author:** ![Worlingham](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/worlingham/32/15763_2.png) [@Worlingham](https://forum.juce.com/u/Worlingham)\
**Post date:** [May 31, 2023, 10:32pm UTC](https://forum.juce.com/t/popping-per-second-with-sine-oscillator/56456/13 "2023-05-31T22:32:39Z")

</div>

Re-installed JUCE library, have done this in a blank audio application and still getting the same click with **only** decimal frequencies!

```auto
    bufferToFill.clearActiveBufferRegion();
    // Your audio-processing code goes here!
    
    // For more details, see the help for AudioProcessor::getNextAudioBlock()

    auto* bufferL = bufferToFill.buffer->getWritePointer(0);
    auto* bufferR = bufferToFill.buffer->getWritePointer(1);

    static float CurrentTime = 0;
    static float increment = 1.0f / sampleRate_;

  
        for (int spl = 0; spl < bufferToFill.buffer->getNumSamples(); spl++) {

            CurrentTime = CurrentTime + increment;
            if (CurrentTime >= 1.0f) {
                CurrentTime = CurrentTime - 1.0f;
            }
            float y = sinf(CurrentTime * 440.2399f * juce::MathConstants<float>::twoPi);

            bufferL[spl] = y;
            bufferR[spl] = y;
        }
   
    

```

---

<div class="post-metadata">

**Author:** ![Marcusonic](https://avatars.discourse-cdn.com/v4/letter/m/f0a364/32.png) [@Marcusonic](https://forum.juce.com/u/Marcusonic)\
**Post date:** [June 1, 2023, 12:39am UTC](https://forum.juce.com/t/popping-per-second-with-sine-oscillator/56456/14 "2023-06-01T00:39:32Z")

</div>

The problem may be that the time is not synchronized with the frequency, if time is 1 + X and you subtract 1, you get X which is not at the same angle. I guess that only integers multiplied by 1 fit in the cycle.

anyway, it is not necessary to know the actual time since the increment in each sample is related to the frequency and the sample rate.

```
        float freq = 440.2399f / getSampleRate();

        phase+= freq;
        if (phase>1.0f) phase-=1.0f;
        float y = sinf(phase * juce::MathConstants<float>::twoPi);

```

---

<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:** [June 1, 2023, 12:45pm UTC](https://forum.juce.com/t/popping-per-second-with-sine-oscillator/56456/15 "2023-06-01T12:45:39Z")

</div>

The way to think about it is that in your code CurrentTime is going to wrap every second but the sine wave that you want probably isn’t going to cycle nicely every second, which means you’ll get a discontinuity in the generated sine wave when when CurrentTime wraps. If that happens every second, then chances are you’ll hear a click/pop.

---

<div class="post-metadata">

**Author:** ![Worlingham](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/worlingham/32/15763_2.png) [@Worlingham](https://forum.juce.com/u/Worlingham)\
**Post date:** [June 4, 2023, 6:44pm UTC](https://forum.juce.com/t/popping-per-second-with-sine-oscillator/56456/16 "2023-06-04T18:44:40Z")

</div>

![Info](https://us1.discourse-cdn.com/flex026/uploads/juce/original/2X/d/db6f122327eb9aab4a75a91f37c83f49afeecccf.png)  
Aha, thank you v much both you were correct. Not sure why I just couldn’t picture it in my head! I was naively thinking if I just multiplied a 1 second accumulator by frequency that it would do that, but didn’t think about how that it’s necessarily aligned. Plotted it in sheets (lol) which makes it a bit clearer
