0

For a coursework exercise I need to create a sine oscillator with which to alter the delay time in playing back an echo of the sound (a flanger). This oscillator needs to have an adjustable frequency.

The value returned by the function should be between 1 and -1, which I achieved with this function:

public void oscillateNumber(){
    for (int i = 0; i < 200; i++){
            oscResult = Math.sin((Number1* Math.PI)/180.0);
        updateNumber();
    }
}

And by having Number1 varying between -180 and 180 (found this solution here: How to use a Sine / Cosine wave to return an oscillating number)

How could I go about incorporating a frequency into this oscillator? The frequency needs to be adjustable between 0 and 5Hz...

I am completely new to this material so I am not entirely grasping the mechanics of this, another function I found is

public void oscillateNumber3(){
    for (int i = 0; i < 400; i++){
        oscResult = (float)Math.sin( angle ); 
        angle += (float)(2*Math.PI) * frequency / 44100f;
        java.lang.System.out.println(oscResult);
    }
}

Where if I add this value to the delay it gives me a bit more resemblance to the effect but I am not sure it's actually correct...

Any pointer to this would be really appreciated.

UPDATE

Ok so following Oli's pointer I came up with this function for continuously modulating the delay with a number produced by the oscillator, I'm not quite sure about the loop though:

public void oscillatorNumber(int frequency, int sampleRate){
    for (int t = 0; t < (sampleRate * frequency); t++){
        oscResult = (float)Math.sin( angle ); 
        angle += (float)(2*Math.PI) * 2 * (t / 44100); // sin(2*pi* f  *(t/Fs))
        java.lang.System.out.println(oscResult);
    }
}

Does this look about right?

Community
  • 1
  • 1
Alex
  • 561
  • 2
  • 9
  • 28

1 Answers1

1

The general expression for a sinusoidal oscillator is:

y(t) = sin(2*pi*f*t)

where f is the frequency in Hz, and t is the time in seconds.

Oliver Charlesworth
  • 267,707
  • 33
  • 569
  • 680
  • Thanks for that Oli, could you have a look at the function I posted in the Update? I need something that would yield an oscillator value continuously as I apply the other effect, but i think that doesn't quite do it... Would you just check the elapsed time at each update to get `t`? – Alex Feb 26 '12 at 20:56