-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathApp.hx
165 lines (137 loc) · 5.04 KB
/
App.hx
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
import js.Browser.document;
import js.Browser.window;
import js.html.CanvasElement;
import js.html.CanvasRenderingContext2D;
import js.html.audio.AnalyserNode;
import js.html.audio.AudioContext;
import js.html.audio.GainNode;
import js.lib.Promise;
import js.lib.Uint8Array;
enum abstract NoiseType(String) from String to String {
var white;
var pink;
var brown;
}
var color = { bg: "#000", fg: "#fff" };
var audio : AudioContext;
var gain : GainNode;
var noise : NoiseType;
var analyser : AnalyserNode;
var noises : Map<NoiseType,Dynamic>;
var activeNoises : Array<NoiseType> = [];
var canvas : CanvasElement;
var ctx : CanvasRenderingContext2D;
var freqs : Uint8Array;
var times : Uint8Array;
var animationFrameId : Int;
function update( time : Float ) {
animationFrameId = window.requestAnimationFrame( update );
analyser.getByteFrequencyData( freqs );
analyser.getByteTimeDomainData( times );
var v : Float;
var hw = canvas.width/2, hh = canvas.height/2;
ctx.clearRect( 0, 0, canvas.width, canvas.height );
ctx.strokeStyle = noise;
ctx.beginPath();
for( i in 0...analyser.fftSize ) {
v = i * (Math.PI/2)/180;
ctx.lineTo(hw + Math.cos(v) * ( 100 + times[i] ), hh + Math.sin(v) * ( 100 + times[i]) );
}
ctx.stroke();
}
function handleWindowResize(e) {
canvas.width = window.innerWidth;
canvas.height = window.innerHeight;
}
function initAudio() : Promise<Map<NoiseType,Dynamic>> {
if( noises != null )
return Promise.resolve( noises );
audio = new AudioContext();
gain = audio.createGain();
gain.gain.value = 1.0;
gain.connect( audio.destination );
analyser = audio.createAnalyser();
//analyser.smoothingTimeConstant = SMOOTHING;
//analyser.fftSize = 1024;
//analyser.minDecibels = -140;
//analyser.maxDecibels = 0;
analyser.connect( gain );
freqs = new Uint8Array( analyser.frequencyBinCount );
times = new Uint8Array( analyser.frequencyBinCount );
function addWorklet( name : String ) {
return untyped audio.audioWorklet.addModule('$name.js').then( r -> {
return js.Syntax.code("new AudioWorkletNode({0},{1})", audio, name );
});
}
return Promise.all([
addWorklet('white-noise-processor'),
addWorklet('pink-noise-processor'),
addWorklet('brown-noise-processor'),
]).then( r -> {
return noises = [white => r[0], pink => r[1], brown => r[2]];
});
/*
var noiseBufferSize = 4096;
var whiteNoise = audio.createScriptProcessor( noiseBufferSize, 1, 1 );
whiteNoise.onaudioprocess = e -> {
Noise.generateWhiteNoise( e.outputBuffer.getChannelData(0), noiseBufferSize );
};
var brownNoise = audio.createScriptProcessor( noiseBufferSize, 1, 1 );
brownNoise.onaudioprocess = e -> {
Noise.generateBrownNoise( e.outputBuffer.getChannelData(0), noiseBufferSize );
};
var pinkNoise = audio.createScriptProcessor( noiseBufferSize, 1, 1 );
pinkNoise.onaudioprocess = e -> {
Noise.generatePinkNoise( e.outputBuffer.getChannelData(0), noiseBufferSize );
};
*/
}
function main() {
window.onload = () -> {
canvas = cast document.getElementById( 'spectrum' );
canvas.width = window.innerWidth;
canvas.height = window.innerHeight;
ctx = canvas.getContext2d();
ctx.fillStyle = color.bg;
ctx.strokeStyle = color.fg;
var noiseTypes = [white, brown, pink];
for( type in noiseTypes ) {
var e = document.getElementById( type );
e.onclick = _ -> {
initAudio().then( (noises:Map<NoiseType,Dynamic>) -> {
if( animationFrameId == null ) {
animationFrameId = window.requestAnimationFrame( update );
}
var n = noises.get( noise = type );
if( e.classList.contains('active') ) {
e.classList.remove( 'active' );
n.disconnect();
} else {
e.classList.add( 'active' );
n.connect( analyser );
}
});
}
}
var volumeElement = document.getElementById("volume");
var timer = new haxe.Timer( 2000 );
window.addEventListener( 'wheel', e -> {
if( gain != null ) {
if( e.deltaY < 0 ) {
gain.gain.value += 0.1;
gain.gain.value = Math.min( gain.gain.value, 1.0 );
} else {
gain.gain.value -= 0.1;
gain.gain.value = Math.max( gain.gain.value, 0.0 );
}
volumeElement.textContent = 'VOL:'+Std.int(gain.gain.value * 100);
timer.run = () -> {
volumeElement.textContent = 'Noise';
timer.stop();
timer = new haxe.Timer( 2000 );
}
}
}, false );
window.addEventListener( 'resize', handleWindowResize, false );
}
}