※現在、「wonderfl build flash online」求人コンテンツ制作に関してのアンケートを実施中です!みなさまのお力添えを頂いて、続々とアンケート結果が集まっていますが、まだまだ募集しております。ご協力のほど、どうぞよろしくお願いいたします!

wonderfl運営事務局
→アンケートページ(※ログインしてからお答えいただけるようになっています。)

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


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="#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
Get Adobe Flash Player