/* * -------------------------------------- * sazameki -- audio manipulating library * http://sazameki.org/ * -------------------------------------- * * - developed by Takaaki Yamazaki * http://www.zkdesign.jp/ * - supported by Spark project * http://www.libspark.org/ */ /* * Licensed under the MIT License * * Copyright (c) 2008 Takaaki Yamazaki * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal * in the Software without restriction, including without limitation the rights * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell * copies of the Software, and to permit persons to whom the Software is * furnished to do so, subject to the following conditions: * * The above copyright notice and this permission notice shall be included in * all copies or substantial portions of the Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN * THE SOFTWARE. */ package { import flash.display.Sprite; import flash.media.Sound; import flash.utils.ByteArray; import org.sazameki.audio.core.AudioSetting; import org.sazameki.audio.core.Sample; import org.sazameki.audio.core.SoundFactory; import org.sazameki.audio.engine.ssGenerator.engine.EngineMax; import org.sazameki.audio.engine.ssGenerator.engine.ISsEngine; import org.sazameki.audio.engine.ssGenerator.SsGenerator; import org.sazameki.audio.events.AudioEvent; import org.sazameki.audio.events.AudioProgressEvent; import org.sazameki.audio.format.Wav; /** * sazameki examples * @author Takaaki Yamazaki */ public class Example extends Sprite{ public function Example() { sazamekiCoreSample(); //ssGeneratorSample(); } private function sazamekiCoreSample():void { //---------------------------------------------------- // sazameki core(org.sazameki.audio.core) // 音の変換などの基本的な機能を提供するクラス。 //convert signals(array of org.sazameki.core.Sample) to flash.media.Sound object var samples:Array=makeNoiseSamples(); var audioSetting:AudioSetting=new AudioSetting(2,44100,16); var factory:SoundFactory=new SoundFactory(); factory.addEventListener(AudioEvent.COMPLETE,onCoreSoundCreated); factory.generateSound(samples,audioSetting); } private function makeNoiseSamples():Array{ //simply generate 1sec(44100samples) noise var arr:Array=new Array(); for(var i:int=0;i<44100;i++){ arr.push(new Sample(Math.random()*0.5,Math.random()*0.5)); } return arr; } private function onCoreSoundCreated(e:AudioEvent):void { e.sound.play(); } private function ssGeneratorSample():void { //---------------------------------------------------- // SsGenerator(org.sazameki.audio.engine.ssGenerator) : short sound generator // 音を定義した文字列に基づいて、flash.media.Soundオブジェクトを生成して返す。 //prepare SsEngine(always required!) var engine:ISsEngine=new EngineMax(); //create ssGenerator instance var ssGen:SsGenerator=new SsGenerator(engine); //add listener ssGen.addEventListener(AudioEvent.COMPLETE,onSound); //generate sound /* simply generate 1sec sine wave ssGen.generateByString('l:1000,g_sine:440'); //*/ //* a little complex sound 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'); //*/ /* if you want to create .wav file... ssGen.addEventListener(AudioProgressEvent.ON_DATA,onSoundDataCreated); ssGen.generateByString('l:1000,g_sine:440',false); //*/ } private function onSound(e:AudioEvent):void { e.sound.play(); } private function onSoundDataCreated(e:AudioProgressEvent):void { //convert samples(array of Sample) to .wav format var converter:Wav=new Wav(); var wavData:ByteArray=converter.encode(e.data,e.setting); //and you can save .wav file to server or local...etc. } } }