| 1 |
/* |
|---|
| 2 |
* -------------------------------------- |
|---|
| 3 |
* sazameki -- audio manipulating library |
|---|
| 4 |
* http://sazameki.org/ |
|---|
| 5 |
* -------------------------------------- |
|---|
| 6 |
* |
|---|
| 7 |
* - developed by Takaaki Yamazaki |
|---|
| 8 |
* http://www.zkdesign.jp/ |
|---|
| 9 |
* - supported by Spark project |
|---|
| 10 |
* http://www.libspark.org/ |
|---|
| 11 |
*/ |
|---|
| 12 |
|
|---|
| 13 |
/* |
|---|
| 14 |
* Licensed under the MIT License |
|---|
| 15 |
* |
|---|
| 16 |
* Copyright (c) 2008 Takaaki Yamazaki |
|---|
| 17 |
* |
|---|
| 18 |
* Permission is hereby granted, free of charge, to any person obtaining a copy |
|---|
| 19 |
* of this software and associated documentation files (the "Software"), to deal |
|---|
| 20 |
* in the Software without restriction, including without limitation the rights |
|---|
| 21 |
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
|---|
| 22 |
* copies of the Software, and to permit persons to whom the Software is |
|---|
| 23 |
* furnished to do so, subject to the following conditions: |
|---|
| 24 |
* |
|---|
| 25 |
* The above copyright notice and this permission notice shall be included in |
|---|
| 26 |
* all copies or substantial portions of the Software. |
|---|
| 27 |
* |
|---|
| 28 |
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
|---|
| 29 |
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
|---|
| 30 |
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
|---|
| 31 |
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
|---|
| 32 |
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
|---|
| 33 |
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
|---|
| 34 |
* THE SOFTWARE. |
|---|
| 35 |
*/ |
|---|
| 36 |
|
|---|
| 37 |
package { |
|---|
| 38 |
import flash.display.Sprite; |
|---|
| 39 |
import flash.media.Sound; |
|---|
| 40 |
import flash.utils.ByteArray; |
|---|
| 41 |
import org.sazameki.audio.core.AudioSetting; |
|---|
| 42 |
import org.sazameki.audio.core.Sample; |
|---|
| 43 |
import org.sazameki.audio.core.SoundFactory; |
|---|
| 44 |
import org.sazameki.audio.engine.ssGenerator.engine.EngineMax; |
|---|
| 45 |
import org.sazameki.audio.engine.ssGenerator.engine.ISsEngine; |
|---|
| 46 |
import org.sazameki.audio.engine.ssGenerator.SsGenerator; |
|---|
| 47 |
import org.sazameki.audio.events.AudioEvent; |
|---|
| 48 |
import org.sazameki.audio.events.AudioProgressEvent; |
|---|
| 49 |
import org.sazameki.audio.format.Wav; |
|---|
| 50 |
|
|---|
| 51 |
/** |
|---|
| 52 |
* sazameki examples |
|---|
| 53 |
* @author Takaaki Yamazaki |
|---|
| 54 |
*/ |
|---|
| 55 |
public class Example extends Sprite{ |
|---|
| 56 |
|
|---|
| 57 |
public function Example() |
|---|
| 58 |
{ |
|---|
| 59 |
sazamekiCoreSample(); |
|---|
| 60 |
//ssGeneratorSample(); |
|---|
| 61 |
|
|---|
| 62 |
} |
|---|
| 63 |
|
|---|
| 64 |
private function sazamekiCoreSample():void |
|---|
| 65 |
{ |
|---|
| 66 |
//---------------------------------------------------- |
|---|
| 67 |
// sazameki core(org.sazameki.audio.core) |
|---|
| 68 |
// 音の変換などの基本的な機能を提供するクラス。 |
|---|
| 69 |
|
|---|
| 70 |
//convert signals(array of org.sazameki.core.Sample) to flash.media.Sound object |
|---|
| 71 |
var samples:Array=makeNoiseSamples(); |
|---|
| 72 |
var audioSetting:AudioSetting=new AudioSetting(2,44100,16); |
|---|
| 73 |
var factory:SoundFactory=new SoundFactory(); |
|---|
| 74 |
factory.addEventListener(AudioEvent.COMPLETE,onCoreSoundCreated); |
|---|
| 75 |
factory.generateSound(samples,audioSetting); |
|---|
| 76 |
} |
|---|
| 77 |
|
|---|
| 78 |
private function makeNoiseSamples():Array{ |
|---|
| 79 |
//simply generate 1sec(44100samples) noise |
|---|
| 80 |
var arr:Array=new Array(); |
|---|
| 81 |
|
|---|
| 82 |
for(var i:int=0;i<44100;i++){ |
|---|
| 83 |
arr.push(new Sample(Math.random()*0.5,Math.random()*0.5)); |
|---|
| 84 |
} |
|---|
| 85 |
|
|---|
| 86 |
return arr; |
|---|
| 87 |
} |
|---|
| 88 |
private function onCoreSoundCreated(e:AudioEvent):void |
|---|
| 89 |
{ |
|---|
| 90 |
e.sound.play(); |
|---|
| 91 |
} |
|---|
| 92 |
|
|---|
| 93 |
|
|---|
| 94 |
private function ssGeneratorSample():void |
|---|
| 95 |
{ |
|---|
| 96 |
//---------------------------------------------------- |
|---|
| 97 |
// SsGenerator(org.sazameki.audio.engine.ssGenerator) : short sound generator |
|---|
| 98 |
// 音を定義した文字列に基づいて、flash.media.Soundオブジェクトを生成して返す。 |
|---|
| 99 |
|
|---|
| 100 |
//prepare SsEngine(always required!) |
|---|
| 101 |
var engine:ISsEngine=new EngineMax(); |
|---|
| 102 |
//create ssGenerator instance |
|---|
| 103 |
var ssGen:SsGenerator=new SsGenerator(engine); |
|---|
| 104 |
//add listener |
|---|
| 105 |
ssGen.addEventListener(AudioEvent.COMPLETE,onSound); |
|---|
| 106 |
|
|---|
| 107 |
//generate sound |
|---|
| 108 |
|
|---|
| 109 |
/* simply generate 1sec sine wave |
|---|
| 110 |
ssGen.generateByString('l:1000,g_sine:440'); |
|---|
| 111 |
//*/ |
|---|
| 112 |
|
|---|
| 113 |
//* a little complex sound |
|---|
| 114 |
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'); |
|---|
| 115 |
//*/ |
|---|
| 116 |
|
|---|
| 117 |
/* if you want to create .wav file... |
|---|
| 118 |
ssGen.addEventListener(AudioProgressEvent.ON_DATA,onSoundDataCreated); |
|---|
| 119 |
ssGen.generateByString('l:1000,g_sine:440',false); |
|---|
| 120 |
//*/ |
|---|
| 121 |
} |
|---|
| 122 |
private function onSound(e:AudioEvent):void |
|---|
| 123 |
{ |
|---|
| 124 |
e.sound.play(); |
|---|
| 125 |
} |
|---|
| 126 |
|
|---|
| 127 |
private function onSoundDataCreated(e:AudioProgressEvent):void |
|---|
| 128 |
{ |
|---|
| 129 |
//convert samples(array of Sample) to .wav format |
|---|
| 130 |
var converter:Wav=new Wav(); |
|---|
| 131 |
var wavData:ByteArray=converter.encode(e.data,e.setting); |
|---|
| 132 |
|
|---|
| 133 |
//and you can save .wav file to server or local...etc. |
|---|
| 134 |
} |
|---|
| 135 |
|
|---|
| 136 |
} |
|---|
| 137 |
|
|---|
| 138 |
} |
|---|