Explorar el Código

Waveform updates, RMS and midpoint only

Noah hace 8 años
padre
commit
ebc596313a
Se han modificado 5 ficheros con 25 adiciones y 27 borrados
  1. 16 18
      audiogram/waveform.js
  2. 1 1
      client/sample-wave.js
  3. 5 5
      renderer/patterns.js
  4. 2 2
      test/frame-test.js
  5. 1 1
      test/waveform-test.js

+ 16 - 18
audiogram/waveform.js Ver fichero

@@ -36,46 +36,44 @@ function processSamples(samples, numFrames, samplesPerFrame) {
36 36
       perPoint = Math.floor(perFrame / samplesPerFrame),
37 37
       range = d3.range(samplesPerFrame),
38 38
       maxFrame,
39
-      maxMedian = max = 0;
39
+      maxRms = maxMid = 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);
46
+          var pointSamples = frameSamples.slice(point * perPoint, (point + 1) * perPoint),
47
+              midpoint = pointSamples[Math.floor(pointSamples.length / 2)];
47 48
 
48 49
           var rms = Math.sqrt(d3.sum(pointSamples.map(function(d){
49 50
             return d * d;
50 51
           })) / perPoint);
51 52
 
52
-          if (rms > max) {
53
-            max = rms;
53
+          if (rms > maxRms) {
54
+            maxRms = rms;
55
+            maxFrame = frame;
56
+          }
57
+
58
+          if (Math.abs(midpoint) > maxMid) {
59
+            maxMid = Math.abs(midpoint);
54 60
           }
55 61
 
56 62
           // Min value, max value, and midpoint value
57
-          return [-rms, rms, pointSamples[Math.floor(pointSamples.length / 2)]];
63
+          return [rms, midpoint];
58 64
 
59
-        }),
60
-        median = d3.median(points.map(function(point){
61
-          return point[1];
62
-        }));
63
-
64
-    if (median > maxMedian) {
65
-      maxMedian = median;
66
-      maxFrame = frame;
67
-    }
65
+        });
68 66
 
69 67
     return points;
70 68
 
71 69
   });
72 70
 
73
-  // Scale up to -1 / 1
74
-  var adjustment = 1 / max;
75
-
76 71
   var adjusted = unadjusted.map(function(frame){
77 72
     return frame.map(function(point){
78
-      return point.map(function(p){ return adjustment * p; });
73
+      return [
74
+        point[0] / maxRms,
75
+        point[1] / maxMid
76
+      ];
79 77
     });
80 78
   });
81 79
 

La diferencia del archivo ha sido suprimido porque es demasiado grande
+ 1 - 1
client/sample-wave.js


+ 5 - 5
renderer/patterns.js Ver fichero

@@ -36,13 +36,13 @@ function curve(interpolator) {
36 36
 
37 37
     var top = data.map(function(d,i){
38 38
 
39
-      return [x(i), baseline - height(d[1])];
39
+      return [x(i), baseline - height(d[0])];
40 40
 
41 41
     });
42 42
 
43 43
     var bottom = data.map(function(d,i){
44 44
 
45
-      return [x(i), baseline + height(-d[0])];
45
+      return [x(i), baseline + height(d[0])];
46 46
 
47 47
     }).reverse();
48 48
 
@@ -90,9 +90,9 @@ function bars(round) {
90 90
 
91 91
     data.forEach(function(val, i){
92 92
 
93
-      var h = height(-val[0]) + height(val[1]),
93
+      var h = height(val[0]) * 2,
94 94
           x = barX(i),
95
-          y = baseline - height(val[1]);
95
+          y = baseline - height(val[0]);
96 96
 
97 97
       context.fillRect(x, y, barWidth, h);
98 98
 
@@ -133,7 +133,7 @@ function bricks(rainbow) {
133 133
 
134 134
     data.forEach(function(val, i){
135 135
 
136
-      var bricks = Math.max(1, Math.floor(height(val[1]) / (brickHeight + brickGap))),
136
+      var bricks = Math.max(1, Math.floor(height(val[0]) / (brickHeight + brickGap))),
137 137
           x = barX(i);
138 138
 
139 139
       d3.range(bricks).forEach(function(b){

+ 2 - 2
test/frame-test.js Ver fichero

@@ -12,10 +12,10 @@ var frameDir = path.join(__dirname, "tmp", "frames");
12 12
 
13 13
 var waveform = [
14 14
   [
15
-    [0, 0], [-1, 1], [0, 0]
15
+    [0, 0], [1, 1], [0, 0]
16 16
   ],
17 17
   [
18
-    [-1, 1], [-0.1, 0.1], [-1, 1]
18
+    [1, 1], [0.1, 0.1], [1, 1]
19 19
   ]
20 20
 ];
21 21
 

+ 1 - 1
test/waveform-test.js Ver fichero

@@ -28,7 +28,7 @@ tape("Waveform", function(test) {
28 28
 
29 29
       test.assert(waveform.every(function(frame){
30 30
         return frame.length === options.samplesPerFrame && frame.every(function(f){
31
-          return f.length === 3 && f.every(function(d){ return typeof d === "number"; }) && f[0] <= 0 && f[0] >= -1 && f[1] >= 0 && f[1] <= 1 && f[2] >= -1 && f[2] <= 1;
31
+          return f.length === 2 && f.every(function(d){ return typeof d === "number"; }) && f[0] >= 0 && f[0] <= 1 && f[1] >= -1 && f[1] <= 1;
32 32
         });
33 33
       }));
34 34