forked from corbanbrook/dsp.js
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrfft.html
More file actions
84 lines (70 loc) · 2.61 KB
/
rfft.html
File metadata and controls
84 lines (70 loc) · 2.61 KB
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
<html>
<head>
<script language="javascript" src="js/processing.js"></script>
<script language="javascript" src="js/init.js"></script>
<script language="javascript" src="../dsp.js"></script>
</head>
<body>
<style>
body { background: black; margin:0; padding:0;}
</style>
<script>
var frameBufferSize = 4096;
var bufferSize = frameBufferSize/2;
var signal = new Float32Array(bufferSize);
var peak = new Float32Array(bufferSize);
var fft = new RFFT(bufferSize, 44100);
function loadedMetaData(event) {
var audio = document.getElementById('input');
audio.mozFrameBufferLength = frameBufferSize;
audio.addEventListener("MozAudioAvailable", audioAvailable, false);
}
function audioAvailable(event) {
// deinterleave and mix down to mono
signal = DSP.getChannel(DSP.MIX, event.frameBuffer);
// perform forward transform
fft.forward(signal);
// calculate peak values
for ( var i = 0; i < bufferSize; i++ ) {
fft.spectrum[i] *= -1 * Math.log((fft.bufferSize/2 - i) * (0.5/fft.bufferSize/2)) * fft.bufferSize; // equalize, attenuates low freqs and boosts highs
if ( peak[i] < fft.spectrum[i] ) {
peak[i] = fft.spectrum[i];
} else {
peak[i] *= 0.99; // peak slowly falls until a new peak is found
}
}
}
</script>
<script target="#fft" type="application/processing">
void setup() {
size(1400, 600);
colorMode(HSB, 360, 100, 100);
strokeWeight(3);
frameRate(60);
//strokeCap(SQUARE);
}
void draw() {
background(0);
//fill(0, 60);
//rect(0, 0, width, height);
for ( int i = 0; i < fft.spectrum.length/2; i += 3 ) {
if (3 * i > width) { break; }
var magnitude = fft.spectrum[i];
// draw magnitudes
stroke((i) % 360, 60, constrain(magnitude * 6, 20, 100));
line(3*i, height, 3*i, height - magnitude * 16);
// draw peak indicators
stroke((i) % 360, 60, constrain(magnitude * 100, 50, 100));
line(3*i, height - peak[i] * 16 - 1, 3*i, height - peak[i] * 16);
}
/*
if (frameCount % 100 == 0 ) {
println(FRAME_RATE);
}
*/
}
</script>
<audio id="input" src="audio/corban-peddle.ogg" controls="true" onloadedmetadata="loadedMetaData(event);"></audio><br />
<div><canvas id="fft"></canvas></div>
</body>
</html>