Created
May 25, 2009 07:15
-
-
Save propella/117424 to your computer and use it in GitHub Desktop.
A simple sin wave sound.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// SineSound.as | |
// A simple sine sound. | |
// | |
// compile option | |
// mxmlc -target-player 10.0.0 SineSound.as | |
package { | |
import flash.display.Sprite; | |
import flash.events.SampleDataEvent; | |
import flash.media.Sound; | |
public class SineSound extends Sprite | |
{ | |
private var frequency:Number= 440; | |
private var gain:Number= 0.25; | |
private var rate:int= 44100; | |
private var pitch:Number= rate / frequency; | |
public function SineSound() | |
{ | |
var s:Sound= new Sound(); | |
s.addEventListener(SampleDataEvent.SAMPLE_DATA, makeWave); | |
s.play(); | |
} | |
private function makeWave(event:SampleDataEvent):void { | |
for (var i:int= 0; i < 8192; i++) { | |
var pos:int= i + event.position; | |
var phase:Number= (pos % pitch) / pitch; // 0 <= phase < pitch | |
var v:Number= Math.sin(2 * Math.PI * phase) * gain; | |
// var v:Number= phase < 0.5 ? gain : -gain; // Rectangle Wave | |
event.data.writeFloat(v); | |
event.data.writeFloat(v); | |
} | |
} | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment