My favorites | Sign in
Logo
                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
package
{
import flash.text.TextFieldAutoSize;
import flash.text.TextFormat;
import flash.display.Bitmap;
import flash.display.BitmapData;
import flash.display.Sprite;
import flash.display.StageAlign;
import flash.display.StageQuality;
import flash.display.StageScaleMode;
import flash.events.Event;
import flash.filters.BlurFilter;
import flash.geom.Point;
import flash.media.Sound;
import flash.media.SoundMixer;
import flash.net.URLRequest;
import flash.text.TextField;
import flash.utils.ByteArray;

/**
* @author mrdoob
*/
[SWF(width = "1024", height = "768", backgroundColor = "#000000", frameRate = "30")]

public class Main extends Sprite
{
private var h : int = 60;
private var distortX : Number = 1.17;

private var message : TextField;

private var container : Sprite;

private var line : Sprite;
private var linesB : Array;
private var linesBD : Array;

private var fft : ByteArray;
private var fftBD : BitmapData;

private var filter : Array;

private var count : int = -1;


public function Main()
{
addEventListener(Event.ADDED_TO_STAGE, init);
}

public function init(e : Event) : void
{
removeEventListener(Event.ADDED_TO_STAGE, init);

stage.quality = StageQuality.HIGH;
stage.align = StageAlign.TOP_LEFT;
stage.scaleMode = StageScaleMode.NO_SCALE;
stage.addEventListener(Event.RESIZE, onStageResize);

var sound : Sound = new Sound(new URLRequest("01. Joy Division - Disorder.mp3"));
sound.play();

container = new Sprite();
addChild(container);

linesB = new Array();
linesBD = new Array();

for (var i : int = 0; i < 100; i++)
{
linesBD[i] = new BitmapData(300, h + 1, true, 0x00000000);
linesB[i] = new Bitmap(linesBD[i]);
linesB[i].y = (i * 4) - h;
container.addChild(linesB[i]);
}

line = new Sprite();

fft = new ByteArray();
fftBD = new BitmapData(256, 3, false, 0x000000);

filter = new Array();

for (i = 0; i < 256; i++)
filter[i] = Math.pow( 1 - (Math.abs(i-128) / 128) , 4) * 2;

message = new TextField();
message.defaultTextFormat = new TextFormat("sans",10,0xffffff);
message.text = "wops! I can't access the audio, maybe you have a tab open with another flash playing?";
message.autoSize = TextFieldAutoSize.LEFT;
message.mouseEnabled = false;
message.selectable = false;
message.visible = false;
addChild(message);

onStageResize();
addEventListener(Event.ENTER_FRAME, onEnterFrame);
}

private function onEnterFrame(e : Event) : void
{
// .. Audio?

if (SoundMixer.areSoundsInaccessible())
{
message.visible = true;
return;
}
else
{
message.visible = false;
}

SoundMixer.computeSpectrum(fft, true, 4);

fftBD.scroll(0, -1);

for (var i : int = 0; i < 256; i++)
{
fft.position = 512 - (i * 4);
fftBD.setPixel(i, 2, fft.readFloat() * filter[i] * 0xff + (Math.random() * 0x10) );
}

fftBD.applyFilter(fftBD, fftBD.rect, new Point(), new BlurFilter(16, 16));

drawLine();

// .. Spread

for (i = 0; i < 99; i++)
linesB[i].bitmapData = linesB[i+1].bitmapData;

// .. Copy graphic to BitmapData

count ++;
count %= 100;

linesBD[count].fillRect(linesBD[99].rect, 0x00000000);
linesBD[count].draw(line);

linesB[99].bitmapData = linesBD[count];
}

private function drawLine() : void
{
line.graphics.clear();

// .. Draw Bg

line.graphics.beginFill( 0x000000 );
line.graphics.moveTo(0, h - ((fftBD.getPixel(i, 2) / 0xff) * h));

for (var i : int = 0; i < 256; i ++)
line.graphics.lineTo(i * distortX, h - ((fftBD.getPixel(i, 2) / 0xff) * h));

line.graphics.lineTo(256 * distortX, h + 1);
line.graphics.lineTo(0, h + 1);
line.graphics.endFill();

// .. Draw Line

line.graphics.lineStyle( 0, 0xC0C0C0 );
line.graphics.moveTo(Math.random() * distortX, h - .5 - ((fftBD.getPixel(0, 2) / 0xff) * h));

for (i = 1; i < 256; i ++)
line.graphics.lineTo((i + Math.random()) * distortX, h - .5 - ((fftBD.getPixel(i, 2) / 0xff) * h));

line.graphics.lineStyle();
}

public function onStageResize( e : Event = null ) : void
{
container.x = (stage.stageWidth >> 1) - 150;
container.y = (stage.stageHeight >> 1) - 200;

message.x = (stage.stageWidth >> 1) - (message.width >> 1);
message.y = (stage.stageHeight >> 1);
}
}
}
Show details Hide details

Change log

r172 by i...@mrdoob.com on Apr 18, 2009   Diff
@all
more refactoring
Go to: 
Project members, sign in to write a code review

Older revisions

r156 by i...@mrdoob.com on Mar 16, 2009   Diff
@av
- saville updated

@webcam
- added laughing man
...
r155 by i...@mrdoob.com on Mar 08, 2009   Diff
@experiments
- saville AV
All revisions of this file

File info

Size: 4483 bytes, 180 lines
Hosted by Google Code