# Dynamic sounds that click and pop when played

**URL:** https://forum.kirupa.com/t/dynamic-sounds-that-click-and-pop-when-played/287431
**Category:** flash
**Created:** [April 30, 2009, 10:30pm UTC](https://forum.kirupa.com/t/dynamic-sounds-that-click-and-pop-when-played/287431 "2009-04-30T22:30:32Z")
**Posts on this page:** 1
**Page:** 1

<div class="post-metadata">

### Author: ![lazarusomega200](https://avatars.discourse-cdn.com/v4/letter/l/d6d6ee/32.png) [@lazarusomega200](https://forum.kirupa.com/u/lazarusomega200)
#### Post date: [April 30, 2009, 10:30pm UTC](https://forum.kirupa.com/t/dynamic-sounds-that-click-and-pop-when-played/287431/1 "2009-04-30T22:30:32Z")

</div>

I am having problems with dynamic sound. I have my program set to change frequencies on a timer and I get this annoying clicking sound every time it changes the frequency. Can anybody help?

## Here’s my code:

//variables for sound section follows

import flash.events.TimerEvent;  
import flash.utils.Timer;

var AMP\_MULTIPLIER:Number = .25; //amplitude  
var leftfreq:Number = 110; //main frequency  
var isitvisible:Boolean = true;

var SAMPLING\_RATE:int = 44100;  
var TWO\_PI:Number = 2\*Math.PI;  
var TWO\_PI\_OVER\_SR:Number = TWO\_PI/SAMPLING\_RATE;  
var leftsound:Sound = new Sound();  
var rightsound:Sound = new Sound();  
var isitleft:Boolean = true;

var trans1:SoundTransform = new SoundTransform(.6,-1);  
var trans2:SoundTransform = new SoundTransform(.6,1);

//variables for light flash follow

var StartingHTZ:Number = 14; //starting frequency in HTZ  
var rightfreq:Number = leftfreq - StartingHTZ;  
var EndingHTZ:Number = 5.5; //ending frequency in HTZ  
var TotalTime:Number = 600 //total time in seconds of sequence displaying flash  
var subflashrate:Number = 50; //number of flashes between eyes  
var increment:Number = (((1000/EndingHTZ/2) - (1000/StartingHTZ/2))/TotalTime);  
var milliseconds:Number = (1000/StartingHTZ/2);  
var curcnt:Number = 300;  
var counter:int = 0;  
var counter2:int = 0;  
var seconds:Number = 0;  
var subcounter:Number = 0;

var soundchange:Number = (((leftfreq - rightfreq) - EndingHTZ)/TotalTime); //left freq always needs to be higher than right freq for this to work

var eyelength:Number = 43; //length of eye sequence in frames  
var eyetime:Number = (TotalTime\*1000)/eyelength; //timer for controling movement through the eye closing sequence  
var eyeframe:Number = 0;

var eyetimer:Timer = new Timer(eyetime,eyelength);  
var duration:Timer = new Timer(1000,TotalTime);  
var flash1:Timer = new Timer(milliseconds,1);  
var maintimer:Timer = new Timer(1000);

//flashing light section

function adjustflashrate(event:TimerEvent):void  
{  
if (isitleft == true)  
{  
leftfreq = leftfreq - soundchange;  
isitleft = false;  
}  
else  
{  
rightfreq = rightfreq + soundchange;  
isitleft = true;  
// trace((leftfreq - rightfreq) + " is the HTZ, " + soundchange + " is the change");  
}  
milliseconds = milliseconds + increment;  
flash1.delay = milliseconds;  
}

function flasher(event:TimerEvent):void  
{  
if (counter \> 50)  
{  
this.eye.visible = !this.eye.visible;  
isitvisible = this.eye.visible;  
if (isitvisible == false)  
{  
counter = 0;  
}  
}  
else  
{  
this.light.visible = !this.light.visible;  
isitvisible = this.light.visible;  
}  
if (isitvisible == false)  
{  
counter = counter + 1;  
}  
flash1.start();  
}

//sound section

function sineWave1(event:SampleDataEvent):void {  
var sample:Number  
for (var i:int=0; i\<8192; i++) {  
sample = Math.sin((i+event.position) \* TWO\_PI\_OVER\_SR \* leftfreq);  
event.data.writeFloat(sample \* AMP\_MULTIPLIER);  
event.data.writeFloat(sample \* AMP\_MULTIPLIER);  
}  
}

function sineWave2(event:SampleDataEvent):void {  
var sample:Number  
for (var i:int=0; i\<8192; i++) {  
sample = Math.sin((i+event.position) \* TWO\_PI\_OVER\_SR \* rightfreq);  
event.data.writeFloat(sample \* AMP\_MULTIPLIER);  
event.data.writeFloat(sample \* AMP\_MULTIPLIER);  
}  
}

function changeeyeframe(event:TimerEvent):void  
{  
eyeframe = eyeframe + 1;  
this.eye.gotoAndStop(eyeframe);  
trace(eyeframe);  
}

leftsound.addEventListener(SampleDataEvent.SAMPLE\_DATA, sineWave1);  
rightsound.addEventListener(SampleDataEvent.SAMPLE\_DATA, sineWave2);  
flash1.addEventListener(TimerEvent.TIMER\_COMPLETE, flasher);  
duration.addEventListener(TimerEvent.TIMER,adjustflashrate);  
eyetimer.addEventListener(TimerEvent.TIMER,changeeyeframe);

this.eye.stop();  
this.eye.visible = false;

flash1.start();  
leftsound.play(0, 1, trans1);  
rightsound.play(0,1,trans2);

* * *

Thanks all,

Pete
