-
Notifications
You must be signed in to change notification settings - Fork 45
/
Copy pathgraphtool.js
2515 lines (2268 loc) · 92.9 KB
/
graphtool.js
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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
let doc = d3.select(".graphtool");
doc.html(`
<svg style="display: none;">
<defs>
<g id="baseline-icon" text-anchor="middle" font-size="100px" fill="currentColor">
<text dominant-baseline="central" y="-57">BASE</text>
<text dominant-baseline="central" y="57">-LINE</text>
</g>
<g id="hide-icon">
<path d="M2 6Q7 0 12 6Q7 12 2 6Z" stroke-width="1" stroke="currentColor" fill="none"/>
<circle cx="7" cy="6" r="2" stroke="none" fill="currentColor"/>
<line stroke-width="1" x1="4.4" y1="10.3" x2="10.4" y2="2.3" class="keyBackground"/>
<line stroke-width="1" x1="3.6" y1= "9.7" x2= "9.6" y2="1.7" stroke="currentColor"/>
</g>
<g id="pin-icon" text-anchor="middle" font-size="100px" fill="currentColor">
<text dominant-baseline="central">
PIN
</text>
</g>
</defs>
</svg>
<main class="main">
<section class="parts-primary">
<div class="graphBox" data-sticky-graph="`+ alt_sticky_graph +`" data-animated="`+ alt_animated +`">
<div class="graph-sizer">
<svg id="fr-graph" viewBox="0 0 800 346" data-labels-position="`+ labelsPosition +`"></svg>
</div>
<div class="tools collapseTools">
<div class="copy-url">
<button id="copy-url">Copy URL</button>
<button id="download-faux">Screenshot</button>
</div>
<div class="zoom">
<span>Zoom:</span>
<button>Bass</button>
<button>Mids</button>
<button>Treble</button>
</div>
<div class="normalize">
<span>Normalize:</span>
<div>
<input type="number" inputmode="decimal" id="norm-phon" required min="0" max="100" value="`+ default_norm_db +`" step="1" onclick="this.focus();this.select()"></input>
<span>dB</span>
</div>
<div>
<input type="number" inputmode="decimal" id="norm-fr" required min="20" max="20000" value="`+ default_norm_hz +`" step="1" onclick="this.focus();this.select()"></input>
<span>Hz</span>
</div>
<span class="helptip">
?<span>Choose a dB value to normalize to a target listening level, or a Hz value to make all curves match at that frequency.</span>
</span>
</div>
<div class="smooth">
<span>Smooth:</span>
<input type="number" inputmode="decimal" id="smooth-level" required min="0" value="5" step="any" onclick="this.focus();this.select()"></input>
</div>
<div class="miscTools">
<button id="inspector"><span>╞</span> inspect</button>
<button id="label"><span>▭</span> label</button>
<button id="download"><span><u>⇩</u></span> screenshot</button>
<button id="recolor"><span>○</span> recolor</button>
</div>
<div class="expand-collapse">
<button id="expand-collapse"></button>
</div>
<svg id="expandTools" viewBox="0 0 14 12">
<path d="M2 2h10M2 6h10M2 10h10" stroke-width="2px" stroke="#878156" stroke-linecap="round" transform="translate(0,0.3)"/>
<path d="M2 2h10M2 6h10M2 10h10" stroke-width="2px" stroke="currentColor" stroke-linecap="round"/>
</svg>
</div>
</div>
<div class="manage">
<table class="manageTable">
<colgroup>
<col class="remove">
<col class="phoneId">
<col class="key">
<col class="calibrate">
<col class="baselineButton">
<col class="hideButton">
<col class="lastColumn">
</colgroup>
<tbody class="curves"></tbody>
<tr class="addPhone">
<td class="addButton">⊕</td>
<td class="helpText" colspan="5">(or middle/ctrl-click when selecting; or pin other IEMs)</td>
<td class="addLock">LOCK</td>
</tr>
<tr class="mobile-helper"></tr>
</table>
</div>
<div class="accessories"></div>
<div class="external-links"></div>
</section>
<section class="parts-secondary">
<div class="controls">
<div class="select" data-selected="models">
<div class="selector-tabs">
<button class="brands" data-list="brands">Brands</button>
<button class="models" data-list="models">Models</button>
</div>
<input class="search" type="text" inputmode="search" placeholder="Search" onclick="this.focus();this.select()"/>
<svg class="chevron" viewBox="0 0 12 8" preserveAspectRatio="none">
<path d="M0 0h4c0 1.5,5 3,7 4c-2 1,-7 2.5,-7 4h-4c0 -3,4 -3,4 -4s-4 -1,-4 -4"/>
</svg>
<svg class="stop" viewBox="0 0 4 1">
<path d="M4 1H0C3 1 3.2 0.8 4 0Z"/>
</svg>
<div class="scroll-container">
<div class="scrollOuter" data-list="brands"><div class="scroll" id="brands"></div></div>
<div class="scrollOuter" data-list="models"><div class="scroll" id="phones"></div></div>
</div>
</div>
</div>
</section>
</main>
`);
let pad = { l:15, r:15, t:10, b:36 };
let W0 = 800, W = W0 - pad.l - pad.r,
H0 = 360, H = H0 - pad.t - pad.b;
let gr = doc.select("#fr-graph"),
defs = gr.append("defs");
gr.append("rect").attrs({x:0, y:pad.t-8, width:W0, height:H0-22, rx:4,
"class":"graphBackground"});
watermark(gr);
// Scales
let x = d3.scaleLog()
.domain([20,20000])
.range([pad.l,pad.l+W]);
let yD = [29.5,85], // Decibels
yR = [pad.t+H,pad.t+10];
let y = d3.scaleLinear().domain(yD).range(yR);
// y axis
defs.append("filter").attr("id","blur").attr("filterUnits","userSpaceOnUse")
.attrs({x:-W-4,y:-2,width:W+8,height:4})
.append("feGaussianBlur").attr("in","SourceGraphic")
.attr("stdDeviation", 0.8);
let yAxis = d3.axisLeft(y).tickSize(W).tickSizeOuter(0).tickPadding(1);
function fmtY(ya) {
let d = y.domain(),
r = d[1] - d[0],
t = r<40 ? 1 : 5,
y0= Math.ceil (d[0]/t),
y1= Math.floor(d[1]/t),
isMinor = t===5 ? (()=>false) : ((_,i)=>(y0+i)%5!==0);
yAxis.tickValues(d3.range(y1-y0+1).map(i=>t*(y0+i)))(ya);
ya.select(".domain").remove();
ya.selectAll(".tick line")
.attr("stroke-linecap", "round")
.attrs((_,i) => {
let m = isMinor(_,i);
return {
filter: m ? null : "url(#blur)",
"stroke-width": m ? 0.2*(1-r/45) : 0.15*(1+45/r)
};
});
ya.selectAll(".tick text")
.attr("text-anchor","start")
.attr("x",-W+3)
.attr("dy",-2)
.filter(isMinor).attr("display","none");
}
let yAxisObj = gr.append("g")
.attr("transform", "translate("+(pad.l+W)+",0)")
.call(fmtY);
yAxisObj.insert("text")
.attr("transform","rotate(-90)")
.attr("fill","currentColor")
.attr("text-anchor","end")
.attr("y",-W-2).attr("x",-pad.t)
.text("dB");
// x axis
let xvals = [2,3,4,5,6,8,10,15];
let xAxis = d3.axisBottom(x)
.tickSize(H+3).tickSizeOuter(0)
.tickValues(d3.merge([1,2,3].map(e=>xvals.map(m=>m*Math.pow(10,e)))).concat([20000]))
.tickFormat(f => f>=1000 ? (f/1000)+"k" : f);
let tickPattern = [3,0,0,1,0,0,2,0],
getTickType = i => i===0 || i===3*8 ? 4 : tickPattern[i%8],
tickThickness = [2,4,4,9,15].map(t=>t/10);
function fmtX(xa) {
xAxis(xa);
(xa.selection ? xa.selection() : xa).select(".domain").remove();
xa.selectAll(".tick line")
.attr("stroke", "#333")
.attr("stroke-width", (_,i) => tickThickness[getTickType(i)]);
xa.selectAll(".tick text").filter((_,i) => tickPattern[i%8] === 0)
.attr("font-size","86%")
.attr("font-weight","lighter");
xa.select(".tick:last-of-type text")
.attr("dx",-5)
.text("20kHz");
xa.select(".tick:first-of-type text")
.attr("dx",4)
.text("20Hz");
}
defs.append("clipPath").attr("id","x-clip")
.append("rect").attrs({x:0, y:0, width:W0, height:H0});
let xAxisObj = gr.append("g")
.attr("clip-path", "url(#x-clip)")
.attr("transform", "translate(0,"+pad.t+")")
.call(fmtX);
// Plot line
defs.selectAll().data([0,1]).join("linearGradient")
.attrs({x1:0,y1:0, x2:1,y2:0})
.attr("id", i=>"grad"+i)
.selectAll().data(i=>[i,1-i]).join("stop")
.attr("offset",(_,i)=>i)
.attr("stop-color",j=>["black","white"][j]);
let fW = 7, // Fade width
fWm= 30; // Width at an interior edge
let fade = defs.append("mask")
.attr("id", "graphFade")
.attr("maskUnits", "userSpaceOnUse")
.append("g").attr("transform", "translate("+pad.l+","+pad.t+")");
fade.append("rect").attrs({ x:0, y:0, width:W, height:H, fill:"white" });
let fadeEdge = fade.selectAll().data([0,1]).join("rect")
.attrs(i=>({ x:i?W-fW:0, width:fW, y:0,height:H, fill:"url(#grad"+i+")" }));
let line = d3.line()
.x(d=>x(d[0]))
.y(d=>y(d[1]))
.curve(d3.curveNatural);
// Range buttons
let selectedRange = 3; // Full range
let ranges = [[20,400],[100,4000],[1000,20000], [20,20000]],
edgeWs = [[fW,fWm],[fWm,fWm],[fWm,fW],[fW,fW]];
let rangeSel = doc.select(".zoom").selectAll("button");
rangeSel.on("click", function (_,i) {
let r = selectedRange,
s = selectedRange = r===i ? 3 : i;
rangeSel.classed("selected", (_,j)=>j===s);
x.domain(ranges[s]);
// More time to go between bass and treble
let dur = Math.min(r,s)===0 && Math.max(r,s)===2 ? 1100 : 700;
clearLabels();
gpath.selectAll("path").transition().duration(dur).attr("d", drawLine);
let e = edgeWs[s];
fadeEdge.transition().duration(dur).attrs(i=>({x:i?W-e[i]:0, width:e[i]}));
xAxisObj.transition().duration(dur).call(fmtX);
});
// y-axis scaler
let dB = {
y: y(60),
h: 15,
H: y(60)-y(70),
min: pad.t,
max: pad.t+H,
tr: _ => "translate("+(pad.l-9)+","+dB.y+")"
};
dB.all = gr.append("g").attr("class","dBScaler"),
dB.trans = dB.all.append("g").attr("transform", dB.tr()),
dB.scale = dB.trans.append("g").attr("transform", "scale(1,1)");
dB.scale.selectAll().data([-1,1])
.join("path").attr("stroke","none")
.attr("d", function (s) {
function getPathPart(l) {
let v=l[0].toLowerCase()==="v";
for (let i=2-v; i<l.length; i+=2)
l[i] *= s;
return l[0]+l.slice(1).join(" ");
}
return [ ["M", 9.9,-1 ],
["V", dB.H],
["h",-1 ],
["l",-1 ,-1.5 ],
["l",-2.1, 2 ],
["h",-5.6 ],
["v", -1.5 ],
["q",7,2,8,-7 ],
["V", 29 ],
["c",1,-16,-10,-15,-10,-14],
["V", -1 ] ].map(getPathPart).join("");
});
dB.scale.selectAll().data([10,7,13])
.join("rect").attrs((d,i)=>({x:i*2.8,y:-d,width:0.8,height:2*d,fill:"#bbb"}));
function getDrag(fn) {
return d3.drag()
.on("drag",fn)
.on("start",function(){dB.all.classed("active",true );})
.on("end" ,function(){dB.all.classed("active",false);});
}
dB.mid = dB.all.append("rect")
.attrs({x:(pad.l-11),y:dB.y-dB.h,width:12,height:2*dB.h,opacity:0})
.call(getDrag(function () {
dB.y = d3.event.y;
dB.y = Math.min(dB.y, dB.max-dB.h*(dB.H/15));
dB.y = Math.max(dB.y, dB.min+dB.h*(dB.H/15));
d3.select(this).attr("y",dB.y-dB.h);
dB.trans.attr("transform", dB.tr());
dB.updatey();
}));
dB.circ = dB.trans.selectAll().data([-1,1]).join("circle")
.attrs({cx:5,cy:s=>dB.H*s,r:7,opacity:0})
.call(getDrag(function () {
let h = Math.max(30, Math.abs(d3.event.y));
h = Math.min(h, Math.min(dB.max-dB.y, dB.y-dB.min));
let sc = h/dB.H;
dB.circ.attr("cy",s=>h*s);
dB.scale.attr("transform", "scale(1,"+sc+")");
dB.h = 15*sc;
dB.mid.attrs({y:dB.y-dB.h,height:2*dB.h});
dB.updatey();
}));
let yCenter = 60;
dB.updatey = function (dom) {
let d = l => l[1]-l[0];
y.domain(yR.map(y=>yCenter+(y-dB.y)*(15/dB.h)*d(yD)/d(yR)));
yAxisObj.call(fmtY);
let getTr = o => o ? "translate(0,"+(y(o)-y(0))+")" : null;
clearLabels();
gpath.selectAll("path").call(redrawLine);
}
// Label drawing and screenshot
let getFullName = p => p.dispBrand+" "+p.dispName,
getChannelName = p => n => getFullName(p) + " ("+n+")";
let labelButton = doc.select("#label"),
labelsShown = false;
function setLabelButton(l) {
labelButton.classed("selected", labelsShown = l);
}
function clearLabels() {
gr.selectAll(".lineLabel").remove();
setLabelButton(false);
}
function drawLabels() {
let curves = d3.merge(
activePhones.filter(p=>!p.hide).map(p =>
p.isTarget||!p.samp||p.avg ? p.activeCurves
: LR.map((l,i) => ({
p:p, o:getO(i), id:getChannelName(p)(l), multi:true,
l:(n=>p.channels.slice(i*n,(i+1)*n))(sampnums.length)
.filter(c=>c!==null)
}))
)
);
if (!curves.length) return;
let bcurves = curves.slice(),
bp = baseline.p;
if (bp && bp.hide) {
bcurves.push({
p:bp, o:0,
id:"Baseline: "+(bp.isTarget?bp.fullName:getFullName(bp))
});
}
gr.selectAll(".lineLabel").remove();
let g = gr.selectAll(".lineLabel").data(bcurves)
.join("g").attr("class","lineLabel").attr("opacity", 0);
let t = g.append("text")
.attrs({x:0, y:0, fill:c=>getTooltipColor(c)})
.text(c=>c.id);
g.datum(function(){return this.getBBox();});
g.select("text").attrs(b=>({x:3-b.x, y:3-b.y}));
g.insert("rect", "text")
.attrs(b=>({x:2, y:2, width:b.width+2, height:b.height+2}));
let boxes = g.data(),
w = boxes.map(b=>b.width +6),
h = boxes.map(b=>b.height+6);
// Slice to fit in range
let r = x.domain().map(v => d3.bisectLeft(f_values, v));
rsl = a => a.slice(Math.max(r[0],0), r[1]+1);
let rf_values = rsl(f_values);
let v = curves.map(c => {
let o = getOffset(c.p);
return (c.multi?c.l:[c.l])
.map(l => rsl(baseline.fn(l).map(d=>d[1]+o)));
});
let tr;
if (curves.length === 1) {
let x0 = 50, y0 = 10,
sl = range_to_slice([0,w[0]], o=>x0+o),
e = d3.extent(d3.merge(v[0].map(sl)).map(y));
if (y0+h[0] >= e[0]) { y0 = Math.max(y0, e[1]); }
tr = [[x0,y0]];
} else {
let n = v.length;
let invd = (sc,d) => sc.invert(d)-sc.invert(0),
xr = x.range(),
yd = y.domain(),
wind = w => Math.ceil((w/(xr[1]-xr[0]))*rf_values.length),
mw = wind(d3.min(w));
let winReduce = (l,w,d0,fn) => {
l = l.slice();
for (let d=d0; d<w; ) {
let diff = Math.min(2*d,w) - d;
for (let i=0; i<l.length-diff; i++) {
l[i] = fn(l[i], l[i+diff]);
}
d += diff;
}
l.length -= w-d0;
return l;
}
let rangeGetters = [Math.min, Math.max].map(f => {
let r = c => c.reduce((a,b)=>a.map((ai,i)=>f(ai,b[i])));
let t = v.map(c => winReduce(r(c), mw, 1, f));
return w => t.map(c => winReduce(c, w, mw, f));
});
let top = 0; // Use top left if we can't find a spot
tr = v.map((_,j) => {
let we = wind(w[j]),
he = -invd(y,h[j]),
range = d3.transpose(rangeGetters.map(r => r(we))),
ds;
ds = range[j].map(function (r,ri) {
let le = r.length,
s = [[-he,0],[0,he]][ri].map(o=>r.map(d=>d+o)),
d = r.map(_=>1e10);
for (let k=0; k<n; k++) if (k!==j) {
let t = range[k];
for (let i=0; i<le; i++) {
d[i] = Math.min(d[i], Math.max(s[0][i]-t[1][i],
t[0][i]-s[1][i]));
}
}
return d;
});
let sep = 0, pos = null;
ds.forEach(function (drow,k) {
for (let ii=0; ii<drow.length; ) {
let i=ii, d=drow[i],
rjk=range[j][k], m=rjk[i];
while (ii++, ii<drow.length && rjk[ii]===m) {
let di = drow[ii];
if (di<d && di<1) break;
d = Math.max(d,drow[ii]);
}
let clip = x => x/Math.sqrt(1+x*x);
d = 4*clip(d/4) + clip((ii-i)/3);
i = Math.floor((i+ii)/2);
let dl = drow.length,
r = i/dl;
d *= Math.sqrt((0.8+r)*Math.sqrt(1-r));
d *= clip(0.2+Math.max(0,(i>=15?drow[i-15]:0)+(i<dl-15?drow[i+15]:0)));
if (d>sep) {
let dy = range[j][k][i]+(k?he:0),
yd = y.domain();
if (yd[0]+he<=dy && dy<=yd[1]) { sep=d; pos=[i,dy]; }
}
}
});
return pos ? [x(rf_values[pos[0]]), y(pos[1])]
: [60, 20+30*top++];
});
}
for (let j=curves.length; j<bcurves.length; j++) {
tr.push([pad.l+(W-w[j])/2, pad.t+H-h[j]+2]);
}
g.attr("transform",(_,i)=>"translate("+tr[i].join(",")+")");
g.attr("opacity",null);
setLabelButton(true);
}
labelButton.on("click", () => (labelsShown?clearLabels:drawLabels)());
function saveGraph(ext) {
let fn = {png:saveSvgAsPng, svg:saveSvg}[ext];
let showControls = s => dB.all.attr("visibility",s?null:"hidden");
gpath.selectAll("path").classed("highlight",false);
drawLabels();
showControls(false);
fn(gr.node(), "graph."+ext, {scale:3})
.then(()=>showControls(true));
// Analytics event
if (analyticsEnabled) { pushEventTag("clicked_download", targetWindow); }
}
doc.select("#download")
.on("click", () => saveGraph("png"))
.on("contextmenu", function () {
d3.event.returnValue=false;
let b = d3.select(this);
let choice = b.selectAll("div")
.data(["png","svg"]).join("div")
.styles({position:"absolute", left:0, top:(_,i)=>i*1.3+"em",
background:"inherit", padding:"0.1em 1em"})
.text(d => "As ."+d)
.on("click", function (d) {
saveGraph(d);
choice.remove();
d3.event.stopPropagation();
});
b.on("blur", ()=>choice.remove());
});
// Graph smoothing
let pair = (arr,fn) => arr.slice(1).map((v,i)=>fn(v,arr[i]));
function smooth_prep(h, d) {
let rh = h.map(d=>1/d),
G = [ rh.slice(0,rh.length-1),
pair(rh, (a,b)=>-(a+b)),
rh.slice(1) ],
dv = d3.range(rh.length+1).map(i=>d(i)),
dG = G.map((r,j) => r.map((e,i) => e*dv[i+j])),
d2 = dv.map(e=>e*e),
h6 = h.map(d=>d/6),
M = [ pair(h6, (a,b)=>2*(a+b)),
h6.slice(1,h6.length-1),
h6.slice(3).map(_=>0) ];
dG.forEach((_,k) =>
dG.slice(k).forEach((g,i) =>
dG[i].slice(k).forEach((a,j) => M[k][j] += a*g[j])
)
);
// Diagonal LDL decomposition of M
let md = [M[0][0]],
ml = M.slice(1).map(m=>[m[0]/md]);
d3.range(1,M[0].length).forEach(j => {
let n = ml.length,
p = md.slice(-n).reverse().map((d,i)=>d*ml[i][j-1-i]),
a = M.map((m,k) => m[j] - d3.sum(p.slice(0,n-k),
(a,i) => a*ml[k+i][j-1-i]));
md.push(a[0]);
ml.forEach((l,j)=>l.push(a[j+1]/a[0]));
});
return { G:G, md:md, ml:ml, d2:d2 };
}
function smooth_eval(p, y) {
let Gy = p.G[0].map(_=>0),
n = Gy.length;
p.G.forEach((r,j) => r.forEach((e,i) => Gy[i] += e*y[i+j]));
// Forward substitution and multiply by p.md
for (let i=0; i<n; i++) {
let yi = Gy[i];
p.ml.forEach((m,k) => { let j=i+k+1; if (j<n) Gy[j] -= m[i]*yi; });
Gy[i] /= p.md[i];
}
// Back substitution
for (let i=n; i--; ) {
let yi = Gy[i];
p.ml.forEach((m,k) => { let j=i-k-1; if (j>=0) Gy[j] -= m[j]*yi; });
}
let u = y.slice();
p.G.forEach((r,j) => r.forEach((e,i) => u[i+j] -= e*p.d2[i+j]*Gy[i]));
return u;
}
let smooth_level = 5,
smooth_scale = 0.01*(typeof scale_smoothing !== "undefined" ? scale_smoothing : 1),
smooth_param = undefined;
function smooth(y, c) {
if (smooth_level === 0) { return y; }
let get_param = fv => {
let x = fv.map(f=>Math.log(f)),
h = pair(x, (a,b)=>a-b),
s = smooth_level*smooth_scale,
d = i => s*Math.pow(1/80,Math.pow(i/x.length,2));
return smooth_prep(h, d);
}
let p;
if (y.length!==f_values.length) {
p = get_param(c.map(d=>d[0]));
} else {
if (!smooth_param) { smooth_param = get_param(f_values); }
p = smooth_param;
}
return smooth_eval(p, y);
}
function smoothPhone(p) {
if (p.smooth !== smooth_level) {
p.channels = p.rawChannels.map(
c=>c?smooth(c.map(d=>d[1]),c).map((d,i)=>[c[i][0],d]):c
);
p.smooth = smooth_level;
setCurves(p);
}
}
doc.select("#smooth-level").on("change input", function () {
if (!this.checkValidity()) return;
smooth_level = +this.value;
smooth_param = undefined;
line.curve(smooth_level ? d3.curveNatural : d3.curveCardinal.tension(0.5));
activePhones.forEach(smoothPhone);
updatePaths();
});
// Normalization with target loudness
const iso223_params = { // :2003
f : [ 20, 25, 31.5, 40, 50, 63, 80, 100, 125, 160, 200, 250, 315, 400, 500, 630, 800, 1000, 1250, 1600, 2000, 2500, 3150, 4000, 5000, 6300, 8000, 10000, 12500],
a_f: [0.532, 0.506, 0.48, 0.455, 0.432, 0.409, 0.387, 0.367, 0.349, 0.33, 0.315, 0.301, 0.288, 0.276, 0.267, 0.259, 0.253, 0.25, 0.246, 0.244, 0.243, 0.243, 0.243, 0.242, 0.242, 0.245, 0.254, 0.271, 0.301],
L_U: [-31.6, -27.2, -23, -19.1, -15.9, -13, -10.3, -8.1, -6.2, -4.5, -3.1, -2, -1.1, -0.4, 0, 0.3, 0.5, 0, -2.7, -4.1, -1, 1.7, 2.5, 1.2, -2.1, -7.1, -11.2, -10.7, -3.1],
T_f: [ 78.5, 68.7, 59.5, 51.1, 44, 37.5, 31.5, 26.5, 22.1, 17.9, 14.4, 11.4, 8.6, 6.2, 4.4, 3, 2.2, 2.4, 3.5, 1.7, -1.3, -4.2, -6, -5.4, -1.5, 6, 12.6, 13.9, 12.3]
};
const free_field = [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0.0725,0.1,0.1,0.1,0.1,0.1,0.1,0.1,0.1,0.1,0.1,0.1,0.1,0.1,0.1,0.1,0.1,0.1,0.1,0.1,0.1,0.1,0.1,0.1,0.1,0.1,0.1,0.1,0.1,0.1,0.1,0.1,0.1,0.1,0.1,0.1,0.1,0.1,0.1,0.1,0.1,0.1,0.1,0.1,0.1,0.1,0.1,0.1,0.1,0.1,0.1,0.1,0.1,0.1,0.1,0.1,0.1,0.1,0.1,0.1,0.1,0.1,0.1,0.1,0.1,0.1,0.1,0.1,0.1,0.1,0.1,0.1,0.1,0.1,0.1,0.1,0.1,0.1,0.1,0.1,0.1,0.1,0.1,0.1,0.1,0.1,0.1,0.1,0.0896,0,0,0,0,0,0.1,0.1,0.1,0.1,0.1,0.1,0.1,0.1,0.1,0.1,0.0967,0,0,0,0,0,0,0,0.0886,0.1,0.1,0.1,0.1,0.1,0.1,0.1,0.0656,0,0,0,0,0,0.024,0.1,0.1,0.1,0.1,0.1,0.1,0.1,0.1,0.045,0,0,0,0,0,0,0.029,0.1,0.1,0.1,0.1,0.1,0.1,0.1,0.1,0.1,0.1,0.1,0.1,0.1524,0.2,0.2,0.2386,0.3395,0.4,0.437,0.5,0.5287,0.6225,0.7,0.7063,0.7962,0.8,0.8941,0.9,0.9863,1,1.0729,1.1,1.1544,1.2,1.2504,1.3,1.3,1.3,1.3,1.3163,1.4,1.4,1.4,1.4,1.4017,1.4846,1.5,1.5,1.5748,1.6,1.6,1.653,1.7,1.7,1.7487,1.8,1.8341,1.9,1.9,1.9229,2,2,2,2.1,2.1,2.1897,2.2,2.2,2.2674,2.3,2.3,2.3567,2.4,2.4,2.4446,2.5,2.5262,2.6,2.6234,2.7149,2.8,2.8038,2.9011,2.9969,3.0913,3.1845,3.2762,3.3757,3.4649,3.5617,3.657,3.751,3.8,3.8432,3.9332,4,4,4,4.0121,4.1,4.1,4.1,4.0079,4,4,4,4,3.9334,3.9,3.9,3.9,3.8541,3.8,3.8,3.768,3.7,3.6761,3.6,3.6,3.5927,3.5,3.5,3.5,3.5,3.5,3.5761,3.6,3.6,3.6604,3.7,3.7514,3.8,3.8,3.8349,3.9,3.9218,4.0199,4.1123,4.2076,4.3016,4.3985,4.6816,5.0515,5.4222,5.8036,6.1097,6.4656,6.8461,7.3316,7.9083,8.4305,8.9369,9.5105,10.0759,10.6024,11.0027,11.4847,12.0482,12.5152,12.8994,13.2776,13.7381,14.1303,14.5168,14.8858,15.273,15.6547,15.9731,16.2596,16.542,16.7857,17.0111,17.2325,17.3532,17.522,17.6,17.6,17.6,17.6,17.5044,17.41,17.3145,17.2205,17.1255,17.0318,16.9373,16.784,16.6459,16.4536,16.2578,16.1234,15.967,15.8736,15.7552,15.566,15.3879,15.2881,15.0958,14.9064,14.8099,14.6287,14.5201,14.3477,14.2307,14.0709,13.9399,13.7916,13.6514,13.5552,13.4604,13.367,13.2718,13.1766,13.0812,12.9743,12.7916,12.6975,12.602,12.5078,12.3247,12.0547,11.7686,11.4154,11.1009,10.9385,10.7344,10.3998,10.0163,9.6382,9.2957,8.9799,8.6248,8.3404,8.0424,7.674,7.3851,7.0061,6.5307,6.1484,5.7696,5.4662,5.1084,4.7302,4.3498,3.971,3.6455,3.4075,3.1343,2.7917,2.5376,2.3484,2.1585,1.9849,1.9107,2,2,2,2.0894,2.1844,2.2787,2.374,2.6057,2.8265,3.0161,3.2057,3.3954,3.5851,3.8122,4.0967,4.354,4.5651,4.8509,5.1459,5.5259,5.9041,6.1881,6.5643,6.8561,7.1418,7.4251,7.7093,8.0593,8.3192,8.4541,8.5493,8.6437,8.7,8.7336,8.8,8.8,8.8,8.8,8.7926,8.7,8.7,8.6079,8.5133,8.5,8.4237,8.1863,7.968,7.7786,7.4219,6.948,6.4299,5.8212,5.1563,4.4634,3.7042,2.8897,1.9005,1.2368,0.5651,-0.2856,-0.8593,-2.9].map(v=>v-7);
function init_normalize(fv) { // Interpolate values for find_offset
let par = [], ff = [];
par.free_field = ff;
const p = iso223_params;
let i = 0;
fv.forEach(function (f) {
if (f >= p.f[i]) { i++; }
let i0 = Math.max(0,i-1),
i1 = Math.min(i,p.f.length-1),
g;
if (i0===i1) {
g = n => p[n][i0];
} else {
let ll= [p.f[i0],p.f[i1],f].map(x=>Math.log(x)),
l = (ll[2]-ll[0])/(ll[1]-ll[0]);
g = n => { let v=p[n]; return v[i0]+l*(v[i1]-v[i0]); };
}
let a = g("a_f"),
m = a * (Math.log10(4)-10 + g("L_U")/10),
k = (0.005076/Math.pow(10,m)) - Math.pow(10, a*g("T_f")/10),
c = Math.pow(10, 9.4 + 4*m) / fv.length;
par.push({a:a, k:k, c:c});
ffi = Math.floor(0.5+48*Math.log2(f/19.4806));
ff.push(free_field[Math.max(0,Math.min(479,ffi))]);
});
return par;
}
// Find the appropriate offset (in dB) for fr so that the total loudness
// is equal to target (in phon)
let norm_par = []; // Cached interpolated ISO parameters
function find_offset(c, target) {
let par;
if (c.length!==f_values.length) {
par = init_normalize(c.map(d=>d[0]));
} else {
if (!norm_par.length) { norm_par = init_normalize(f_values); }
par = norm_par;
}
let fr = c.map(v=>v[1]);
let x = 0; // Initial offset
function getStep(o) {
const l10 = Math.log(10)/10;
let v=0, d=0;
par.forEach(function (p,i) {
let a=p.a, k=p.k, c=p.c, ds,v0,v1;
v0 = Math.exp(l10*(fr[i]+o-par.free_field[i]));
ds = l10 * v0;
v1 = k + Math.pow(v0,a);
ds *= a * Math.pow(v0,a-1);
v += c * Math.pow(v1,4);
ds *= c * 4 * Math.pow(v1,3);
d += ds;
});
// value: Math.log(v)/l10
// deriv: d / (l10*v)
return (Math.log(v) - target*l10) * (v/d);
}
let dx;
do {
dx = getStep(x);
x -= dx;
} while (Math.abs(dx) > 0.01);
return x;
}
// File loading and channel management
const LR = typeof default_channels !== "undefined" ? default_channels
: ["L","R"];
let getO = i => LR.length>1 ? -1+i*2/(LR.length-1) : 0;
const sampnums = typeof num_samples !== "undefined" ? d3.range(1,num_samples+1)
: [""];
function loadFiles(p, callback) {
let l = f => d3.text(DIR+f+".txt").catch(()=>null);
let f = p.isTarget ? [l(p.fileName)]
: d3.merge(LR.map(s =>
sampnums.map(n => l(p.fileName+" "+s+n))));
Promise.all(f).then(function (frs) {
if (!frs.some(f=>f!==null)) {
alert("Headphone not found!");
} else {
let ch = frs.map(f => f && tsvParse(f));
if (!f_values) { f_values = ch[0].map(d=>d[0]); }
callback(ch);
}
});
}
let validChannels = p => p.channels.filter(c=>c!==null);
let numChannels = p => d3.sum(p.channels, c=>c!==null);
let notMultichannel = LR.length===1 ? p=>true : p=>p.isTarget;
let hasChannelSel = p => !notMultichannel(p) && numChannels(p)>1;
let keyExt = LR.length===1 ? 16 : 0;
let keyLeft= keyExt ? 0 : sampnums.length>1 ? 11 : 0;
if (keyLeft) d3.select(".key").style("width","17%")
function avgCurves(curves) {
return curves
.map(c=>c.map(d=>Math.pow(10,d[1]/20)))
.reduce((as,bs) => as.map((a,i) => a+bs[i]))
.map((x,i) => [curves[0][i][0], 20*Math.log10(x/curves.length)]);
}
function getAvg(p) {
if (p.avg) return p.activeCurves[0].l;
let v = validChannels(p);
return v.length===1 ? v[0] : avgCurves(v);
}
function hasImbalance(p) {
if (!hasChannelSel(p)) return false;
let as = p.channels[0], bs = p.channels[1];
let s0=0, s1=0;
return as.some((a,i) => {
let d = a[1]-bs[i][1];
d *= 1/(50 * Math.sqrt(1+Math.pow(a[0]/1e4,6)));
s0 = Math.max(s0+d,0);
s1 = Math.max(s1-d,0);
return Math.max(s0,s1) > max_channel_imbalance;
});
}
let activePhones = [];
let baseline0 = { p:null, l:null, fn:l=>l },
baseline = baseline0;
let gpath = gr.insert("g",".dBScaler")
.attr("fill","none")
.attr("stroke-width",2.3)
.attr("mask","url(#graphFade)");
function hl(p, h) {
gpath.selectAll("path").filter(c=>c.p===p).classed("highlight",h);
}
let table = doc.select(".curves");
let ld_p1 = 1.1673039782614187;
function getCurveColor(id, o) {
let p1 = ld_p1,
p2 = p1*p1,
p3 = p2*p1;
let t = o/32;
let i=id/p3+0.76, j=id/p2+0.79, k=id/p1+0.32;
if (id < 0) { return d3.hcl(360*(1-(-i)%1),5,66); } // Target
let th = 2*Math.PI*i;
i += Math.cos(th-0.3)/24 + Math.cos(6*th)/32;
let s = Math.sin(2*Math.PI*i);
return d3.hcl(360*((i + t/p2)%1),
88+30*(j%1 + 1.3*s - t/p3),
36+22*(k%1 + 1.1*s + 6*t*(1-s)));
}
let getColor_AC = c => getCurveColor(c.p.id, c.o);
let getColor_ph = (p,i) => getCurveColor(p.id, p.activeCurves[i].o);
function getDivColor(id, active) {
let c = getCurveColor(id,0);
c.l = 100-(80-Math.min(c.l,60))/(active?1.5:3);
c.c = (c.c-20)/(active?3:4);
return c;
}
function color_curveToText(c) {
if (!alt_layout) {
c.l = c.l/5 + 10;
c.c /= 3;
}
return c;
}
let getTooltipColor = curve => color_curveToText(getColor_AC(curve));
let getTextColor = p => color_curveToText(getCurveColor(p.id,0));
let getBgColor = p => {
let c=getCurveColor(p.id,0).rgb();
['r','g','b'].forEach(p=>c[p]=255-(255-Math.max(0,c[p]))*0.85);
return c;
}
let cantCompare;
let noTargets = typeof disallow_target !== "undefined" && disallow_target;
if (noTargets || typeof max_compare !== "undefined") {
const currency = [
["$", "#348542"],
["¥", "#d11111"],
["€", "#2961d4"],
["฿", "#dcaf1d"]
];
let currencyCounter = -1,
lastMessage = null,
messageWeight = 0;
let cantTarget = p => false;
if (noTargets) {
if (typeof allow_targets === "undefined") {
cantTarget = p => p.isTarget;
} else {
let r = f => f.replace(/ Target$/,""),
a = allow_targets.map(r);
cantTarget = p => p.isTarget && a.indexOf(r(p.fileName))<0;
}
}
let ct = typeof restrict_target === "undefined" || restrict_target,
ccfilter = ct ? (l => l) : (l => l.filter(p=>!p.isTarget));
cantCompare = function(ps, add, p, noMessage) {
let count = ccfilter(ps).length + (add||0) - (!ct&&p&&p.isTarget?1:0);
if (count<max_compare && !(p&&cantTarget(p))) { return false; }
if (noMessage) { return true; }
let div = doc.append("div");
let c = currency[currencyCounter++ % currency.length];
let lm = lastMessage;
lastMessage = Date.now();
messageWeight *= Math.pow(2, (lm?lm-lastMessage:0)/3e4); // 30-second half-life
messageWeight++;
if (!currencyCounter || messageWeight>=2) {
messageWeight /= 2;
let button = div.attr("class","cashMessage")
.html(premium_html)
.append("button").text("Fine")
.on("mousedown", ()=>messageWeight=0);
button.node().focus();
let back = doc.append("div")
.attr("class","fadeAll");
[button,back].forEach(e =>
e.on("click", ()=>[div,back].forEach(e=>e.remove()))
);
} else {
div.attr("class","cash")
.style("color",c[1]).text(c[0])
.transition().duration(120).remove();
}
return true;
}
} else {
cantCompare = function(m) { return false; }
}
let phoneNumber = 0; // I'm so sorry it just happened
// Find a phone id which doesn't have a color conflict with pins
let nextPN = 0; // Cached value; invalidated when pinned headphones change
function nextPhoneNumber() {
if (nextPN === null) {
nextPN = phoneNumber;
let pin = activePhones.filter(p => p.pin).map(p=>p.id);
if (pin.length) {
let p3 = ld_p1*ld_p1*ld_p1,
l = a => b => Math.abs(((a-b)/p3 + 0.5) % 1 - 0.5),
d = id => d3.min(pin, l(id));
for (let i=nextPN, max=d(i); max<0.12 && ++i<phoneNumber+3; ) {
let m = d(i);
if (m > max) { max=m; nextPN=i; }
}
}
}
return nextPN;
}
function getPhoneNumber() {
let pn = nextPhoneNumber();
phoneNumber = pn + 1;
nextPN = null;
return pn;
}
function setPhoneTr(phtr) {
phtr.each(function (p) {
p.highlight = p.active;
let o = p.objs; if (!o) return;
p.objs = o = o.filter(q=>q.active);
if (o.length === 0) {
delete p.objs;
} else if (!p.active) {
p.id = o[0].id;
p.highlight = true;
}
});
phtr.style("background",p=>p.isTarget&&!p.active?null:getDivColor(p.id,p.highlight))
.style("border-color",p=>p.highlight?getDivColor(p.id,1):null);
phtr.filter(p=>!p.isTarget)
.selectAll(".remove").data(p=>p.highlight?[p]:[])
.join("span").attr("class","remove").text("⊗")
.on("click", p => { d3.event.stopPropagation(); removeCopies(p); });
}
let channelbox_x = c => c?-86:-36,
channelbox_tr = c => "translate("+channelbox_x(c)+",0)";
function setCurves(p, avg, lr, samp) {
if (avg ===undefined) avg = p.avg;
if (samp===undefined) samp = avg ? false : LR.length===1||p.ssamp||false;
else { p.ssamp = samp; if (samp) avg = false; }
let dx = +avg - +p.avg,
n = sampnums.length,
selCh = (l,i) => l.slice(i*n,(i+1)*n);
p.avg = avg;
p.samp = samp = n>1 && samp;
if (!p.isTarget) {
let id = getChannelName(p),
v = cs => cs.filter(c=>c!==null),
cs = p.channels,
cv = v(cs),
mc = cv.length>1,
pc = (idstr, l, oi) => ({id:id(idstr), l:l, p:p,
o:oi===undefined?0:getO(oi)});
p.activeCurves
= avg && mc ? [pc("AVG", avgCurves(cv))]
: !samp && mc ? LR.map((l,i) => pc(l, avgCurves(v(selCh(cs,i))), i))
: cs.map((l,i) => {
let j = Math.floor(i/n);
return pc(LR[j]+sampnums[i%n], l, j);
}).filter(c => c.l);
} else {
p.activeCurves = [{id:p.fullName, l:p.channels[0], p:p, o:0}];
}
let y = 0;
let k = d3.selectAll(".keyLine").filter(q=>q===p);
let ksb = k.select(".keySelBoth").attr("display","none");
p.lr = lr;
if (lr!==undefined) {
p.activeCurves = p.samp ? selCh(p.activeCurves, lr) : [p.activeCurves[lr]];
y = [-1,1][lr];
ksb.attr("display",null).attr("y", [0,-12][lr]);
}
k.select(".keyMask")
.transition().duration(400)
.attr("x", channelbox_x(avg))
.attrTween("y", function () {
let y0 = +this.getAttribute("y"),
y1 = 12*(-1+y);
if (!dx) { return d3.interpolateNumber(y0,y1); }
let ym = y0 + (y1-y0)*(3-2*dx)/6;
y0-=ym; y1-=ym;
return t => { t-=1/2; return ym+(t<0?y0:y1)*Math.pow(2,20*(Math.abs(t)-1/2)); };
});
k.select(".keySel").attr("transform", channelbox_tr(avg));
k.selectAll(".keySamp").attr("opacity",(_,i)=>i===+samp?1:0.6);
}
function updateCurves() {
setCurves.apply(null, arguments);
updatePaths();
}
let drawLine = d => line(baseline.fn(d.l));
function redrawLine(p) {
let getTr = o => o ? "translate(0,"+(y(o)-y(0))+")" : null;
p.attr("transform", c => getTr(getOffset(c.p))).attr("d", drawLine);
}
function updateYCenter() {
let c = yCenter;
yCenter = baseline.p ? 0 : norm_sel ? 60 : norm_phon;
y.domain(y.domain().map(d=>d+(yCenter-c)));
yAxisObj.call(fmtY);
}
function setBaseline(b, no_transition) {
baseline = b;
updateYCenter();
if (no_transition) return;
clearLabels();
gpath.selectAll("path")
.transition().duration(500).ease(d3.easeQuad)
.attr("d", drawLine);
table.selectAll("tr").select(".button")
.classed("selected", p=>p===baseline.p);
// Analytics event
if (analyticsEnabled && b.p) { pushPhoneTag("baseline_set", b.p); }
}
function getBaseline(p) {
let b = getAvg(p).map(d => d[1]+getOffset(p));
return { p:p, fn:l=>l.map((e,i)=>[e[0],e[1]-b[Math.min(i,b.length-1)]]) };
}
function setOffset(p, o) {
p.offset = +o;
if (baseline.p === p) { baseline = getBaseline(p); }
updatePaths();