skip to main |skip to sidebar

2007-08-19

声音编辑不是梦! - AS3.0的声音合成

以前,在Flex2刚发布时,有人说AS3.0能够编辑声音,方法是用ByteArray与SoundMixer。但被证明这是的,SoundMixer能读不能写!

但现在新的方法出现了!把声音用SWF的格式写到ByteArray里,再用Loader读进来,最后把它变为Sound!

实现这个的是Andre Michelle 与 Joa Ebert 开发的popforge Project。

Sample:

package {
 import de.popforge.audio.output.Audio;
 import de.popforge.audio.output.AudioBuffer;
 import de.popforge.audio.output.Sample;
 import de.popforge.audio.output.SoundFactory;
 
 import flash.display.Sprite;
 import flash.events.Event;
 import flash.utils.describeType;
 import flash.media.Sound;

 public class popforge extends Sprite
 {
  public function popforge()
  {
   var samples:Array = [];
   var phase:Number = 2;
   var freq:Number = 0.12
   for( var i: int = 0 ; i < 44100*3 ; i++ ) {
   
    //-- create an amplitude [-1,1]
   
    var amplitude:Number = Math.sin( phase * Math.PI * 2 );
   
    //-- create a sample
   
    var sample:Sample = new Sample( amplitude, amplitude );
   
    //-- push in array
   
    samples.push( sample );
   
    //-- increase phase
   
    phase += freq / Audio.RATE44100;
   
   }
   SoundFactory.fromArray(samples,
    Audio.MONO,
     Audio.BIT16, 
     Audio.RATE44100, 
     func);
  }
  private function func(event:Sound):void {
   event.play();
  }
 }
}

没有评论: