-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
204 lines (175 loc) · 5.11 KB
/
index.html
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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
<!DOCTYPE html>
<meta charset="utf-8">
<style>
.node {
stroke: #fff;
stroke-width: 1.5px;
}
.link {
stroke: #999;
stroke-opacity: .6;
}
</style>
<body>
<script src="timbre/timbre.js"></script>
<script src="timbre/jsmad.js"></script>
<script src="timbre/mp3_decode.js"></script>
<script src="d3/d3.js"></script>
<script>
var width = 1280,
height = 720;
var array = new Array(128);
var array2 = new Array(128);
var scale = d3.scale.linear()
.domain([-10, 30])
.range([0, 100])
.clamp(true);
var color = d3.scale.category10();
var force = d3.layout.force()
.charge(-120)
.linkDistance(30)
.size([width, height]);
var svg = d3.select("body").append("svg")
.attr("width", width)
.attr("height", height);
var gseli = 128;
var rects = svg.selectAll("rect")
.data(array)
.enter()
.append("rect")
.attr("x", function(d, i){
return i*width/128;
})
.attr("y", 600)
.attr("width", width/128-1)
.attr("height", 100)
.style("fill", function(d, i){
return color(i);
});
var rects2 = svg.selectAll("rect2")
.data(array2)
.enter()
.append("rect")
.attr("x", function(d, i){
return i*width/128;
})
.attr("y", 0)
.attr("width", width/128-1)
.attr("height", 0)
.style("fill", function(d, i){
return color(i);
});
function updateRects(seli){
rects.transition().duration(100)
.attr("x", function(d, i){
var selwidth = 5*width/128;
var otherwidth = (width-selwidth)/127;
if(i <= seli) return i*otherwidth;
else
return (i-1)*otherwidth+selwidth;
})
.attr("width", function(d, i){
var selwidth = 5*width/128 - 1;
var otherwidth = (width-selwidth+1)/127-1;
if(seli == i) return selwidth;
else
return otherwidth;
})
.attr("y", function(d, i){
if(seli == i) return 600-5*scale(fft.spectrum[i]);
else
return 600-scale(fft.spectrum[i]);
})
.attr("height", function(d, i){
if(seli == i) return 5*scale(fft.spectrum[i]);
else
return scale(fft.spectrum[i]);
})
.attr("opacity", function(d, i){
if(seli == i || seli == 128) return 1;
else return 0.3;
});
}
function updateRects2(seli){
rects2.transition().duration(100)
.attr("x", function(d, i){
var selwidth = 5*width/128;
var otherwidth = (width-selwidth)/127;
if(i <= seli) return i*otherwidth;
else
return (i-1)*otherwidth+selwidth;
})
.attr("width", function(d, i){
var selwidth = 5*width/128 - 1;
var otherwidth = (width-selwidth+1)/127-1;
if(seli == i) return selwidth;
else
return otherwidth;
});
}
rects.on("mouseover", function(d, i){
gseli = i;
updateRects(i);
updateRects2(i);
})
.on("mouseout", function(d, i){
gseli = 128;
updateRects(128);
updateRects2(128);
});
rects.append("title").text(function(d, i){
return "period"+i;
})
var txt = svg.append("text")
.attr("x", 400)
.attr("y", 650)
.attr("font-family", "Consolas")
.attr("font-size", "30px")
.attr("fill", "black");
var txt2 = svg.append("text")
.text("Playing: 夜桜街道 (instrumental)")
.attr("x", 900)
.attr("y", 650)
.attr("font-family", "Consolas")
.attr("font-size", "20px")
.attr("fill", "gray");
function changetxt(i, data){
txt.text(function(d){
return "period"+i+": "+data;
})
}
var wav = T("audio", {loop:true}).loadthis("/resource/test.mp3", function() {
this.play();
}).on("ended", function() {
this.pause();
});
var fft = T("spectrum", {size:512, interval:100}).on("data", function() {
array = fft.spectrum;
rects.transition().duration(100)
.attr("y", function(d, i){
if(gseli == 128) {
txt.text(function(d){
return "No period selected!";
})
}
if(gseli == i) {
changetxt(i, fft.spectrum[i]);
return 600-5*scale(fft.spectrum[i]);}
else
return 600-scale(fft.spectrum[i]);
})
.attr("height", function(d, i){
if(gseli == i) return 5*scale(fft.spectrum[i]);
else
return scale(fft.spectrum[i]);
});
}).listen(wav);
var fft2 = T("spectrum", {size:512, interval:500}).on("data", function() {
rects2.transition().duration(500).ease("bounce")
.attr("y", 0)
.attr("height", function(d, i){
if(scale(fft.spectrum[i]) > 70) return 100;
return 0;
});
}).listen(wav);
</script>