開発環境 |
Apache Flex SDK 4.12.1 |
|
FlashDevelop 4.6.2.5 |
実行環境 |
Microsoft Windows 8.1 (64bit) |
プロジェクトの種類 |
ActionScript 3/AS3 Project |
プロジェクト名 |
WaveFormFM |
Main.as
package
{
import flash.display.Graphics;
import flash.display.Sprite;
import flash.events.Event;
import flash.events.MouseEvent;
import flash.events.SampleDataEvent;
import flash.media.Sound;
import flash.media.SoundChannel;
import flash.media.SoundTransform;
import flash.text.Font;
import flash.text.TextField;
import flash.text.TextFieldAutoSize;
import flash.text.TextFormat;
import mx.utils.StringUtil;
public class Main extends Sprite
{
private const PI2:Number = Math.PI * 2;
private const SR:int = 44100; // sampling rate
private const WIDTH:int = 500;
private const HEIGHT:int = 50;
private const C:int = 220; // キャリア周波数
private const M:int = 440; // モジュレータ周波数
private var beta:Number;
private var sound:Sound = new Sound;
private var soundChannel:SoundChannel = null;
public function Main():void
{
if (stage) init();
else addEventListener(Event.ADDED_TO_STAGE, init);
}
private function init(e:Event = null):void
{
removeEventListener(Event.ADDED_TO_STAGE, init);
// entry point
var textFormat:TextFormat = new TextFormat(null, 20);
var tf:TextField = new TextField;
tf.x = 10;
tf.y = 10;
tf.autoSize = TextFieldAutoSize.LEFT;
tf.border = true;
tf.defaultTextFormat = textFormat;
tf.text = StringUtil.substitute("C = {0}Hz, M = {1}Hz", C, M);
addChild(tf);
//
const betalist:Array = [ 0.1, 1, 10, 100 ]; // 変調指数
for (var r:int = 0; r < betalist.length; r++)
{
tf = new TextField;
tf.x = 10;
tf.y = 50 + (HEIGHT * 2 + 10) * r;
tf.width = 100;
tf.height = HEIGHT * 2;
tf.border = true;
tf.defaultTextFormat = textFormat;
tf.text = "β=" + betalist[r];
addChild(tf);
var sp:Sprite = new Sprite;
sp.name = betalist[r];
sp.x = 120;
sp.y = tf.y;
sp.addEventListener(MouseEvent.MOUSE_DOWN, onMouseDown);
sp.addEventListener(MouseEvent.MOUSE_UP, onMouseUp);
addChild(sp);
var g:Graphics = sp.graphics;
g.clear();
g.beginFill(0xeeeeee);
g.drawRect(0, 0, WIDTH, HEIGHT * 2);
g.endFill();
g.lineStyle(1);
g.moveTo(WIDTH, HEIGHT);
g.lineTo(0, HEIGHT);
g.lineStyle(1, 0xff0000);
var wf:Array = generate(betalist[r]);
for (var i:int = 0; i < WIDTH; i++)
{
g.lineTo(i, HEIGHT - HEIGHT * wf[i]);
}
}
sound.addEventListener(SampleDataEvent.SAMPLE_DATA, onSampleData);
}
private function generate(b:Number):Array
{
var wf:Array = new Array(WIDTH);
for (var i:int = 0; i < WIDTH; i++)
{
var t:Number = i / SR;
wf[i] = Math.sin(PI2 * C * t + b * Math.sin(PI2 * M * t));
}
return wf;
}
private function onSampleData(e:SampleDataEvent):void
{
for (var i:int = 0; i < 8192; i++)
{
var t:Number = (e.position + i) / SR;
var value:Number = Math.sin(PI2 * C * t + beta * Math.sin(PI2 * M * t));
e.data.writeFloat(value);
e.data.writeFloat(value);
}
}
private function onMouseDown(e:MouseEvent):void
{
beta = parseFloat(e.target.name);
var st:SoundTransform = new SoundTransform;
st.volume = 0.2;
soundChannel = sound.play(0, 0, st);
}
private function onMouseUp(e:MouseEvent):void
{
if (soundChannel != null)
{
soundChannel.stop();
soundChannel = null;
}
}
}
}
最終更新:2014年06月27日 14:14