Noah hace 8 años
padre
commit
eb03a5a575
Se han modificado 1 ficheros con 11 adiciones y 12 borrados
  1. 11 12
      audiogram/waveform.js

+ 11 - 12
audiogram/waveform.js Ver fichero

@@ -36,30 +36,29 @@ function processSamples(samples, numFrames, samplesPerFrame) {
36 36
       perPoint = Math.floor(perFrame / samplesPerFrame),
37 37
       range = d3.range(samplesPerFrame),
38 38
       maxFrame,
39
-      maxMedian = min = max = 0;
39
+      maxMedian = max = 0;
40 40
 
41 41
   var unadjusted = d3.range(numFrames).map(function(frame){
42 42
 
43 43
     var frameSamples = samples.slice(frame * perFrame, (frame + 1) * perFrame),
44 44
         points = range.map(function(point){
45 45
 
46
-          var pointSamples = frameSamples.slice(point * perPoint, (point + 1) * perPoint),
47
-              localMin = localMax = 0;
46
+          var pointSamples = frameSamples.slice(point * perPoint, (point + 1) * perPoint);
48 47
 
49
-          for (var i = 0, l = pointSamples.length; i < l; i++) {
50
-            if (pointSamples[i] < localMin) localMin = pointSamples[i];
51
-            if (pointSamples[i] > localMax) localMax = pointSamples[i];
52
-          }
48
+          var rms = Math.sqrt(d3.sum(pointSamples.map(function(d){
49
+            return d * d;
50
+          })) / perPoint);
53 51
 
54
-          if (localMin < min) min = localMin;
55
-          if (localMax > max) max = localMax;
52
+          if (rms > max) {
53
+            max = rms;
54
+          }
56 55
 
57 56
           // Min value, max value, and midpoint value
58
-          return [localMin, localMax, pointSamples[Math.floor(pointSamples.length / 2)]];
57
+          return [-rms, rms, pointSamples[Math.floor(pointSamples.length / 2)]];
59 58
 
60 59
         }),
61 60
         median = d3.median(points.map(function(point){
62
-          return point[1] - point[0];
61
+          return point[1];
63 62
         }));
64 63
 
65 64
     if (median > maxMedian) {
@@ -72,7 +71,7 @@ function processSamples(samples, numFrames, samplesPerFrame) {
72 71
   });
73 72
 
74 73
   // Scale up to -1 / 1
75
-  var adjustment = 1 / Math.max(Math.abs(min), Math.abs(max));
74
+  var adjustment = 1 / max;
76 75
 
77 76
   var adjusted = unadjusted.map(function(frame){
78 77
     return frame.map(function(point){