# Sinewave Generator Produces Horrible Buzz

**URL:** <https://forum.juce.com/t/sinewave-generator-produces-horrible-buzz/31076>\
**Category:** Getting Started\
**Created:** [January 1, 2019, 8:35pm UTC](https://forum.juce.com/t/sinewave-generator-produces-horrible-buzz/31076 "2019-01-01T20:35:14Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![rarrar](https://avatars.discourse-cdn.com/v4/letter/r/65b543/32.png) [@rarrar](https://forum.juce.com/u/rarrar)\
**Post date:** [January 1, 2019, 8:35pm UTC](https://forum.juce.com/t/sinewave-generator-produces-horrible-buzz/31076/1 "2019-01-01T20:35:14Z")

</div>

Hello I’m a newbie here and I can’t seem to even create a proper sinewave generator.  
Could someone please point out what’s wrong with my code? It sort of works but always produces a horrible buzzing sound. The sliders and variables are all setup properly. So I’m thinking it has something to do with my getNextAudioBlock function:

```auto
void MainComponent::getNextAudioBlock (const AudioSourceChannelInfo& bufferToFill)
{
    bufferToFill.clearActiveBufferRegion();
    for (int channel = 0; channel < bufferToFill.buffer->getNumChannels(); ++channel){
		auto* buffer = bufferToFill.buffer->getWritePointer(channel,bufferToFill.startSample);
		for (auto sample = 0; sample < bufferToFill.numSamples; ++sample) {
			buffer[sample] = std::sin( sample * MathConstants<double>::twoPi * frequencySlider.getValue() / currentSampleRate ) * ( volumeSlider.getValue() / 100 );
		}
		
	}
}
```

---

<div class="post-metadata">

**Author:** ![holy-city](https://avatars.discourse-cdn.com/v4/letter/h/8491ac/32.png) [@holy-city](https://forum.juce.com/u/holy-city)\
**Post date:** [January 1, 2019, 8:37pm UTC](https://forum.juce.com/t/sinewave-generator-produces-horrible-buzz/31076/2 "2019-01-01T20:37:23Z")

</div>

You need to use a modulo counter in the sine calculation, not the sample index of the current buffer.

---

<div class="post-metadata">

**Author:** ![danielrudrich](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/danielrudrich/32/3735_2.png) [@danielrudrich](https://forum.juce.com/u/danielrudrich)\
**Post date:** [January 1, 2019, 8:39pm UTC](https://forum.juce.com/t/sinewave-generator-produces-horrible-buzz/31076/3 "2019-01-01T20:39:26Z")

</div>

The phase of your sinewave starts over again with each call of `getNextAudioBlock(...)` as it starts with zero (`auto sample = 0`) -\> your sine starts from 0, this causes your buzzing.

To avoid that, you need a member variable like `phase`, which you increment for each sample by the right amount. This tutorial will help you: [https://docs.juce.com/master/tutorial\_sine\_synth.html](https://docs.juce.com/master/tutorial_sine_synth.html)

BTW: When you write ``` before and after your code, it will be formatted nicely.

---

<div class="post-metadata">

**Author:** ![rarrar](https://avatars.discourse-cdn.com/v4/letter/r/65b543/32.png) [@rarrar](https://forum.juce.com/u/rarrar)\
**Post date:** [January 1, 2019, 9:19pm UTC](https://forum.juce.com/t/sinewave-generator-produces-horrible-buzz/31076/4 "2019-01-01T21:19:06Z")

</div>

Thank you! It worked!
