Getting Popps on trying to do a Simple Harmonic Motion

Greetings. Ok: Total Newbie here.

I’m trying to perform a sinusoidal sound wave through the Simple Harmonic Motion, but I’m getting these weird pop’s… Well, I’ve done too much, and I’m trying even more. Perhaps some blessed programming soul could help…

So, below is the code I’m trying:

void FormasDeOndaAudioProcessor::processBlock (juce::AudioBuffer< float >& buffer, juce::MidiBuffer& midiMessages) {

juce::ScopedNoDenormals noDenormals;

auto totalNumInputChannels = getTotalNumInputChannels();

auto totalNumOutputChannels = getTotalNumOutputChannels();

double taxaAmostragem = getSampleRate();

for ( auto i = totalNumInputChannels; i < totalNumOutputChannels; ++i)

buffer.clear (i, 0, buffer.getNumSamples());

auto * channel0 = buffer.getWritePointer(0);

auto * channel1 = buffer.getWritePointer(1);

for (int amostra = 0; amostra < buffer.getNumSamples(); amostra++) {
channel0[amostra] = cos(juce::MathConstants::twoPi * *apvts.getRawParameterValue(“SENO”) * (contaSample/taxaAmostragem)) * *apvts.getRawParameterValue(“VSENO”)/100.f;
channel1[amostra] = cos((juce::MathConstants::twoPi * *apvts.getRawParameterValue(“SENO”)) * contaSample/taxaAmostragem) * *apvts.getRawParameterValue(“VSENO”)/100.f;
contaSample++;
if (contaSample >= taxaAmostragem) contaSample = 0;
}
}

I’m not sure, but I’m guessing that will only work if the sine period is an integral number of samples. Try the JUCE ToneGeneratorAudioSource.

Matt

1 Like

Hi Matt. Thanks for the answer. Actually, I’ve found that I needed a variable to compute the time when counting the samples. So I’ve made the counter and worked. Below is the right code (using the t variable):

for (int amostra = 0; amostra < buffer.getNumSamples(); amostra++) {
channel0[amostra] = cos(juce::MathConstants::twoPi * *apvts.getRawParameterValue(“SENO”) * (t + (contaSample/taxaAmostragem))) * (*apvts.getRawParameterValue(“VSENO”)/100.f);
channel1[amostra] = cos(juce::MathConstants::twoPi * *apvts.getRawParameterValue(“SENO”) * (t + (contaSample/taxaAmostragem))) * (*apvts.getRawParameterValue(“VSENO”)/100.f);
contaSample++;
if (contaSample >= taxaAmostragem) {
contaSample = 0.f;
t++;
}
}