1

I have started using sound to synthesis audio, I don't know why I get those noisy clicks sounds within the sounds?

My loops is:

for(i in 0...2048)
{
var phase:Float = position / 44100.0 * Math.PI * 2;
position+=1;
sample = Math.sin(phase * v);   // where v varies between 200 to 400
event.data.writeFloat(sample); // left
event.data.writeFloat(sample); // right
}

Any idea?

EDIT

What I need to do is to interpolate frequency within the loop:

var phaserad:Float= 2*Math.PI/44100;
var delta:Float = current_v - new_v;
var p:Int= 2048;
for(i in 0...2048)
{
p--;
v = new_v + delta * (p / 2048); // v will interpolate from current_v to new_v
position += v * phaserad;
sample = Math.sin(position);    
event.data.writeFloat(sample); // left
event.data.writeFloat(sample); // right
}

current_v = new_v;

but, I couldn't hear anything, I tried another approach:

var delta:Float = current_v - new_v;
var p:Int= 2048;

for(i in 0...2048)
{
var phase:Float = position / 44100.0 * Math.PI * 2;
position+=1;

p--;
v = new_v + delta * (p / 2048); // v will interpolate from current_v to new_v

sample = Math.sin(phase * v);   // where v varies between 200 to 400
event.data.writeFloat(sample); // left
event.data.writeFloat(sample); // right
}

but, the frequency will keep going up, and it won't stop at expected new_v

simo
  • 23,342
  • 38
  • 121
  • 218

2 Answers2

0

If you want a continuous sine wave your phase needs to from 0 -> 360 -> 0 -> 360 degrees etc.

Tyler Eaves
  • 12,879
  • 1
  • 32
  • 39
0

You have your wave generator set about right, but the approach with saved position requires your v to be constant and position unaltered. Given your description of v, you change it during your playback somehow, but changing v results in sine form to be disrupted. So, instead of recalculating position from scratch, you should accumulate phase and add current shift based on v to receive an uninterrupted waveform.

const phaserad:Number=2*Math.PI/44100;
for(var i:int=0;i<2048;i++)
{
    position+=v*phaserad; // requires this var to be "Number" type
    sample = Math.sin(position);   // where v varies between 200 to 400
    event.data.writeFloat(sample); // left
    event.data.writeFloat(sample); // right
}
Vesper
  • 18,599
  • 6
  • 39
  • 61
  • Please see EDIT above – simo Jul 04 '14 at 09:36
  • A sound 2048 samples long is 0.05 second length. You need to calculate desired length of your sound prior to running loops, then have your `v` assignment to be divided by the calculated length instead of 2048. Length in samples is `(length_in_seconds)*44100`. – Vesper Jul 04 '14 at 09:41
  • I am writing a simulator, frequency changes about 40 per second, I will try to do it, thanks – simo Jul 04 '14 at 10:50