forked from: forked from: forked from: forked from: SoundFork
♥0 |
Line 71 |
Modified 2009-07-18 17:29:15 |
MIT License
archived:2017-03-20 09:50:53
ActionScript3 source code
/**
* Copyright murderdeathkitty ( http://wonderfl.net/user/murderdeathkitty )
* MIT License ( http://www.opensource.org/licenses/mit-license.php )
* Downloaded from: http://wonderfl.net/c/v186
*/
// forked from failrate's forked from: forked from: forked from: SoundFork
// forked from murderdeathkitty's forked from: forked from: SoundFork
// forked from failrate's forked from: SoundFork
// forked from murderdeathkitty's SoundFork
// forked from murderdeathkitty's flash on 2009-7-17
package {
import flash.display.*;
import flash.events.*;
import flash.media.*;
import flash.utils.*;
import flash.net.*;
public class SoundFork extends Sprite {
private var sound: Sound;
private var channel: SoundChannel
private var freq1:Number = 100;
private var freq2:Number = 100;
private var steps:Array;
private var stepIndex:uint = 0;
private var latency:int = 40;
private var ticks:int = 0;
public function SoundFork(): void
{
steps = [[60, 100], [100, 140], [40, 80]];
addEventListener(Event.ADDED_TO_STAGE, start);
addEventListener(Event.REMOVED_FROM_STAGE, stop);
}
public function start(e: Event = null): void
{
sound = new Sound();
sound.addEventListener(SampleDataEvent.SAMPLE_DATA, generate);
channel = sound.play();
}
public function stop(e: Event = null): void
{
if(channel != null){
channel.stop();
channel = null;
}
}
//replace this with your own sound!!!
//output is 44100 hz, stereo. values between -1 and 1
private function generate(e: SampleDataEvent): void
{
++ticks;
if ( ticks > latency )
{
ticks = 0;
++stepIndex;
if ( stepIndex >= steps.length )
{
stepIndex = 0;
}
}
var data: ByteArray = e.data;
var i: Number;
var output1: Number;
var output2: Number;
var envelope:Number =0;
freq1 += (Math.random()-0.5)*1;
var min_freq:Number = steps[stepIndex][0];
var max_freq:Number = steps[stepIndex][1];
if ( freq1 <= min_freq ) { freq1 = min_freq; }
if ( freq1 >= max_freq ) { freq1 = max_freq; }
freq2 += (Math.random()-0.5)*1;
var ratio: Number = 0.3333;
if ( freq2 <= min_freq*ratio ) { freq2 = min_freq*ratio; }
if ( freq2 >= max_freq*ratio ) { freq2 = max_freq*ratio; }
for(i = 0; i < 8000; ++i){
if ( i < 4000 ) { envelope += 1.0/1820; }
else { envelope -= 1.0/1820; }
output1 = (Math.sin(i/freq1) * envelope/2)*(Math.sin(i/(freq2+100)) * envelope/2);
output2 = (Math.sin(i/freq2) * envelope/2)*(Math.sin(i/(freq1+100)) * envelope/2);
data.writeFloat(output2*0.8 + output2*0.2);//left
data.writeFloat(output1*0.8 + output1*0.2);//right
}
}
}
}