notice: Flash editor updated! Join the development! Thanks to MiniBuilder


embed

FORKED
  1. // forked from seagirl's Dynamic Sound Generation
  2. // write as3 code here..
  3. package
  4. {
  5.     import flash.display.Sprite;
  6.     import flash.events.Event;
  7.     import flash.events.TimerEvent;
  8.     import flash.utils.Timer;
  9.     [SWF(backgroundColor="#000000")]
  10.     
  11.     public class DSG extends Sprite
  12.     {
  13.         public function DSG()
  14.         {
  15.             super();
  16.             
  17.             init();
  18.         }
  19.         
  20.         private var timer:Timer;
  21.         private var noteNumbers:Array = [6062646567697172];
  22.         
  23.         private function init():void
  24.         {
  25.             ring();
  26.             
  27.             timer = new Timer(1000);
  28.             timer.addEventListener(TimerEvent.TIMER, timerHandler);
  29.             timer.start();
  30.         }
  31.         
  32.         private function ring():void
  33.         {
  34.             if (noteNumbers.length == 0)
  35.                 return;
  36.             
  37.             var noteNumber:int = noteNumbers[int(Math.random() * noteNumbers.length)];    
  38.             var attack:Number = Math.random() * 5;
  39.                         var decay:Number = Math.random() * 5;
  40.                         var release:Number = 10 - attack - decay;
  41.             new Tone(noteNumber, 28, attack, decay, 5, release).start();
  42.             new Particle(noteNumber, 28, attack, decay, 5, release, this).start();
  43.         }
  44.         
  45.         private function timerHandler(event:TimerEvent):void
  46.         {
  47.             ring();
  48.         }
  49.         
  50.     }
  51. }
  52. import flash.display.Sprite;
  53. import flash.events.Event;
  54. import flash.events.TimerEvent;
  55. import flash.utils.Timer;
  56. class Particle extends Sprite
  57. {
  58.     public static const UNIT:int = 10;
  59.     
  60.     public static const TIMER_RATE:int = 20;
  61.     
  62.     public function Particle(noteNumber:uint, length:Number, volume:Number, attack:Number, decay:Number, sustain:Number, release:Number, parent:Sprite)
  63.     {    
  64.         this.noteNumber = noteNumber;
  65.         this.length = length;
  66.         this.volume = volume;
  67.         this.attack = attack;
  68.         this.decay = decay;
  69.         this.sustain = sustain;
  70.         this.release = release;
  71.         
  72.         amplify = volume > sustain ? volume : sustain;
  73.         
  74.         parent.addChildAt(this0);
  75.         
  76.         check();
  77.         initializeVelocity();
  78.     }
  79.     
  80.     private var timer:Timer;
  81.     
  82.     private var radius:Number = 0;
  83.     private var alphaValue:Number = 1;
  84.     private var amplify:Number = 0;
  85.     
  86.     private var state:int = 1// 1: Attack, 2: Decay, 3: Release
  87.     
  88.     private var noteNumber:uint;
  89.     private var length:Number;
  90.     private var volume:Number;
  91.     private var attack:Number;
  92.     private var decay:Number;
  93.     private var sustain:Number;
  94.     private var release:Number;
  95.     
  96.     private var v1:Number;
  97.     private var v2:Number;
  98.     private var v3:Number;
  99.     
  100.     private function check():void
  101.     {
  102.         if (attack + decay + release != UNIT)
  103.         {
  104.              attack = 0;
  105.              decay = 5;
  106.              release = 5;
  107.         }
  108.             
  109.         if (volume > UNIT)
  110.             volume = UNIT;
  111.         
  112.         if (volume < 0)
  113.             volume = 0;
  114.             
  115.         if (sustain > UNIT)
  116.             sustain = UNIT;
  117.             
  118.         if (sustain < 0)
  119.             sustain = 0;
  120.     }
  121.     
  122.     private function initializeVelocity():void
  123.     {            
  124.         v1 = volume / (TIMER_RATE * length * attack / UNIT);
  125.         v2 = (volume - sustain) / (TIMER_RATE * length * decay / UNIT);
  126.         v3 = sustain / (TIMER_RATE * length * release / UNIT);
  127.         
  128.         alphaValue = volume > sustain ? volume : sustain;
  129.     }
  130.     
  131.     private function init():void
  132.     {
  133.         timer = new Timer(1000 / TIMER_RATE);
  134.         timer.addEventListener(TimerEvent.TIMER, timerHandler);
  135.         timer.start();
  136.     }
  137.     
  138.     private function update():void
  139.     {
  140.         drawGraphics();
  141.         updateParams();
  142.     }
  143.     
  144.     private function drawGraphics():void
  145.     {
  146.         graphics.clear();
  147.         
  148.         graphics.beginFill(0xFFFFFF, alphaValue / 10);
  149.         graphics.drawCircle(00, radius * 0.8 * amplify);
  150.         graphics.endFill();
  151.     }
  152.     
  153.     private function updateParams():void
  154.     {
  155.         if (state == 1)
  156.         {
  157.             radius += v1;
  158.             if (radius >= volume)
  159.             {
  160.                 radius = volume;
  161.                 state = 2;
  162.             }
  163.         }
  164.         else if (state == 2)
  165.         {
  166.             if (volume < sustain)
  167.             {
  168.                 radius -= v2;
  169.                 
  170.                 if (radius >= sustain)
  171.                 {
  172.                     radius = sustain;
  173.                     state = 3;
  174.                 }
  175.             }
  176.             else
  177.             {
  178.                 alphaValue -= v2;
  179.                 
  180.                 if (alphaValue <= sustain)
  181.                 {
  182.                     alphaValue = sustain;
  183.                     state = 3;
  184.                 }
  185.             }    
  186.         }
  187.         else if (state == 3)
  188.         {
  189.             alphaValue -= v3;
  190.             
  191.             if (alphaValue <= 0)
  192.             {
  193.                 alphaValue = 0;
  194.                 
  195.                 stop();
  196.             }
  197.         }
  198.     }
  199.     
  200.     public function start():void
  201.     {
  202.         if (!parent)
  203.             throw new Error("parent must be specified.");
  204.         
  205.         if (x == 0 && y == 0)
  206.         {
  207.             x = Math.random() * stage.stageWidth;
  208.             y = Math.random() * stage.stageHeight;
  209.         }
  210.         
  211.         timer = new Timer(1000 / TIMER_RATE);
  212.         timer.addEventListener(TimerEvent.TIMER, timerHandler);
  213.         timer.start();
  214.     }
  215.     
  216.     public function stop():void
  217.     {
  218.         timer.removeEventListener(TimerEvent.TIMER, timerHandler);
  219.         
  220.         parent.removeChild(this);
  221.         
  222.         dispatchEvent(new Event(Event.COMPLETE));
  223.     }
  224.     
  225.     private function timerHandler(event:TimerEvent):void
  226.     {
  227.         update();
  228.     }
  229.     
  230. }
  231. import flash.events.Event;
  232. import flash.events.EventDispatcher;
  233. import flash.events.SampleDataEvent;
  234. import flash.media.Sound;
  235. import flash.utils.ByteArray;
  236. class Tone extends EventDispatcher
  237. {
  238.     public static const SAMPLING_RATE:Number = 44100;
  239.     public static const BUFFER_SIZE:int = 8192;
  240.         
  241.     public static const UNIT:int = 10;
  242.     
  243.     public function Tone(noteNumber:uint, length:Number, volume:Number, attack:Number, decay:Number, sustain:Number, release:Number)
  244.     {    
  245.         this.frequency = noteNumber2frequency(noteNumber);
  246.         this.length = length;
  247.         this.volume = volume;
  248.         this.attack = attack;
  249.         this.decay = decay;
  250.         this.sustain = sustain;
  251.         this.release = release;
  252.         
  253.         check();
  254.         initializeVelocity();
  255.     }
  256.     
  257.     private var sound:Sound;
  258.     
  259.     private var phase:Number = 0;
  260.     private var amplify:Number = 0;
  261.     
  262.     private var state:int = 1// 1: Attack, 2: Decay, 3: Release
  263.     
  264.     private var frequency:Number;
  265.     private var length:Number;
  266.     private var volume:Number;
  267.     private var attack:Number;
  268.     private var decay:Number;
  269.     private var sustain:Number;
  270.     private var release:Number;
  271.     
  272.     private var v1:Number;
  273.     private var v2:Number;
  274.     private var v3:Number;
  275.         
  276.     private function check():void
  277.     {
  278.         if (attack + decay + release != UNIT)
  279.         {
  280.              attack = 0;
  281.              decay = 5;
  282.              release = 5;
  283.         }
  284.             
  285.         if (volume > UNIT)
  286.             volume = UNIT;
  287.         
  288.         if (volume < 0)
  289.             volume = 0;
  290.             
  291.         if (sustain > UNIT)
  292.             sustain = UNIT;
  293.             
  294.         if (sustain < 0)
  295.             sustain = 0;
  296.     }
  297.     
  298.     private function initializeVelocity():void
  299.     {            
  300.         v1 = volume / (SAMPLING_RATE * length * attack / UNIT);
  301.         v2 = (volume - sustain) / (SAMPLING_RATE * length * decay / UNIT);
  302.         v3 = sustain / (SAMPLING_RATE * length * release / UNIT);
  303.     }
  304.     
  305.     private function updateAmplify():void
  306.     {
  307.         if (state == 1)
  308.         {
  309.             amplify += v1;
  310.             if (amplify >= volume)
  311.             {
  312.                 amplify = volume;
  313.                 state = 2;
  314.             }
  315.         }
  316.         else if (state == 2)
  317.         {
  318.             amplify -= v2;
  319.             
  320.             if (volume < sustain)
  321.             {
  322.                 if (amplify >= sustain)
  323.                 {
  324.                     amplify = sustain;
  325.                     state = 3;
  326.                 }
  327.             }
  328.             else
  329.             {
  330.                 if (amplify <= sustain)
  331.                 {
  332.                     amplify = sustain;
  333.                     state = 3;
  334.                 }
  335.             }    
  336.         }
  337.         else if (state == 3)
  338.         {
  339.             amplify -= v3;
  340.             
  341.             if (amplify <= 0)
  342.             {
  343.                 amplify = 0;
  344.                 
  345.                 stop();
  346.             }
  347.         }
  348.     }
  349.     
  350.     public function start():void
  351.     {                    
  352.         sound = new Sound();
  353.         sound.addEventListener(SampleDataEvent.SAMPLE_DATA, soundSampleDataHandler);
  354.         sound.play();
  355.     }
  356.     
  357.     public function stop():void
  358.     {
  359.         sound.removeEventListener(SampleDataEvent.SAMPLE_DATA, soundSampleDataHandler);
  360.         dispatchEvent(new Event(Event.COMPLETE));
  361.     }
  362.     
  363.     private function soundSampleDataHandler(event:SampleDataEvent):void
  364.     {    
  365.         var bytes:ByteArray = new ByteArray();
  366.         
  367.         for (var i:int = 0; i < BUFFER_SIZE; ++i)
  368.         {
  369.             phase += frequency / SAMPLING_RATE;  
  370.             
  371.             var phaseAngle:Number = phase * Math.PI * 2;
  372.             var sample:Number = Math.sin(phaseAngle) * amplify / UNIT;
  373.             
  374.             sample *= 0.2;
  375.             
  376.             bytes.writeFloat(sample);
  377.             bytes.writeFloat(sample);
  378.             
  379.             updateAmplify();
  380.         }
  381.         
  382.         event.data.writeBytes(bytes);
  383.     }
  384.     
  385. }
  386. function noteNumber2frequency(value:uint):Number
  387. {
  388.     if (value > 127)
  389.         value = 127;
  390.     
  391.     return 440 * Math.pow(2, (value - 69) / 12);
  392. }
noswf
  1. // forked from seagirl's Dynamic Sound Generation
  2. // write as3 code here..
  3. package
  4. {
  5.     import flash.display.Sprite;
  6.     import flash.events.Event;
  7.     import flash.events.TimerEvent;
  8.     import flash.utils.Timer;
  9.     [SWF(backgroundColor="#ffffff")]
  10.     
  11.     public class DSG extends Sprite
  12.     {
  13.         public function DSG()
  14.         {
  15.             super();
  16.             
  17.             init();
  18.         }
  19.         
  20.         private var timer:Timer;
  21.         private var noteNumbers:Array = [8082848587899192];
  22.         
  23.         private function init():void
  24.         {
  25.             ring();
  26.             
  27.             timer = new Timer(1000);
  28.             timer.addEventListener(TimerEvent.TIMER, timerHandler);
  29.             timer.start();
  30.         }
  31.         
  32.         private function ring():void
  33.         {
  34.             if (noteNumbers.length == 0)
  35.                 return;
  36.             
  37.             var noteNumber:int = noteNumbers[int(Math.random() * noteNumbers.length)];    
  38.             var attack:Number = Math.random() * 5;
  39.                         var decay:Number = Math.random() * 5;
  40.                         var release:Number = 10 - attack - decay;
  41.             new Tone(noteNumber, 28, attack, decay, 5, release).start();
  42.             new Particle(noteNumber, 28, attack, decay, 5, release, this).start();
  43.         }
  44.         
  45.         private function timerHandler(event:TimerEvent):void
  46.         {
  47.             ring();
  48.         }
  49.         
  50.     }
  51. }
  52. import flash.display.Sprite;
  53. import flash.events.Event;
  54. import flash.events.TimerEvent;
  55. import flash.utils.Timer;
  56. class Particle extends Sprite
  57. {
  58.     public static const UNIT:int = Math.round(Math.random()*100);
  59.     
  60.     public static const TIMER_RATE:int = 200;
  61.     
  62.     public function Particle(noteNumber:uint, length:Number, volume:Number, attack:Number, decay:Number, sustain:Number, release:Number, parent:Sprite)
  63.     {    
  64.         this.noteNumber = noteNumber;
  65.         this.length = length;
  66.         this.volume = volume;
  67.         this.attack = attack;
  68.         this.decay = decay;
  69.         this.sustain = sustain;
  70.         this.release = release;
  71.         
  72.         amplify = volume > sustain ? volume : sustain;
  73.         
  74.         parent.addChildAt(this0);
  75.         
  76.         check();
  77.         initializeVelocity();
  78.     }
  79.     
  80.     private var timer:Timer;
  81.     
  82.     private var radius:Number = 0;
  83.     private var alphaValue:Number = 1;
  84.     private var amplify:Number = 0;
  85.     
  86.     private var state:int = 1// 1: Attack, 2: Decay, 3: Release
  87.     
  88.     private var noteNumber:uint;
  89.     private var length:Number;
  90.     private var volume:Number;
  91.     private var attack:Number;
  92.     private var decay:Number;
  93.     private var sustain:Number;
  94.     private var release:Number;
  95.     
  96.     private var v1:Number;
  97.     private var v2:Number;
  98.     private var v3:Number;
  99.     
  100.     private function check():void
  101.     {
  102.         if (attack + decay + release != UNIT)
  103.         {
  104.              attack = 0;
  105.              decay = 1;
  106.              release = 1;
  107.         }
  108.             
  109.         if (volume > UNIT)
  110.             volume = UNIT;
  111.         
  112.         if (volume < 0)
  113.             volume = 0;
  114.             
  115.         if (sustain > UNIT)
  116.             sustain = UNIT;
  117.             
  118.         if (sustain < 0)
  119.             sustain = 0;
  120.     }
  121.     
  122.     private function initializeVelocity():void
  123.     {            
  124.         v1 = volume / (TIMER_RATE * length * attack / UNIT);
  125.         v2 = (volume - sustain) / (TIMER_RATE * length * decay / UNIT);
  126.         v3 = sustain / (TIMER_RATE * length * release / UNIT);
  127.         
  128.         alphaValue = volume > sustain ? volume : sustain;
  129.     }
  130.     
  131.     private function init():void
  132.     {
  133.         timer = new Timer(10000 / TIMER_RATE);
  134.         timer.addEventListener(TimerEvent.TIMER, timerHandler);
  135.         timer.start();
  136.     }
  137.     
  138.     private function update():void
  139.     {
  140.         drawGraphics();
  141.         updateParams();
  142.     }
  143.     
  144.     private function drawGraphics():void
  145.     {
  146.         graphics.clear();
  147.         
  148.         graphics.beginFill(0xFF0000, alphaValue / 40);
  149.         graphics.drawRect(-500,-5001000,1000);
  150.         graphics.endFill();
  151.     }
  152.     
  153.     private function updateParams():void
  154.     {
  155.         if (state == 1)
  156.         {
  157.             radius += v1;
  158.             if (radius >= volume)
  159.             {
  160.                 radius = volume;
  161.                 state = 2;
  162.             }
  163.         }
  164.         else if (state == 2)
  165.         {
  166.             if (volume < sustain)
  167.             {
  168.                 radius -= v2;
  169.                 
  170.                 if (radius >= sustain)
  171.                 {
  172.                     radius = sustain;
  173.                     state = 3;
  174.                 }
  175.             }
  176.             else
  177.             {
  178.                 alphaValue -= v2;
  179.                 
  180.                 if (alphaValue <= sustain)
  181.                 {
  182.                     alphaValue = sustain;
  183.                     state = 3;
  184.                 }
  185.             }    
  186.         }
  187.         else if (state == 3)
  188.         {
  189.             alphaValue -= v3;
  190.             
  191.             if (alphaValue <= 0)
  192.             {
  193.                 alphaValue = 0;
  194.                 
  195.                 stop();
  196.             }
  197.         }
  198.     }
  199.     
  200.     public function start():void
  201.     {
  202.         if (!parent)
  203.             throw new Error("parent must be specified.");
  204.         
  205.         if (x == 0 && y == 0)
  206.         {
  207.             x = Math.random() * stage.stageWidth;
  208.             y = Math.random() * stage.stageHeight;
  209.         }
  210.         
  211.         timer = new Timer(1000 / TIMER_RATE);
  212.         timer.addEventListener(TimerEvent.TIMER, timerHandler);
  213.         timer.start();
  214.     }
  215.     
  216.     public function stop():void
  217.     {
  218.         timer.removeEventListener(TimerEvent.TIMER, timerHandler);
  219.         
  220.         parent.removeChild(this);
  221.         
  222.         dispatchEvent(new Event(Event.COMPLETE));
  223.     }
  224.     
  225.     private function timerHandler(event:TimerEvent):void
  226.     {
  227.         update();
  228.     }
  229.     
  230. }
  231. import flash.events.Event;
  232. import flash.events.EventDispatcher;
  233. import flash.events.SampleDataEvent;
  234. import flash.media.Sound;
  235. import flash.utils.ByteArray;
  236. class Tone extends EventDispatcher
  237. {
  238.     public static const SAMPLING_RATE:Number = 44100;
  239.     public static const BUFFER_SIZE:int = 8192;
  240.         
  241.     public static const UNIT:int = Math.round(Math.random()*50);
  242.     
  243.     public function Tone(noteNumber:uint, length:Number, volume:Number, attack:Number, decay:Number, sustain:Number, release:Number)
  244.     {    
  245.         this.frequency = noteNumber2frequency(noteNumber);
  246.         this.length = length;
  247.         this.volume = volume;
  248.         this.attack = attack;
  249.         this.decay = decay;
  250.         this.sustain = sustain;
  251.         this.release = release;
  252.         
  253.         check();
  254.         initializeVelocity();
  255.     }
  256.     
  257.     private var sound:Sound;
  258.     
  259.     private var phase:Number = 0;
  260.     private var amplify:Number = 0;
  261.     
  262.     private var state:int = 1// 1: Attack, 2: Decay, 3: Release
  263.     
  264.     private var frequency:Number;
  265.     private var length:Number;
  266.     private var volume:Number;
  267.     private var attack:Number;
  268.     private var decay:Number;
  269.     private var sustain:Number;
  270.     private var release:Number;
  271.     
  272.     private var v1:Number;
  273.     private var v2:Number;
  274.     private var v3:Number;
  275.         
  276.     private function check():void
  277.     {
  278.         if (attack + decay + release != UNIT)
  279.         {
  280.              attack = 0;
  281.              decay = 5;
  282.              release = 5;
  283.         }
  284.             
  285.         if (volume > UNIT)
  286.             volume = UNIT;
  287.         
  288.         if (volume < 0)
  289.             volume = 0;
  290.             
  291.         if (sustain > UNIT)
  292.             sustain = UNIT;
  293.             
  294.         if (sustain < 0)
  295.             sustain = 0;
  296.     }
  297.     
  298.     private function initializeVelocity():void
  299.     {            
  300.         v1 = volume / (SAMPLING_RATE * length * attack / UNIT);
  301.         v2 = (volume - sustain) / (SAMPLING_RATE * length * decay / UNIT);
  302.         v3 = sustain / (SAMPLING_RATE * length * release / UNIT);
  303.     }
  304.     
  305.     private function updateAmplify():void
  306.     {
  307.         if (state == 1)
  308.         {
  309.             amplify += v1;
  310.             if (amplify >= volume)
  311.             {
  312.                 amplify = volume;
  313.                 state = 2;
  314.             }
  315.         }
  316.         else if (state == 2)
  317.         {
  318.             amplify -= v2;
  319.             
  320.             if (volume < sustain)
  321.             {
  322.                 if (amplify >= sustain)
  323.                 {
  324.                     amplify = sustain;
  325.                     state = 3;
  326.                 }
  327.             }
  328.             else
  329.             {
  330.                 if (amplify <= sustain)
  331.                 {
  332.                     amplify = sustain;
  333.                     state = 3;
  334.                 }
  335.             }    
  336.         }
  337.         else if (state == 3)
  338.         {
  339.             amplify -= v3;
  340.             
  341.             if (amplify <= 0)
  342.             {
  343.                 amplify = 0;
  344.                 
  345.                 stop();
  346.             }
  347.         }
  348.     }
  349.     
  350.     public function start():void
  351.     {                    
  352.         sound = new Sound();
  353.         sound.addEventListener(SampleDataEvent.SAMPLE_DATA, soundSampleDataHandler);
  354.         sound.play();
  355.     }
  356.     
  357.     public function stop():void
  358.     {
  359.         sound.removeEventListener(SampleDataEvent.SAMPLE_DATA, soundSampleDataHandler);
  360.         dispatchEvent(new Event(Event.COMPLETE));
  361.     }
  362.     
  363.     private function soundSampleDataHandler(event:SampleDataEvent):void
  364.     {    
  365.         var bytes:ByteArray = new ByteArray();
  366.         
  367.         for (var i:int = 0; i < BUFFER_SIZE; ++i)
  368.         {
  369.             phase += frequency / SAMPLING_RATE;  
  370.             
  371.             var phaseAngle:Number = phase * Math.PI * 2;
  372.             var sample:Number = Math.sin(phaseAngle) * amplify / UNIT;
  373.             
  374.             sample *= 0.2;
  375.             
  376.             bytes.writeFloat(sample);
  377.             bytes.writeFloat(sample);
  378.             
  379.             updateAmplify();
  380.         }
  381.         
  382.         event.data.writeBytes(bytes);
  383.     }
  384.     
  385. }
  386. function noteNumber2frequency(value:uint):Number
  387. {
  388.     if (value > 127)
  389.         value = 127;
  390.     
  391.     return 440 * Math.pow(2, (value - 69) / 12);
  392. }
noswf
  1. // forked from seagirl's Dynamic Sound Generation
  2. // write as3 code here..
  3. package
  4. {
  5.     import flash.display.Sprite;
  6.     import flash.events.Event;
  7.     import flash.events.TimerEvent;
  8.     import flash.utils.Timer;
  9.     [SWF(backgroundColor="#000000")]
  10.     
  11.     public class DSG extends Sprite
  12.     {
  13.         public function DSG()
  14.         {
  15.             super();
  16.             
  17.             init();
  18.         }
  19.         
  20.         private var timer:Timer;
  21.         private var noteNumbers:Array = [6819961467697172];
  22.         
  23.         private function init():void
  24.         {
  25.             ring();
  26.             
  27.             timer = new Timer(1000);
  28.             timer.addEventListener(TimerEvent.TIMER, timerHandler);
  29.             timer.start();
  30.         }
  31.         
  32.         private function ring():void
  33.         {
  34.             if (noteNumbers.length == 0)
  35.                 return;
  36.             
  37.             var noteNumber:int = noteNumbers[int(Math.random() * noteNumbers.length)];    
  38.             var attack:Number = Math.random() * 5;
  39.                         var decay:Number = Math.random() * 5;
  40.                         var release:Number = 10 - attack - decay;
  41.             new Tone(noteNumber, 28, attack, decay, 5, release).start();
  42.             new Particle(noteNumber, 28, attack, decay, 5, release, this).start();
  43.         }
  44.         
  45.         private function timerHandler(event:TimerEvent):void
  46.         {
  47.             ring();
  48.         }
  49.         
  50.     }
  51. }
  52. import flash.display.Sprite;
  53. import flash.events.Event;
  54. import flash.events.TimerEvent;
  55. import flash.utils.Timer;
  56. class Particle extends Sprite
  57. {
  58.     public static const UNIT:int = 10;
  59.     
  60.     public static const TIMER_RATE:int = 20;
  61.     
  62.     public function Particle(noteNumber:uint, length:Number, volume:Number, attack:Number, decay:Number, sustain:Number, release:Number, parent:Sprite)
  63.     {    
  64.         this.noteNumber = noteNumber;
  65.         this.length = length;
  66.         this.volume = volume;
  67.         this.attack = attack;
  68.         this.decay = decay;
  69.         this.sustain = sustain;
  70.         this.release = release;
  71.         
  72.         amplify = volume > sustain ? volume : sustain;
  73.         
  74.         parent.addChildAt(this0);
  75.         
  76.         check();
  77.         initializeVelocity();
  78.     }
  79.     
  80.     private var timer:Timer;
  81.     
  82.     private var radius:Number = 0;
  83.     private var alphaValue:Number = 2;
  84.     private var amplify:Number = 0;
  85.     
  86.     private var state:int = 1// 1: Attack, 2: Decay, 3: Release
  87.     
  88.     private var noteNumber:uint;
  89.     private var length:Number;
  90.     private var volume:Number;
  91.     private var attack:Number;
  92.     private var decay:Number;
  93.     private var sustain:Number;
  94.     private var release:Number;
  95.     
  96.     private var v1:Number;
  97.     private var v2:Number;
  98.     private var v3:Number;
  99.     
  100.     private function check():void
  101.     {
  102.         if (attack + decay + release != UNIT)
  103.         {
  104.              attack = 0;
  105.              decay = 5;
  106.              release = 5;
  107.         }
  108.             
  109.         if (volume > UNIT)
  110.             volume = UNIT;
  111.         
  112.         if (volume < 0)
  113.             volume = 0;
  114.             
  115.         if (sustain > UNIT)
  116.             sustain = UNIT;
  117.             
  118.         if (sustain < 0)
  119.             sustain = 0;
  120.     }
  121.     
  122.     private function initializeVelocity():void
  123.     {            
  124.         v1 = volume / (TIMER_RATE * length * attack / UNIT);
  125.         v2 = (volume - sustain) / (TIMER_RATE * length * decay / UNIT);
  126.         v3 = sustain / (TIMER_RATE * length * release / UNIT);
  127.         
  128.         alphaValue = volume > sustain ? volume : sustain;
  129.     }
  130.     
  131.     private function init():void
  132.     {
  133.         timer = new Timer(1000 / TIMER_RATE);
  134.         timer.addEventListener(TimerEvent.TIMER, timerHandler);
  135.         timer.start();
  136.     }
  137.     
  138.     private function update():void
  139.     {
  140.         drawGraphics();
  141.         updateParams();
  142.     }
  143.     
  144.     private function drawGraphics():void
  145.     {
  146.         graphics.clear();
  147.         
  148.         graphics.beginFill(0xFFFFFF, alphaValue / 10);
  149.         graphics.drawCircle(00, radius * 0.8 * amplify);
  150.         graphics.endFill();
  151.     }
  152.     
  153.     private function updateParams():void
  154.     {
  155.         if (state == 1)
  156.         {
  157.             radius += v1;
  158.             if (radius >= volume)
  159.             {
  160.                 radius = volume;
  161.                 state = 2;
  162.             }
  163.         }
  164.         else if (state == 2)
  165.         {
  166.             if (volume < sustain)
  167.             {
  168.                 radius -= v2;
  169.                 
  170.                 if (radius >= sustain)
  171.                 {
  172.                     radius = sustain;
  173.                     state = 3;
  174.                 }
  175.             }
  176.             else
  177.             {
  178.                 alphaValue -= v2;
  179.                 
  180.                 if (alphaValue <= sustain)
  181.                 {
  182.                     alphaValue = sustain;
  183.                     state = 3;
  184.                 }
  185.             }    
  186.         }
  187.         else if (state == 3)
  188.         {
  189.             alphaValue -= v3;
  190.             
  191.             if (alphaValue <= 0)
  192.             {
  193.                 alphaValue = 0;
  194.                 
  195.                 stop();
  196.             }
  197.         }
  198.     }
  199.     
  200.     public function start():void
  201.     {
  202.         if (!parent)
  203.             throw new Error("parent must be specified.");
  204.         
  205.         if (x == 0 && y == 0)
  206.         {
  207.             x = Math.random() * stage.stageWidth;
  208.             y = Math.random() * stage.stageHeight;
  209.         }
  210.         
  211.         timer = new Timer(1000 / TIMER_RATE);
  212.         timer.addEventListener(TimerEvent.TIMER, timerHandler);
  213.         timer.start();
  214.     }
  215.     
  216.     public function stop():void
  217.     {
  218.         timer.removeEventListener(TimerEvent.TIMER, timerHandler);
  219.         
  220.         parent.removeChild(this);
  221.         
  222.         dispatchEvent(new Event(Event.COMPLETE));
  223.     }
  224.     
  225.     private function timerHandler(event:TimerEvent):void
  226.     {
  227.         update();
  228.     }
  229.     
  230. }
  231. import flash.events.Event;
  232. import flash.events.EventDispatcher;
  233. import flash.events.SampleDataEvent;
  234. import flash.media.Sound;
  235. import flash.utils.ByteArray;
  236. class Tone extends EventDispatcher
  237. {
  238.     public static const SAMPLING_RATE:Number = 44100;
  239.     public static const BUFFER_SIZE:int = 8192;
  240.         
  241.     public static const UNIT:int = 10;
  242.     
  243.     public function Tone(noteNumber:uint, length:Number, volume:Number, attack:Number, decay:Number, sustain:Number, release:Number)
  244.     {    
  245.         this.frequency = noteNumber2frequency(noteNumber);
  246.         this.length = length;
  247.         this.volume = volume;
  248.         this.attack = attack;
  249.         this.decay = decay;
  250.         this.sustain = sustain;
  251.         this.release = release;
  252.         
  253.         check();
  254.         initializeVelocity();
  255.     }
  256.     
  257.     private var sound:Sound;
  258.     
  259.     private var phase:Number = 0;
  260.     private var amplify:Number = 0;
  261.     
  262.     private var state:int = 1// 1: Attack, 2: Decay, 3: Release
  263.     
  264.     private var frequency:Number;
  265.     private var length:Number;
  266.     private var volume:Number;
  267.     private var attack:Number;
  268.     private var decay:Number;
  269.     private var sustain:Number;
  270.     private var release:Number;
  271.     
  272.     private var v1:Number;
  273.     private var v2:Number;
  274.     private var v3:Number;
  275.         
  276.     private function check():void
  277.     {
  278.         if (attack + decay + release != UNIT)
  279.         {
  280.              attack = 0;
  281.              decay = 5;
  282.              release = 5;
  283.         }
  284.             
  285.         if (volume > UNIT)
  286.             volume = UNIT;
  287.         
  288.         if (volume < 0)
  289.             volume = 0;
  290.             
  291.         if (sustain > UNIT)
  292.             sustain = UNIT;
  293.             
  294.         if (sustain < 0)
  295.             sustain = 0;
  296.     }
  297.     
  298.     private function initializeVelocity():void
  299.     {            
  300.         v1 = volume / (SAMPLING_RATE * length * attack / UNIT);
  301.         v2 = (volume - sustain) / (SAMPLING_RATE * length * decay / UNIT);
  302.         v3 = sustain / (SAMPLING_RATE * length * release / UNIT);
  303.     }
  304.     
  305.     private function updateAmplify():void
  306.     {
  307.         if (state == 1)
  308.         {
  309.             amplify += v3;
  310.             if (amplify >= volume)
  311.             {
  312.                 amplify = volume;
  313.                 state = 2;
  314.             }
  315.         }
  316.         else if (state == 2)
  317.         {
  318.             amplify -= v2;
  319.             
  320.             if (volume < sustain)
  321.             {
  322.                 if (amplify >= sustain)
  323.                 {
  324.                     amplify = sustain;
  325.                     state = 3;
  326.                 }
  327.             }
  328.             else
  329.             {
  330.                 if (amplify <= sustain)
  331.                 {
  332.                     amplify = sustain;
  333.                     state = 15;
  334.                 }
  335.             }    
  336.         }
  337.         else if (state == 3)
  338.         {
  339.             amplify -= v3;
  340.             
  341.             if (amplify <= 0)
  342.             {
  343.                 amplify = 0;
  344.                 
  345.                 stop();
  346.             }
  347.         }
  348.     }
  349.     
  350.     public function start():void
  351.     {                    
  352.         sound = new Sound();
  353.         sound.addEventListener(SampleDataEvent.SAMPLE_DATA, soundSampleDataHandler);
  354.         sound.play();
  355.     }
  356.     
  357.     public function stop():void
  358.     {
  359.         sound.removeEventListener(SampleDataEvent.SAMPLE_DATA, soundSampleDataHandler);
  360.         dispatchEvent(new Event(Event.COMPLETE));
  361.     }
  362.     
  363.     private function soundSampleDataHandler(event:SampleDataEvent):void
  364.     {    
  365.         var bytes:ByteArray = new ByteArray();
  366.         
  367.         for (var i:int = 0; i < BUFFER_SIZE; ++i)
  368.         {
  369.             phase += frequency / SAMPLING_RATE;  
  370.             
  371.             var phaseAngle:Number = phase * Math.PI * 2;
  372.             var sample:Number = Math.sin(phaseAngle) * amplify / UNIT;
  373.             
  374.             sample *= 0.2;
  375.             
  376.             bytes.writeFloat(sample);
  377.             bytes.writeFloat(sample);
  378.             
  379.             updateAmplify();
  380.         }
  381.         
  382.         event.data.writeBytes(bytes);
  383.     }
  384.     
  385. }
  386. function noteNumber2frequency(value:uint):Number
  387. {
  388.     if (value > 127)
  389.         value = 127;
  390.     
  391.     return 440 * Math.pow(2, (value - 69) / 12);
  392. }
noswf
  1. // forked from seagirl's Dynamic Sound Generation
  2. // write as3 code here..
  3. package
  4. {
  5.     import flash.display.Sprite;
  6.     import flash.events.Event;
  7.     import flash.events.TimerEvent;
  8.     import flash.utils.Timer;
  9.     [SWF(backgroundColor="#000000")]
  10.     
  11.     public class DSG extends Sprite
  12.     {
  13.         public function DSG()
  14.         {
  15.             super();
  16.             
  17.             init();
  18.         }
  19.         
  20.         private var timer:Timer;
  21.         private var noteNumbers:Array = [6062646567697172];
  22.         
  23.         private function init():void
  24.         {
  25.             ring();
  26.             
  27.             timer = new Timer(1000);
  28.             timer.addEventListener(TimerEvent.TIMER, timerHandler);
  29.             timer.start();
  30.         }
  31.         
  32.         private function ring():void
  33.         {
  34.             if (noteNumbers.length == 0)
  35.                 return;
  36.             
  37.             var noteNumber:int = noteNumbers[int(Math.random() * noteNumbers.length)];    
  38.             var attack:Number = Math.random() * 5;
  39.                         var decay:Number = Math.random() * 5;
  40.                         var release:Number = 10 - attack - decay;
  41.             new Tone(noteNumber, 28, attack, decay, 5, release).start();
  42.             new Particle(noteNumber, 28, attack, decay, 5, release, this).start();
  43.         }
  44.         
  45.         private function timerHandler(event:TimerEvent):void
  46.         {
  47.             ring();
  48.         }
  49.         
  50.     }
  51. }
  52. import flash.display.Sprite;
  53. import flash.events.Event;
  54. import flash.events.TimerEvent;
  55. import flash.utils.Timer;
  56. class Particle extends Sprite
  57. {
  58.     public static const UNIT:int = 10;
  59.     
  60.     public static const TIMER_RATE:int = 20;
  61.     
  62.     public function Particle(noteNumber:uint, length:Number, volume:Number, attack:Number, decay:Number, sustain:Number, release:Number, parent:Sprite)
  63.     {    
  64.         this.noteNumber = noteNumber;
  65.         this.length = length;
  66.         this.volume = volume;
  67.         this.attack = attack;
  68.         this.decay = decay;
  69.         this.sustain = sustain;
  70.         this.release = release;
  71.         
  72.         amplify = volume > sustain ? volume : sustain;
  73.         
  74.         parent.addChildAt(this0);
  75.         
  76.         check();
  77.         initializeVelocity();
  78.     }
  79.     
  80.     private var timer:Timer;
  81.     
  82.     private var radius:Number = 0;
  83.     private var alphaValue:Number = 1;
  84.     private var amplify:Number = 0;
  85.     
  86.     private var state:int = 1// 1: Attack, 2: Decay, 3: Release
  87.     
  88.     private var noteNumber:uint;
  89.     private var length:Number;
  90.     private var volume:Number;
  91.     private var attack:Number;
  92.     private var decay:Number;
  93.     private var sustain:Number;
  94.     private var release:Number;
  95.     
  96.     private var v1:Number;
  97.     private var v2:Number;
  98.     private var v3:Number;
  99.     
  100.     private function check():void
  101.     {
  102.         if (attack + decay + release != UNIT)
  103.         {
  104.              attack = 0;
  105.              decay = 5;
  106.              release = 5;
  107.         }
  108.             
  109.         if (volume > UNIT)
  110.             volume = UNIT;
  111.         
  112.         if (volume < 0)
  113.             volume = 0;
  114.             
  115.         if (sustain > UNIT)
  116.             sustain = UNIT;
  117.             
  118.         if (sustain < 0)
  119.             sustain = 0;
  120.     }
  121.     
  122.     private function initializeVelocity():void
  123.     {            
  124.         v1 = volume / (TIMER_RATE * length * attack / UNIT);
  125.         v2 = (volume - sustain) / (TIMER_RATE * length * decay / UNIT);
  126.         v3 = sustain / (TIMER_RATE * length * release / UNIT);
  127.         
  128.         alphaValue = volume > sustain ? volume : sustain;
  129.     }
  130.     
  131.     private function init():void
  132.     {
  133.         timer = new Timer(1000 / TIMER_RATE);
  134.         timer.addEventListener(TimerEvent.TIMER, timerHandler);
  135.         timer.start();
  136.     }
  137.     
  138.     private function update():void
  139.     {
  140.         drawGraphics();
  141.         updateParams();
  142.     }
  143.     
  144.     private function drawGraphics():void
  145.     {
  146.         graphics.clear();
  147.         
  148.         graphics.beginFill(0xFFFFFF, alphaValue / 10);
  149.         graphics.drawCircle(00, radius * 0.8 * amplify);
  150.         graphics.endFill();
  151.     }
  152.     
  153.     private function updateParams():void
  154.     {
  155.         if (state == 1)
  156.         {
  157.             radius += v1;
  158.             if (radius >= volume)
  159.             {
  160.                 radius = volume;
  161.                 state = 2;
  162.             }
  163.         }
  164.         else if (state == 2)
  165.         {
  166.             if (volume < sustain)
  167.             {
  168.                 radius -= v2;
  169.                 
  170.                 if (radius >= sustain)
  171.                 {
  172.                     radius = sustain;
  173.                     state = 3;
  174.                 }
  175.             }
  176.             else
  177.             {
  178.                 alphaValue -= v2;
  179.                 
  180.                 if (alphaValue <= sustain)
  181.                 {
  182.                     alphaValue = sustain;
  183.                     state = 3;
  184.                 }
  185.             }    
  186.         }
  187.         else if (state == 3)
  188.         {
  189.             alphaValue -= v3;
  190.             
  191.             if (alphaValue <= 0)
  192.             {
  193.                 alphaValue = 0;
  194.                 
  195.                 stop();
  196.             }
  197.         }
  198.     }
  199.     
  200.     public function start():void
  201.     {
  202.         if (!parent)
  203.             throw new Error("parent must be specified.");
  204.         
  205.         if (x == 0 && y == 0)
  206.         {
  207.             x = Math.random() * stage.stageWidth;
  208.             y = Math.random() * stage.stageHeight;
  209.         }
  210.         
  211.         timer = new Timer(1000 / TIMER_RATE);
  212.         timer.addEventListener(TimerEvent.TIMER, timerHandler);
  213.         timer.start();
  214.     }
  215.     
  216.     public function stop():void
  217.     {
  218.         timer.removeEventListener(TimerEvent.TIMER, timerHandler);
  219.         
  220.         parent.removeChild(this);
  221.         
  222.         dispatchEvent(new Event(Event.COMPLETE));
  223.     }
  224.     
  225.     private function timerHandler(event:TimerEvent):void
  226.     {
  227.         update();
  228.     }
  229.     
  230. }
  231. import flash.events.Event;
  232. import flash.events.EventDispatcher;
  233. import flash.events.SampleDataEvent;
  234. import flash.media.Sound;
  235. import flash.utils.ByteArray;
  236. class Tone extends EventDispatcher
  237. {
  238.     public static const SAMPLING_RATE:Number = 44100;
  239.     public static const BUFFER_SIZE:int = 8192;
  240.         
  241.     public static const UNIT:int = 10;
  242.     
  243.     public function Tone(noteNumber:uint, length:Number, volume:Number, attack:Number, decay:Number, sustain:Number, release:Number)
  244.     {    
  245.         this.frequency = noteNumber2frequency(noteNumber);
  246.         this.length = length;
  247.         this.volume = volume;
  248.         this.attack = attack;
  249.         this.decay = decay;
  250.         this.sustain = sustain;
  251.         this.release = release;
  252.         
  253.         check();
  254.         initializeVelocity();
  255.     }
  256.     
  257.     private var sound:Sound;
  258.     
  259.     private var phase:Number = 0;
  260.     private var amplify:Number = 0;
  261.     
  262.     private var state:int = 1// 1: Attack, 2: Decay, 3: Release
  263.     
  264.     private var frequency:Number;
  265.     private var length:Number;
  266.     private var volume:Number;
  267.     private var attack:Number;
  268.     private var decay:Number;
  269.     private var sustain:Number;
  270.     private var release:Number;
  271.     
  272.     private var v1:Number;
  273.     private var v2:Number;
  274.     private var v3:Number;
  275.         
  276.     private function check():void
  277.     {
  278.         if (attack + decay + release != UNIT)
  279.         {
  280.              attack = 0;
  281.              decay = 5;
  282.              release = 5;
  283.         }
  284.             
  285.         if (volume > UNIT)
  286.             volume = UNIT;
  287.         
  288.         if (volume < 0)
  289.             volume = 0;
  290.             
  291.         if (sustain > UNIT)
  292.             sustain = UNIT;
  293.             
  294.         if (sustain < 0)
  295.             sustain = 0;
  296.     }
  297.     
  298.     private function initializeVelocity():void
  299.     {            
  300.         v1 = volume / (SAMPLING_RATE * length * attack / UNIT);
  301.         v2 = (volume - sustain) / (SAMPLING_RATE * length * decay / UNIT);
  302.         v3 = sustain / (SAMPLING_RATE * length * release / UNIT);
  303.     }
  304.     
  305.     private function updateAmplify():void
  306.     {
  307.         if (state == 1)
  308.         {
  309.             amplify += v1;
  310.             if (amplify >= volume)
  311.             {
  312.                 amplify = volume;
  313.                 state = 2;
  314.             }
  315.         }
  316.         else if (state == 2)
  317.         {
  318.             amplify -= v2;
  319.             
  320.             if (volume < sustain)
  321.             {
  322.                 if (amplify >= sustain)
  323.                 {
  324.                     amplify = sustain;
  325.                     state = 3;
  326.                 }
  327.             }
  328.             else
  329.             {
  330.                 if (amplify <= sustain)
  331.                 {
  332.                     amplify = sustain;
  333.                     state = 3;
  334.                 }
  335.             }    
  336.         }
  337.         else if (state == 3)
  338.         {
  339.             amplify -= v3;
  340.             
  341.             if (amplify <= 0)
  342.             {
  343.                 amplify = 0;
  344.                 
  345.                 stop();
  346.             }
  347.         }
  348.     }
  349.     
  350.     public function start():void
  351.     {                    
  352.         sound = new Sound();
  353.         sound.addEventListener(SampleDataEvent.SAMPLE_DATA, soundSampleDataHandler);
  354.         sound.play();
  355.     }
  356.     
  357.     public function stop():void
  358.     {
  359.         sound.removeEventListener(SampleDataEvent.SAMPLE_DATA, soundSampleDataHandler);
  360.         dispatchEvent(new Event(Event.COMPLETE));
  361.     }
  362.     
  363.     private function soundSampleDataHandler(event:SampleDataEvent):void
  364.     {    
  365.         var bytes:ByteArray = new ByteArray();
  366.         
  367.         for (var i:int = 0; i < BUFFER_SIZE; ++i)
  368.         {
  369.             phase += frequency / SAMPLING_RATE;  
  370.             
  371.             var phaseAngle:Number = phase * Math.PI * 2;
  372.             var sample:Number = Math.sin(phaseAngle) * amplify / UNIT;
  373.             
  374.             sample *= 0.2;
  375.             
  376.             bytes.writeFloat(sample);
  377.             bytes.writeFloat(sample);
  378.             
  379.             updateAmplify();
  380.         }
  381.         
  382.         event.data.writeBytes(bytes);
  383.     }
  384.     
  385. }
  386. function noteNumber2frequency(value:uint):Number
  387. {
  388.     if (value > 127)
  389.         value = 127;
  390.     
  391.     return 440 * Math.pow(2, (value - 69) / 12);
  392. }
noswf
  1. // forked from seagirl's Dynamic Sound Generation
  2. // write as3 code here..
  3. package
  4. {
  5.     import flash.display.Sprite;
  6.     import flash.events.Event;
  7.     import flash.events.TimerEvent;
  8.     import flash.utils.Timer;
  9.     [SWF(backgroundColor="#000000")]
  10.     
  11.     public class DSG extends Sprite
  12.     {
  13.         public function DSG()
  14.         {
  15.         
  16.             init();
  17.         }
  18.         
  19.         private var timer:Timer;
  20.         private var noteNumbers:Array = [6062100];
  21.         
  22.         private function init():void
  23.         {
  24.             //ring();
  25.             
  26.             timer = new Timer(1000);
  27.             timer.addEventListener(TimerEvent.TIMER, timerHandler);
  28.             timer.start();
  29.         }
  30.         
  31.         private function ring():void
  32.         {
  33.             if (noteNumbers.length == 0)
  34.                 return;
  35.             
  36.             var noteNumber:int = noteNumbers[int(1)];    
  37.             var attack:Number = Math.random() * 5;
  38.                         var decay:Number = Math.random() * 5;
  39.                         var release:Number = 10 - attack - decay;
  40.             new Tone(noteNumber, 28, attack, decay, 5, release).start();
  41.             //new Particle(noteNumber, 2, 8, attack, decay, 5, release, this).start();
  42.         }
  43.         
  44.         private function timerHandler(event:TimerEvent):void
  45.         {
  46.             ring();
  47.         }
  48.         
  49.     }
  50. }
  51. import flash.display.Sprite;
  52. import flash.events.Event;
  53. import flash.events.TimerEvent;
  54. import flash.utils.Timer;
  55. class Particle extends Sprite
  56. {
  57.     public static const UNIT:int = 10;
  58.     
  59.     public static const TIMER_RATE:int = 20;
  60.     
  61.     public function Particle(noteNumber:uint, length:Number, volume:Number, attack:Number, decay:Number, sustain:Number, release:Number, parent:Sprite)
  62.     {    
  63.         this.noteNumber = noteNumber;
  64.         this.length = length;
  65.         this.volume = volume;
  66.         this.attack = attack;
  67.         this.decay = decay;
  68.         this.sustain = sustain;
  69.         this.release = release;
  70.         
  71.         amplify = volume > sustain ? volume : sustain;
  72.         
  73.         parent.addChildAt(this0);
  74.         
  75.         check();
  76.         initializeVelocity();
  77.     }
  78.     
  79.     private var timer:Timer;
  80.     
  81.     private var radius:Number = 0;
  82.     private var alphaValue:Number = 1;
  83.     private var amplify:Number = 0;
  84.     
  85.     private var state:int = 1// 1: Attack, 2: Decay, 3: Release
  86.     
  87.     private var noteNumber:uint;
  88.     private var length:Number;
  89.     private var volume:Number;
  90.     private var attack:Number;
  91.     private var decay:Number;
  92.     private var sustain:Number;
  93.     private var release:Number;
  94.     
  95.     private var v1:Number;
  96.     private var v2:Number;
  97.     private var v3:Number;
  98.     
  99.     private function check():void
  100.     {
  101.         if (attack + decay + release != UNIT)
  102.         {
  103.              attack = 0;
  104.              decay = 5;
  105.              release = 5;
  106.         }
  107.             
  108.         if (volume > UNIT)
  109.             volume = UNIT;
  110.         
  111.         if (volume < 0)
  112.             volume = 0;
  113.             
  114.         if (sustain > UNIT)
  115.             sustain = UNIT;
  116.             
  117.         if (sustain < 0)
  118.             sustain = 0;
  119.     }
  120.     
  121.     private function initializeVelocity():void
  122.     {            
  123.         v1 = volume / (TIMER_RATE * length * attack / UNIT);
  124.         v2 = (volume - sustain) / (TIMER_RATE * length * decay / UNIT);
  125.         v3 = sustain / (TIMER_RATE * length * release / UNIT);
  126.         
  127.         alphaValue = volume > sustain ? volume : sustain;
  128.     }
  129.     
  130.     private function init():void
  131.     {
  132.         timer = new Timer(1000 / TIMER_RATE);
  133.         timer.addEventListener(TimerEvent.TIMER, timerHandler);
  134.         timer.start();
  135.     }
  136.     
  137.     private function update():void
  138.     {
  139.         drawGraphics();
  140.         updateParams();
  141.     }
  142.     
  143.     private function drawGraphics():void
  144.     {
  145.         graphics.clear();
  146.         
  147.         graphics.beginFill(0xFFFFFF, alphaValue / 10);
  148.         graphics.drawCircle(00, radius * 0.8 * amplify);
  149.         graphics.endFill();
  150.     }
  151.     
  152.     private function updateParams():void
  153.     {
  154.         if (state == 1)
  155.         {
  156.             radius += v1;
  157.             if (radius >= volume)
  158.             {
  159.                 radius = volume;
  160.                 state = 2;
  161.             }
  162.         }
  163.         else if (state == 2)
  164.         {
  165.             if (volume < sustain)
  166.             {
  167.                 radius -= v2;
  168.                 
  169.                 if (radius >= sustain)
  170.                 {
  171.                     radius = sustain;
  172.                     state = 3;
  173.                 }
  174.             }
  175.             else
  176.             {
  177.                 alphaValue -= v2;
  178.                 
  179.                 if (alphaValue <= sustain)
  180.                 {
  181.                     alphaValue = sustain;
  182.                     state = 3;
  183.                 }
  184.             }    
  185.         }
  186.         else if (state == 3)
  187.         {
  188.             alphaValue -= v3;
  189.             
  190.             if (alphaValue <= 0)
  191.             {
  192.                 alphaValue = 0;
  193.                 
  194.                 stop();
  195.             }
  196.         }
  197.     }
  198.     
  199.     public function start():void
  200.     {
  201.         if (!parent)
  202.             throw new Error("parent must be specified.");
  203.         
  204.         if (x == 0 && y == 0)
  205.         {
  206.             x = Math.random() * stage.stageWidth;
  207.             y = Math.random() * stage.stageHeight;
  208.         }
  209.         
  210.         timer = new Timer(1000 / TIMER_RATE);
  211.         timer.addEventListener(TimerEvent.TIMER, timerHandler);
  212.         timer.start();
  213.     }
  214.     
  215.     public function stop():void
  216.     {
  217.         timer.removeEventListener(TimerEvent.TIMER, timerHandler);
  218.         
  219.         parent.removeChild(this);
  220.         
  221.         dispatchEvent(new Event(Event.COMPLETE));
  222.     }
  223.     
  224.     private function timerHandler(event:TimerEvent):void
  225.     {
  226.         update();
  227.     }
  228.     
  229. }
  230. import flash.events.Event;
  231. import flash.events.EventDispatcher;
  232. import flash.events.SampleDataEvent;
  233. import flash.media.Sound;
  234. import flash.utils.ByteArray;
  235. class Tone extends EventDispatcher
  236. {
  237.     public static const SAMPLING_RATE:Number = 44100;
  238.     public static const BUFFER_SIZE:int = 441;
  239.         
  240.     public static const UNIT:int = 10;
  241.     
  242.     public function Tone(noteNumber:uint, length:Number, volume:Number, attack:Number, decay:Number, sustain:Number, release:Number)
  243.     {    
  244.         this.frequency = noteNumber2frequency(noteNumber);
  245.         this.length = length;
  246.         this.volume = volume;
  247.         this.attack = attack;
  248.         this.decay = decay;
  249.         this.sustain = sustain;
  250.         this.release = release;
  251.         
  252.         check();
  253.         initializeVelocity();
  254.     }
  255.     
  256.     private var sound:Sound;
  257.     
  258.     private var phase:Number = 0;
  259.     private var amplify:Number = 0;
  260.     
  261.     private var state:int = 1// 1: Attack, 2: Decay, 3: Release
  262.     
  263.     private var frequency:Number;
  264.     private var length:Number;
  265.     private var volume:Number;
  266.     private var attack:Number;
  267.     private var decay:Number;
  268.     private var sustain:Number;
  269.     private var release:Number;
  270.     
  271.     private var v1:Number;
  272.     private var v2:Number;
  273.     private var v3:Number;
  274.         
  275.     private function check():void
  276.     {
  277.         if (attack + decay + release != UNIT)
  278.         {
  279.              attack = 0;
  280.              decay = 5;
  281.              release = 5;
  282.         }
  283.             
  284.         if (volume > UNIT)
  285.             volume = UNIT;
  286.         
  287.         if (volume < 0)
  288.             volume = 0;
  289.             
  290.         if (sustain > UNIT)
  291.             sustain = UNIT;
  292.             
  293.         if (sustain < 0)
  294.             sustain = 0;
  295.     }
  296.     
  297.     private function initializeVelocity():void
  298.     {            
  299.         v1 = volume / (SAMPLING_RATE * length * attack / UNIT);
  300.         v2 = (volume - sustain) / (SAMPLING_RATE * length * decay / UNIT);
  301.         v3 = sustain / (SAMPLING_RATE * length * release / UNIT);
  302.     }
  303.     
  304.     private function updateAmplify():void
  305.     {
  306.         if (state == 1)
  307.         {
  308.             amplify += v1;
  309.             if (amplify >= volume)
  310.             {
  311.                 amplify = volume;
  312.                 state = 2;
  313.             }
  314.         }
  315.         else if (state == 2)
  316.         {
  317.             amplify -= v2;
  318.             
  319.             if (volume < sustain)
  320.             {
  321.                 if (amplify >= sustain)
  322.                 {
  323.                     amplify = sustain;
  324.                     state = 3;
  325.                 }
  326.             }
  327.             else
  328.             {
  329.                 if (amplify <= sustain)
  330.                 {
  331.                     amplify = sustain;
  332.                     state = 3;
  333.                 }
  334.             }    
  335.         }
  336.         else if (state == 3)
  337.         {
  338.             amplify -= v3;
  339.             
  340.             if (amplify <= 0)
  341.             {
  342.                 amplify = 0;
  343.                 
  344.                 stop();
  345.             }
  346.         }
  347.     }
  348.     
  349.     public function start():void
  350.     {                    
  351.         sound = new Sound();
  352.         sound.addEventListener(SampleDataEvent.SAMPLE_DATA, soundSampleDataHandler);
  353.         sound.play();
  354.     }
  355.     
  356.     public function stop():void
  357.     {
  358.         sound.removeEventListener(SampleDataEvent.SAMPLE_DATA, soundSampleDataHandler);
  359.         dispatchEvent(new Event(Event.COMPLETE));
  360.     }
  361.     
  362.     private function soundSampleDataHandler(event:SampleDataEvent):void
  363.     {    
  364.         var bytes:ByteArray = new ByteArray();
  365.         
  366.         for (var i:int = 0; i < BUFFER_SIZE; ++i)
  367.         {
  368.             phase += frequency / SAMPLING_RATE;  
  369.             
  370.             var phaseAngle:Number = phase * Math.PI * 2;
  371.             var sample:Number = Math.sin(phaseAngle) * amplify / UNIT;
  372.             
  373.             sample *= 0.2;
  374.             
  375.             bytes.writeFloat(sample);
  376.             bytes.writeFloat(sample);
  377.             
  378.             updateAmplify();
  379.         }
  380.         
  381.         event.data.writeBytes(bytes);
  382.     }
  383.     
  384. }
  385. function noteNumber2frequency(value:uint):Number
  386. {
  387.     if (value > 127)
  388.         value = 127;
  389.     
  390.     return 440 * Math.pow(2, (value - 69) / 12);
  391. }
noswf
Get Adobe Flash Player