-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwaveform-visualizer.js
More file actions
142 lines (113 loc) · 3.19 KB
/
waveform-visualizer.js
File metadata and controls
142 lines (113 loc) · 3.19 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
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
var SAMPLES_PER_SECOND = 60; // Number to samples per second for processing
var sampled;
var peak = 1;
var audioCtx, audio, source;
init();
getData('03_Ikram_Choudhury_-_Dance_World_Pineapple_Zone.mp3', onLoaded);
function init() {
audioCtx = new AudioContext();
// var source = audioCtx.createBufferSource();
audio = document.createElement( 'audio' );
audio.controls = true;
audio.style.position = 'absolute';
audio.style.left = 'calc(50% - 150px)';
audio.style.bottom = '25px';
audio.style.transition = 'opacity 1s';
audio.play();
document.body.appendChild( audio );
source = audioCtx.createMediaElementSource(audio);
document.addEventListener('keydown', function(e) {
var pressed = true;
switch(e.keyCode) {
case 32:
if (audio.paused) {
audio.play()
} else {
audio.pause();
}
break;
case 37:
audio.currentTime -= 0.5;
break;
case 39:
audio.currentTime += 0.5;
break;
default:
pressed = false;
}
if (pressed) e.preventDefault();
});
}
function handleFileSelect(evt) {
evt.stopPropagation();
evt.preventDefault();
var files = evt.dataTransfer.files; // FileList object.
var reader = new FileReader();
reader.onload = function(event) {
loadAudioBuffer(event.target.result);
};
// files is a FileList of File objects. List some properties.
var output = [];
for (var i = 0, f; f = files[i]; i++) {
console.log(
f.name, 'type: ', f.type || 'n/a', ') - ',
f.size, ' bytes, last modified: ',
f.lastModifiedDate ? f.lastModifiedDate.toLocaleDateString() : 'n/a'
);
audio.src = URL.createObjectURL(f);
reader.readAsArrayBuffer(f);
}
}
function handleDragOver(evt) {
evt.stopPropagation();
evt.preventDefault();
evt.dataTransfer.dropEffect = 'copy'; // Explicitly show this is a copy.
}
function getData(url) {
var request = new XMLHttpRequest();
request.open('GET', url, true);
request.responseType = 'arraybuffer';
request.onload = function() {
var audioData = request.response;
loadAudioBuffer(audioData);
audio.src = url;
}
request.send();
}
function loadAudioBuffer(audioData) {
audioCtx.decodeAudioData(audioData, function(buffer) {
source.buffer = buffer;
source.connect(audioCtx.destination);
onLoaded(buffer.getChannelData(0), buffer.getChannelData(1), buffer.sampleRate);
});
}
function onLoaded(data, dataR, rate) {
var SAMPLES_PER_PICK = rate / SAMPLES_PER_SECOND;
sampled = new Float32Array(data.length / SAMPLES_PER_PICK | 0);
//// Finding peak for normalization
// peak = -Infinity;
// for (var i = 0; i < sampled.length; i++) {
// var abs = sampled[i];
// if (abs > peak) peak = abs;
// }
// Make Sample Data
for (var i = 0; i < sampled.length; i++) {
var max = -Infinity;
var min = Infinity;
var avg = 0;
var squaredSum = 0;
for (var j =0; j < SAMPLES_PER_PICK; j++ ) {
var s = Math.max(Math.abs(data[i * SAMPLES_PER_PICK + j]), Math.abs(data[i * SAMPLES_PER_PICK + j]));
max = Math.max(max, s);
min = Math.min(min, s);
avg += s;
squaredSum += s * s;
}
avg /= SAMPLES_PER_PICK;
// sampled[i] = avg;
// sampled[i] = max;
// sampled[i] = (max + min) / 2;
// sampled[i] = s;
sampled[i] = Math.sqrt(squaredSum / SAMPLES_PER_PICK);
}
}