root/as3/sazameki/branches/fp10/src/Example.as

リビジョン 649, 3.1 kB (コミッタ: zk33, コミット時期: 4 年 前)

サンプラー的なもの、まだ途中だけどひとまず動くようになったのでコミットしておいてみる。

Line 
1 package  {
2         import flash.display.Sprite;
3         import flash.media.Sound;
4         import flash.utils.ByteArray;
5         import org.sazameki.audio.core.AudioSetting;
6         import org.sazameki.audio.core.Sample;
7         import org.sazameki.audio.core.SoundFactory;
8         import org.sazameki.audio.engine.ssGenerator.engine.EngineMax;
9         import org.sazameki.audio.engine.ssGenerator.engine.ISsEngine;
10         import org.sazameki.audio.engine.ssGenerator.SsGenerator;
11         import org.sazameki.audio.events.AudioEvent;
12         import org.sazameki.audio.events.AudioProgressEvent;
13         import org.sazameki.audio.format.Wav;
14        
15         /**
16         * sazameki examples
17         * @author Takaaki Yamazaki
18         */
19         public class Example extends Sprite{
20                
21                 public function Example()
22                 {
23                         sazamekiCoreSample();
24                         //ssGeneratorSample();
25                        
26                 }
27                
28                 private function sazamekiCoreSample():void
29                 {
30                         //----------------------------------------------------
31                         // sazameki core(org.sazameki.audio.core)
32                         // 音の変換などの基本的な機能を提供するクラス。
33                        
34                         //convert signals(array of org.sazameki.core.Sample) to flash.media.Sound object
35                         var samples:Array=makeNoiseSamples();
36                         var audioSetting:AudioSetting=new AudioSetting(2,44100,16);
37                         var factory:SoundFactory=new SoundFactory();
38                         factory.addEventListener(AudioEvent.COMPLETE,onCoreSoundCreated);
39                         factory.generateSound(samples,audioSetting);
40                 }
41                
42                 private function makeNoiseSamples():Array{
43                         //simply generate 1sec(44100samples) noise
44                         var arr:Array=new Array();
45                        
46                         for(var i:int=0;i<44100;i++){
47                                 arr.push(new Sample(Math.random()*0.5,Math.random()*0.5));
48                         }
49                        
50                         return arr;
51                 }
52                 private function onCoreSoundCreated(e:AudioEvent):void
53                 {
54                         e.sound.play();
55                 }
56                
57                
58                 private function ssGeneratorSample():void
59                 {
60                         //----------------------------------------------------
61                         // SsGenerator(org.sazameki.audio.engine.ssGenerator) : short sound generator
62                         // 音を定義した文字列に基づいて、flash.media.Soundオブジェクトを生成して返す。
63                        
64                         //prepare SsEngine(always required!)
65                         var engine:ISsEngine=new EngineMax();
66                         //create ssGenerator instance
67                         var ssGen:SsGenerator=new SsGenerator(engine);
68                         //add listener
69                         ssGen.addEventListener(AudioEvent.COMPLETE,onSound);
70                        
71                         //generate sound
72                        
73                         /* simply generate 1sec sine wave
74                         ssGen.generateByString('l:1000,g_sine:440');
75                         //*/
76                        
77                         //* a little complex sound
78                         ssGen.generateByString('l:250,g_sine:28,g_square:1-1200-55+penv-free-1-250-0,l:1500,e_delay:250-0.3-0.3');
79                         //*/
80                        
81                         /* if you want to create .wav file...
82                         ssGen.addEventListener(AudioProgressEvent.ON_DATA,onSoundDataCreated);
83                         ssGen.generateByString('l:1000,g_sine:440',false);
84                         //*/
85                 }
86                 private function onSound(e:AudioEvent):void
87                 {
88                         e.sound.play();
89                 }
90
91                 private function onSoundDataCreated(e:AudioProgressEvent):void
92                 {
93                         //convert samples(array of Sample) to .wav format
94                         var converter:Wav=new Wav();
95                         var wavData:ByteArray=converter.encode(e.data,e.setting);
96                        
97                         //and you can save .wav file to server or local...etc.
98                 }
99
100         }
101        
102 }
Note: リポジトリブラウザについてのヘルプは TracBrowser を参照してください。